Salome HOME
SetCurrentFeature call is moved to operation manager to perform it in the commit...
[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 #include <ModelAPI_Tools.h>
30
31 #include <GeomAPI_Pnt2d.h>
32
33 #include <Events_Loop.h>
34
35 #include <QTimer>
36
37 #ifdef _DEBUG
38 #include <QDebug>
39 #endif
40
41 ModuleBase_OperationFeature::ModuleBase_OperationFeature(const QString& theId, QObject* theParent)
42 : ModuleBase_Operation(theId, theParent),
43   myIsEditing(false)
44 {
45 }
46
47 ModuleBase_OperationFeature::~ModuleBase_OperationFeature()
48 {
49   clearPreselection();
50 }
51
52 FeaturePtr ModuleBase_OperationFeature::feature() const
53 {
54   return myFeature;
55 }
56
57 bool ModuleBase_OperationFeature::isValid() const
58 {
59   if (!myFeature || !myFeature->data()->isValid())
60     return true; // rename operation
61   if (myFeature->isAction())
62     return true;
63   //Get validators for the Id
64   SessionPtr aMgr = ModelAPI_Session::get();
65   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
66   bool aValid = aFactory->validate(myFeature);
67
68   // the feature exec state should be checked in order to do not apply features, which result can not
69   // be built. E.g. extrusion on sketch, where the "to" is a perpendicular plane to the sketch
70   bool isDone = ( myFeature->data()->execState() == ModelAPI_StateDone
71                || myFeature->data()->execState() == ModelAPI_StateMustBeUpdated );
72
73   return aValid && isDone;
74 }
75
76 void ModuleBase_OperationFeature::startOperation()
77 {
78   FeaturePtr aFeature = feature();
79   if (!aFeature.get() || !isEditOperation())
80     return;
81
82   myVisualizedObjects.clear();
83   // store hidden result features
84   std::list<ResultPtr> aResults = aFeature->results();
85   std::list<ResultPtr>::const_iterator aIt;
86   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
87     ObjectPtr anObject = *aIt;
88     if (anObject.get() && !anObject->isDisplayed()) {
89       myVisualizedObjects.insert(*aIt);
90       anObject->setDisplayed(true);
91     }
92   }
93   if (!aFeature->isDisplayed()) {
94     myVisualizedObjects.insert(aFeature);
95     aFeature->setDisplayed(true);
96   }
97   Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
98 }
99
100 void ModuleBase_OperationFeature::stopOperation()
101 {
102   FeaturePtr aFeature = feature();
103   if (!aFeature.get() || !isEditOperation())
104     return;
105
106   // store hidden result features
107   std::list<ResultPtr> aResults = aFeature->results();
108   std::list<ResultPtr>::const_iterator aIt;
109   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
110     ObjectPtr anObject = *aIt;
111     if (anObject.get() && myVisualizedObjects.find(anObject) != myVisualizedObjects.end()) {
112       anObject->setDisplayed(false);
113     }
114   }
115   if (myVisualizedObjects.find(aFeature) != myVisualizedObjects.end()) {
116     aFeature->setDisplayed(false);
117   }
118   aFeature->setStable(true);
119   if (myVisualizedObjects.size() > 0)
120     Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
121 }
122
123 FeaturePtr ModuleBase_OperationFeature::createFeature(const bool theFlushMessage)
124 {
125   if (myParentFeature.get()) {
126     myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
127   } else {
128     std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
129     myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
130   }
131   if (myFeature) {  // TODO: generate an error if feature was not created
132     setIsModified(true);
133     // Model update should call "execute" of a feature.
134     //myFeature->execute();
135     // Init default values
136     /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
137      QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
138      for (; anIt != aLast; anIt++) {
139      (*anIt)->storeValue(aFeature);
140      }*/
141   }
142
143   if (theFlushMessage)
144     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
145   return myFeature;
146 }
147
148 void ModuleBase_OperationFeature::setFeature(FeaturePtr theFeature)
149 {
150   myFeature = theFeature;
151   myFeature->setStable(false);
152   myIsEditing = true;
153 }
154
155 bool ModuleBase_OperationFeature::hasObject(ObjectPtr theObj) const
156 {
157   FeaturePtr aFeature = feature();
158   if (aFeature) {
159     if (aFeature == theObj)
160       return true;
161     std::list<ResultPtr> aResults = aFeature->results();
162     std::list<ResultPtr>::const_iterator aIt;
163     for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
164       if (theObj == (*aIt))
165         return true;
166     }
167   }
168   return false;
169 }
170
171 bool ModuleBase_OperationFeature::isDisplayedOnStart(ObjectPtr theObject)
172 {
173   return myVisualizedObjects.find(theObject) != myVisualizedObjects.end();
174 }
175
176 void ModuleBase_OperationFeature::start()
177 {
178   setIsModified(false);
179   QString anId = getDescription()->operationId();
180   if (myIsEditing) {
181     anId = anId.append(EditSuffix());
182   }
183   ModelAPI_Session::get()->startOperation(anId.toStdString());
184
185   emit beforeStarted();
186   startOperation();
187
188   if (!myIsEditing) {
189     FeaturePtr aFeature = createFeature();
190     // if the feature is not created, there is no sense to start the operation
191     if (aFeature.get() == NULL) {
192       // it is necessary to abor the operation in the session and emit the aborted signal
193       // in order to update commands status in the workshop, to be exact the feature action
194       // to be unchecked
195       abort();
196       return;
197     }
198   }
199   startOperation();
200   emit started();
201
202 }
203
204 void ModuleBase_OperationFeature::abort()
205 {
206   emit beforeAborted();
207
208   // the viewer update should be blocked in order to avoid the features blinking before they are
209   // hidden
210   std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
211       new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
212   Events_Loop::loop()->send(aMsg);
213
214   // the widgets of property panel should not process any events come from data mode
215   // after abort clicked. Some signal such as redisplay/create influence on content
216   // of the object browser and viewer context. Therefore it influence to the current
217   // selection and if the active widget listens it, the attribute value is errnoneous
218   // changed.
219   ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
220   if (aPropertyPanel)
221     aPropertyPanel->cleanContent();
222
223   abortOperation();
224   stopOperation();
225
226   SessionPtr aMgr = ModelAPI_Session::get();
227   aMgr->abortOperation();
228   emit stopped();
229   // the viewer update should be unblocked in order to avoid the features blinking before they are
230   // hidden
231   aMsg = std::shared_ptr<Events_Message>(
232                 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
233
234   Events_Loop::loop()->send(aMsg);
235
236   emit aborted();
237 }
238
239 bool ModuleBase_OperationFeature::commit()
240 {
241   if (canBeCommitted()) {
242     // the widgets of property panel should not process any events come from data mode
243     // after commit clicked. Some signal such as redisplay/create influence on content
244     // of the object browser and viewer context. Therefore it influence to the current
245     // selection and if the active widget listens it, the attribute value is errnoneous
246     // changed.
247     ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
248     if (aPropertyPanel)
249       aPropertyPanel->cleanContent();
250
251     SessionPtr aMgr = ModelAPI_Session::get();
252     /// Set current feature and remeber old current feature
253
254     emit beforeCommitted();
255     commitOperation();
256     aMgr->finishOperation();
257
258     stopOperation();
259     emit stopped();
260     emit committed();
261
262     afterCommitOperation();
263     return true;
264   }
265   return false;
266 }
267
268 void ModuleBase_OperationFeature::activateByPreselection()
269 {
270   if (myPreSelection.empty())
271     return;
272
273   ModuleBase_ModelWidget* aFilledWgt = 0;
274   ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
275   if (aPropertyPanel) {
276     const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
277     if (!aWidgets.empty()) {
278       ModuleBase_ModelWidget* aWgt = 0;
279       QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
280       bool isSet = false;
281       // 1. apply the selection to controls
282       for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
283         aWgt = (*aWIt);
284         if (!aWgt->canSetValue())
285           continue;
286         aPropertyPanel->setPreselectionWidget(aWgt);
287         if (!aWgt->setSelection(myPreSelection, true)) {
288           isSet = false;
289           break;
290         } else {
291           isSet = true;
292           aFilledWgt = aWgt;
293         }
294       }
295       aPropertyPanel->setPreselectionWidget(NULL);
296       // in order to redisplay object in the viewer, the update/redisplay signals should be flushed
297       // it is better to perform it not in setSelection of each widget, but do it here,
298       // after the preselection is processed
299       ModuleBase_ModelWidget::updateObject(myFeature);
300
301       // 3. a signal should be emitted before the next widget activation
302       // because, the activation of the next widget will give a focus to the widget. As a result
303       // the value of the widget is initialized. And commit may happens until the value is entered.
304       if (aFilledWgt)
305         emit activatedByPreselection();
306     }
307     // 4. activate the next obligatory widget
308     aPropertyPanel->activateNextWidget(aFilledWgt);
309   }
310
311   clearPreselection();
312 }
313
314 void ModuleBase_OperationFeature::setParentFeature(CompositeFeaturePtr theParent)
315 {
316   myParentFeature = theParent;
317 }
318
319 CompositeFeaturePtr ModuleBase_OperationFeature::parentFeature() const
320 {
321   return myParentFeature;
322 }
323
324 void ModuleBase_OperationFeature::setPreviousCurrentFeature(const FeaturePtr& theFeature)
325 {
326   myPreviousCurrentFeature = theFeature;
327 }
328
329 FeaturePtr ModuleBase_OperationFeature::previousCurrentFeature()
330 {
331   return myPreviousCurrentFeature;
332 }
333
334 void ModuleBase_OperationFeature::initSelection(ModuleBase_ISelection* theSelection,
335                                          ModuleBase_IViewer* theViewer)
336 {
337   clearPreselection();
338
339   QList<ModuleBase_ViewerPrs> aPreSelected;
340   // Check that the selected result are not results of operation feature
341   FeaturePtr aFeature = feature();
342   if (aFeature) {
343     QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
344
345     std::list<ResultPtr> aResults = aFeature->results();
346     QObjectPtrList aResList;
347     std::list<ResultPtr>::const_iterator aIt;
348     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
349       aResList.append(*aIt);
350
351     foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
352       if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
353         aPreSelected.append(aPrs);
354     }
355   } else
356     aPreSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
357
358   myPreSelection = aPreSelected;
359 }
360
361 void ModuleBase_OperationFeature::clearPreselection()
362 {
363   myPreSelection.clear();
364 }
365
366 void ModuleBase_OperationFeature::setPropertyPanel(ModuleBase_IPropertyPanel* theProp) 
367 {
368   ModuleBase_Operation::setPropertyPanel(theProp);
369
370   theProp->setEditingMode(isEditOperation());
371
372   if (theProp) {
373     const QList<ModuleBase_ModelWidget*>& aWidgets = theProp->modelWidgets();
374     QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
375     for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
376       ModuleBase_ModelWidget* aWgt = (*aWIt);
377       connect(aWgt, SIGNAL(valuesChanged()), this, SLOT(onValuesChanged()));
378     }
379   }
380
381   // Do not activate widgets by default if the current operation is editing operation
382   // Because we don't know which widget is going to be edited. 
383   if (!isEditOperation()) {
384     // 4. activate the first obligatory widget
385     theProp->activateNextWidget(NULL);
386   }
387   else {
388     // set focus on Ok button in order to operation manager could process Enter press
389     if (theProp)
390       theProp->setFocusOnOkButton();
391   }
392 }