QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
qgslayoutitemundocommand.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayoutitemundocommand.cpp
3 ------------------------
4 begin : July 2017
5 copyright : (C) 2017 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19#include "qgslayoutitem.h"
20#include "qgsreadwritecontext.h"
21#include "qgslayout.h"
22#include "qgsproject.h"
23#include "qgslayoutundostack.h"
24
26QgsLayoutItemUndoCommand::QgsLayoutItemUndoCommand( QgsLayoutItem *item, const QString &text, int id, QUndoCommand *parent )
27 : QgsAbstractLayoutUndoCommand( text, id, parent )
28 , mItemUuid( item->uuid() )
29 , mLayout( item->layout() )
30 , mItemType( item->type() )
31{
32
33}
34
35bool QgsLayoutItemUndoCommand::mergeWith( const QUndoCommand *command )
36{
37 if ( command->id() == 0 )
38 return false;
39
40 const QgsLayoutItemUndoCommand *c = dynamic_cast<const QgsLayoutItemUndoCommand *>( command );
41 if ( !c )
42 {
43 return false;
44 }
45 if ( c->itemUuid() != itemUuid() )
46 return false;
47
48 setAfterState( c->afterState() );
49 return true;
50}
51
52void QgsLayoutItemUndoCommand::saveState( QDomDocument &stateDoc ) const
53{
54 stateDoc.clear();
55 QDomElement documentElement = stateDoc.createElement( QStringLiteral( "ItemState" ) );
56
57 QgsLayoutItem *item = mLayout->itemByUuid( mItemUuid );
58 if ( item )
59 {
60 item->writeXml( documentElement, stateDoc, QgsReadWriteContext() );
61 stateDoc.appendChild( documentElement );
62 }
63 else
64 {
65 QgsDebugMsg( QStringLiteral( "QgsLayoutItemUndoCommand::saveState: could not retrieve item %1 for saving state" ).arg( mItemUuid ) );
66 }
67}
68
69void QgsLayoutItemUndoCommand::restoreState( QDomDocument &stateDoc )
70{
71 // find item by uuid...
72 QgsLayoutItem *item = mLayout->itemByUuid( mItemUuid );
73 if ( !item )
74 {
75 // uh oh - it's been deleted! we need to create a new instance
76 item = recreateItem( mItemType, mLayout );
77 }
78
79 item->readXml( stateDoc.documentElement().firstChild().toElement(), stateDoc, QgsReadWriteContext() );
81 mLayout->project()->setDirty( true );
82 mLayout->undoStack()->notifyUndoRedoOccurred( item );
83}
84
85QgsLayoutItem *QgsLayoutItemUndoCommand::recreateItem( int itemType, QgsLayout *layout )
86{
87 QgsLayoutItem *item = QgsApplication::layoutItemRegistry()->createItem( itemType, layout );
88 mLayout->addLayoutItemPrivate( item );
89 return item;
90}
91
92QString QgsLayoutItemUndoCommand::itemUuid() const
93{
94 return mItemUuid;
95}
96
97QgsLayout *QgsLayoutItemUndoCommand::layout() const
98{
99 return mLayout;
100}
101
102
103//
104// QgsLayoutItemDeleteUndoCommand
105//
106
107QgsLayoutItemDeleteUndoCommand::QgsLayoutItemDeleteUndoCommand( QgsLayoutItem *item, const QString &text, int id, QUndoCommand *parent )
108 : QgsLayoutItemUndoCommand( item, text, id, parent )
109{
110 saveBeforeState();
111}
112
113bool QgsLayoutItemDeleteUndoCommand::mergeWith( const QUndoCommand * )
114{
115 return false;
116}
117
118void QgsLayoutItemDeleteUndoCommand::redo()
119{
120 if ( mFirstRun )
121 {
122 mFirstRun = false;
123 return;
124 }
125
126 QgsLayoutItem *item = layout()->itemByUuid( itemUuid() );
127 //Q_ASSERT_X( item, "QgsLayoutItemDeleteUndoCommand::redo", "could not find item to re-delete!" );
128
129 layout()->undoStack()->blockCommands( true );
130 if ( item )
131 layout()->removeLayoutItemPrivate( item );
132 layout()->undoStack()->blockCommands( false );
133}
134
135QgsLayoutItemAddItemCommand::QgsLayoutItemAddItemCommand( QgsLayoutItem *item, const QString &text, int id, QUndoCommand *parent )
136 : QgsLayoutItemUndoCommand( item, text, id, parent )
137{
138 saveAfterState();
139}
140
141bool QgsLayoutItemAddItemCommand::containsChange() const
142{
143 return true;
144}
145
146bool QgsLayoutItemAddItemCommand::mergeWith( const QUndoCommand * )
147{
148 return false;
149}
150
151void QgsLayoutItemAddItemCommand::undo()
152{
153 if ( mFirstRun )
154 {
155 mFirstRun = false;
156 return;
157 }
158
159 QgsLayoutItem *item = layout()->itemByUuid( itemUuid() );
160 if ( item )
161 {
162 layout()->removeLayoutItemPrivate( item );
163 }
164}
165
166
Base class for commands to undo/redo layout and layout object changes.
static QgsLayoutItemRegistry * layoutItemRegistry()
Returns the application's layout item registry, used for layout item types.
QgsLayoutItem * createItem(int type, QgsLayout *layout) const
Creates a new instance of a layout item given the item type, and target layout.
Base class for graphical items within a QgsLayout.
bool writeXml(QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context) const
Stores the item state in a DOM element.
virtual void finalizeRestoreFromXml()
Called after all pending items have been restored from XML.
bool readXml(const QDomElement &itemElement, const QDomDocument &document, const QgsReadWriteContext &context)
Sets the item state from a DOM element.
void blockCommands(bool blocked)
Sets whether undo commands for the layout should be temporarily blocked.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:50
QgsLayoutItem * itemByUuid(const QString &uuid, bool includeTemplateUuids=false) const
Returns the layout item with matching uuid unique identifier, or nullptr if a matching item could not...
Definition: qgslayout.cpp:244
QgsLayoutUndoStack * undoStack()
Returns a pointer to the layout's undo stack, which manages undo/redo states for the layout and it's ...
Definition: qgslayout.cpp:692
The class is used as a container of context for various read/write operations on other objects.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
#define QgsDebugMsg(str)
Definition: qgslogger.h:38