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