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