QGIS API Documentation  3.0.2-Girona (307d082)
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 #include "qgslayoutmultiframe.h"
20 #include "qgsreadwritecontext.h"
21 #include "qgslayout.h"
22 #include "qgsproject.h"
23 
25 QgsLayoutMultiFrameUndoCommand::QgsLayoutMultiFrameUndoCommand( QgsLayoutMultiFrame *frame, const QString &text, int id, QUndoCommand *parent )
26  : QgsAbstractLayoutUndoCommand( text, id, parent )
27  , mFrameUuid( frame->uuid() )
28  , mLayout( frame->layout() )
29  , mItemType( frame->type() )
30 {
31 
32 }
33 
34 bool QgsLayoutMultiFrameUndoCommand::mergeWith( const QUndoCommand *command )
35 {
36  if ( command->id() == 0 )
37  return false;
38 
39  const QgsLayoutMultiFrameUndoCommand *c = dynamic_cast<const QgsLayoutMultiFrameUndoCommand *>( command );
40  if ( !c )
41  {
42  return false;
43  }
44  if ( c->multiFrameUuid() != multiFrameUuid() )
45  return false;
46 
47  setAfterState( c->afterState() );
48  return true;
49 }
50 
51 void QgsLayoutMultiFrameUndoCommand::saveState( QDomDocument &stateDoc ) const
52 {
53  stateDoc.clear();
54  QDomElement documentElement = stateDoc.createElement( QStringLiteral( "ItemState" ) );
55 
56  QgsLayoutMultiFrame *item = mLayout->multiFrameByUuid( mFrameUuid );
57  Q_ASSERT_X( item, "QgsLayoutMultiFrameUndoCommand::saveState", "could not retrieve item for saving state" );
58 
59  item->writeXml( documentElement, stateDoc, QgsReadWriteContext(), true );
60  stateDoc.appendChild( documentElement );
61 }
62 
63 void QgsLayoutMultiFrameUndoCommand::restoreState( QDomDocument &stateDoc )
64 {
65  // find item by uuid...
66  QgsLayoutMultiFrame *item = mLayout->multiFrameByUuid( mFrameUuid );
67  if ( !item )
68  {
69  // uh oh - it's been deleted! we need to create a new instance
70  item = recreateItem( mItemType, mLayout );
71  }
72 
73  item->readXml( stateDoc.documentElement().firstChild().toElement(), stateDoc, QgsReadWriteContext(), true );
74  item->finalizeRestoreFromXml();
75  mLayout->project()->setDirty( true );
76 }
77 
78 QgsLayoutMultiFrame *QgsLayoutMultiFrameUndoCommand::recreateItem( int itemType, QgsLayout *layout )
79 {
81  mLayout->addMultiFrame( item );
82  return item;
83 }
84 
85 QString QgsLayoutMultiFrameUndoCommand::multiFrameUuid() const
86 {
87  return mFrameUuid;
88 }
89 
90 QgsLayout *QgsLayoutMultiFrameUndoCommand::layout() const
91 {
92  return mLayout;
93 }
94 
95 
96 //
97 // QgsLayoutMultiFrameDeleteUndoCommand
98 //
99 
100 QgsLayoutMultiFrameDeleteUndoCommand::QgsLayoutMultiFrameDeleteUndoCommand( QgsLayoutMultiFrame *item, const QString &text, int id, QUndoCommand *parent )
101  : QgsLayoutMultiFrameUndoCommand( item, text, id, parent )
102 {
103  saveBeforeState();
104 }
105 
106 bool QgsLayoutMultiFrameDeleteUndoCommand::mergeWith( const QUndoCommand * )
107 {
108  return false;
109 }
110 
111 void QgsLayoutMultiFrameDeleteUndoCommand::redo()
112 {
113  if ( mFirstRun )
114  {
115  mFirstRun = false;
116  return;
117  }
118 
119  QgsLayoutMultiFrame *item = layout()->multiFrameByUuid( multiFrameUuid() );
120  //Q_ASSERT_X( item, "QgsLayoutMultiFrameDeleteUndoCommand::redo", "could not find item to re-delete!" );
121 
122  if ( item )
123  {
124  layout()->removeMultiFrame( item );
125  delete item;
126  }
127 }
128 
129 QgsLayoutMultiFrameAddItemCommand::QgsLayoutMultiFrameAddItemCommand( QgsLayoutMultiFrame *frame, const QString &text, int id, QUndoCommand *parent )
130  : QgsLayoutMultiFrameUndoCommand( frame, text, id, parent )
131 {
132  saveAfterState();
133 }
134 
135 bool QgsLayoutMultiFrameAddItemCommand::containsChange() const
136 {
137  return true;
138 }
139 
140 bool QgsLayoutMultiFrameAddItemCommand::mergeWith( const QUndoCommand * )
141 {
142  return false;
143 }
144 
145 void QgsLayoutMultiFrameAddItemCommand::undo()
146 {
147  if ( mFirstRun )
148  {
149  mFirstRun = false;
150  return;
151  }
152 
153  QgsLayoutMultiFrame *item = layout()->multiFrameByUuid( multiFrameUuid() );
154  if ( item )
155  {
156  layout()->removeMultiFrame( item );
157  delete item;
158  }
159 }
160 
161 
The class is used as a container of context for various read/write operations on other objects...
QgsLayoutMultiFrame * multiFrameByUuid(const QString &uuid, bool includeTemplateUuids=false) const
Returns the layout multiframe with matching uuid unique identifier, or a nullptr if a matching multif...
Definition: qgslayout.cpp:276
Base class for commands to undo/redo layout and layout object changes.
bool readXml(const QDomElement &itemElement, const QDomDocument &document, const QgsReadWriteContext &context, bool includeFrames=false)
Sets the item state from a DOM element.
Abstract base class for layout items with the ability to distribute the content to several frames (Qg...
static QgsLayoutItemRegistry * layoutItemRegistry()
Returns the application&#39;s layout item registry, used for layout item types.
const QgsLayout * layout() const
Returns the layout the object is attached to.
QPointer< QgsLayout > mLayout
void removeMultiFrame(QgsLayoutMultiFrame *multiFrame)
Removes a multiFrame from the layout (but does not delete it).
Definition: qgslayout.cpp:576
virtual void finalizeRestoreFromXml()
Called after all pending items have been restored from XML.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:49
QgsLayoutFrame * frame(int index) const
Returns the child frame at a specified index from the multiframe.
QgsLayoutMultiFrame * createMultiFrame(int type, QgsLayout *layout) const
Creates a new instance of a layout multiframe given the multiframe type, and target layout...
bool writeXml(QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context, bool includeFrames=false) const
Stores the multiframe state in a DOM element.