Salome HOME
Make Data not null, but invalid after the feature remove
[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 || !myFeature->data()->isValid())
68     return true; // rename operation
69   if (myFeature->isAction())
70     return true;
71   //Get validators for the Id
72   SessionPtr aMgr = ModelAPI_Session::get();
73   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
74   bool aValid = aFactory->validate(myFeature);
75
76   // the feature exec state should be checked in order to do not apply features, which result can not
77   // be built. E.g. extrusion on sketch, where the "to" is a perpendicular plane to the sketch
78   bool isDone = myFeature->data()->execState() == ModelAPI_StateDone;
79
80   return aValid && isDone;
81 }
82
83
84 bool ModuleBase_Operation::canBeCommitted() const
85 {
86   return isValid();
87 }
88
89 FeaturePtr ModuleBase_Operation::createFeature(const bool theFlushMessage)
90 {
91   if (myParentFeature.get()) {
92     myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
93   } else {
94     std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
95     myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
96   }
97   if (myFeature) {  // TODO: generate an error if feature was not created
98     myIsModified = true;
99     // Model update should call "execute" of a feature.
100     //myFeature->execute();
101     // Init default values
102     /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
103      QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
104      for (; anIt != aLast; anIt++) {
105      (*anIt)->storeValue(aFeature);
106      }*/
107   }
108
109   if (theFlushMessage)
110     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
111   return myFeature;
112 }
113
114 void ModuleBase_Operation::setFeature(FeaturePtr theFeature)
115 {
116   myFeature = theFeature;
117   myIsEditing = true;
118 }
119
120 bool ModuleBase_Operation::hasObject(ObjectPtr theObj) const
121 {
122   FeaturePtr aFeature = feature();
123   if (aFeature) {
124     if (aFeature == theObj)
125       return true;
126     std::list<ResultPtr> aResults = aFeature->results();
127     std::list<ResultPtr>::const_iterator aIt;
128     for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
129       if (theObj == (*aIt))
130         return true;
131     }
132   }
133   return false;
134 }
135
136 void ModuleBase_Operation::start()
137 {
138   QString anId = getDescription()->operationId();
139   if (myIsEditing) {
140     anId = anId.append(EditSuffix());
141   }
142   ModelAPI_Session::get()->startOperation(anId.toStdString());
143
144   if (!myIsEditing) {
145     FeaturePtr aFeature = createFeature();
146     // if the feature is not created, there is no sense to start the operation
147     if (aFeature.get() == NULL) {
148       // it is necessary to abor the operation in the session and emit the aborted signal
149       // in order to update commands status in the workshop, to be exact the feature action
150       // to be unchecked
151       abort();
152       return;
153     }
154   }
155   /// Set current feature and remeber old current feature
156   if (myIsEditing) {
157     SessionPtr aMgr = ModelAPI_Session::get();
158     DocumentPtr aDoc = aMgr->activeDocument();
159     myCurrentFeature = aDoc->currentFeature(true);
160     aDoc->setCurrentFeature(feature(), false);
161   }
162
163   startOperation();
164   emit started();
165
166 }
167
168 void ModuleBase_Operation::postpone()
169 {
170   postponeOperation();
171   emit postponed();
172 }
173
174 void ModuleBase_Operation::resume()
175 {
176   resumeOperation();
177   emit resumed();
178 }
179
180 void ModuleBase_Operation::abort()
181 {
182   if (myIsEditing) {
183     SessionPtr aMgr = ModelAPI_Session::get();
184     DocumentPtr aDoc = aMgr->activeDocument();
185     aDoc->setCurrentFeature(myCurrentFeature, true);
186     myCurrentFeature = FeaturePtr();
187   }
188   abortOperation();
189   emit aborted();
190
191   stopOperation();
192   // is is necessary to deactivate current widgets before the model operation is aborted
193   // because abort removes the feature and activated filters should not check it
194   propertyPanel()->cleanContent();
195
196   ModelAPI_Session::get()->abortOperation();
197   emit stopped();
198 }
199
200 bool ModuleBase_Operation::commit()
201 {
202   if (canBeCommitted()) {
203     SessionPtr aMgr = ModelAPI_Session::get();
204     /// Set current feature and remeber old current feature
205     if (myIsEditing) {
206       DocumentPtr aDoc = aMgr->activeDocument();
207       bool aIsOp = aMgr->isOperation();
208       if (!aIsOp)
209         aMgr->startOperation();
210       aDoc->setCurrentFeature(myCurrentFeature, true);
211       if (!aIsOp)
212         aMgr->finishOperation();
213       myCurrentFeature = FeaturePtr();
214     }
215     commitOperation();
216     // check whether there are modifications performed during the current operation
217     // in the model
218     // in case if there are no modifications, do not increase the undo/redo stack
219     if (aMgr->isModified())
220       aMgr->finishOperation();
221     else
222       aMgr->abortOperation();
223
224     stopOperation();
225     emit stopped();
226     emit committed();
227
228     afterCommitOperation();
229     return true;
230   }
231   return false;
232 }
233
234 void ModuleBase_Operation::setRunning(bool theState)
235 {
236   emit triggered(theState);
237 }
238
239 void ModuleBase_Operation::activateByPreselection()
240 {
241   if (!myPropertyPanel || myPreSelection.empty()) {
242     myPropertyPanel->activateNextWidget(NULL);
243     return;
244   }
245   const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
246   if (aWidgets.empty()) {
247     myPropertyPanel->activateNextWidget(NULL);
248     return;
249   }
250   
251   ModuleBase_ModelWidget* aWgt, *aFilledWgt = 0;
252   QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
253   bool isSet = false;
254   // 1. apply the selection to controls
255   int aCurrentPosition = 0;
256   for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
257     aWgt = (*aWIt);
258     if (!aWgt->canSetValue())
259       continue;
260
261     if (!aWgt->setSelection(myPreSelection, aCurrentPosition/*aValue*/)) {
262       isSet = false;
263       break;
264     } else {
265       isSet = true;
266       aFilledWgt = aWgt;
267     }
268   }
269   // 2. ignore not obligatory widgets
270   /*for (; aWIt != aWidgets.constEnd(); ++aWIt) {
271     aWgt = (*aWIt);
272     if (aWgt && aWgt->isObligatory())
273       continue;
274     aFilledWgt = aWgt;
275   }*/
276
277   // 3. a signal should be emitted before the next widget activation
278   // because, the activation of the next widget will give a focus to the widget. As a result
279   // the value of the widget is initialized. And commit may happens until the value is entered.
280   if (aFilledWgt)
281     emit activatedByPreselection();
282
283   // 4. activate the next obligatory widget
284   myPropertyPanel->activateNextWidget(aFilledWgt);
285 }
286
287 void ModuleBase_Operation::setParentFeature(CompositeFeaturePtr theParent)
288 {
289   myParentFeature = theParent;
290 }
291
292 CompositeFeaturePtr ModuleBase_Operation::parentFeature() const
293 {
294   return myParentFeature;
295 }
296
297 void ModuleBase_Operation::initSelection(ModuleBase_ISelection* theSelection,
298                                          ModuleBase_IViewer* theViewer)
299 {
300   clearPreselection();
301
302   QList<ModuleBase_ViewerPrs> aPreSelected;
303   // Check that the selected result are not results of operation feature
304   FeaturePtr aFeature = feature();
305   if (aFeature) {
306     QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
307
308     std::list<ResultPtr> aResults = aFeature->results();
309     QObjectPtrList aResList;
310     std::list<ResultPtr>::const_iterator aIt;
311     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
312       aResList.append(*aIt);
313
314     foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
315       if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
316         aPreSelected.append(aPrs);
317     }
318   } else
319     aPreSelected = theSelection->getSelected();
320
321   // convert the selection values to the values, which are set to the operation widgets
322
323   //Handle(V3d_View) aView = theViewer->activeView();
324   //foreach (ModuleBase_ViewerPrs aPrs, aPreSelected) {
325   //  ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
326   //  aValue->setObject(aPrs.object());
327
328   //  double aX, anY;
329   //  if (getViewerPoint(aPrs, theViewer, aX, anY))
330   //    aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
331   //  myPreSelection.append(aValue);
332   //}
333   myPreSelection = aPreSelected;
334 }
335
336 //void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
337 //{
338 //  //activateByPreselection();
339 //  //if (theWidget && myPropertyPanel) {
340 //  //  myPropertyPanel->activateNextWidget();
341 //  ////  //emit activateNextWidget(myActiveWidget);
342 //  //}
343 //}
344
345 //bool ModuleBase_Operation::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
346 //{
347 //  ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
348 //  if (!aActiveWgt)
349 //    return false;
350 //  ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
351 //  aValue->setObject(theFeature);
352 //  aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
353 //  bool isApplyed = aActiveWgt->setValue(aValue);
354 //
355 //  delete aValue;
356 //  myIsModified = (myIsModified || isApplyed);
357 //  return isApplyed;
358 //}
359
360 bool ModuleBase_Operation::getViewerPoint(ModuleBase_ViewerPrs thePrs,
361                                                ModuleBase_IViewer* theViewer,
362                                                double& theX, double& theY)
363 {
364   return false;
365 }
366
367 void ModuleBase_Operation::clearPreselection()
368 {
369   myPreSelection.clear();
370 }
371
372 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp) 
373
374   myPropertyPanel = theProp; 
375   myPropertyPanel->setEditingMode(isEditOperation());
376
377   // Do not activate widgets by default if the current operation is editing operation
378   // Because we don't know which widget is going to be edited. 
379   if (!isEditOperation())
380     activateByPreselection();
381 }
382
383 bool ModuleBase_Operation::isGranted(QString theId) const
384 {
385   return myNestedFeatures.contains(theId);
386 }