QGIS API Documentation  3.8.0-Zanzibar (11aff65)
qgslayoutserializableobject.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayoutserializableobject.cpp
3  -------------------------------
4  begin : July 2017
5  copyright : (C) 2017 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
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 
19 #include "qgsreadwritecontext.h"
20 #include "qgslayout.h"
21 #include "qgsproject.h"
22 
24 class QgsLayoutSerializableObjectUndoCommand: public QgsAbstractLayoutUndoCommand
25 {
26  public:
27 
28  QgsLayoutSerializableObjectUndoCommand( QgsLayoutSerializableObject *object, const QString &text, int id, QUndoCommand *parent SIP_TRANSFERTHIS = nullptr )
29  : QgsAbstractLayoutUndoCommand( text, id, parent )
30  , mObject( object )
31  {}
32 
33  bool mergeWith( const QUndoCommand *command ) override
34  {
35  if ( command->id() == 0 )
36  return false;
37 
38  const QgsLayoutSerializableObjectUndoCommand *c = dynamic_cast<const QgsLayoutSerializableObjectUndoCommand *>( command );
39  if ( !c )
40  {
41  return false;
42  }
43 
44  if ( mObject->stringType() != c->mObject->stringType() )
45  return false;
46 
47  setAfterState( c->afterState() );
48  return true;
49  }
50 
51  protected:
52 
53  void saveState( QDomDocument &stateDoc ) const override
54  {
55  stateDoc.clear();
56  QDomElement documentElement = stateDoc.createElement( QStringLiteral( "UndoState" ) );
57  mObject->writeXml( documentElement, stateDoc, QgsReadWriteContext() );
58  stateDoc.appendChild( documentElement );
59  }
60  void restoreState( QDomDocument &stateDoc ) override
61  {
62  if ( !mObject )
63  {
64  return;
65  }
66 
67  mObject->readXml( stateDoc.documentElement().firstChild().toElement(), stateDoc, QgsReadWriteContext() );
68  mObject->layout()->project()->setDirty( true );
69  }
70 
71  private:
72 
73  QgsLayoutSerializableObject *mObject = nullptr;
74 };
76 
77 
78 QgsAbstractLayoutUndoCommand *QgsLayoutSerializableObject::createCommand( const QString &text, int id, QUndoCommand *parent )
79 {
80  return new QgsLayoutSerializableObjectUndoCommand( this, text, id, parent );
81 }
The class is used as a container of context for various read/write operations on other objects...
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:46
virtual void restoreState(QDomDocument &stateDoc)=0
Restores the state of the object from the specified stateDoc.
Base class for commands to undo/redo layout and layout object changes.
QgsAbstractLayoutUndoCommand * createCommand(const QString &text, int id, QUndoCommand *parent=nullptr) override
Creates a new layout undo command with the specified text and parent.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
virtual void saveState(QDomDocument &stateDoc) const =0
Saves the state of the object to the specified stateDoc.
void setAfterState(const QDomDocument &stateDoc)
Manually sets the after state for the command.
An interface for layout objects which can be stored and read from DOM elements.