QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 "qgsproject.h"
23 #include "qgslogger.h"
24 
26  : QUndoCommand( text, parent )
27  , mItem( item )
28  , mMultiFrame( nullptr )
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 {
44 }
45 
47 {
49 }
50 
52 {
53  if ( mFirstRun )
54  {
55  mFirstRun = false;
56  return;
57  }
59 }
60 
62 {
64 }
65 
67 {
68  QgsComposerItem* item = nullptr;
69  if ( mMultiFrame )
70  {
71  //item is a frame, so it needs to be handled differently
72  //in this case the target item is the matching frame number, as subsequent
73  //changes to the multiframe may have deleted mItem
75  {
76  item = mMultiFrame->frame( mFrameNumber );
77  }
78  }
79  else if ( mItem )
80  {
81  item = mItem;
82  }
83 
84  return item;
85 }
86 
88 {
90 }
91 
93 {
95 }
96 
98 {
99  const QgsComposerItem* source = item();
100  if ( !source )
101  {
102  return;
103  }
104 
105  stateDoc.clear();
106  QDomElement documentElement = stateDoc.createElement( "ComposerItemState" );
107  source->writeXML( documentElement, stateDoc );
108  stateDoc.appendChild( documentElement );
109 }
110 
112 {
113  QgsComposerItem* destItem = item();
114  if ( !destItem )
115  {
116  return;
117  }
118 
119  destItem->readXML( stateDoc.documentElement().firstChild().toElement(), stateDoc );
120  destItem->repaint();
121  QgsProject::instance()->setDirty( true );
122 }
123 
124 //
125 //QgsComposerMergeCommand
126 //
127 
129  : QgsComposerItemCommand( item, text )
130  , mContext( c )
131 {
132 }
133 
135 {
136 }
137 
139 {
140  QgsComposerItem* thisItem = item();
141  if ( !thisItem )
142  {
143  return false;
144  }
145 
146  const QgsComposerItemCommand* c = dynamic_cast<const QgsComposerItemCommand*>( command );
147  if ( !c || thisItem != c->item() )
148  {
149  return false;
150  }
151 
152  mAfterState = c->afterState();
153  return true;
154 }
155 
void setDirty(bool b=true)
Flag the project as dirty (modified).
Definition: qgsproject.cpp:410
QgsComposerItemCommand(QgsComposerItem *item, const QString &text, QUndoCommand *parent=nullptr)
QDomNode appendChild(const QDomNode &newChild)
QgsComposerMergeCommand(Context c, QgsComposerItem *item, const QString &text)
QString toString(int indent) const
QDomDocument afterState() const
A item that forms part of a map composition.
void savePreviousState()
Saves current item state as previous state.
QDomElement documentElement() const
virtual bool writeXML(QDomElement &elem, QDomDocument &doc) const
Stores item state in DOM element.
void undo() override
Reverses the command.
QDomElement toElement() const
QgsComposerItem * item() const
Returns the target item the command applies to.
void saveState(QDomDocument &stateDoc) const
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
QString text() const
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.
bool isNull() const
void clear()
QDomNode firstChild() const
Undo command to undo/redo all composer item related changes.
QDomDocument mAfterState
XML containing the state after executing the command.
int frameCount() const
Returns the number of frames associated with this multiframe.
virtual bool readXML(const QDomElement &itemElem, const QDomDocument &doc)
Sets item state from DOM element.
QgsComposerMultiFrame * multiFrame() const
Returns the parent multiframe for the frame.
QgsComposerItem * mItem
Target item of the command.
QgsComposerMultiFrame * mMultiFrame
Parameters for frame items.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Definition: qgsproject.cpp:382
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.
QDomElement createElement(const QString &tagName)
QObject * parent() const
QgsComposerFrame * frame(int i) const
Returns a child frame from the multiframe.
QDomDocument mPreviousState
XML that saves the state before executing the command.