Salome HOME
Merge remote-tracking branch 'remotes/origin/HigherLevelObjectsHistory'
[modules/shaper.git] / src / ModuleBase / ModuleBase_Operation.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "ModuleBase_Operation.h"
21
22 #include "ModuleBase_OperationDescription.h"
23 #include "ModuleBase_ModelWidget.h"
24 #include "ModuleBase_ViewerPrs.h"
25 #include "ModuleBase_IPropertyPanel.h"
26 #include "ModuleBase_ISelection.h"
27 #include "ModuleBase_IViewer.h"
28
29 #include <ModelAPI_AttributeDouble.h>
30 #include <ModelAPI_Document.h>
31 #include <ModelAPI_Feature.h>
32 #include <ModelAPI_Data.h>
33 #include <ModelAPI_Document.h>
34 #include <ModelAPI_Events.h>
35 #include <ModelAPI_Result.h>
36 #include <ModelAPI_Object.h>
37 #include <ModelAPI_Validator.h>
38 #include <ModelAPI_Session.h>
39
40 #include <GeomAPI_Pnt2d.h>
41
42 #include <Events_Loop.h>
43
44 #include <QTimer>
45
46 #ifdef _DEBUG
47 #include <QDebug>
48 #endif
49
50 ModuleBase_Operation::ModuleBase_Operation(const QString& theId, QObject* theParent)
51     : QObject(theParent),
52       myIsModified(false),
53       myPropertyPanel(NULL)
54 {
55   myDescription = new ModuleBase_OperationDescription(theId);
56 }
57
58 ModuleBase_Operation::~ModuleBase_Operation()
59 {
60   delete myDescription;
61 }
62
63 const QStringList& ModuleBase_Operation::grantedOperationIds() const
64 {
65   return myGrantedIds;
66 }
67
68 void ModuleBase_Operation::setGrantedOperationIds(const QStringList& theList)
69 {
70   myGrantedIds = theList;
71 }
72
73 QString ModuleBase_Operation::id() const
74 {
75   return getDescription()->operationId();
76 }
77
78 bool ModuleBase_Operation::isValid() const
79 {
80   return true;
81 }
82
83 bool ModuleBase_Operation::canBeCommitted() const
84 {
85   return isValid();
86 }
87
88 bool ModuleBase_Operation::start()
89 {
90   myIsModified = false;
91
92   ModelAPI_Session::get()->startOperation(id().toStdString());
93
94   startOperation();
95   emit started();
96
97   return true;
98 }
99
100 void ModuleBase_Operation::postpone()
101 {
102   postponeOperation();
103   emit postponed();
104 }
105
106 void ModuleBase_Operation::resume()
107 {
108   resumeOperation();
109   emit resumed();
110 }
111
112 void ModuleBase_Operation::abort()
113 {
114   // the viewer update should be blocked in order to avoid the features blinking before they are
115   // hidden
116   //std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
117   //    new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
118   //Events_Loop::loop()->send(aMsg);
119
120   ModelAPI_Session::get()->abortOperation();
121
122   emit stopped();
123   // the viewer update should be unblocked in order to avoid the features blinking before they are
124   // hidden
125   //aMsg = std::shared_ptr<Events_Message>(
126   //              new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
127   //Events_Loop::loop()->send(aMsg);
128
129   emit aborted();
130 }
131
132 bool ModuleBase_Operation::commit()
133 {
134   if (canBeCommitted()) {
135     ModuleBase_IPropertyPanel* aPanel = propertyPanel();
136     if (aPanel)
137       aPanel->onAcceptData();
138
139     SessionPtr aMgr = ModelAPI_Session::get();
140
141     commitOperation();
142     aMgr->finishOperation();
143
144     stopOperation();
145     emit stopped();
146     emit committed();
147
148     afterCommitOperation();
149     return true;
150   }
151   return false;
152 }
153
154 void ModuleBase_Operation::onValuesChanged()
155 {
156   myIsModified = true;
157 }
158
159 void ModuleBase_Operation::onValueStateChanged(int thePreviousState)
160 {
161   if (propertyPanel()) {
162     ModuleBase_ModelWidget* aWidget = propertyPanel()->activeWidget();
163     if (aWidget) {
164       if (aWidget->getValueState() == ModuleBase_ModelWidget::ModifiedInPP)
165         myIsModified = true;
166     }
167   }
168 }
169
170 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
171 {
172   myPropertyPanel = theProp;
173 }
174
175 bool ModuleBase_Operation::isGranted(QString theId) const
176 {
177   return myGrantedIds.contains(theId);
178 }