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