Skip to content

Commit 80e0e07

Browse files
committedOct 6, 2017
Correctly create undo commands for item addition and removal
1 parent cac08f9 commit 80e0e07

File tree

5 files changed

+70
-2
lines changed

5 files changed

+70
-2
lines changed
 

‎src/core/layout/qgslayout.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ void QgsLayout::removeLayoutItem( QgsLayoutItem *item )
372372
#if 0 //TODO
373373
emit itemRemoved( item );
374374
#endif
375+
item->deleteLater();
375376
}
376377

377378
QgsLayoutUndoStack *QgsLayout::undoStack()

‎src/core/layout/qgslayoutitemundocommand.cpp

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,39 @@ void QgsLayoutItemDeleteUndoCommand::redo()
116116
QgsLayoutItem *item = layout()->itemByUuid( itemUuid() );
117117
Q_ASSERT_X( item, "QgsLayoutItemDeleteUndoCommand::redo", "could not find item to re-delete!" );
118118

119-
layout()->removeItem( item );
120-
item->deleteLater();
119+
layout()->removeLayoutItem( item );
121120
}
122121

122+
QgsLayoutItemAddItemCommand::QgsLayoutItemAddItemCommand( QgsLayoutItem *item, const QString &text, int id, QUndoCommand *parent )
123+
: QgsLayoutItemUndoCommand( item, text, id, parent )
124+
{
125+
saveAfterState();
126+
}
127+
128+
bool QgsLayoutItemAddItemCommand::containsChange() const
129+
{
130+
return true;
131+
}
132+
133+
bool QgsLayoutItemAddItemCommand::mergeWith( const QUndoCommand * )
134+
{
135+
return false;
136+
}
137+
138+
void QgsLayoutItemAddItemCommand::undo()
139+
{
140+
if ( mFirstRun )
141+
{
142+
mFirstRun = false;
143+
return;
144+
}
145+
146+
QgsLayoutItem *item = layout()->itemByUuid( itemUuid() );
147+
if ( item )
148+
{
149+
layout()->removeLayoutItem( item );
150+
}
151+
}
152+
153+
123154
///@endcond

‎src/core/layout/qgslayoutitemundocommand.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,35 @@ class CORE_EXPORT QgsLayoutItemDeleteUndoCommand: public QgsLayoutItemUndoComman
107107

108108
};
109109

110+
111+
/**
112+
* \ingroup core
113+
* An undo command subclass for layout item addition undo commands.
114+
*
115+
* QgsLayoutItemAddItemCommand is a specific layout undo command which handles
116+
* layout item creation. When applied (e.g. as a result of a 'redo' action),
117+
* the associated layout item is recreated and added to the layout.
118+
*
119+
* \since QGIS 3.0
120+
*/
121+
class CORE_EXPORT QgsLayoutItemAddItemCommand: public QgsLayoutItemUndoCommand
122+
{
123+
public:
124+
125+
/**
126+
* Constructor for QgsLayoutItemAddItemCommand.
127+
* \param item associated layout item
128+
* \param text undo command descriptive text
129+
* \param id optional undo command id, used for automatic command merging
130+
* \param parent command
131+
*/
132+
QgsLayoutItemAddItemCommand( QgsLayoutItem *item, const QString &text, int id = 0, QUndoCommand *parent SIP_TRANSFERTHIS = nullptr );
133+
bool containsChange() const override;
134+
bool mergeWith( const QUndoCommand *command ) override;
135+
void undo() override;
136+
137+
};
138+
110139
///@endcond
111140

112141
#endif

‎src/gui/layout/qgslayoutview.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#include "qgssettings.h"
2929
#include "qgsrectangle.h"
3030
#include "qgsapplication.h"
31+
#include "qgslayoutitemundocommand.h"
32+
#include "qgsproject.h"
3133
#include <memory>
3234
#include <QDesktopWidget>
3335
#include <QMenu>
@@ -583,9 +585,12 @@ void QgsLayoutView::deleteSelectedItems()
583585
//delete selected items
584586
for ( QgsLayoutItem *item : selectedItems )
585587
{
588+
QgsLayoutItemDeleteUndoCommand *deleteCommand = new QgsLayoutItemDeleteUndoCommand( item, QString() );
586589
currentLayout()->removeLayoutItem( item );
590+
currentLayout()->undoStack()->stack()->push( deleteCommand );
587591
}
588592
currentLayout()->undoStack()->endMacro();
593+
currentLayout()->project()->setDirty( true );
589594

590595
#if 0
591596
}

‎src/gui/layout/qgslayoutviewtooladditem.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "qgslayoutitemguiregistry.h"
2727
#include "qgslayoutnewitempropertiesdialog.h"
2828
#include "qgssettings.h"
29+
#include "qgslayoutitemundocommand.h"
2930
#include <QGraphicsRectItem>
3031
#include <QPen>
3132
#include <QBrush>
@@ -118,6 +119,7 @@ void QgsLayoutViewToolAddItem::layoutReleaseEvent( QgsLayoutViewMouseEvent *even
118119
settings.setValue( QStringLiteral( "LayoutDesigner/lastSizeUnit" ), static_cast< int >( item->sizeWithUnits().units() ) );
119120

120121
layout()->addLayoutItem( item );
122+
layout()->undoStack()->stack()->push( new QgsLayoutItemAddItemCommand( item, tr( "Created %1" ).arg( QgsApplication::layoutItemRegistry()->itemMetadata( mItemType )->visibleName() ) ) );
121123
layout()->setSelectedItem( item );
122124
}
123125

0 commit comments

Comments
 (0)
Please sign in to comment.