Salome HOME
Issue #1302 Restricting preselection to the first argument only: mirror feature has...
[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   return myFeature;
178 }
179
180 void ModuleBase_OperationFeature::setFeature(FeaturePtr theFeature)
181 {
182   myFeature = theFeature;
183   myIsEditing = true;
184 }
185
186 bool ModuleBase_OperationFeature::hasObject(ObjectPtr theObj) const
187 {
188   FeaturePtr aFeature = feature();
189   if (aFeature) {
190     if (aFeature == theObj)
191       return true;
192     std::list<ResultPtr> aResults = aFeature->results();
193     std::list<ResultPtr>::const_iterator aIt;
194     for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
195       if (theObj == (*aIt))
196         return true;
197     }
198 #ifdef DEBUG_DO_NOT_ACTIVATE_SUB_FEATURE
199     if (aFeature->isMacro()) {
200       // macro feature may refers to sub-features, which also should be deactivated when the operation
201       // is active, e.g. rectangle'lines.
202       FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObj);
203       std::list<AttributePtr> anAttributes = aFeature->data()->attributes(
204                                               ModelAPI_AttributeRefList::typeId());
205       std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
206       bool aFoundObject = false;
207       for (; anIt != aLast && !aFoundObject; anIt++) {
208         std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
209                                          std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*anIt);
210         for (int i = 0, aNb = aCurSelList->size(); i < aNb && !aFoundObject; i++) {
211           ObjectPtr anObject = aCurSelList->object(i);
212           FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
213           if (aFeature.get()) {
214             aFoundObject = anObjectFeature == aFeature;
215           }
216         }
217       }
218       return aFoundObject;
219     }
220 #endif
221   }
222   return false;
223 }
224
225 bool ModuleBase_OperationFeature::isDisplayedOnStart(ObjectPtr theObject)
226 {
227   return myVisualizedObjects.find(theObject) != myVisualizedObjects.end();
228 }
229
230 bool ModuleBase_OperationFeature::start()
231 {
232   setIsModified(false);
233   QString anId = getDescription()->operationId();
234   if (myIsEditing) {
235     anId = anId.append(EditSuffix());
236   }
237   ModelAPI_Session::get()->startOperation(anId.toStdString());
238
239   emit beforeStarted();
240   startOperation();
241
242   if (!myIsEditing) {
243     FeaturePtr aFeature = createFeature();
244     // if the feature is not created, there is no sense to start the operation
245     if (aFeature.get() == NULL) {
246       // it is necessary to abor the operation in the session and emit the aborted signal
247       // in order to update commands status in the workshop, to be exact the feature action
248       // to be unchecked
249       abort();
250       return false;
251     }
252   }
253   //Already called startOperation();
254   emit started();
255   return true;
256 }
257
258 void ModuleBase_OperationFeature::abort()
259 {
260   emit beforeAborted();
261
262   // the viewer update should be blocked in order to avoid the features blinking before they are
263   // hidden
264   std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
265       new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
266   Events_Loop::loop()->send(aMsg);
267
268   // the widgets of property panel should not process any events come from data mode
269   // after abort clicked. Some signal such as redisplay/create influence on content
270   // of the object browser and viewer context. Therefore it influence to the current
271   // selection and if the active widget listens it, the attribute value is errnoneous
272   // changed.
273   ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
274   if (aPropertyPanel)
275     aPropertyPanel->cleanContent();
276
277   if (myFeature.get())
278     myFeature->setStable(true);
279
280   abortOperation();
281   stopOperation();
282
283   SessionPtr aMgr = ModelAPI_Session::get();
284   aMgr->abortOperation();
285   emit stopped();
286   // the viewer update should be unblocked in order to avoid the features blinking before they are
287   // hidden
288   aMsg = std::shared_ptr<Events_Message>(
289                 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
290
291   Events_Loop::loop()->send(aMsg);
292
293   emit aborted();
294 }
295
296 bool ModuleBase_OperationFeature::commit()
297 {
298   ModuleBase_IPropertyPanel* aPanel = propertyPanel();
299   if (aPanel) {
300     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
301     if (anActiveWidget && anActiveWidget->getValueState() == ModuleBase_ModelWidget::ModifiedInPP) {
302       anActiveWidget->storeValue();
303     }
304   }
305   if (canBeCommitted()) {
306     emit beforeCommitted();
307     // the widgets of property panel should not process any events come from data mode
308     // after commit clicked. Some signal such as redisplay/create influence on content
309     // of the object browser and viewer context. Therefore it influence to the current
310     // selection and if the active widget listens it, the attribute value is errnoneous
311     // changed.
312     ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
313     if (aPropertyPanel)
314       aPropertyPanel->cleanContent();
315
316     myFeature->setStable(true);
317
318     SessionPtr aMgr = ModelAPI_Session::get();
319     /// Set current feature and remeber old current feature
320
321     commitOperation();
322     aMgr->finishOperation();
323
324     stopOperation();
325     emit stopped();
326     emit committed();
327
328     afterCommitOperation();
329     return true;
330   }
331   return false;
332 }
333
334 void ModuleBase_OperationFeature::activateByPreselection(ModuleBase_IWorkshop* theWorkshop)
335 {
336   if (myPreSelection.empty())
337     return;
338
339   ModuleBase_ISelection::filterSelectionOnEqualPoints(myPreSelection);
340
341   std::string aGreedAttributeId = ModuleBase_Tools::findGreedAttribute(theWorkshop, myFeature);
342
343   ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
344   ModuleBase_ModelWidget* aFilledWgt = 0;
345   if (aPropertyPanel) {
346     const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
347     QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
348     ModuleBase_ModelWidget* aWgt = 0;
349     if (!aWidgets.empty()) {
350       if (!aGreedAttributeId.empty()) {
351         // set preselection to greed widget
352         for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
353           aWgt = (*aWIt);
354           if (aWgt->attributeID() == aGreedAttributeId) {
355             aPropertyPanel->setPreselectionWidget(aWgt);
356             aWgt->setSelection(myPreSelection, true);
357             aPropertyPanel->setPreselectionWidget(NULL);
358             break;
359           }
360         }
361         // activate first not greed widget
362         std::string aFirstAttributeId = aWidgets.front()->attributeID();
363         if (aFirstAttributeId == aGreedAttributeId) // activate next widget after greeded
364           aFilledWgt = aWidgets.front();
365         else
366           aFilledWgt = NULL; // activate first widget of the panel
367       }
368       else {
369         bool isSet = false;
370         // 1. apply the selection to controls
371         for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
372           aWgt = (*aWIt);
373           if (!aWgt->canSetValue())
374             continue;
375           aPropertyPanel->setPreselectionWidget(aWgt);
376           if (!aWgt->setSelection(myPreSelection, true)) {
377             isSet = false;
378             break;
379           } else {
380             isSet = true;
381             aFilledWgt = aWgt;
382           }
383         }
384       }
385       aPropertyPanel->setPreselectionWidget(NULL);
386       // in order to redisplay object in the viewer, the update/redisplay signals should be flushed
387       // it is better to perform it not in setSelection of each widget, but do it here,
388       // after the preselection is processed
389       ModuleBase_ModelWidget::updateObject(myFeature);
390
391       // 3. a signal should be emitted before the next widget activation
392       // because, the activation of the next widget will give a focus to the widget. As a result
393       // the value of the widget is initialized. And commit may happens until the value is entered.
394       if (aFilledWgt)
395         emit activatedByPreselection();
396     }
397   }
398   // 4. activate the next obligatory widget
399   aPropertyPanel->activateNextWidget(aFilledWgt);
400
401   clearPreselection();
402 }
403
404 void ModuleBase_OperationFeature::setParentFeature(CompositeFeaturePtr theParent)
405 {
406   myParentFeature = theParent;
407 }
408
409 CompositeFeaturePtr ModuleBase_OperationFeature::parentFeature() const
410 {
411   return myParentFeature;
412 }
413
414 void ModuleBase_OperationFeature::setPreviousCurrentFeature(const FeaturePtr& theFeature)
415 {
416   myPreviousCurrentFeature = theFeature;
417 }
418
419 FeaturePtr ModuleBase_OperationFeature::previousCurrentFeature()
420 {
421   return myPreviousCurrentFeature;
422 }
423
424 void ModuleBase_OperationFeature::initSelection(ModuleBase_ISelection* theSelection,
425                                          ModuleBase_IViewer* theViewer)
426 {
427   clearPreselection();
428
429   QList<ModuleBase_ViewerPrs> aPreSelected;
430   // Check that the selected result are not results of operation feature
431   FeaturePtr aFeature = feature();
432   if (aFeature) {
433     QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
434
435     std::list<ResultPtr> aResults = aFeature->results();
436     QObjectPtrList aResList;
437     std::list<ResultPtr>::const_iterator aIt;
438     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
439       aResList.append(*aIt);
440
441     foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
442       if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
443         aPreSelected.append(aPrs);
444     }
445   } else
446     aPreSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
447
448   myPreSelection = aPreSelected;
449 }
450
451 void ModuleBase_OperationFeature::clearPreselection()
452 {
453   myPreSelection.clear();
454 }
455
456 void ModuleBase_OperationFeature::setPropertyPanel(ModuleBase_IPropertyPanel* theProp) 
457 {
458   ModuleBase_Operation::setPropertyPanel(theProp);
459
460   theProp->setEditingMode(isEditOperation());
461
462   if (theProp) {
463     const QList<ModuleBase_ModelWidget*>& aWidgets = theProp->modelWidgets();
464     QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
465     for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
466       ModuleBase_ModelWidget* aWgt = (*aWIt);
467       connect(aWgt, SIGNAL(valuesChanged()), this, SLOT(onValuesChanged()));
468     }
469   }
470
471   // Do not activate widgets by default if the current operation is editing operation
472   // Because we don't know which widget is going to be edited. 
473   if (!isEditOperation()) {
474     // 4. activate the first obligatory widget
475     theProp->activateNextWidget(NULL);
476   }
477   else {
478     // set focus on Ok button in order to operation manager could process Enter press
479     if (theProp)
480       theProp->setFocusOnOkButton();
481   }
482 }