Salome HOME
Issue #389 Undo/redo problem on sketch line creation
[modules/shaper.git] / src / ModuleBase / ModuleBase_Operation.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 /*
4  * ModuleBase_Operation.cpp
5  *
6  *  Created on: Apr 2, 2014
7  *      Author: sbh
8  */
9
10 #include "ModuleBase_Operation.h"
11
12 #include "ModuleBase_OperationDescription.h"
13 #include "ModuleBase_ModelWidget.h"
14 #include "ModuleBase_ViewerPrs.h"
15 #include "ModuleBase_IPropertyPanel.h"
16 #include "ModuleBase_ISelection.h"
17 #include "ModuleBase_IViewer.h"
18
19 #include <ModelAPI_AttributeDouble.h>
20 #include <ModelAPI_Document.h>
21 #include <ModelAPI_Feature.h>
22 #include <ModelAPI_Data.h>
23 #include <ModelAPI_Document.h>
24 #include <ModelAPI_Events.h>
25 #include <ModelAPI_Result.h>
26 #include <ModelAPI_Object.h>
27 #include <ModelAPI_Validator.h>
28 #include <ModelAPI_Session.h>
29
30 #include <GeomAPI_Pnt2d.h>
31
32 #include <Events_Loop.h>
33
34 #include <QTimer>
35
36 #ifdef _DEBUG
37 #include <QDebug>
38 #endif
39
40 ModuleBase_Operation::ModuleBase_Operation(const QString& theId, QObject* theParent)
41     : QObject(theParent),
42       myIsEditing(false),
43       myIsModified(false),
44       myPropertyPanel(NULL)
45 {
46   myDescription = new ModuleBase_OperationDescription(theId);
47 }
48
49 ModuleBase_Operation::~ModuleBase_Operation()
50 {
51   delete myDescription;
52   clearPreselection();
53 }
54
55 QString ModuleBase_Operation::id() const
56 {
57   return getDescription()->operationId();
58 }
59
60 FeaturePtr ModuleBase_Operation::feature() const
61 {
62   return myFeature;
63 }
64
65 bool ModuleBase_Operation::isValid() const
66 {
67   if (!myFeature)
68     return true; // rename operation
69   //Get validators for the Id
70   SessionPtr aMgr = ModelAPI_Session::get();
71   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
72   return aFactory->validate(myFeature);
73 }
74
75
76 bool ModuleBase_Operation::canBeCommitted() const
77 {
78   return isValid();
79 }
80
81 void ModuleBase_Operation::flushUpdated()
82 {
83   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
84 }
85
86 void ModuleBase_Operation::flushCreated()
87 {
88   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
89 }
90
91 FeaturePtr ModuleBase_Operation::createFeature(const bool theFlushMessage)
92 {
93   if (myParentFeature.get()) {
94     myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
95   } else {
96     std::shared_ptr<ModelAPI_Document> aDoc = document();
97     myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
98   }
99   if (myFeature) {  // TODO: generate an error if feature was not created
100     myIsModified = true;
101     // Model update should call "execute" of a feature.
102     //myFeature->execute();
103     // Init default values
104     /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
105      QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
106      for (; anIt != aLast; anIt++) {
107      (*anIt)->storeValue(aFeature);
108      }*/
109   }
110
111   if (theFlushMessage)
112     flushCreated();
113   return myFeature;
114 }
115
116 void ModuleBase_Operation::setFeature(FeaturePtr theFeature)
117 {
118   myFeature = theFeature;
119   myIsEditing = true;
120 }
121
122 bool ModuleBase_Operation::hasObject(ObjectPtr theObj) const
123 {
124   FeaturePtr aFeature = feature();
125   if (aFeature) {
126     if (aFeature == theObj)
127       return true;
128     std::list<ResultPtr> aResults = aFeature->results();
129     std::list<ResultPtr>::const_iterator aIt;
130     for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
131       if (theObj == (*aIt))
132         return true;
133     }
134   }
135   return false;
136 }
137
138
139 std::shared_ptr<ModelAPI_Document> ModuleBase_Operation::document() const
140 {
141   return ModelAPI_Session::get()->moduleDocument();
142 }
143
144
145 void ModuleBase_Operation::start()
146 {
147   ModelAPI_Session::get()->startOperation();
148
149   if (!myIsEditing)
150     createFeature();
151
152   startOperation();
153   emit started();
154
155 }
156
157 void ModuleBase_Operation::postpone()
158 {
159   postponeOperation();
160   emit postponed();
161 }
162
163 void ModuleBase_Operation::resume()
164 {
165   resumeOperation();
166   emit resumed();
167 }
168
169 void ModuleBase_Operation::abort()
170 {
171   abortOperation();
172   emit aborted();
173
174   stopOperation();
175
176   ModelAPI_Session::get()->abortOperation();
177   emit stopped();
178 }
179
180 bool ModuleBase_Operation::commit()
181 {
182   if (canBeCommitted()) {
183     commitOperation();
184     // check whether there are modifications performed during the current operation
185     // in the model
186     // in case if there are no modifications, do not increase the undo/redo stack
187     if (ModelAPI_Session::get()->isModified())
188       ModelAPI_Session::get()->finishOperation();
189     else
190       ModelAPI_Session::get()->abortOperation();
191
192     stopOperation();
193     emit stopped();
194     emit committed();
195
196     afterCommitOperation();
197     return true;
198   }
199   return false;
200 }
201
202 void ModuleBase_Operation::setRunning(bool theState)
203 {
204   if (!theState) {
205     abort();
206   }
207 }
208
209 void ModuleBase_Operation::activateByPreselection()
210 {
211   if (!myPropertyPanel || myPreSelection.empty()) {
212     myPropertyPanel->activateNextWidget(NULL);
213     return;
214   }
215   const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
216   if (aWidgets.empty()) {
217     myPropertyPanel->activateNextWidget(NULL);
218     return;
219   }
220   
221   ModuleBase_ModelWidget* aWgt, *aFilledWgt = 0;
222   QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
223   QList<ModuleBase_ViewerPrs>::const_iterator aPIt;
224   bool isSet = false;
225   for (aWIt = aWidgets.constBegin(), aPIt = myPreSelection.constBegin();
226        (aWIt != aWidgets.constEnd()) && (aPIt != myPreSelection.constEnd());
227        ++aWIt) {
228     aWgt = (*aWIt);
229     ModuleBase_ViewerPrs aValue = (*aPIt);
230     if (!aWgt->canSetValue())
231       continue;
232
233     ++aPIt;
234     if (!aWgt->setSelection(aValue)) {
235       isSet = false;
236       break;
237     } else {
238       isSet = true;
239       aFilledWgt = aWgt;
240     }
241   }
242
243   myPropertyPanel->activateNextWidget(aFilledWgt);
244   if (aFilledWgt)
245     emit activatedByPreselection();
246
247 }
248
249 void ModuleBase_Operation::setParentFeature(CompositeFeaturePtr theParent)
250 {
251   myParentFeature = theParent;
252 }
253
254 CompositeFeaturePtr ModuleBase_Operation::parentFeature() const
255 {
256   return myParentFeature;
257 }
258
259 void ModuleBase_Operation::initSelection(ModuleBase_ISelection* theSelection,
260                                          ModuleBase_IViewer* theViewer)
261 {
262   clearPreselection();
263
264   QList<ModuleBase_ViewerPrs> aPreSelected;
265   // Check that the selected result are not results of operation feature
266   FeaturePtr aFeature = feature();
267   if (aFeature) {
268     QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
269
270     std::list<ResultPtr> aResults = aFeature->results();
271     QObjectPtrList aResList;
272     std::list<ResultPtr>::const_iterator aIt;
273     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
274       aResList.append(*aIt);
275
276     foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
277       if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
278         aPreSelected.append(aPrs);
279     }
280   } else
281     aPreSelected = theSelection->getSelected();
282
283   // convert the selection values to the values, which are set to the operation widgets
284
285   //Handle(V3d_View) aView = theViewer->activeView();
286   //foreach (ModuleBase_ViewerPrs aPrs, aPreSelected) {
287   //  ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
288   //  aValue->setObject(aPrs.object());
289
290   //  double aX, anY;
291   //  if (getViewerPoint(aPrs, theViewer, aX, anY))
292   //    aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
293   //  myPreSelection.append(aValue);
294   //}
295   myPreSelection = aPreSelected;
296 }
297
298 //void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
299 //{
300 //  //activateByPreselection();
301 //  //if (theWidget && myPropertyPanel) {
302 //  //  myPropertyPanel->activateNextWidget();
303 //  ////  //emit activateNextWidget(myActiveWidget);
304 //  //}
305 //}
306
307 //bool ModuleBase_Operation::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
308 //{
309 //  ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
310 //  if (!aActiveWgt)
311 //    return false;
312 //  ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
313 //  aValue->setObject(theFeature);
314 //  aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
315 //  bool isApplyed = aActiveWgt->setValue(aValue);
316 //
317 //  delete aValue;
318 //  myIsModified = (myIsModified || isApplyed);
319 //  return isApplyed;
320 //}
321
322 bool ModuleBase_Operation::getViewerPoint(ModuleBase_ViewerPrs thePrs,
323                                                ModuleBase_IViewer* theViewer,
324                                                double& theX, double& theY)
325 {
326   return false;
327 }
328
329 void ModuleBase_Operation::clearPreselection()
330 {
331   myPreSelection.clear();
332 }
333
334 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp) 
335
336   myPropertyPanel = theProp; 
337   myPropertyPanel->setEditingMode(isEditOperation());
338
339   // Do not activate widgets by default if the current operation is editing operation
340   // Because we don't know which widget is going to be edited. 
341   if (!isEditOperation())
342     activateByPreselection();
343 }
344
345 bool ModuleBase_Operation::isGranted(QString theId) const
346 {
347   return myNestedFeatures.contains(theId);
348 }