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