Salome HOME
1. Correction for perfomance problem by Apply button state update: do not listen...
[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 #include "ModuleBase_Tools.h"
19
20 #include <ModelAPI_AttributeDouble.h>
21 #include <ModelAPI_Document.h>
22 #include <ModelAPI_Feature.h>
23 #include <ModelAPI_Data.h>
24 #include <ModelAPI_Document.h>
25 #include <ModelAPI_Events.h>
26 #include <ModelAPI_Result.h>
27 #include <ModelAPI_Object.h>
28 #include <ModelAPI_Validator.h>
29 #include <ModelAPI_Session.h>
30 #include <ModelAPI_Tools.h>
31
32 #include <GeomAPI_Pnt2d.h>
33
34 #include <Events_Loop.h>
35
36 #include <QTimer>
37
38 // the define to check the activated object as a sub-feature by argument of
39 // the operation feature. E.g. rectangle feature(operation), line(in argument) to be not activated
40 #define DEBUG_DO_NOT_ACTIVATE_SUB_FEATURE
41 #ifdef DEBUG_DO_NOT_ACTIVATE_SUB_FEATURE
42 #include <ModelAPI_AttributeRefList.h>
43 #endif
44
45 #ifdef _DEBUG
46 #include <QDebug>
47 #endif
48
49 ModuleBase_OperationFeature::ModuleBase_OperationFeature(const QString& theId, QObject* theParent)
50 : ModuleBase_Operation(theId, theParent),
51   myIsEditing(false)
52 {
53 }
54
55 ModuleBase_OperationFeature::~ModuleBase_OperationFeature()
56 {
57   clearPreselection();
58 }
59
60 void ModuleBase_OperationFeature::setEditOperation(const bool& isEditState
61                                                    /*const bool theRestartTransaction*/)
62 {
63   bool isCurrentEditState = isEditOperation();
64   if (isCurrentEditState == isEditState)
65     return;
66
67   /*
68   // this case is obsolete as it was not approved for reentrant sketch operation
69   // it was implemented when isEditState did not exist and only edit operation can be set
70   if (theRestartTransaction) {
71     // finsh previous create operation
72     emit beforeCommitted();
73     SessionPtr aMgr = ModelAPI_Session::get();
74     ModelAPI_Session::get()->finishOperation();
75
76     // start new edit operation
77     myIsEditing = true;
78     QString anId = getDescription()->operationId();
79     if (myIsEditing) {
80       anId = anId.append(EditSuffix());
81     }
82     ModelAPI_Session::get()->startOperation(anId.toStdString());
83     emit beforeStarted();
84   } else*/
85   myIsEditing = isEditState;
86
87   propertyPanel()->setEditingMode(isEditOperation());
88 }
89
90 FeaturePtr ModuleBase_OperationFeature::feature() const
91 {
92   return myFeature;
93 }
94
95 bool ModuleBase_OperationFeature::isValid() const
96 {
97   if (!myFeature || !myFeature->data()->isValid())
98     return true; // rename operation
99   if (myFeature->isAction())
100     return true;
101
102   std::string anError = ModelAPI_Tools::getFeatureError(myFeature);
103   return anError.empty();
104 }
105
106 void ModuleBase_OperationFeature::startOperation()
107 {
108   FeaturePtr aFeature = feature();
109   if (!aFeature.get() || !isEditOperation())
110     return;
111
112   if (aFeature.get() && isEditOperation())
113     aFeature->setStable(false);
114
115   myVisualizedObjects.clear();
116   // store hidden result features
117   std::list<ResultPtr> aResults = aFeature->results();
118   std::list<ResultPtr>::const_iterator aIt;
119   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
120     ObjectPtr anObject = *aIt;
121     if (anObject.get() && !anObject->isDisplayed()) {
122       myVisualizedObjects.insert(*aIt);
123       anObject->setDisplayed(true);
124     }
125   }
126   if (!aFeature->isDisplayed()) {
127     myVisualizedObjects.insert(aFeature);
128     aFeature->setDisplayed(true);
129   }
130   Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
131 }
132
133 void ModuleBase_OperationFeature::stopOperation()
134 {
135   FeaturePtr aFeature = feature();
136   if (!aFeature.get() || !isEditOperation())
137     return;
138
139   // store hidden result features
140   std::list<ResultPtr> aResults = aFeature->results();
141   std::list<ResultPtr>::const_iterator aIt;
142   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
143     ObjectPtr anObject = *aIt;
144     if (anObject.get() && myVisualizedObjects.find(anObject) != myVisualizedObjects.end()) {
145       anObject->setDisplayed(false);
146     }
147   }
148   if (myVisualizedObjects.find(aFeature) != myVisualizedObjects.end()) {
149     aFeature->setDisplayed(false);
150   }
151   if (myVisualizedObjects.size() > 0)
152     Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
153 }
154
155 FeaturePtr ModuleBase_OperationFeature::createFeature(const bool theFlushMessage)
156 {
157   if (myParentFeature.get()) {
158     myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
159   } else {
160     std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
161     myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
162   }
163   if (myFeature) {  // TODO: generate an error if feature was not created
164     setIsModified(true);
165     // Model update should call "execute" of a feature.
166     //myFeature->execute();
167     // Init default values
168     /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
169      QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
170      for (; anIt != aLast; anIt++) {
171      (*anIt)->storeValue(aFeature);
172      }*/
173   }
174
175   if (theFlushMessage) {
176     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
177     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
178   }
179   return myFeature;
180 }
181
182 void ModuleBase_OperationFeature::setFeature(FeaturePtr theFeature)
183 {
184   myFeature = theFeature;
185   myIsEditing = true;
186 }
187
188 bool ModuleBase_OperationFeature::hasObject(ObjectPtr theObj) const
189 {
190   FeaturePtr aFeature = feature();
191   if (aFeature) {
192     if (aFeature == theObj)
193       return true;
194     std::list<ResultPtr> aResults = aFeature->results();
195     std::list<ResultPtr>::const_iterator aIt;
196     for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
197       if (theObj == (*aIt))
198         return true;
199     }
200 #ifdef DEBUG_DO_NOT_ACTIVATE_SUB_FEATURE
201     if (aFeature->isMacro()) {
202       // macro feature may refers to sub-features, which also should be deactivated when the operation
203       // is active, e.g. rectangle'lines.
204       FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObj);
205       std::list<AttributePtr> anAttributes = aFeature->data()->attributes(
206                                               ModelAPI_AttributeRefList::typeId());
207       std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
208       bool aFoundObject = false;
209       for (; anIt != aLast && !aFoundObject; anIt++) {
210         std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
211                                          std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*anIt);
212         for (int i = 0, aNb = aCurSelList->size(); i < aNb && !aFoundObject; i++) {
213           ObjectPtr anObject = aCurSelList->object(i);
214           FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
215           if (aFeature.get()) {
216             aFoundObject = anObjectFeature == aFeature;
217           }
218         }
219       }
220       return aFoundObject;
221     }
222 #endif
223   }
224   return false;
225 }
226
227 bool ModuleBase_OperationFeature::isDisplayedOnStart(ObjectPtr theObject)
228 {
229   return myVisualizedObjects.find(theObject) != myVisualizedObjects.end();
230 }
231
232 bool ModuleBase_OperationFeature::start()
233 {
234   setIsModified(false);
235   QString anId = getDescription()->operationId();
236   if (myIsEditing) {
237     anId = anId.append(EditSuffix());
238   }
239   ModelAPI_Session::get()->startOperation(anId.toStdString());
240
241   emit beforeStarted();
242   startOperation();
243
244   if (!myIsEditing) {
245     FeaturePtr aFeature = createFeature();
246     // if the feature is not created, there is no sense to start the operation
247     if (aFeature.get() == NULL) {
248       // it is necessary to abor the operation in the session and emit the aborted signal
249       // in order to update commands status in the workshop, to be exact the feature action
250       // to be unchecked
251       abort();
252       return false;
253     }
254   }
255   //Already called startOperation();
256   emit started();
257   return true;
258 }
259
260 void ModuleBase_OperationFeature::abort()
261 {
262   emit beforeAborted();
263
264   // the viewer update should be blocked in order to avoid the features blinking before they are
265   // hidden
266   std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
267       new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
268   Events_Loop::loop()->send(aMsg);
269
270   // the widgets of property panel should not process any events come from data mode
271   // after abort clicked. Some signal such as redisplay/create influence on content
272   // of the object browser and viewer context. Therefore it influence to the current
273   // selection and if the active widget listens it, the attribute value is errnoneous
274   // changed.
275   ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
276   if (aPropertyPanel)
277     aPropertyPanel->cleanContent();
278
279   if (myFeature.get())
280     myFeature->setStable(true);
281
282   abortOperation();
283   stopOperation();
284
285   SessionPtr aMgr = ModelAPI_Session::get();
286   aMgr->abortOperation();
287   emit stopped();
288   // the viewer update should be unblocked in order to avoid the features blinking before they are
289   // hidden
290   aMsg = std::shared_ptr<Events_Message>(
291                 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
292
293   Events_Loop::loop()->send(aMsg);
294
295   emit aborted();
296 }
297
298 bool ModuleBase_OperationFeature::commit()
299 {
300   ModuleBase_IPropertyPanel* aPanel = propertyPanel();
301   if (aPanel) {
302     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
303     if (anActiveWidget && anActiveWidget->getValueState() == ModuleBase_ModelWidget::ModifiedInPP) {
304       anActiveWidget->storeValue();
305     }
306   }
307   if (canBeCommitted()) {
308     emit beforeCommitted();
309     // the widgets of property panel should not process any events come from data mode
310     // after commit clicked. Some signal such as redisplay/create influence on content
311     // of the object browser and viewer context. Therefore it influence to the current
312     // selection and if the active widget listens it, the attribute value is errnoneous
313     // changed.
314     ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
315     if (aPropertyPanel)
316       aPropertyPanel->cleanContent();
317
318     myFeature->setStable(true);
319
320     SessionPtr aMgr = ModelAPI_Session::get();
321     /// Set current feature and remeber old current feature
322
323     commitOperation();
324     aMgr->finishOperation();
325
326     stopOperation();
327     emit stopped();
328     emit committed();
329
330     afterCommitOperation();
331     return true;
332   }
333   return false;
334 }
335
336 ModuleBase_ModelWidget* ModuleBase_OperationFeature::activateByPreselection(
337                                               const std::string& theGreedAttributeId)
338 {
339   ModuleBase_ModelWidget* aWidget = 0;
340   if (myPreSelection.empty())
341     return aWidget;
342   // equal vertices should not be used here
343   ModuleBase_ISelection::filterSelectionOnEqualPoints(myPreSelection);
344
345   ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
346   ModuleBase_ModelWidget* aFilledWgt = 0;
347   if (aPropertyPanel) {
348     const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
349     QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
350     ModuleBase_ModelWidget* aWgt = 0;
351     if (!aWidgets.empty()) {
352       if (!theGreedAttributeId.empty()) {
353         // set preselection to greed widget
354         for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
355           aWgt = (*aWIt);
356           if (aWgt->attributeID() == theGreedAttributeId) {
357             aPropertyPanel->setPreselectionWidget(aWgt);
358             aWgt->setSelection(myPreSelection, true);
359             aPropertyPanel->setPreselectionWidget(NULL);
360             aFilledWgt = aWgt;
361             break;
362           }
363         }
364       }
365       else {
366         bool isSet = false;
367         // 1. apply the selection to controls
368         for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
369           aWgt = (*aWIt);
370           if (!aWgt->canAcceptFocus())
371             continue;
372           aPropertyPanel->setPreselectionWidget(aWgt);
373           if (!aWgt->setSelection(myPreSelection, true)) {
374             isSet = false;
375             break;
376           } else {
377             isSet = true;
378             aFilledWgt = aWgt;
379           }
380         }
381       }
382       aPropertyPanel->setPreselectionWidget(NULL);
383       // in order to redisplay object in the viewer, the update/redisplay signals should be flushed
384       // it is better to perform it not in setSelection of each widget, but do it here,
385       // after the preselection is processed
386       ModuleBase_Tools::flushUpdated(myFeature);
387     }
388   }
389   clearPreselection();
390
391   return aFilledWgt;
392 }
393
394 void ModuleBase_OperationFeature::setParentFeature(CompositeFeaturePtr theParent)
395 {
396   myParentFeature = theParent;
397 }
398
399 CompositeFeaturePtr ModuleBase_OperationFeature::parentFeature() const
400 {
401   return myParentFeature;
402 }
403
404 void ModuleBase_OperationFeature::setPreviousCurrentFeature(const FeaturePtr& theFeature)
405 {
406   myPreviousCurrentFeature = theFeature;
407 }
408
409 FeaturePtr ModuleBase_OperationFeature::previousCurrentFeature()
410 {
411   return myPreviousCurrentFeature;
412 }
413
414 void ModuleBase_OperationFeature::initSelection(ModuleBase_ISelection* theSelection,
415                                          ModuleBase_IViewer* theViewer)
416 {
417   QList<ModuleBase_ViewerPrsPtr> aPreSelected;
418   // Check that the selected result are not results of operation feature
419   FeaturePtr aFeature = feature();
420   if (aFeature) {
421     QList<ModuleBase_ViewerPrsPtr> aSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
422
423     std::list<ResultPtr> aResults = aFeature->results();
424     QObjectPtrList aResList;
425     std::list<ResultPtr>::const_iterator aIt;
426     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
427       aResList.append(*aIt);
428
429     foreach (ModuleBase_ViewerPrsPtr aPrs, aSelected) {
430       if ((!aResList.contains(aPrs->object())) && (aPrs->object() != aFeature))
431         aPreSelected.append(aPrs);
432     }
433   } else
434     aPreSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
435
436   setPreselection(aPreSelected);
437 }
438
439 void ModuleBase_OperationFeature::setPreselection(const QList<ModuleBase_ViewerPrsPtr>& theValues)
440 {
441   clearPreselection();
442   myPreSelection = theValues;
443 }
444
445 void ModuleBase_OperationFeature::clearPreselection()
446 {
447   myPreSelection.clear();
448 }
449
450 void ModuleBase_OperationFeature::setPropertyPanel(ModuleBase_IPropertyPanel* theProp) 
451 {
452   ModuleBase_Operation::setPropertyPanel(theProp);
453
454   theProp->setEditingMode(isEditOperation());
455
456   if (theProp) {
457     const QList<ModuleBase_ModelWidget*>& aWidgets = theProp->modelWidgets();
458     QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
459     for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
460       ModuleBase_ModelWidget* aWgt = (*aWIt);
461       connect(aWgt, SIGNAL(valuesChanged()), this, SLOT(onValuesChanged()));
462     }
463   }
464
465   // Do not activate widgets by default if the current operation is editing operation
466   // Because we don't know which widget is going to be edited. 
467   if (!isEditOperation()) {
468     // 4. activate the first obligatory widget
469     theProp->activateNextWidget(NULL);
470   }
471   else {
472     // set focus on Ok button in order to operation manager could process Enter press
473     if (theProp)
474       theProp->setFocusOnOkButton();
475   }
476 }