Salome HOME
001d22c59de99fed04b3135add33ad87560120a6
[modules/shaper.git] / src / ModuleBase / ModuleBase_Operation.cpp
1 // Copyright (C) 2014-2021  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 #include <Config_WidgetAPI.h>
44 #include <Config_Keywords.h>
45
46 #include <QTimer>
47
48 #ifdef _DEBUG
49 #include <QDebug>
50 #endif
51
52 ModuleBase_Operation::ModuleBase_Operation(const QString& theId, QObject* theParent)
53     : QObject(theParent),
54       myIsModified(false),
55       myPropertyPanel(NULL),
56   myHideFacesVisibilityState(false)
57 {
58   myDescription = new ModuleBase_OperationDescription(theId);
59 }
60
61 ModuleBase_Operation::~ModuleBase_Operation()
62 {
63   delete myDescription;
64 }
65
66 const QStringList& ModuleBase_Operation::grantedOperationIds() const
67 {
68   return myGrantedIds;
69 }
70
71 void ModuleBase_Operation::setGrantedOperationIds(const QStringList& theList)
72 {
73   myGrantedIds = theList;
74 }
75
76 QString ModuleBase_Operation::id() const
77 {
78   return getDescription()->operationId();
79 }
80
81 bool ModuleBase_Operation::isValid() const
82 {
83   return true;
84 }
85
86 bool ModuleBase_Operation::canBeCommitted() const
87 {
88   return isValid();
89 }
90
91 bool ModuleBase_Operation::start()
92 {
93   myIsModified = false;
94
95   ModelAPI_Session::get()->startOperation(id().toStdString());
96
97   startOperation();
98   emit started();
99
100   return true;
101 }
102
103 void ModuleBase_Operation::postpone()
104 {
105   postponeOperation();
106   emit postponed();
107 }
108
109 void ModuleBase_Operation::resume()
110 {
111   resumeOperation();
112   emit resumed();
113 }
114
115 void ModuleBase_Operation::abort()
116 {
117   // the viewer update should be blocked in order to avoid the features blinking before they are
118   // hidden
119   //std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
120   //    new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
121   //Events_Loop::loop()->send(aMsg);
122
123   ModelAPI_Session::get()->abortOperation();
124
125   emit stopped();
126   // the viewer update should be unblocked in order to avoid the features blinking before they are
127   // hidden
128   //aMsg = std::shared_ptr<Events_Message>(
129   //              new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
130   //Events_Loop::loop()->send(aMsg);
131
132   emit aborted();
133 }
134
135 bool ModuleBase_Operation::commit()
136 {
137   if (canBeCommitted()) {
138     ModuleBase_IPropertyPanel* aPanel = propertyPanel();
139     if (aPanel)
140       aPanel->onAcceptData();
141
142     SessionPtr aMgr = ModelAPI_Session::get();
143
144     commitOperation();
145     aMgr->finishOperation();
146
147     stopOperation();
148     emit stopped();
149     emit committed();
150
151     afterCommitOperation();
152     return true;
153   }
154   return false;
155 }
156
157 void ModuleBase_Operation::onValuesChanged()
158 {
159   myIsModified = true;
160 }
161
162 void ModuleBase_Operation::onValueStateChanged(int /*thePreviousState*/)
163 {
164   if (propertyPanel()) {
165     ModuleBase_ModelWidget* aWidget = propertyPanel()->activeWidget();
166     if (aWidget) {
167       if (aWidget->getValueState() == ModuleBase_ModelWidget::ModifiedInPP)
168         myIsModified = true;
169     }
170   }
171 }
172
173 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
174 {
175   myPropertyPanel = theProp;
176 }
177
178 bool ModuleBase_Operation::isGranted(QString theId) const
179 {
180   return myGrantedIds.contains(theId);
181 }
182
183 bool  ModuleBase_Operation::isModified() const
184 {
185   if (myDescription->hasXmlRepresentation()) {
186     Config_WidgetAPI aWidgetApi(myDescription->xmlRepresentation().toStdString());
187     if (!aWidgetApi.getBooleanAttribute(ABORT_CONFIRMATION, true))
188       return false;
189   }
190   //if (myPropertyPanel)
191   //  return myPropertyPanel->isModified();
192   //return false;
193   // Most of operation causes creation of a feature
194   return true;
195 }