QGIS API Documentation  2.99.0-Master (90ae728)
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  , mMultiFrame( nullptr )
30  , mFrameNumber( 0 )
31  , mFirstRun( true )
32 {
33  //is item a frame?
34  QgsComposerFrame* frame = dynamic_cast<QgsComposerFrame*>( mItem );
35  if ( frame )
36  {
37  //store parent multiframe and frame index
38  mMultiFrame = frame->multiFrame();
40  }
41 }
42 
44 {
46 }
47 
49 {
50  if ( mFirstRun )
51  {
52  mFirstRun = false;
53  return;
54  }
56 }
57 
59 {
60  return !( mPreviousState.isNull() || mAfterState.isNull() || mPreviousState.toString() == mAfterState.toString() );
61 }
62 
64 {
65  QgsComposerItem* item = nullptr;
66  if ( mMultiFrame )
67  {
68  //item is a frame, so it needs to be handled differently
69  //in this case the target item is the matching frame number, as subsequent
70  //changes to the multiframe may have deleted mItem
72  {
73  item = mMultiFrame->frame( mFrameNumber );
74  }
75  }
76  else if ( mItem )
77  {
78  item = mItem;
79  }
80 
81  return item;
82 }
83 
85 {
87 }
88 
90 {
92 }
93 
94 void QgsComposerItemCommand::saveState( QDomDocument& stateDoc ) const
95 {
96  const QgsComposerItem* source = item();
97  if ( !source )
98  {
99  return;
100  }
101 
102  stateDoc.clear();
103  QDomElement documentElement = stateDoc.createElement( QStringLiteral( "ComposerItemState" ) );
104  source->writeXml( documentElement, stateDoc );
105  stateDoc.appendChild( documentElement );
106 }
107 
108 void QgsComposerItemCommand::restoreState( QDomDocument& stateDoc ) const
109 {
110  QgsComposerItem* destItem = item();
111  if ( !destItem )
112  {
113  return;
114  }
115 
116  destItem->readXml( stateDoc.documentElement().firstChild().toElement(), stateDoc );
117  destItem->repaint();
118  destItem->composition()->project()->setDirty( true );
119 }
120 
121 //
122 //QgsComposerMergeCommand
123 //
124 
126  : QgsComposerItemCommand( item, text )
127  , mContext( c )
128 {
129 }
130 
131 bool QgsComposerMergeCommand::mergeWith( const QUndoCommand * command )
132 {
133  QgsComposerItem* thisItem = item();
134  if ( !thisItem )
135  {
136  return false;
137  }
138 
139  const QgsComposerItemCommand* c = dynamic_cast<const QgsComposerItemCommand*>( command );
140  if ( !c || thisItem != c->item() )
141  {
142  return false;
143  }
144 
145  mAfterState = c->afterState();
146  return true;
147 }
148 
void setDirty(bool b=true)
Flag the project as dirty (modified).
Definition: qgsproject.cpp:386
QgsComposerItemCommand(QgsComposerItem *item, const QString &text, QUndoCommand *parent=nullptr)
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.
QgsComposerFrame * frame(int i) const
Returns a child frame from the multiframe.
QDomDocument mPreviousState
XML that saves the state before executing the command.