Salome HOME
Updated copyright comment
[modules/shaper.git] / src / ModuleBase / ModuleBase_OperationFeature.cpp
1 // Copyright (C) 2014-2024  CEA, EDF
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   return myFeature;
179 }
180
181 void ModuleBase_OperationFeature::setFeature(FeaturePtr theFeature)
182 {
183   myFeature = theFeature;
184   myIsEditing = true;
185 }
186
187 bool ModuleBase_OperationFeature::hasObject(ObjectPtr theObj) const
188 {
189   FeaturePtr aFeature = feature();
190   if (aFeature) {
191     if (aFeature == theObj)
192       return true;
193     ResultPtr anObjRes = std::dynamic_pointer_cast<ModelAPI_Result>(theObj);
194     if (anObjRes.get() && aFeature == aFeature->document()->feature(anObjRes)) {
195       return true;
196     }
197 #ifdef DEBUG_DO_NOT_ACTIVATE_SUB_FEATURE
198     if (aFeature->isMacro()) {
199       // macro feature may refers to sub-features,
200       // 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
206         anIt = anAttributes.begin(), aLast = anAttributes.end();
207       bool aFoundObject = false;
208       for (; anIt != aLast && !aFoundObject; anIt++) {
209         std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
210                                std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*anIt);
211         for (int i = 0, aNb = aCurSelList->size(); i < aNb && !aFoundObject; i++) {
212           ObjectPtr aCurObj = aCurSelList->object(i);
213           FeaturePtr aCurFeat = std::dynamic_pointer_cast<ModelAPI_Feature>(aCurObj);
214           if (aCurFeat.get()) {
215             aFoundObject = anObjectFeature == aCurFeat;
216           }
217         }
218       }
219       return aFoundObject;
220     }
221 #endif
222   }
223   return false;
224 }
225
226 bool ModuleBase_OperationFeature::isDisplayedOnStart(ObjectPtr theObject)
227 {
228   return myVisualizedObjects.find(theObject) != myVisualizedObjects.end();
229 }
230
231 bool ModuleBase_OperationFeature::start()
232 {
233 #ifdef DEBUG_OPERATION_START
234   qDebug("ModuleBase_OperationFeature::start -- begin");
235 #endif
236   QString anId = getDescription()->operationId();
237   if (myIsEditing) {
238     anId = anId.append(EditSuffix());
239   }
240   ModelAPI_Session::get()->startOperation(anId.toStdString());
241
242   emit beforeStarted();
243   startOperation();
244
245   if (!myIsEditing) {
246     FeaturePtr aFeature = createFeature();
247     // if the feature is not created, there is no sense to start the operation
248     if (aFeature.get() == NULL) {
249       // it is necessary to abor the operation in the session and emit the aborted signal
250       // in order to update commands status in the workshop, to be exact the feature action
251       // to be unchecked
252       abort();
253 #ifdef DEBUG_OPERATION_START
254   qDebug("ModuleBase_OperationFeature::start -- end : IMPOSSIBLE to start");
255 #endif
256       return false;
257     }
258   }
259   //Already called startOperation();
260   emit started();
261 #ifdef DEBUG_OPERATION_START
262   qDebug("ModuleBase_OperationFeature::start -- end");
263 #endif
264   return true;
265 }
266
267 void ModuleBase_OperationFeature::abort()
268 {
269 #ifdef DEBUG_OPERATION_START
270   qDebug("ModuleBase_OperationFeature::abort -- begin");
271 #endif
272
273   emit beforeAborted();
274
275   // the viewer update should be blocked in order to avoid the features blinking before they are
276   // hidden
277   std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
278       new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
279   Events_Loop::loop()->send(aMsg);
280
281   if (myFeature.get())
282   {
283     static const Events_ID anEvent = Events_Loop::eventByName(EVENT_VISUAL_ATTRIBUTES);
284     ModelAPI_EventCreator::get()->sendUpdated(myFeature, anEvent);
285   }
286
287   // the widgets of property panel should not process any events come from data mode
288   // after abort clicked. Some signal such as redisplay/create influence on content
289   // of the object browser and viewer context. Therefore it influence to the current
290   // selection and if the active widget listens it, the attribute value is errnoneous
291   // changed.
292   ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
293   if (aPropertyPanel)
294     aPropertyPanel->cleanContent();
295
296   if (myFeature.get())
297     myFeature->setStable(true);
298
299   abortOperation();
300   stopOperation();
301
302   SessionPtr aMgr = ModelAPI_Session::get();
303   aMgr->abortOperation();
304   emit stopped();
305   // the viewer update should be unblocked in order to avoid the features blinking before they are
306   // hidden
307   aMsg = std::shared_ptr<Events_Message>(
308                 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
309
310   Events_Loop::loop()->send(aMsg);
311
312   emit aborted();
313 #ifdef DEBUG_OPERATION_START
314   qDebug("ModuleBase_OperationFeature::abort -- end");
315 #endif
316 }
317
318 bool ModuleBase_OperationFeature::commit()
319 {
320 #ifdef DEBUG_OPERATION_START
321   qDebug("ModuleBase_OperationFeature::commit -- begin");
322 #endif
323   ModuleBase_IPropertyPanel* aPanel = propertyPanel();
324   if (aPanel) {
325     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
326     if (anActiveWidget && anActiveWidget->getValueState() == ModuleBase_ModelWidget::ModifiedInPP) {
327       anActiveWidget->storeValue();
328     }
329     aPanel->onAcceptData();
330   }
331   if (canBeCommitted()) {
332     emit beforeCommitted();
333     // the widgets of property panel should not process any events come from data mode
334     // after commit clicked. Some signal such as redisplay/create influence on content
335     // of the object browser and viewer context. Therefore it influence to the current
336     // selection and if the active widget listens it, the attribute value is errnoneous
337     // changed.
338     ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
339     if (aPropertyPanel)
340       aPropertyPanel->cleanContent();
341
342     myFeature->setStable(true);
343
344     commitOperation();
345
346     stopOperation();
347
348     SessionPtr aMgr = ModelAPI_Session::get();
349     aMgr->finishOperation();
350     // Finish operation has to be before stopped because stopped caused update of Object browser
351     // If it will be done before of cleaning of obsolete objects it will cause crash
352     emit stopped();
353     emit committed();
354
355     afterCommitOperation();
356 #ifdef DEBUG_OPERATION_START
357   qDebug("ModuleBase_OperationFeature::commit -- end");
358 #endif
359     return true;
360   }
361 #ifdef DEBUG_OPERATION_START
362   qDebug("ModuleBase_OperationFeature::commit -- end : IMPOSSIBLE to commit");
363 #endif
364   return false;
365 }
366
367 ModuleBase_ModelWidget* ModuleBase_OperationFeature::activateByPreselection(
368                                               const std::string& theGreedAttributeId)
369 {
370   ModuleBase_ModelWidget* aWidget = 0;
371   if (myPreSelection.empty())
372     return aWidget;
373
374   ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
375   ModuleBase_ModelWidget* aFilledWgt = 0;
376   if (aPropertyPanel) {
377     const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
378     QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
379     ModuleBase_ModelWidget* aWgt = 0;
380     if (!aWidgets.empty()) {
381       // equal vertices should not be used here
382       ModuleBase_ISelection::filterSelectionOnEqualPoints(myPreSelection);
383
384       if (!theGreedAttributeId.empty()) {
385         // set preselection to greed widget
386         for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
387           aWgt = (*aWIt);
388           if (aWgt->attributeID() == theGreedAttributeId) {
389             aPropertyPanel->setPreselectionWidget(aWgt);
390             if (aWgt->setSelection(myPreSelection, true)) {
391               aPropertyPanel->setPreselectionWidget(NULL);
392               aFilledWgt = aWgt;
393               break;
394             }
395             else { // do not process invalid for greed widget selection
396               break;
397             }
398           }
399         }
400       }
401       else {
402         // 1. apply the selection to controls
403         for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
404           aWgt = (*aWIt);
405           if (!aWgt->canAcceptFocus())
406             continue;
407           aPropertyPanel->setPreselectionWidget(aWgt);
408           if (myPreSelection.empty() || !aWgt->setSelection(myPreSelection, true))
409             break;
410           else
411             aFilledWgt = aWgt;
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(
447   const QList<ModuleBase_ViewerPrsPtr>& thePreSelected)
448 {
449   QObjectPtrList aCurrentFeatureResults;
450
451   // Check that the selected result are not results of operation feature
452   FeaturePtr aFeature = feature();
453   if (aFeature) {
454     std::list<ResultPtr> aResults;
455     ModelAPI_Tools::allResults(aFeature, aResults);
456     std::list<ResultPtr>::const_iterator aIt;
457     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
458       aCurrentFeatureResults.append(*aIt);
459   }
460
461   if (aCurrentFeatureResults.empty()) /// filtering of selection is not necessary
462     setPreselection(thePreSelected);
463   else { // create preselection list without results of current feature
464     QList<ModuleBase_ViewerPrsPtr> aPreSelected;
465     foreach (ModuleBase_ViewerPrsPtr aPrs, thePreSelected) {
466       if ((!aCurrentFeatureResults.contains(aPrs->object())) && (aPrs->object() != aFeature))
467         aPreSelected.append(aPrs);
468     }
469     setPreselection(aPreSelected);
470   }
471 }
472
473 void ModuleBase_OperationFeature::setPreselection(const QList<ModuleBase_ViewerPrsPtr>& theValues)
474 {
475   clearPreselection();
476   myPreSelection = theValues;
477 }
478
479 void ModuleBase_OperationFeature::clearPreselection()
480 {
481   myPreSelection.clear();
482 }
483
484 void ModuleBase_OperationFeature::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
485 {
486   ModuleBase_Operation::setPropertyPanel(theProp);
487
488   theProp->setEditingMode(isEditOperation());
489
490   if (theProp) {
491     const QList<ModuleBase_ModelWidget*>& aWidgets = theProp->modelWidgets();
492     QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
493     for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
494       ModuleBase_ModelWidget* aWgt = (*aWIt);
495       connect(aWgt, SIGNAL(valuesChanged()), this, SLOT(onValuesChanged()));
496       connect(aWgt, SIGNAL(valueStateChanged(int)), this, SLOT(onValueStateChanged(int)));
497     }
498   }
499
500   // Do not activate widgets by default if the current operation is editing operation
501   // Because we don't know which widget is going to be edited.
502   if (!isEditOperation()) {
503     // 4. activate the first obligatory widget
504     theProp->activateNextWidget(NULL);
505   }
506   else {
507     // set focus on Ok button in order to operation manager could process Enter press
508     if (theProp)
509       theProp->setFocusOnOkButton();
510   }
511 }
512
513 void ModuleBase_OperationFeature::resumeOperation()
514 {
515   if (myRestartTransactionOnResume) {
516     ModelAPI_Session::get()->startOperation(this->id().toStdString(), true);
517     myRestartTransactionOnResume = false;
518   }
519 }