mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-14 00:07:35 -04:00
Correctly create undo commands for item addition and removal
This commit is contained in:
parent
cac08f93eb
commit
80e0e07202
@ -372,6 +372,7 @@ void QgsLayout::removeLayoutItem( QgsLayoutItem *item )
|
||||
#if 0 //TODO
|
||||
emit itemRemoved( item );
|
||||
#endif
|
||||
item->deleteLater();
|
||||
}
|
||||
|
||||
QgsLayoutUndoStack *QgsLayout::undoStack()
|
||||
|
@ -116,8 +116,39 @@ void QgsLayoutItemDeleteUndoCommand::redo()
|
||||
QgsLayoutItem *item = layout()->itemByUuid( itemUuid() );
|
||||
Q_ASSERT_X( item, "QgsLayoutItemDeleteUndoCommand::redo", "could not find item to re-delete!" );
|
||||
|
||||
layout()->removeItem( item );
|
||||
item->deleteLater();
|
||||
layout()->removeLayoutItem( item );
|
||||
}
|
||||
|
||||
QgsLayoutItemAddItemCommand::QgsLayoutItemAddItemCommand( QgsLayoutItem *item, const QString &text, int id, QUndoCommand *parent )
|
||||
: QgsLayoutItemUndoCommand( item, text, id, parent )
|
||||
{
|
||||
saveAfterState();
|
||||
}
|
||||
|
||||
bool QgsLayoutItemAddItemCommand::containsChange() const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool QgsLayoutItemAddItemCommand::mergeWith( const QUndoCommand * )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
void QgsLayoutItemAddItemCommand::undo()
|
||||
{
|
||||
if ( mFirstRun )
|
||||
{
|
||||
mFirstRun = false;
|
||||
return;
|
||||
}
|
||||
|
||||
QgsLayoutItem *item = layout()->itemByUuid( itemUuid() );
|
||||
if ( item )
|
||||
{
|
||||
layout()->removeLayoutItem( item );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
///@endcond
|
||||
|
@ -107,6 +107,35 @@ class CORE_EXPORT QgsLayoutItemDeleteUndoCommand: public QgsLayoutItemUndoComman
|
||||
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* \ingroup core
|
||||
* An undo command subclass for layout item addition undo commands.
|
||||
*
|
||||
* QgsLayoutItemAddItemCommand is a specific layout undo command which handles
|
||||
* layout item creation. When applied (e.g. as a result of a 'redo' action),
|
||||
* the associated layout item is recreated and added to the layout.
|
||||
*
|
||||
* \since QGIS 3.0
|
||||
*/
|
||||
class CORE_EXPORT QgsLayoutItemAddItemCommand: public QgsLayoutItemUndoCommand
|
||||
{
|
||||
public:
|
||||
|
||||
/**
|
||||
* Constructor for QgsLayoutItemAddItemCommand.
|
||||
* \param item associated layout item
|
||||
* \param text undo command descriptive text
|
||||
* \param id optional undo command id, used for automatic command merging
|
||||
* \param parent command
|
||||
*/
|
||||
QgsLayoutItemAddItemCommand( QgsLayoutItem *item, const QString &text, int id = 0, QUndoCommand *parent SIP_TRANSFERTHIS = nullptr );
|
||||
bool containsChange() const override;
|
||||
bool mergeWith( const QUndoCommand *command ) override;
|
||||
void undo() override;
|
||||
|
||||
};
|
||||
|
||||
///@endcond
|
||||
|
||||
#endif
|
||||
|
@ -28,6 +28,8 @@
|
||||
#include "qgssettings.h"
|
||||
#include "qgsrectangle.h"
|
||||
#include "qgsapplication.h"
|
||||
#include "qgslayoutitemundocommand.h"
|
||||
#include "qgsproject.h"
|
||||
#include <memory>
|
||||
#include <QDesktopWidget>
|
||||
#include <QMenu>
|
||||
@ -583,9 +585,12 @@ void QgsLayoutView::deleteSelectedItems()
|
||||
//delete selected items
|
||||
for ( QgsLayoutItem *item : selectedItems )
|
||||
{
|
||||
QgsLayoutItemDeleteUndoCommand *deleteCommand = new QgsLayoutItemDeleteUndoCommand( item, QString() );
|
||||
currentLayout()->removeLayoutItem( item );
|
||||
currentLayout()->undoStack()->stack()->push( deleteCommand );
|
||||
}
|
||||
currentLayout()->undoStack()->endMacro();
|
||||
currentLayout()->project()->setDirty( true );
|
||||
|
||||
#if 0
|
||||
}
|
||||
|
@ -26,6 +26,7 @@
|
||||
#include "qgslayoutitemguiregistry.h"
|
||||
#include "qgslayoutnewitempropertiesdialog.h"
|
||||
#include "qgssettings.h"
|
||||
#include "qgslayoutitemundocommand.h"
|
||||
#include <QGraphicsRectItem>
|
||||
#include <QPen>
|
||||
#include <QBrush>
|
||||
@ -118,6 +119,7 @@ void QgsLayoutViewToolAddItem::layoutReleaseEvent( QgsLayoutViewMouseEvent *even
|
||||
settings.setValue( QStringLiteral( "LayoutDesigner/lastSizeUnit" ), static_cast< int >( item->sizeWithUnits().units() ) );
|
||||
|
||||
layout()->addLayoutItem( item );
|
||||
layout()->undoStack()->stack()->push( new QgsLayoutItemAddItemCommand( item, tr( "Created %1" ).arg( QgsApplication::layoutItemRegistry()->itemMetadata( mItemType )->visibleName() ) ) );
|
||||
layout()->setSelectedItem( item );
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user