QGIS API Documentation 3.99.0-Master (09f76ad7019)
Loading...
Searching...
No Matches
qgslayoutmultiframeundocommand.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayoutmultiframeundocommand.cpp
3 ----------------------
4 begin : October 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 "qgsfeedback.h"
21#include "qgslayout.h"
22#include "qgslayoutmultiframe.h"
23#include "qgsproject.h"
24#include "qgsreadwritecontext.h"
25
26#include <QString>
27
28using namespace Qt::StringLiterals;
29
31QgsLayoutMultiFrameUndoCommand::QgsLayoutMultiFrameUndoCommand( QgsLayoutMultiFrame *frame, const QString &text, int id, QUndoCommand *parent )
32 : QgsAbstractLayoutUndoCommand( text, id, parent )
33 , mFrameUuid( frame->uuid() )
34 , mLayout( frame->layout() )
35 , mItemType( frame->type() )
36{
37
38}
39
40bool QgsLayoutMultiFrameUndoCommand::mergeWith( const QUndoCommand *command )
41{
42 if ( command->id() == 0 )
43 return false;
44
45 const QgsLayoutMultiFrameUndoCommand *c = dynamic_cast<const QgsLayoutMultiFrameUndoCommand *>( command );
46 if ( !c )
47 {
48 return false;
49 }
50 if ( c->multiFrameUuid() != multiFrameUuid() )
51 return false;
52
53 setAfterState( c->afterState() );
54 return true;
55}
56
57void QgsLayoutMultiFrameUndoCommand::saveState( QDomDocument &stateDoc ) const
58{
59 stateDoc.clear();
60 QDomElement documentElement = stateDoc.createElement( u"ItemState"_s );
61
62 QgsLayoutMultiFrame *item = mLayout->multiFrameByUuid( mFrameUuid );
63 Q_ASSERT_X( item, "QgsLayoutMultiFrameUndoCommand::saveState", "could not retrieve item for saving state" );
64
65 item->writeXml( documentElement, stateDoc, QgsReadWriteContext(), true );
66 stateDoc.appendChild( documentElement );
67}
68
69void QgsLayoutMultiFrameUndoCommand::restoreState( QDomDocument &stateDoc )
70{
71 // find item by uuid...
72 QgsLayoutMultiFrame *item = mLayout->multiFrameByUuid( mFrameUuid );
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(), true );
81 mLayout->project()->setDirty( true );
82}
83
84QgsLayoutMultiFrame *QgsLayoutMultiFrameUndoCommand::recreateItem( int itemType, QgsLayout *layout )
85{
87 mLayout->addMultiFrame( item );
88 return item;
89}
90
91QString QgsLayoutMultiFrameUndoCommand::multiFrameUuid() const
92{
93 return mFrameUuid;
94}
95
96QgsLayout *QgsLayoutMultiFrameUndoCommand::layout() const
97{
98 return mLayout;
99}
100
101
102//
103// QgsLayoutMultiFrameDeleteUndoCommand
104//
105
106QgsLayoutMultiFrameDeleteUndoCommand::QgsLayoutMultiFrameDeleteUndoCommand( QgsLayoutMultiFrame *item, const QString &text, int id, QUndoCommand *parent )
107 : QgsLayoutMultiFrameUndoCommand( item, text, id, parent )
108{
109 saveBeforeState();
110}
111
112bool QgsLayoutMultiFrameDeleteUndoCommand::mergeWith( const QUndoCommand * )
113{
114 return false;
115}
116
117void QgsLayoutMultiFrameDeleteUndoCommand::redo()
118{
119 if ( mFirstRun )
120 {
121 mFirstRun = false;
122 return;
123 }
124
125 QgsLayoutMultiFrame *item = layout()->multiFrameByUuid( multiFrameUuid() );
126 //Q_ASSERT_X( item, "QgsLayoutMultiFrameDeleteUndoCommand::redo", "could not find item to re-delete!" );
127
128 if ( item )
129 {
130 layout()->removeMultiFrame( item );
131 delete item;
132 }
133}
134
135QgsLayoutMultiFrameAddItemCommand::QgsLayoutMultiFrameAddItemCommand( QgsLayoutMultiFrame *frame, const QString &text, int id, QUndoCommand *parent )
136 : QgsLayoutMultiFrameUndoCommand( frame, text, id, parent )
137{
138 saveAfterState();
139}
140
141bool QgsLayoutMultiFrameAddItemCommand::containsChange() const
142{
143 return true;
144}
145
146bool QgsLayoutMultiFrameAddItemCommand::mergeWith( const QUndoCommand * )
147{
148 return false;
149}
150
151void QgsLayoutMultiFrameAddItemCommand::undo()
152{
153 if ( mFirstRun )
154 {
155 mFirstRun = false;
156 return;
157 }
158
159 QgsLayoutMultiFrame *item = layout()->multiFrameByUuid( multiFrameUuid() );
160 if ( item )
161 {
162 layout()->removeMultiFrame( item );
163 delete 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.
QgsLayoutMultiFrame * createMultiFrame(int type, QgsLayout *layout) const
Creates a new instance of a layout multiframe given the multiframe type, and target layout.
Abstract base class for layout items with the ability to distribute the content to several frames (Qg...
bool writeXml(QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context, bool includeFrames=false) const
Stores the multiframe 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, bool includeFrames=false)
Sets the item state from a DOM element.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition qgslayout.h:50
void removeMultiFrame(QgsLayoutMultiFrame *multiFrame)
Removes a multiFrame from the layout (but does not delete it).
QgsLayoutMultiFrame * multiFrameByUuid(const QString &uuid, bool includeTemplateUuids=false) const
Returns the layout multiframe with matching uuid unique identifier, or nullptr if a matching multifra...
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