QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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
24class 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
78QgsAbstractLayoutUndoCommand *QgsLayoutSerializableObject::createCommand( const QString &text, int id, QUndoCommand *parent )
79{
80 return new QgsLayoutSerializableObjectUndoCommand( this, text, id, parent );
81}
Base class for commands to undo/redo layout and layout object changes.
An interface for layout objects which can be stored and read from DOM elements.
QgsAbstractLayoutUndoCommand * createCommand(const QString &text, int id, QUndoCommand *parent=nullptr) override
Creates a new layout undo command with the specified text and parent.
The class is used as a container of context for various read/write operations on other objects.
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
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53