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