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"
18 ///@cond NOT_STABLE
19 
20 
QgsModelUndoCommand(QgsProcessingModelAlgorithm * model,const QString & text,int id,QUndoCommand * parent)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 
saveAfterState()29 void QgsModelUndoCommand::saveAfterState()
30 {
31   mAfterState = mModel->toVariant();
32 }
33 
id() const34 int QgsModelUndoCommand::id() const
35 {
36   return mId;
37 }
38 
undo()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 
redo()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 
mergeWith(const QUndoCommand * other)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 
84 ///@endcond
85