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