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