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