QGIS API Documentation  2.7.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 
25 QgsComposerItemCommand::QgsComposerItemCommand( QgsComposerItem* item, const QString& text, QUndoCommand* parent )
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 {
63  return !( mPreviousState.isNull() || mAfterState.isNull() || mPreviousState.toString() == mAfterState.toString() );
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 
97 void QgsComposerItemCommand::saveState( QDomDocument& stateDoc ) const
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 
111 void QgsComposerItemCommand::restoreState( QDomDocument& stateDoc ) const
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 
138 bool QgsComposerMergeCommand::mergeWith( const QUndoCommand * command )
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 redo()
Replays the command.
virtual bool writeXML(QDomElement &elem, QDomDocument &doc) const
Stores item state in DOM element.
QgsComposerMergeCommand(Context c, QgsComposerItem *item, const QString &text)
A item that forms part of a map composition.
void savePreviousState()
Saves current item state as previous state.
void saveState(QDomDocument &stateDoc) const
void restoreState(QDomDocument &stateDoc) const
bool containsChange() const
Returns true if previous state and after state are valid and different.
bool mergeWith(const QUndoCommand *command)
int frameCount() const
Returns the number of frames associated with this multiframe.
Frame item for a composer multiframe item.
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.
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:362
QDomDocument afterState() const
int frameIndex(QgsComposerFrame *frame) const
Returns the index of a frame within the multiframe.
QgsComposerFrame * frame(int i) const
Returns a child frame from the multiframe.
QgsComposerItemCommand(QgsComposerItem *item, const QString &text, QUndoCommand *parent=0)
QgsComposerMultiFrame * multiFrame() const
Returns the parent multiframe for the frame.
QgsComposerItem * item() const
Returns the target item the command applies to.
QDomDocument mPreviousState
XML that saves the state before executing the command.
void undo()
Reverses the command.
void dirty(bool b)
Definition: qgsproject.cpp:396