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