]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_OperationFeature.cpp
Salome HOME
1. setEditOperation should setCurrentFeature to the previous feature in the previous...
[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 void 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;
224     }
225   }
226   //Already called startOperation();
227   emit started();
228
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   myFeature->setStable(true);
251
252   abortOperation();
253   stopOperation();
254
255   SessionPtr aMgr = ModelAPI_Session::get();
256   aMgr->abortOperation();
257   emit stopped();
258   // the viewer update should be unblocked in order to avoid the features blinking before they are
259   // hidden
260   aMsg = std::shared_ptr<Events_Message>(
261                 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
262
263   Events_Loop::loop()->send(aMsg);
264
265   emit aborted();
266 }
267
268 bool ModuleBase_OperationFeature::commit()
269 {
270   if (canBeCommitted()) {
271     emit beforeCommitted();
272     // the widgets of property panel should not process any events come from data mode
273     // after commit clicked. Some signal such as redisplay/create influence on content
274     // of the object browser and viewer context. Therefore it influence to the current
275     // selection and if the active widget listens it, the attribute value is errnoneous
276     // changed.
277     ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
278     if (aPropertyPanel)
279       aPropertyPanel->cleanContent();
280
281     myFeature->setStable(true);
282
283     SessionPtr aMgr = ModelAPI_Session::get();
284     /// Set current feature and remeber old current feature
285
286     commitOperation();
287     aMgr->finishOperation();
288
289     stopOperation();
290     emit stopped();
291     emit committed();
292
293     afterCommitOperation();
294     return true;
295   }
296   return false;
297 }
298
299 void ModuleBase_OperationFeature::activateByPreselection()
300 {
301   if (myPreSelection.empty())
302     return;
303
304   ModuleBase_ISelection::filterSelectionOnEqualPoints(myPreSelection);
305
306   ModuleBase_ModelWidget* aFilledWgt = 0;
307   ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
308   if (aPropertyPanel) {
309     const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
310     if (!aWidgets.empty()) {
311       ModuleBase_ModelWidget* aWgt = 0;
312       QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
313       bool isSet = false;
314       // 1. apply the selection to controls
315       for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
316         aWgt = (*aWIt);
317         if (!aWgt->canSetValue())
318           continue;
319         aPropertyPanel->setPreselectionWidget(aWgt);
320         if (!aWgt->setSelection(myPreSelection, true)) {
321           isSet = false;
322           break;
323         } else {
324           isSet = true;
325           aFilledWgt = aWgt;
326         }
327       }
328       aPropertyPanel->setPreselectionWidget(NULL);
329       // in order to redisplay object in the viewer, the update/redisplay signals should be flushed
330       // it is better to perform it not in setSelection of each widget, but do it here,
331       // after the preselection is processed
332       ModuleBase_ModelWidget::updateObject(myFeature);
333
334       // 3. a signal should be emitted before the next widget activation
335       // because, the activation of the next widget will give a focus to the widget. As a result
336       // the value of the widget is initialized. And commit may happens until the value is entered.
337       if (aFilledWgt)
338         emit activatedByPreselection();
339     }
340     // 4. activate the next obligatory widget
341     aPropertyPanel->activateNextWidget(aFilledWgt);
342   }
343
344   clearPreselection();
345 }
346
347 void ModuleBase_OperationFeature::setParentFeature(CompositeFeaturePtr theParent)
348 {
349   myParentFeature = theParent;
350 }
351
352 CompositeFeaturePtr ModuleBase_OperationFeature::parentFeature() const
353 {
354   return myParentFeature;
355 }
356
357 void ModuleBase_OperationFeature::setPreviousCurrentFeature(const FeaturePtr& theFeature)
358 {
359   myPreviousCurrentFeature = theFeature;
360 }
361
362 FeaturePtr ModuleBase_OperationFeature::previousCurrentFeature()
363 {
364   return myPreviousCurrentFeature;
365 }
366
367 void ModuleBase_OperationFeature::initSelection(ModuleBase_ISelection* theSelection,
368                                          ModuleBase_IViewer* theViewer)
369 {
370   clearPreselection();
371
372   QList<ModuleBase_ViewerPrs> aPreSelected;
373   // Check that the selected result are not results of operation feature
374   FeaturePtr aFeature = feature();
375   if (aFeature) {
376     QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
377
378     std::list<ResultPtr> aResults = aFeature->results();
379     QObjectPtrList aResList;
380     std::list<ResultPtr>::const_iterator aIt;
381     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
382       aResList.append(*aIt);
383
384     foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
385       if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
386         aPreSelected.append(aPrs);
387     }
388   } else
389     aPreSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
390
391   myPreSelection = aPreSelected;
392 }
393
394 void ModuleBase_OperationFeature::clearPreselection()
395 {
396   myPreSelection.clear();
397 }
398
399 void ModuleBase_OperationFeature::setPropertyPanel(ModuleBase_IPropertyPanel* theProp) 
400 {
401   ModuleBase_Operation::setPropertyPanel(theProp);
402
403   theProp->setEditingMode(isEditOperation());
404
405   if (theProp) {
406     const QList<ModuleBase_ModelWidget*>& aWidgets = theProp->modelWidgets();
407     QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
408     for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
409       ModuleBase_ModelWidget* aWgt = (*aWIt);
410       connect(aWgt, SIGNAL(valuesChanged()), this, SLOT(onValuesChanged()));
411     }
412   }
413
414   // Do not activate widgets by default if the current operation is editing operation
415   // Because we don't know which widget is going to be edited. 
416   if (!isEditOperation()) {
417     // 4. activate the first obligatory widget
418     theProp->activateNextWidget(NULL);
419   }
420   else {
421     // set focus on Ok button in order to operation manager could process Enter press
422     if (theProp)
423       theProp->setFocusOnOkButton();
424   }
425 }