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