QGIS API Documentation  2.11.0-Master
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( 0 )
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 = 0;
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()->dirty( 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 
virtual bool writeXML(QDomElement &elem, QDomDocument &doc) const
Stores item state in DOM element.
QDomNode appendChild(const QDomNode &newChild)
QgsComposerMergeCommand(Context c, QgsComposerItem *item, const QString &text)
QString toString(int indent) const
A item that forms part of a map composition.
void savePreviousState()
Saves current item state as previous state.
QDomElement documentElement() const
void undo() override
Reverses the command.
void saveState(QDomDocument &stateDoc) const
void restoreState(QDomDocument &stateDoc) const
QDomElement toElement() const
bool containsChange() const
Returns true if previous state and after state are valid and different.
void repaint() override
int frameCount() const
Returns the number of frames associated with this multiframe.
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.
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.
virtual bool readXML(const QDomElement &itemElem, const QDomDocument &doc)
Sets item state from DOM element.
QgsComposerItem * mItem
Target item of the command.
QgsComposerMultiFrame * mMultiFrame
Parameters for frame items.
static QgsProject * instance()
access to canonical QgsProject instance
Definition: qgsproject.cpp:352
QDomDocument afterState() const
int frameIndex(QgsComposerFrame *frame) const
Returns the index of a frame within the multiframe.
void redo() override
Replays the command.
QgsComposerFrame * frame(int i) const
Returns a child frame from the multiframe.
QDomElement createElement(const QString &tagName)
QgsComposerItemCommand(QgsComposerItem *item, const QString &text, QUndoCommand *parent=0)
QgsComposerMultiFrame * multiFrame() const
Returns the parent multiframe for the frame.
QObject * parent() const
QgsComposerItem * item() const
Returns the target item the command applies to.
QDomDocument mPreviousState
XML that saves the state before executing the command.
void dirty(bool b)
Definition: qgsproject.cpp:381