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