QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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