QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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
20#include "qgslayout.h"
21#include "qgslayoutitem.h"
22#include "qgslayoutundostack.h"
23#include "qgsproject.h"
24#include "qgsreadwritecontext.h"
25
27QgsLayoutItemUndoCommand::QgsLayoutItemUndoCommand( QgsLayoutItem *item, const QString &text, int id, QUndoCommand *parent )
28 : QgsAbstractLayoutUndoCommand( text, id, parent )
29 , mItemUuid( item->uuid() )
30 , mLayout( item->layout() )
31 , mItemType( item->type() )
32{
33
34}
35
36bool QgsLayoutItemUndoCommand::mergeWith( const QUndoCommand *command )
37{
38 if ( command->id() == 0 )
39 return false;
40
41 const QgsLayoutItemUndoCommand *c = dynamic_cast<const QgsLayoutItemUndoCommand *>( command );
42 if ( !c )
43 {
44 return false;
45 }
46 if ( c->itemUuid() != itemUuid() )
47 return false;
48
49 setAfterState( c->afterState() );
50 return true;
51}
52
53void QgsLayoutItemUndoCommand::saveState( QDomDocument &stateDoc ) const
54{
55 stateDoc.clear();
56 QDomElement documentElement = stateDoc.createElement( QStringLiteral( "ItemState" ) );
57
58 QgsLayoutItem *item = mLayout->itemByUuid( mItemUuid );
59 if ( item )
60 {
61 item->writeXml( documentElement, stateDoc, QgsReadWriteContext() );
62 stateDoc.appendChild( documentElement );
63 }
64 else
65 {
66 QgsDebugError( QStringLiteral( "QgsLayoutItemUndoCommand::saveState: could not retrieve item %1 for saving state" ).arg( mItemUuid ) );
67 }
68}
69
70void QgsLayoutItemUndoCommand::restoreState( QDomDocument &stateDoc )
71{
72 // find item by uuid...
73 QgsLayoutItem *item = mLayout->itemByUuid( mItemUuid );
74 if ( !item )
75 {
76 // uh oh - it's been deleted! we need to create a new instance
77 item = recreateItem( mItemType, mLayout );
78 }
79
80 item->readXml( stateDoc.documentElement().firstChild().toElement(), stateDoc, QgsReadWriteContext() );
82 mLayout->project()->setDirty( true );
83 mLayout->undoStack()->notifyUndoRedoOccurred( item );
84}
85
86QgsLayoutItem *QgsLayoutItemUndoCommand::recreateItem( int itemType, QgsLayout *layout )
87{
88 QgsLayoutItem *item = QgsApplication::layoutItemRegistry()->createItem( itemType, layout );
89 mLayout->addLayoutItemPrivate( item );
90 return item;
91}
92
93QString QgsLayoutItemUndoCommand::itemUuid() const
94{
95 return mItemUuid;
96}
97
98QgsLayout *QgsLayoutItemUndoCommand::layout() const
99{
100 return mLayout;
101}
102
103
104//
105// QgsLayoutItemDeleteUndoCommand
106//
107
108QgsLayoutItemDeleteUndoCommand::QgsLayoutItemDeleteUndoCommand( QgsLayoutItem *item, const QString &text, int id, QUndoCommand *parent )
109 : QgsLayoutItemUndoCommand( item, text, id, parent )
110{
111 saveBeforeState();
112}
113
114bool QgsLayoutItemDeleteUndoCommand::mergeWith( const QUndoCommand * )
115{
116 return false;
117}
118
119void QgsLayoutItemDeleteUndoCommand::redo()
120{
121 if ( mFirstRun )
122 {
123 mFirstRun = false;
124 return;
125 }
126
127 QgsLayoutItem *item = layout()->itemByUuid( itemUuid() );
128 //Q_ASSERT_X( item, "QgsLayoutItemDeleteUndoCommand::redo", "could not find item to re-delete!" );
129
130 layout()->undoStack()->blockCommands( true );
131 if ( item )
132 layout()->removeLayoutItemPrivate( item );
133 layout()->undoStack()->blockCommands( false );
134}
135
136QgsLayoutItemAddItemCommand::QgsLayoutItemAddItemCommand( QgsLayoutItem *item, const QString &text, int id, QUndoCommand *parent )
137 : QgsLayoutItemUndoCommand( item, text, id, parent )
138{
139 saveAfterState();
140}
141
142bool QgsLayoutItemAddItemCommand::containsChange() const
143{
144 return true;
145}
146
147bool QgsLayoutItemAddItemCommand::mergeWith( const QUndoCommand * )
148{
149 return false;
150}
151
152void QgsLayoutItemAddItemCommand::undo()
153{
154 if ( mFirstRun )
155 {
156 mFirstRun = false;
157 return;
158 }
159
160 QgsLayoutItem *item = layout()->itemByUuid( itemUuid() );
161 if ( item )
162 {
163 layout()->removeLayoutItemPrivate( item );
164 }
165}
166
167
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...
QgsLayoutUndoStack * undoStack()
Returns a pointer to the layout's undo stack, which manages undo/redo states for the layout and it's ...
A container for the context for various read/write operations on 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 QgsDebugError(str)
Definition qgslogger.h:57