QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
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 #include "qgsprocessingmodelalgorithm.h"
19 
20 
21 QgsModelUndoCommand::QgsModelUndoCommand( QgsProcessingModelAlgorithm *model, const QString &text, int id, QUndoCommand *parent )
22  : QUndoCommand( text, parent )
23  , mModel( model )
24  , mId( id )
25 {
26  mBeforeState = model->toVariant();
27 }
28 
29 void QgsModelUndoCommand::saveAfterState()
30 {
31  mAfterState = mModel->toVariant();
32 }
33 
34 int QgsModelUndoCommand::id() const
35 {
36  return mId;
37 }
38 
39 void QgsModelUndoCommand::undo()
40 {
41  QUndoCommand::undo();
42 
43  // some settings must live "outside" the undo stack
44  const QVariantMap params = mModel->designerParameterValues();
45 
46  mModel->loadVariant( mBeforeState );
47 
48  mModel->setDesignerParameterValues( params );
49 }
50 
51 void QgsModelUndoCommand::redo()
52 {
53  if ( mFirstRun )
54  {
55  mFirstRun = false;
56  return;
57  }
58  QUndoCommand::redo();
59 
60  // some settings must live "outside" the undo stack
61  const QVariantMap params = mModel->designerParameterValues();
62 
63  mModel->loadVariant( mAfterState );
64 
65  mModel->setDesignerParameterValues( params );
66 }
67 
68 bool QgsModelUndoCommand::mergeWith( const QUndoCommand *other )
69 {
70  if ( other->id() == 0 || other->id() != mId )
71  return false;
72 
73  if ( const QgsModelUndoCommand *c = dynamic_cast<const QgsModelUndoCommand *>( other ) )
74  {
75  mAfterState = c->mAfterState;
76  return true;
77  }
78  else
79  {
80  return false;
81  }
82 }
83 
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