QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
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
20#include "qgslayout.h"
21#include "qgsproject.h"
22#include "qgsreadwritecontext.h"
23
24#include <QString>
25
26using namespace Qt::StringLiterals;
27
29class QgsLayoutSerializableObjectUndoCommand: public QgsAbstractLayoutUndoCommand
30{
31 public:
32
33 QgsLayoutSerializableObjectUndoCommand( QgsLayoutSerializableObject *object, const QString &text, int id, QUndoCommand *parent SIP_TRANSFERTHIS = nullptr )
34 : QgsAbstractLayoutUndoCommand( text, id, parent )
35 , mObject( object )
36 {}
37
38 bool mergeWith( const QUndoCommand *command ) override
39 {
40 if ( command->id() == 0 )
41 return false;
42
43 const QgsLayoutSerializableObjectUndoCommand *c = dynamic_cast<const QgsLayoutSerializableObjectUndoCommand *>( command );
44 if ( !c )
45 {
46 return false;
47 }
48
49 if ( mObject->stringType() != c->mObject->stringType() )
50 return false;
51
52 setAfterState( c->afterState() );
53 return true;
54 }
55
56 protected:
57
58 void saveState( QDomDocument &stateDoc ) const override
59 {
60 stateDoc.clear();
61 QDomElement documentElement = stateDoc.createElement( u"UndoState"_s );
62 mObject->writeXml( documentElement, stateDoc, QgsReadWriteContext() );
63 stateDoc.appendChild( documentElement );
64 }
65 void restoreState( QDomDocument &stateDoc ) override
66 {
67 if ( !mObject )
68 {
69 return;
70 }
71
72 mObject->readXml( stateDoc.documentElement().firstChild().toElement(), stateDoc, QgsReadWriteContext() );
73 mObject->layout()->project()->setDirty( true );
74 }
75
76 private:
77
78 QgsLayoutSerializableObject *mObject = nullptr;
79};
81
82
83QgsAbstractLayoutUndoCommand *QgsLayoutSerializableObject::createCommand( const QString &text, int id, QUndoCommand *parent )
84{
85 return new QgsLayoutSerializableObjectUndoCommand( this, text, id, parent );
86}
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
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:53