QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties 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 "qgsproject.h"
21 
22 QgsComposerItemCommand::QgsComposerItemCommand( QgsComposerItem* item, const QString& text, QUndoCommand* parent ):
23  QUndoCommand( text, parent ), mItem( item ), mFirstRun( true )
24 {
25 }
26 
28 {
29 }
30 
32 {
34 }
35 
37 {
38  if ( mFirstRun )
39  {
40  mFirstRun = false;
41  return;
42  }
44 }
45 
47 {
48  return !( mPreviousState.isNull() || mAfterState.isNull() || mPreviousState.toString() == mAfterState.toString() );
49 }
50 
52 {
54 }
55 
57 {
59 }
60 
61 void QgsComposerItemCommand::saveState( QDomDocument& stateDoc ) const
62 {
63  if ( mItem )
64  {
65  stateDoc.clear();
66  QDomElement documentElement = stateDoc.createElement( "ComposerItemState" );
67  mItem->writeXML( documentElement, stateDoc );
68  stateDoc.appendChild( documentElement );
69  }
70 }
71 
72 void QgsComposerItemCommand::restoreState( QDomDocument& stateDoc ) const
73 {
74  if ( mItem )
75  {
76  mItem->readXML( stateDoc.documentElement().firstChild().toElement(), stateDoc );
77  mItem->repaint();
78  QgsProject::instance()->dirty( true );
79  }
80 }
81 
82 QgsComposerMergeCommand::QgsComposerMergeCommand( Context c, QgsComposerItem* item, const QString& text ): QgsComposerItemCommand( item, text ), mContext( c )
83 {
84 }
85 
87 {
88 }
89 
90 bool QgsComposerMergeCommand::mergeWith( const QUndoCommand * command )
91 {
92  const QgsComposerItemCommand* c = dynamic_cast<const QgsComposerItemCommand*>( command );
93  if ( !c || mItem != c->item() )
94  {
95  return false;
96  }
97  mAfterState = c->afterState();
98  return true;
99 }
100 
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)
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.
const QgsComposerItem * item() const
static QgsProject * instance()
access to canonical QgsProject instance
Definition: qgsproject.cpp:362
QDomDocument afterState() const
QgsComposerItemCommand(QgsComposerItem *item, const QString &text, QUndoCommand *parent=0)
QDomDocument mPreviousState
XML that saves the state before executing the command.
void undo()
Reverses the command.
void dirty(bool b)
Definition: qgsproject.cpp:396