Skip to content

Commit

Permalink
Correctly create undo commands for item addition and removal
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Oct 6, 2017
1 parent cac08f9 commit 80e0e07
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/core/layout/qgslayout.cpp
Expand Up @@ -372,6 +372,7 @@ void QgsLayout::removeLayoutItem( QgsLayoutItem *item )
#if 0 //TODO
emit itemRemoved( item );
#endif
item->deleteLater();
}

QgsLayoutUndoStack *QgsLayout::undoStack()
Expand Down
35 changes: 33 additions & 2 deletions src/core/layout/qgslayoutitemundocommand.cpp
Expand Up @@ -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
29 changes: 29 additions & 0 deletions src/core/layout/qgslayoutitemundocommand.h
Expand Up @@ -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
5 changes: 5 additions & 0 deletions src/gui/layout/qgslayoutview.cpp
Expand Up @@ -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>
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions src/gui/layout/qgslayoutviewtooladditem.cpp
Expand Up @@ -26,6 +26,7 @@
#include "qgslayoutitemguiregistry.h"
#include "qgslayoutnewitempropertiesdialog.h"
#include "qgssettings.h"
#include "qgslayoutitemundocommand.h"
#include <QGraphicsRectItem>
#include <QPen>
#include <QBrush>
Expand Down Expand Up @@ -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 );
}

Expand Down

0 comments on commit 80e0e07

Please sign in to comment.