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