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