QGIS API Documentation  2.99.0-Master (53aba61)
qgscomposeritemcommand.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscomposeritemcommand.cpp
3  --------------------------
4  begin : 2010-11-18
5  copyright : (C) 2010 by Marco Hugentobler
6  email : marco dot hugentobler at sourcepole dot ch
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 
18 #include "qgscomposeritemcommand.h"
19 #include "qgscomposeritem.h"
20 #include "qgscomposerframe.h"
21 #include "qgscomposermultiframe.h"
22 #include "qgscomposition.h"
23 #include "qgsproject.h"
24 #include "qgslogger.h"
25 
26 QgsComposerItemCommand::QgsComposerItemCommand( QgsComposerItem *item, const QString &text, QUndoCommand *parent )
27  : QUndoCommand( text, parent )
28  , mItem( item )
29  , mFrameNumber( 0 )
30  , mFirstRun( true )
31 {
32  //is item a frame?
33  QgsComposerFrame *frame = dynamic_cast<QgsComposerFrame *>( mItem );
34  if ( frame )
35  {
36  //store parent multiframe and frame index
37  mMultiFrame = frame->multiFrame();
39  }
40 }
41 
43 {
45 }
46 
48 {
49  if ( mFirstRun )
50  {
51  mFirstRun = false;
52  return;
53  }
55 }
56 
58 {
59  return !( mPreviousState.isNull() || mAfterState.isNull() || mPreviousState.toString() == mAfterState.toString() );
60 }
61 
63 {
64  QgsComposerItem *item = nullptr;
65  if ( mMultiFrame )
66  {
67  //item is a frame, so it needs to be handled differently
68  //in this case the target item is the matching frame number, as subsequent
69  //changes to the multiframe may have deleted mItem
71  {
72  item = mMultiFrame->frame( mFrameNumber );
73  }
74  }
75  else if ( mItem )
76  {
77  item = mItem;
78  }
79 
80  return item;
81 }
82 
84 {
86 }
87 
89 {
91 }
92 
93 void QgsComposerItemCommand::saveState( QDomDocument &stateDoc ) const
94 {
95  const QgsComposerItem *source = item();
96  if ( !source )
97  {
98  return;
99  }
100 
101  stateDoc.clear();
102  QDomElement documentElement = stateDoc.createElement( QStringLiteral( "ComposerItemState" ) );
103  source->writeXml( documentElement, stateDoc );
104  stateDoc.appendChild( documentElement );
105 }
106 
107 void QgsComposerItemCommand::restoreState( QDomDocument &stateDoc ) const
108 {
109  QgsComposerItem *destItem = item();
110  if ( !destItem )
111  {
112  return;
113  }
114 
115  destItem->readXml( stateDoc.documentElement().firstChild().toElement(), stateDoc );
116  destItem->repaint();
117  destItem->composition()->project()->setDirty( true );
118 }
119 
120 //
121 //QgsComposerMergeCommand
122 //
123 
125  : QgsComposerItemCommand( item, text )
126  , mContext( c )
127 {
128 }
129 
130 bool QgsComposerMergeCommand::mergeWith( const QUndoCommand *command )
131 {
132  QgsComposerItem *thisItem = item();
133  if ( !thisItem )
134  {
135  return false;
136  }
137 
138  const QgsComposerItemCommand *c = dynamic_cast<const QgsComposerItemCommand *>( command );
139  if ( !c || thisItem != c->item() )
140  {
141  return false;
142  }
143 
144  mAfterState = c->afterState();
145  return true;
146 }
147 
void setDirty(bool b=true)
Flag the project as dirty (modified).
Definition: qgsproject.cpp:410
QgsComposerMergeCommand(Context c, QgsComposerItem *item, const QString &text)
QDomDocument afterState() const
A item that forms part of a map composition.
void savePreviousState()
Saves current item state as previous state.
void undo() override
Reverses the command.
QgsComposerItem * item() const
Returns the target item the command applies to.
void saveState(QDomDocument &stateDoc) const
virtual bool readXml(const QDomElement &itemElem, const QDomDocument &doc)
Sets item state from DOM element.
bool containsChange() const
Returns true if previous state and after state are valid and different.
void repaint() override
Frame item for a composer multiframe item.
bool mergeWith(const QUndoCommand *command) override
void saveAfterState()
Saves current item state as after state.
bool mFirstRun
Flag to prevent the first redo() if the command is pushed to the undo stack.
Undo command to undo/redo all composer item related changes.
QDomDocument mAfterState
XML containing the state after executing the command.
const QgsComposition * composition() const
Returns the composition the item is attached to.
QgsProject * project() const
The project associated with the composition.
int frameCount() const
Returns the number of frames associated with this multiframe.
QgsComposerMultiFrame * multiFrame() const
Returns the parent multiframe for the frame.
QgsComposerItem * mItem
Target item of the command.
QgsComposerMultiFrame * mMultiFrame
Parameters for frame items Parent multiframe.
int frameIndex(QgsComposerFrame *frame) const
Returns the index of a frame within the multiframe.
void restoreState(QDomDocument &stateDoc) const
void redo() override
Replays the command.
virtual bool writeXml(QDomElement &elem, QDomDocument &doc) const
Stores item state in DOM element.
QgsComposerItemCommand(QgsComposerItem *item, const QString &text, QUndoCommand *parent=0)
QgsComposerFrame * frame(int i) const
Returns a child frame from the multiframe.
QDomDocument mPreviousState
XML that saves the state before executing the command.