QGIS API Documentation 3.99.0-Master (c22de0620c0)
Loading...
Searching...
No Matches
qgsmodelundocommand.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmodelundocommand.cpp
3 ----------------------------------
4 Date : March 2020
5 Copyright : (C) 2020 Nyall Dawson
6 Email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgsmodelundocommand.h"
17
19
21
22
23QgsModelUndoCommand::QgsModelUndoCommand( QgsProcessingModelAlgorithm *model, const QString &text, CommandOperation id, QUndoCommand *parent )
24 : QUndoCommand( text, parent )
25 , mModel( model )
26 , mOperation( id )
27{
28 mBeforeState = model->toVariant();
29}
30
31QgsModelUndoCommand::QgsModelUndoCommand( QgsProcessingModelAlgorithm *model, const QString &text, const QString &idString, QUndoCommand *parent )
32 : QgsModelUndoCommand( model, text, CommandOperation::Unknown, parent )
33{
34 mIdString = idString;
35}
36
37void QgsModelUndoCommand::saveAfterState()
38{
39 mAfterState = mModel->toVariant();
40}
41
42int QgsModelUndoCommand::id() const
43{
44 // QUndoStack::push() will only try to merge two commands if they have the same ID, and the ID is not -1.
45 // so we always return the same (non -1) value here, and implement the actual merge compatibility logic in
46 // mergeWith
47 return 0;
48}
49
50void QgsModelUndoCommand::undo()
51{
52 QUndoCommand::undo();
53
54 // some settings must live "outside" the undo stack
55 const QVariantMap params = mModel->designerParameterValues();
56
57 mModel->loadVariant( mBeforeState );
58
59 mModel->setDesignerParameterValues( params );
60}
61
62void QgsModelUndoCommand::redo()
63{
64 if ( mFirstRun )
65 {
66 mFirstRun = false;
67 return;
68 }
69 QUndoCommand::redo();
70
71 // some settings must live "outside" the undo stack
72 const QVariantMap params = mModel->designerParameterValues();
73
74 mModel->loadVariant( mAfterState );
75
76 mModel->setDesignerParameterValues( params );
77}
78
79bool QgsModelUndoCommand::mergeWith( const QUndoCommand *other )
80{
81 if ( const QgsModelUndoCommand *c = dynamic_cast<const QgsModelUndoCommand *>( other ) )
82 {
83 bool canMerge = false;
84 if ( !c->idString().isEmpty() && c->idString() == mIdString )
85 {
86 canMerge = true;
87 }
88 else if ( c->operation() != CommandOperation::Unknown && c->operation() == mOperation )
89 {
90 canMerge = true;
91 }
92
93 if ( !canMerge )
94 return false;
95
96 mAfterState = c->mAfterState;
97 return true;
98 }
99 else
100 {
101 return false;
102 }
103}
104
@ Unknown
Unknown/invalid format.
Definition qgswmsutils.h:42
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