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