Salome HOME
Internal Edit operation in a transaction.
[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
19 #include <ModelAPI_AttributeDouble.h>
20 #include <ModelAPI_Document.h>
21 #include <ModelAPI_Feature.h>
22 #include <ModelAPI_Data.h>
23 #include <ModelAPI_Document.h>
24 #include <ModelAPI_Events.h>
25 #include <ModelAPI_Result.h>
26 #include <ModelAPI_Object.h>
27 #include <ModelAPI_Validator.h>
28 #include <ModelAPI_Session.h>
29 #include <ModelAPI_Tools.h>
30
31 #include <GeomAPI_Pnt2d.h>
32
33 #include <Events_Loop.h>
34
35 #include <QTimer>
36
37 #ifdef _DEBUG
38 #include <QDebug>
39 #endif
40
41 ModuleBase_OperationFeature::ModuleBase_OperationFeature(const QString& theId, QObject* theParent)
42 : ModuleBase_Operation(theId, theParent),
43   myIsEditing(false)
44 {
45 }
46
47 ModuleBase_OperationFeature::~ModuleBase_OperationFeature()
48 {
49   clearPreselection();
50 }
51
52 void ModuleBase_OperationFeature::setEditOperation(const bool theRestartTransaction)
53 {
54   if (isEditOperation())
55     return;
56
57   myIsEditing = true;
58   if (theRestartTransaction) {
59     SessionPtr aMgr = ModelAPI_Session::get();
60     DocumentPtr aDoc = aMgr->activeDocument();
61     FeaturePtr aFeature = aDoc->currentFeature(false);
62     ModelAPI_Session::get()->finishOperation();
63     FeaturePtr anAFeature = aDoc->currentFeature(false);
64
65     QString anId = getDescription()->operationId();
66     if (myIsEditing) {
67       anId = anId.append(EditSuffix());
68     }
69     ModelAPI_Session::get()->startOperation(anId.toStdString());
70     emit beforeStarted();
71   }
72
73   propertyPanel()->setEditingMode(isEditOperation());
74 }
75
76 FeaturePtr ModuleBase_OperationFeature::feature() const
77 {
78   return myFeature;
79 }
80
81 bool ModuleBase_OperationFeature::isValid() const
82 {
83   if (!myFeature || !myFeature->data()->isValid())
84     return true; // rename operation
85   if (myFeature->isAction())
86     return true;
87   //Get validators for the Id
88   SessionPtr aMgr = ModelAPI_Session::get();
89   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
90   bool aValid = aFactory->validate(myFeature);
91
92   // the feature exec state should be checked in order to do not apply features, which result can not
93   // be built. E.g. extrusion on sketch, where the "to" is a perpendicular plane to the sketch
94   bool isDone = ( myFeature->data()->execState() == ModelAPI_StateDone
95                || myFeature->data()->execState() == ModelAPI_StateMustBeUpdated );
96
97   return aValid && isDone;
98 }
99
100 void ModuleBase_OperationFeature::startOperation()
101 {
102   FeaturePtr aFeature = feature();
103   if (!aFeature.get() || !isEditOperation())
104     return;
105
106   if (aFeature.get() && isEditOperation())
107     aFeature->setStable(false);
108
109   myVisualizedObjects.clear();
110   // store hidden result features
111   std::list<ResultPtr> aResults = aFeature->results();
112   std::list<ResultPtr>::const_iterator aIt;
113   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
114     ObjectPtr anObject = *aIt;
115     if (anObject.get() && !anObject->isDisplayed()) {
116       myVisualizedObjects.insert(*aIt);
117       anObject->setDisplayed(true);
118     }
119   }
120   if (!aFeature->isDisplayed()) {
121     myVisualizedObjects.insert(aFeature);
122     aFeature->setDisplayed(true);
123   }
124   Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
125 }
126
127 void ModuleBase_OperationFeature::stopOperation()
128 {
129   FeaturePtr aFeature = feature();
130   if (!aFeature.get() || !isEditOperation())
131     return;
132
133   // store hidden result features
134   std::list<ResultPtr> aResults = aFeature->results();
135   std::list<ResultPtr>::const_iterator aIt;
136   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
137     ObjectPtr anObject = *aIt;
138     if (anObject.get() && myVisualizedObjects.find(anObject) != myVisualizedObjects.end()) {
139       anObject->setDisplayed(false);
140     }
141   }
142   if (myVisualizedObjects.find(aFeature) != myVisualizedObjects.end()) {
143     aFeature->setDisplayed(false);
144   }
145   if (myVisualizedObjects.size() > 0)
146     Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
147 }
148
149 FeaturePtr ModuleBase_OperationFeature::createFeature(const bool theFlushMessage)
150 {
151   if (myParentFeature.get()) {
152     myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
153   } else {
154     std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
155     myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
156   }
157   if (myFeature) {  // TODO: generate an error if feature was not created
158     setIsModified(true);
159     // Model update should call "execute" of a feature.
160     //myFeature->execute();
161     // Init default values
162     /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
163      QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
164      for (; anIt != aLast; anIt++) {
165      (*anIt)->storeValue(aFeature);
166      }*/
167   }
168
169   if (theFlushMessage)
170     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
171   return myFeature;
172 }
173
174 void ModuleBase_OperationFeature::setFeature(FeaturePtr theFeature)
175 {
176   myFeature = theFeature;
177   myIsEditing = true;
178 }
179
180 bool ModuleBase_OperationFeature::hasObject(ObjectPtr theObj) const
181 {
182   FeaturePtr aFeature = feature();
183   if (aFeature) {
184     if (aFeature == theObj)
185       return true;
186     std::list<ResultPtr> aResults = aFeature->results();
187     std::list<ResultPtr>::const_iterator aIt;
188     for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
189       if (theObj == (*aIt))
190         return true;
191     }
192   }
193   return false;
194 }
195
196 bool ModuleBase_OperationFeature::isDisplayedOnStart(ObjectPtr theObject)
197 {
198   return myVisualizedObjects.find(theObject) != myVisualizedObjects.end();
199 }
200
201 void ModuleBase_OperationFeature::start()
202 {
203   setIsModified(false);
204   QString anId = getDescription()->operationId();
205   if (myIsEditing) {
206     anId = anId.append(EditSuffix());
207   }
208   ModelAPI_Session::get()->startOperation(anId.toStdString());
209
210   emit beforeStarted();
211   startOperation();
212
213   if (!myIsEditing) {
214     FeaturePtr aFeature = createFeature();
215     // if the feature is not created, there is no sense to start the operation
216     if (aFeature.get() == NULL) {
217       // it is necessary to abor the operation in the session and emit the aborted signal
218       // in order to update commands status in the workshop, to be exact the feature action
219       // to be unchecked
220       abort();
221       return;
222     }
223   }
224   //Already called startOperation();
225   emit started();
226
227 }
228
229 void ModuleBase_OperationFeature::abort()
230 {
231   emit beforeAborted();
232
233   // the viewer update should be blocked in order to avoid the features blinking before they are
234   // hidden
235   std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
236       new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
237   Events_Loop::loop()->send(aMsg);
238
239   // the widgets of property panel should not process any events come from data mode
240   // after abort clicked. Some signal such as redisplay/create influence on content
241   // of the object browser and viewer context. Therefore it influence to the current
242   // selection and if the active widget listens it, the attribute value is errnoneous
243   // changed.
244   ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
245   if (aPropertyPanel)
246     aPropertyPanel->cleanContent();
247
248   myFeature->setStable(true);
249
250   abortOperation();
251   stopOperation();
252
253   SessionPtr aMgr = ModelAPI_Session::get();
254   aMgr->abortOperation();
255   emit stopped();
256   // the viewer update should be unblocked in order to avoid the features blinking before they are
257   // hidden
258   aMsg = std::shared_ptr<Events_Message>(
259                 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
260
261   Events_Loop::loop()->send(aMsg);
262
263   emit aborted();
264 }
265
266 bool ModuleBase_OperationFeature::commit()
267 {
268   if (canBeCommitted()) {
269     emit beforeCommitted();
270     // the widgets of property panel should not process any events come from data mode
271     // after commit clicked. Some signal such as redisplay/create influence on content
272     // of the object browser and viewer context. Therefore it influence to the current
273     // selection and if the active widget listens it, the attribute value is errnoneous
274     // changed.
275     ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
276     if (aPropertyPanel)
277       aPropertyPanel->cleanContent();
278
279     myFeature->setStable(true);
280
281     SessionPtr aMgr = ModelAPI_Session::get();
282     /// Set current feature and remeber old current feature
283
284     commitOperation();
285     aMgr->finishOperation();
286
287     stopOperation();
288     emit stopped();
289     emit committed();
290
291     afterCommitOperation();
292     return true;
293   }
294   return false;
295 }
296
297 void ModuleBase_OperationFeature::activateByPreselection()
298 {
299   if (myPreSelection.empty())
300     return;
301
302   ModuleBase_ISelection::filterSelectionOnEqualPoints(myPreSelection);
303
304   ModuleBase_ModelWidget* aFilledWgt = 0;
305   ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
306   if (aPropertyPanel) {
307     const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
308     if (!aWidgets.empty()) {
309       ModuleBase_ModelWidget* aWgt = 0;
310       QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
311       bool isSet = false;
312       // 1. apply the selection to controls
313       for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
314         aWgt = (*aWIt);
315         if (!aWgt->canSetValue())
316           continue;
317         aPropertyPanel->setPreselectionWidget(aWgt);
318         if (!aWgt->setSelection(myPreSelection, true)) {
319           isSet = false;
320           break;
321         } else {
322           isSet = true;
323           aFilledWgt = aWgt;
324         }
325       }
326       aPropertyPanel->setPreselectionWidget(NULL);
327       // in order to redisplay object in the viewer, the update/redisplay signals should be flushed
328       // it is better to perform it not in setSelection of each widget, but do it here,
329       // after the preselection is processed
330       ModuleBase_ModelWidget::updateObject(myFeature);
331
332       // 3. a signal should be emitted before the next widget activation
333       // because, the activation of the next widget will give a focus to the widget. As a result
334       // the value of the widget is initialized. And commit may happens until the value is entered.
335       if (aFilledWgt)
336         emit activatedByPreselection();
337     }
338     // 4. activate the next obligatory widget
339     aPropertyPanel->activateNextWidget(aFilledWgt);
340   }
341
342   clearPreselection();
343 }
344
345 void ModuleBase_OperationFeature::setParentFeature(CompositeFeaturePtr theParent)
346 {
347   myParentFeature = theParent;
348 }
349
350 CompositeFeaturePtr ModuleBase_OperationFeature::parentFeature() const
351 {
352   return myParentFeature;
353 }
354
355 void ModuleBase_OperationFeature::setPreviousCurrentFeature(const FeaturePtr& theFeature)
356 {
357   myPreviousCurrentFeature = theFeature;
358 }
359
360 FeaturePtr ModuleBase_OperationFeature::previousCurrentFeature()
361 {
362   return myPreviousCurrentFeature;
363 }
364
365 void ModuleBase_OperationFeature::initSelection(ModuleBase_ISelection* theSelection,
366                                          ModuleBase_IViewer* theViewer)
367 {
368   clearPreselection();
369
370   QList<ModuleBase_ViewerPrs> aPreSelected;
371   // Check that the selected result are not results of operation feature
372   FeaturePtr aFeature = feature();
373   if (aFeature) {
374     QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
375
376     std::list<ResultPtr> aResults = aFeature->results();
377     QObjectPtrList aResList;
378     std::list<ResultPtr>::const_iterator aIt;
379     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
380       aResList.append(*aIt);
381
382     foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
383       if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
384         aPreSelected.append(aPrs);
385     }
386   } else
387     aPreSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
388
389   myPreSelection = aPreSelected;
390 }
391
392 void ModuleBase_OperationFeature::clearPreselection()
393 {
394   myPreSelection.clear();
395 }
396
397 void ModuleBase_OperationFeature::setPropertyPanel(ModuleBase_IPropertyPanel* theProp) 
398 {
399   ModuleBase_Operation::setPropertyPanel(theProp);
400
401   theProp->setEditingMode(isEditOperation());
402
403   if (theProp) {
404     const QList<ModuleBase_ModelWidget*>& aWidgets = theProp->modelWidgets();
405     QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
406     for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
407       ModuleBase_ModelWidget* aWgt = (*aWIt);
408       connect(aWgt, SIGNAL(valuesChanged()), this, SLOT(onValuesChanged()));
409     }
410   }
411
412   // Do not activate widgets by default if the current operation is editing operation
413   // Because we don't know which widget is going to be edited. 
414   if (!isEditOperation()) {
415     // 4. activate the first obligatory widget
416     theProp->activateNextWidget(NULL);
417   }
418   else {
419     // set focus on Ok button in order to operation manager could process Enter press
420     if (theProp)
421       theProp->setFocusOnOkButton();
422   }
423 }