Salome HOME
Providing Action class to have a common approach to start/finish/abort model transact...
[modules/shaper.git] / src / ModuleBase / ModuleBase_OperationFeature.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 /*
4  * ModuleBase_OperationFeature.cpp
5  *
6  *  Created on: Apr 2, 2014
7  *      Author: sbh
8  */
9
10 #include "ModuleBase_OperationFeature.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_OperationFeature::ModuleBase_OperationFeature(const QString& theId, QObject* theParent)
41 : ModuleBase_Operation(theId, theParent),
42   myIsEditing(false)
43 {
44 }
45
46 ModuleBase_OperationFeature::~ModuleBase_OperationFeature()
47 {
48   clearPreselection();
49 }
50
51 FeaturePtr ModuleBase_OperationFeature::feature() const
52 {
53   return myFeature;
54 }
55
56 bool ModuleBase_OperationFeature::isValid() const
57 {
58   if (!myFeature || !myFeature->data()->isValid())
59     return true; // rename operation
60   if (myFeature->isAction())
61     return true;
62   //Get validators for the Id
63   SessionPtr aMgr = ModelAPI_Session::get();
64   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
65   bool aValid = aFactory->validate(myFeature);
66
67   // the feature exec state should be checked in order to do not apply features, which result can not
68   // be built. E.g. extrusion on sketch, where the "to" is a perpendicular plane to the sketch
69   bool isDone = ( myFeature->data()->execState() == ModelAPI_StateDone
70                || myFeature->data()->execState() == ModelAPI_StateMustBeUpdated );
71
72   return aValid && isDone;
73 }
74
75 FeaturePtr ModuleBase_OperationFeature::createFeature(const bool theFlushMessage)
76 {
77   if (myParentFeature.get()) {
78     myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
79   } else {
80     std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
81     myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
82   }
83   if (myFeature) {  // TODO: generate an error if feature was not created
84     setIsModified(true);
85     // Model update should call "execute" of a feature.
86     //myFeature->execute();
87     // Init default values
88     /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
89      QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
90      for (; anIt != aLast; anIt++) {
91      (*anIt)->storeValue(aFeature);
92      }*/
93   }
94
95   if (theFlushMessage)
96     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
97   return myFeature;
98 }
99
100 void ModuleBase_OperationFeature::setFeature(FeaturePtr theFeature)
101 {
102   myFeature = theFeature;
103   myIsEditing = true;
104 }
105
106 bool ModuleBase_OperationFeature::hasObject(ObjectPtr theObj) const
107 {
108   FeaturePtr aFeature = feature();
109   if (aFeature) {
110     if (aFeature == theObj)
111       return true;
112     std::list<ResultPtr> aResults = aFeature->results();
113     std::list<ResultPtr>::const_iterator aIt;
114     for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
115       if (theObj == (*aIt))
116         return true;
117     }
118   }
119   return false;
120 }
121
122 void ModuleBase_OperationFeature::start()
123 {
124   setIsModified(false);
125   QString anId = getDescription()->operationId();
126   if (myIsEditing) {
127     anId = anId.append(EditSuffix());
128   }
129   ModelAPI_Session::get()->startOperation(anId.toStdString());
130
131   startOperation();
132
133   if (!myIsEditing) {
134     FeaturePtr aFeature = createFeature();
135     // if the feature is not created, there is no sense to start the operation
136     if (aFeature.get() == NULL) {
137       // it is necessary to abor the operation in the session and emit the aborted signal
138       // in order to update commands status in the workshop, to be exact the feature action
139       // to be unchecked
140       abort();
141       return;
142     }
143   }
144   /// Set current feature and remeber old current feature
145   if (myIsEditing) {
146     SessionPtr aMgr = ModelAPI_Session::get();
147     DocumentPtr aDoc = aMgr->activeDocument();
148     myCurrentFeature = aDoc->currentFeature(true);
149     aDoc->setCurrentFeature(feature(), false);
150   }
151
152   startOperation();
153   emit started();
154
155 }
156
157 void ModuleBase_OperationFeature::abort()
158 {
159   // the viewer update should be blocked in order to avoid the features blinking before they are
160   // hidden
161   std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
162       new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
163   Events_Loop::loop()->send(aMsg);
164
165   // the widgets of property panel should not process any events come from data mode
166   // after abort clicked. Some signal such as redisplay/create influence on content
167   // of the object browser and viewer context. Therefore it influence to the current
168   // selection and if the active widget listens it, the attribute value is errnoneous
169   // changed.
170   ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
171   if (aPropertyPanel)
172     aPropertyPanel->cleanContent();
173
174   SessionPtr aMgr = ModelAPI_Session::get();
175   if (myIsEditing) {
176     DocumentPtr aDoc = aMgr->activeDocument();
177     bool aIsOp = aMgr->isOperation();
178     if (!aIsOp)
179       aMgr->startOperation();
180     aDoc->setCurrentFeature(myCurrentFeature, true);
181     if (!aIsOp)
182       aMgr->finishOperation();
183     myCurrentFeature = FeaturePtr();
184   }
185   abortOperation();
186
187   stopOperation();
188   // is is necessary to deactivate current widgets before the model operation is aborted
189   // because abort removes the feature and activated filters should not check it
190   propertyPanel()->cleanContent();
191
192   aMgr->abortOperation();
193   emit stopped();
194   // the viewer update should be unblocked in order to avoid the features blinking before they are
195   // hidden
196   aMsg = std::shared_ptr<Events_Message>(
197                 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
198
199   Events_Loop::loop()->send(aMsg);
200
201   emit aborted();
202 }
203
204 bool ModuleBase_OperationFeature::commit()
205 {
206   if (canBeCommitted()) {
207     // the widgets of property panel should not process any events come from data mode
208     // after commit clicked. Some signal such as redisplay/create influence on content
209     // of the object browser and viewer context. Therefore it influence to the current
210     // selection and if the active widget listens it, the attribute value is errnoneous
211     // changed.
212     ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
213     if (aPropertyPanel)
214       aPropertyPanel->cleanContent();
215
216     SessionPtr aMgr = ModelAPI_Session::get();
217     /// Set current feature and remeber old current feature
218     if (myIsEditing) {
219       DocumentPtr aDoc = aMgr->activeDocument();
220       bool aIsOp = aMgr->isOperation();
221       if (!aIsOp)
222         aMgr->startOperation();
223       aDoc->setCurrentFeature(myCurrentFeature, true);
224       if (!aIsOp)
225         aMgr->finishOperation();
226       myCurrentFeature = FeaturePtr();
227     }
228     commitOperation();
229     aMgr->finishOperation();
230
231     stopOperation();
232     emit stopped();
233     emit committed();
234
235     afterCommitOperation();
236     return true;
237   }
238   return false;
239 }
240
241 void ModuleBase_OperationFeature::activateByPreselection()
242 {
243   if (myPreSelection.empty())
244     return;
245
246   ModuleBase_ModelWidget* aFilledWgt = 0;
247   ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
248   if (aPropertyPanel) {
249     const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
250     if (!aWidgets.empty()) {
251       ModuleBase_ModelWidget* aWgt = 0;
252       QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
253       bool isSet = false;
254       // 1. apply the selection to controls
255       for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
256         aWgt = (*aWIt);
257         if (!aWgt->canSetValue())
258           continue;
259         aPropertyPanel->setPreselectionWidget(aWgt);
260         if (!aWgt->setSelection(myPreSelection, true)) {
261           isSet = false;
262           break;
263         } else {
264           isSet = true;
265           aFilledWgt = aWgt;
266         }
267       }
268       aPropertyPanel->setPreselectionWidget(NULL);
269       // in order to redisplay object in the viewer, the update/redisplay signals should be flushed
270       // it is better to perform it not in setSelection of each widget, but do it here,
271       // after the preselection is processed
272       ModuleBase_ModelWidget::updateObject(myFeature);
273
274       // 3. a signal should be emitted before the next widget activation
275       // because, the activation of the next widget will give a focus to the widget. As a result
276       // the value of the widget is initialized. And commit may happens until the value is entered.
277       if (aFilledWgt)
278         emit activatedByPreselection();
279     }
280     // 4. activate the next obligatory widget
281     aPropertyPanel->activateNextWidget(aFilledWgt);
282   }
283
284   clearPreselection();
285 }
286
287 void ModuleBase_OperationFeature::setParentFeature(CompositeFeaturePtr theParent)
288 {
289   myParentFeature = theParent;
290 }
291
292 CompositeFeaturePtr ModuleBase_OperationFeature::parentFeature() const
293 {
294   return myParentFeature;
295 }
296
297 void ModuleBase_OperationFeature::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(ModuleBase_ISelection::AllControls);
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(ModuleBase_ISelection::AllControls);
320
321   myPreSelection = aPreSelected;
322 }
323
324 void ModuleBase_OperationFeature::clearPreselection()
325 {
326   myPreSelection.clear();
327 }
328
329 void ModuleBase_OperationFeature::setPropertyPanel(ModuleBase_IPropertyPanel* theProp) 
330 {
331   ModuleBase_Operation::setPropertyPanel(theProp);
332
333   theProp->setEditingMode(isEditOperation());
334
335   if (theProp) {
336     const QList<ModuleBase_ModelWidget*>& aWidgets = theProp->modelWidgets();
337     QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
338     for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
339       ModuleBase_ModelWidget* aWgt = (*aWIt);
340       connect(aWgt, SIGNAL(valuesChanged()), this, SLOT(onValuesChanged()));
341     }
342   }
343
344   // Do not activate widgets by default if the current operation is editing operation
345   // Because we don't know which widget is going to be edited. 
346   if (!isEditOperation()) {
347     // 4. activate the first obligatory widget
348     theProp->activateNextWidget(NULL);
349   }
350 }
351
352 bool ModuleBase_OperationFeature::isGranted(QString theId) const
353 {
354   return myNestedFeatures.contains(theId);
355 }