Salome HOME
Provide reopening of transaction on resuming of Group operation
[modules/shaper.git] / src / ModuleBase / ModuleBase_OperationFeature.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "ModuleBase_OperationFeature.h"
21
22 #include "ModuleBase_OperationDescription.h"
23 #include "ModuleBase_ModelWidget.h"
24 #include "ModuleBase_ViewerPrs.h"
25 #include "ModuleBase_IPropertyPanel.h"
26 #include "ModuleBase_ISelection.h"
27 #include "ModuleBase_IViewer.h"
28 #include "ModuleBase_Tools.h"
29
30 #include <ModelAPI_AttributeDouble.h>
31 #include <ModelAPI_Document.h>
32 #include <ModelAPI_Feature.h>
33 #include <ModelAPI_Data.h>
34 #include <ModelAPI_Document.h>
35 #include <ModelAPI_Events.h>
36 #include <ModelAPI_Result.h>
37 #include <ModelAPI_Object.h>
38 #include <ModelAPI_Validator.h>
39 #include <ModelAPI_Session.h>
40 #include <ModelAPI_Tools.h>
41
42 #include <GeomAPI_Pnt2d.h>
43
44 #include <Events_Loop.h>
45
46 #include <QTimer>
47
48 // the define to check the activated object as a sub-feature by argument of
49 // the operation feature. E.g. rectangle feature(operation), line(in argument) to be not activated
50 #define DEBUG_DO_NOT_ACTIVATE_SUB_FEATURE
51 #ifdef DEBUG_DO_NOT_ACTIVATE_SUB_FEATURE
52 #include <ModelAPI_AttributeRefList.h>
53 #endif
54
55 //#define DEBUG_OPERATION_START
56
57 #ifdef _DEBUG
58 #include <QDebug>
59 #endif
60
61 ModuleBase_OperationFeature::ModuleBase_OperationFeature(const QString& theId, QObject* theParent)
62 : ModuleBase_Operation(theId, theParent), myIsEditing(false), myNeedToBeAborted(false),
63 myRestartTransactionOnResume(false)
64 {
65 }
66
67 ModuleBase_OperationFeature::~ModuleBase_OperationFeature()
68 {
69   clearPreselection();
70 }
71
72 void ModuleBase_OperationFeature::setEditOperation(const bool& isEditState
73                                                    /*const bool theRestartTransaction*/)
74 {
75   bool isCurrentEditState = isEditOperation();
76   if (isCurrentEditState == isEditState)
77     return;
78
79   /*
80   // this case is obsolete as it was not approved for reentrant sketch operation
81   // it was implemented when isEditState did not exist and only edit operation can be set
82   if (theRestartTransaction) {
83     // finsh previous create operation
84     emit beforeCommitted();
85     SessionPtr aMgr = ModelAPI_Session::get();
86     ModelAPI_Session::get()->finishOperation();
87
88     // start new edit operation
89     myIsEditing = true;
90     QString anId = getDescription()->operationId();
91     if (myIsEditing) {
92       anId = anId.append(EditSuffix());
93     }
94     ModelAPI_Session::get()->startOperation(anId.toStdString());
95     emit beforeStarted();
96   } else*/
97   myIsEditing = isEditState;
98
99   propertyPanel()->setEditingMode(isEditOperation());
100 }
101
102 FeaturePtr ModuleBase_OperationFeature::feature() const
103 {
104   return myFeature;
105 }
106
107 bool ModuleBase_OperationFeature::isValid() const
108 {
109   if (!myFeature || !myFeature->data()->isValid())
110     return true; // rename operation
111   if (myFeature->isAction())
112     return true;
113
114   std::string anError = ModelAPI_Tools::getFeatureError(myFeature);
115   //ModuleBase_Tools::translate(myFeature->getKind(), anError);
116   return anError.empty();
117 }
118
119 void ModuleBase_OperationFeature::startOperation()
120 {
121   FeaturePtr aFeature = feature();
122   if (!aFeature.get() || !isEditOperation())
123     return;
124
125   if (aFeature.get() && isEditOperation())
126     aFeature->setStable(false);
127
128   myVisualizedObjects.clear();
129   // store hidden result features
130   std::list<ResultPtr> aResults;
131   ModelAPI_Tools::allResults(aFeature, aResults);
132   std::list<ResultPtr>::const_iterator aIt;
133   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
134     ObjectPtr anObject = *aIt;
135     if (anObject.get() && !anObject->isDisplayed()) {
136       myVisualizedObjects.insert(*aIt);
137       anObject->setDisplayed(true);
138     }
139   }
140   if (!aFeature->isDisplayed()) {
141     myVisualizedObjects.insert(aFeature);
142     aFeature->setDisplayed(true);
143   }
144   Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
145 }
146
147 void ModuleBase_OperationFeature::stopOperation()
148 {
149   FeaturePtr aFeature = feature();
150   if (!aFeature.get() || !isEditOperation())
151     return;
152
153   // store hidden result features
154   std::list<ResultPtr> aResults;
155   ModelAPI_Tools::allResults(aFeature, aResults);
156   std::list<ResultPtr>::const_iterator aIt;
157   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
158     ObjectPtr anObject = *aIt;
159     if (anObject.get() && myVisualizedObjects.find(anObject) != myVisualizedObjects.end()) {
160       anObject->setDisplayed(false);
161     }
162   }
163   if (myVisualizedObjects.find(aFeature) != myVisualizedObjects.end()) {
164     aFeature->setDisplayed(false);
165   }
166   if (myVisualizedObjects.size() > 0)
167     Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
168 }
169
170 FeaturePtr ModuleBase_OperationFeature::createFeature(const bool theFlushMessage)
171 {
172   if (myParentFeature.get()) {
173     myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
174   } else {
175     std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
176     myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
177   }
178   if (myFeature) {  // TODO: generate an error if feature was not created
179     setIsModified(true);
180     // Model update should call "execute" of a feature.
181     //myFeature->execute();
182     // Init default values
183     /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
184      QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
185      for (; anIt != aLast; anIt++) {
186      (*anIt)->storeValue(aFeature);
187      }*/
188   }
189
190   //if (theFlushMessage) {
191   //  Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
192   //  Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
193   //}
194   return myFeature;
195 }
196
197 void ModuleBase_OperationFeature::setFeature(FeaturePtr theFeature)
198 {
199   myFeature = theFeature;
200   myIsEditing = true;
201 }
202
203 bool ModuleBase_OperationFeature::hasObject(ObjectPtr theObj) const
204 {
205   FeaturePtr aFeature = feature();
206   if (aFeature) {
207     if (aFeature == theObj)
208       return true;
209     ResultPtr anObjRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
210     if (anObjRes.get() && aFeature == aFeature->document()->feature(anObjRes)) {
211       return true;
212     }
213 #ifdef DEBUG_DO_NOT_ACTIVATE_SUB_FEATURE
214     if (aFeature->isMacro()) {
215       // macro feature may refers to sub-features,
216       // which also should be deactivated when the operation
217       // is active, e.g. rectangle'lines.
218       FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObj);
219       std::list<AttributePtr> anAttributes = aFeature->data()->attributes(
220                                               ModelAPI_AttributeRefList::typeId());
221       std::list<AttributePtr>::const_iterator
222         anIt = anAttributes.begin(), aLast = anAttributes.end();
223       bool aFoundObject = false;
224       for (; anIt != aLast && !aFoundObject; anIt++) {
225         std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
226                                std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*anIt);
227         for (int i = 0, aNb = aCurSelList->size(); i < aNb && !aFoundObject; i++) {
228           ObjectPtr anObject = aCurSelList->object(i);
229           FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
230           if (aFeature.get()) {
231             aFoundObject = anObjectFeature == aFeature;
232           }
233         }
234       }
235       return aFoundObject;
236     }
237 #endif
238   }
239   return false;
240 }
241
242 bool ModuleBase_OperationFeature::isDisplayedOnStart(ObjectPtr theObject)
243 {
244   return myVisualizedObjects.find(theObject) != myVisualizedObjects.end();
245 }
246
247 bool ModuleBase_OperationFeature::start()
248 {
249 #ifdef DEBUG_OPERATION_START
250   qDebug("ModuleBase_OperationFeature::start -- begin");
251 #endif
252   setIsModified(false);
253   QString anId = getDescription()->operationId();
254   if (myIsEditing) {
255     anId = anId.append(EditSuffix());
256   }
257   ModelAPI_Session::get()->startOperation(anId.toStdString());
258
259   emit beforeStarted();
260   startOperation();
261
262   if (!myIsEditing) {
263     FeaturePtr aFeature = createFeature();
264     // if the feature is not created, there is no sense to start the operation
265     if (aFeature.get() == NULL) {
266       // it is necessary to abor the operation in the session and emit the aborted signal
267       // in order to update commands status in the workshop, to be exact the feature action
268       // to be unchecked
269       abort();
270 #ifdef DEBUG_OPERATION_START
271   qDebug("ModuleBase_OperationFeature::start -- end : IMPOSSIBLE to start");
272 #endif
273       return false;
274     }
275   }
276   //Already called startOperation();
277   emit started();
278 #ifdef DEBUG_OPERATION_START
279   qDebug("ModuleBase_OperationFeature::start -- end");
280 #endif
281   return true;
282 }
283
284 void ModuleBase_OperationFeature::abort()
285 {
286 #ifdef DEBUG_OPERATION_START
287   qDebug("ModuleBase_OperationFeature::abort -- begin");
288 #endif
289
290   emit beforeAborted();
291
292   // the viewer update should be blocked in order to avoid the features blinking before they are
293   // hidden
294   std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
295       new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
296   Events_Loop::loop()->send(aMsg);
297
298   // the widgets of property panel should not process any events come from data mode
299   // after abort clicked. Some signal such as redisplay/create influence on content
300   // of the object browser and viewer context. Therefore it influence to the current
301   // selection and if the active widget listens it, the attribute value is errnoneous
302   // changed.
303   ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
304   if (aPropertyPanel)
305     aPropertyPanel->cleanContent();
306
307   if (myFeature.get())
308     myFeature->setStable(true);
309
310   abortOperation();
311   stopOperation();
312
313   SessionPtr aMgr = ModelAPI_Session::get();
314   aMgr->abortOperation();
315   emit stopped();
316   // the viewer update should be unblocked in order to avoid the features blinking before they are
317   // hidden
318   aMsg = std::shared_ptr<Events_Message>(
319                 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
320
321   Events_Loop::loop()->send(aMsg);
322
323   emit aborted();
324 #ifdef DEBUG_OPERATION_START
325   qDebug("ModuleBase_OperationFeature::abort -- end");
326 #endif
327 }
328
329 bool ModuleBase_OperationFeature::commit()
330 {
331 #ifdef DEBUG_OPERATION_START
332   qDebug("ModuleBase_OperationFeature::commit -- begin");
333 #endif
334   ModuleBase_IPropertyPanel* aPanel = propertyPanel();
335   if (aPanel) {
336     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
337     if (anActiveWidget && anActiveWidget->getValueState() == ModuleBase_ModelWidget::ModifiedInPP) {
338       anActiveWidget->storeValue();
339     }
340     aPanel->onAcceptData();
341   }
342   if (canBeCommitted()) {
343     emit beforeCommitted();
344     // the widgets of property panel should not process any events come from data mode
345     // after commit clicked. Some signal such as redisplay/create influence on content
346     // of the object browser and viewer context. Therefore it influence to the current
347     // selection and if the active widget listens it, the attribute value is errnoneous
348     // changed.
349     ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
350     if (aPropertyPanel)
351       aPropertyPanel->cleanContent();
352
353     myFeature->setStable(true);
354
355     commitOperation();
356
357     stopOperation();
358
359     SessionPtr aMgr = ModelAPI_Session::get();
360     aMgr->finishOperation();
361     // Finish operation has to be before stopped because stopped caused update of Object browser
362     // If it will be done before of cleaning of obsolete objects it will cause crash
363     emit stopped();
364     emit committed();
365
366     afterCommitOperation();
367 #ifdef DEBUG_OPERATION_START
368   qDebug("ModuleBase_OperationFeature::commit -- end");
369 #endif
370     return true;
371   }
372 #ifdef DEBUG_OPERATION_START
373   qDebug("ModuleBase_OperationFeature::commit -- end : IMPOSSIBLE to commit");
374 #endif
375   return false;
376 }
377
378 ModuleBase_ModelWidget* ModuleBase_OperationFeature::activateByPreselection(
379                                               const std::string& theGreedAttributeId)
380 {
381   ModuleBase_ModelWidget* aWidget = 0;
382   if (myPreSelection.empty())
383     return aWidget;
384
385   ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
386   ModuleBase_ModelWidget* aFilledWgt = 0;
387   if (aPropertyPanel) {
388     const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
389     QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
390     ModuleBase_ModelWidget* aWgt = 0;
391     if (!aWidgets.empty()) {
392       // equal vertices should not be used here
393       ModuleBase_ISelection::filterSelectionOnEqualPoints(myPreSelection);
394
395       if (!theGreedAttributeId.empty()) {
396         // set preselection to greed widget
397         for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
398           aWgt = (*aWIt);
399           if (aWgt->attributeID() == theGreedAttributeId) {
400             aPropertyPanel->setPreselectionWidget(aWgt);
401             if (aWgt->setSelection(myPreSelection, true)) {
402               aPropertyPanel->setPreselectionWidget(NULL);
403               aFilledWgt = aWgt;
404               break;
405             }
406             else { // do not process invalid for greed widget selection
407               break;
408             }
409           }
410         }
411       }
412       else {
413         bool isSet = false;
414         // 1. apply the selection to controls
415         for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
416           aWgt = (*aWIt);
417           if (!aWgt->canAcceptFocus())
418             continue;
419           aPropertyPanel->setPreselectionWidget(aWgt);
420           if (myPreSelection.empty() || !aWgt->setSelection(myPreSelection, true)) {
421             isSet = false;
422             break;
423           } else {
424             isSet = true;
425             aFilledWgt = aWgt;
426           }
427         }
428       }
429       aPropertyPanel->setPreselectionWidget(NULL);
430       // in order to redisplay object in the viewer, the update/redisplay signals should be flushed
431       // it is better to perform it not in setSelection of each widget, but do it here,
432       // after the preselection is processed
433       ModuleBase_Tools::flushUpdated(myFeature);
434     }
435   }
436   clearPreselection();
437
438   return aFilledWgt;
439 }
440
441 void ModuleBase_OperationFeature::setParentFeature(CompositeFeaturePtr theParent)
442 {
443   myParentFeature = theParent;
444 }
445
446 CompositeFeaturePtr ModuleBase_OperationFeature::parentFeature() const
447 {
448   return myParentFeature;
449 }
450
451 void ModuleBase_OperationFeature::setPreviousCurrentFeature(const FeaturePtr& theFeature)
452 {
453   myPreviousCurrentFeature = theFeature;
454 }
455
456 FeaturePtr ModuleBase_OperationFeature::previousCurrentFeature()
457 {
458   return myPreviousCurrentFeature;
459 }
460
461 void ModuleBase_OperationFeature::initSelection(
462   const QList<ModuleBase_ViewerPrsPtr>& thePreSelected)
463 {
464   QObjectPtrList aCurrentFeatureResults;
465
466   // Check that the selected result are not results of operation feature
467   FeaturePtr aFeature = feature();
468   if (aFeature) {
469     std::list<ResultPtr> aResults;
470     ModelAPI_Tools::allResults(aFeature, aResults);
471     std::list<ResultPtr>::const_iterator aIt;
472     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
473       aCurrentFeatureResults.append(*aIt);
474   }
475
476   if (aCurrentFeatureResults.empty()) /// filtering of selection is not necessary
477     setPreselection(thePreSelected);
478   else { // create preselection list without results of current feature
479     QList<ModuleBase_ViewerPrsPtr> aPreSelected;
480     foreach (ModuleBase_ViewerPrsPtr aPrs, thePreSelected) {
481       if ((!aCurrentFeatureResults.contains(aPrs->object())) && (aPrs->object() != aFeature))
482         aPreSelected.append(aPrs);
483     }
484     setPreselection(aPreSelected);
485   }
486 }
487
488 void ModuleBase_OperationFeature::setPreselection(const QList<ModuleBase_ViewerPrsPtr>& theValues)
489 {
490   clearPreselection();
491   myPreSelection = theValues;
492 }
493
494 void ModuleBase_OperationFeature::clearPreselection()
495 {
496   myPreSelection.clear();
497 }
498
499 void ModuleBase_OperationFeature::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
500 {
501   ModuleBase_Operation::setPropertyPanel(theProp);
502
503   theProp->setEditingMode(isEditOperation());
504
505   if (theProp) {
506     const QList<ModuleBase_ModelWidget*>& aWidgets = theProp->modelWidgets();
507     QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
508     for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
509       ModuleBase_ModelWidget* aWgt = (*aWIt);
510       connect(aWgt, SIGNAL(valuesChanged()), this, SLOT(onValuesChanged()));
511       connect(aWgt, SIGNAL(valueStateChanged(int)), this, SLOT(onValueStateChanged(int)));
512     }
513   }
514
515   // Do not activate widgets by default if the current operation is editing operation
516   // Because we don't know which widget is going to be edited.
517   if (!isEditOperation()) {
518     // 4. activate the first obligatory widget
519     theProp->activateNextWidget(NULL);
520   }
521   else {
522     // set focus on Ok button in order to operation manager could process Enter press
523     if (theProp)
524       theProp->setFocusOnOkButton();
525   }
526 }
527
528 void ModuleBase_OperationFeature::resumeOperation()
529 {
530   if (myRestartTransactionOnResume) {
531     ModelAPI_Session::get()->startOperation(this->id().toStdString(), true);
532     myRestartTransactionOnResume = false;
533   }
534 }