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