]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_OperationFeature.cpp
Salome HOME
Fix for the issue #905
[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
30 #include <GeomAPI_Pnt2d.h>
31
32 #include <Events_Loop.h>
33
34 #include <QTimer>
35
36 #ifdef _DEBUG
37 #include <QDebug>
38 #endif
39
40 ModuleBase_OperationFeature::ModuleBase_OperationFeature(const QString& theId, QObject* theParent)
41 : ModuleBase_Operation(theId, theParent),
42   myIsEditing(false)
43 {
44 }
45
46 ModuleBase_OperationFeature::~ModuleBase_OperationFeature()
47 {
48   clearPreselection();
49 }
50
51 FeaturePtr ModuleBase_OperationFeature::feature() const
52 {
53   return myFeature;
54 }
55
56 bool ModuleBase_OperationFeature::isValid() const
57 {
58   if (!myFeature || !myFeature->data()->isValid())
59     return true; // rename operation
60   if (myFeature->isAction())
61     return true;
62   //Get validators for the Id
63   SessionPtr aMgr = ModelAPI_Session::get();
64   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
65   bool aValid = aFactory->validate(myFeature);
66
67   // the feature exec state should be checked in order to do not apply features, which result can not
68   // be built. E.g. extrusion on sketch, where the "to" is a perpendicular plane to the sketch
69   bool isDone = ( myFeature->data()->execState() == ModelAPI_StateDone
70                || myFeature->data()->execState() == ModelAPI_StateMustBeUpdated );
71
72   return aValid && isDone;
73 }
74
75 FeaturePtr ModuleBase_OperationFeature::createFeature(const bool theFlushMessage)
76 {
77   if (myParentFeature.get()) {
78     myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
79   } else {
80     std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
81     myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
82   }
83   if (myFeature) {  // TODO: generate an error if feature was not created
84     setIsModified(true);
85     // Model update should call "execute" of a feature.
86     //myFeature->execute();
87     // Init default values
88     /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
89      QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
90      for (; anIt != aLast; anIt++) {
91      (*anIt)->storeValue(aFeature);
92      }*/
93   }
94
95   if (theFlushMessage)
96     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
97   return myFeature;
98 }
99
100 void ModuleBase_OperationFeature::setFeature(FeaturePtr theFeature)
101 {
102   myFeature = theFeature;
103   myIsEditing = true;
104 }
105
106 bool ModuleBase_OperationFeature::hasObject(ObjectPtr theObj) const
107 {
108   FeaturePtr aFeature = feature();
109   if (aFeature) {
110     if (aFeature == theObj)
111       return true;
112     std::list<ResultPtr> aResults = aFeature->results();
113     std::list<ResultPtr>::const_iterator aIt;
114     for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
115       if (theObj == (*aIt))
116         return true;
117     }
118   }
119   return false;
120 }
121
122 void ModuleBase_OperationFeature::start()
123 {
124   setIsModified(false);
125   QString anId = getDescription()->operationId();
126   if (myIsEditing) {
127     anId = anId.append(EditSuffix());
128   }
129   ModelAPI_Session::get()->startOperation(anId.toStdString());
130
131   startOperation();
132
133   if (!myIsEditing) {
134     FeaturePtr aFeature = createFeature();
135     // if the feature is not created, there is no sense to start the operation
136     if (aFeature.get() == NULL) {
137       // it is necessary to abor the operation in the session and emit the aborted signal
138       // in order to update commands status in the workshop, to be exact the feature action
139       // to be unchecked
140       abort();
141       return;
142     }
143   }
144   /// Set current feature and remeber old current feature
145   if (myIsEditing) {
146     SessionPtr aMgr = ModelAPI_Session::get();
147     DocumentPtr aDoc = aMgr->activeDocument();
148     // the parameter of current feature should be false, we should use all feature, not only visible
149     // in order to correctly save the previous feature of the nested operation, where the
150     // features can be not visible in the tree. The problem case is Edit sketch entitity(line)
151     // in the Sketch, created in ExtrusionCut operation. The entity disappears by commit.
152     // When sketch entity operation started, the sketch should be cashed here as the current.
153     // Otherwise(the flag is true), the ExtrusionCut is cashed, when commit happens, the sketch
154     // is disabled, sketch entity is disabled as extrusion cut is created earliest then sketch.
155     // As a result the sketch disappears from the viewer. However after commit it is displayed back.
156     myPreviousCurrentFeature = aDoc->currentFeature(false);
157     aDoc->setCurrentFeature(feature(), false);
158   }
159
160   startOperation();
161   emit started();
162
163 }
164
165 void ModuleBase_OperationFeature::abort()
166 {
167   // the viewer update should be blocked in order to avoid the features blinking before they are
168   // hidden
169   std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
170       new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
171   Events_Loop::loop()->send(aMsg);
172
173   // the widgets of property panel should not process any events come from data mode
174   // after abort clicked. Some signal such as redisplay/create influence on content
175   // of the object browser and viewer context. Therefore it influence to the current
176   // selection and if the active widget listens it, the attribute value is errnoneous
177   // changed.
178   ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
179   if (aPropertyPanel)
180     aPropertyPanel->cleanContent();
181
182   SessionPtr aMgr = ModelAPI_Session::get();
183   if (myIsEditing) {
184     DocumentPtr aDoc = aMgr->activeDocument();
185     bool aIsOp = aMgr->isOperation();
186     if (!aIsOp)
187       aMgr->startOperation();
188     aDoc->setCurrentFeature(myPreviousCurrentFeature, true);
189     if (!aIsOp)
190       aMgr->finishOperation();
191     myPreviousCurrentFeature = FeaturePtr();
192   }
193   abortOperation();
194
195   stopOperation();
196   // is is necessary to deactivate current widgets before the model operation is aborted
197   // because abort removes the feature and activated filters should not check it
198   if (aPropertyPanel) { // feature may be not created (plugin load fail)
199     aPropertyPanel->cleanContent();
200   }
201
202   aMgr->abortOperation();
203   emit stopped();
204   // the viewer update should be unblocked in order to avoid the features blinking before they are
205   // hidden
206   aMsg = std::shared_ptr<Events_Message>(
207                 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
208
209   Events_Loop::loop()->send(aMsg);
210
211   emit aborted();
212 }
213
214 bool ModuleBase_OperationFeature::commit()
215 {
216   if (canBeCommitted()) {
217     // the widgets of property panel should not process any events come from data mode
218     // after commit clicked. Some signal such as redisplay/create influence on content
219     // of the object browser and viewer context. Therefore it influence to the current
220     // selection and if the active widget listens it, the attribute value is errnoneous
221     // changed.
222     ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
223     if (aPropertyPanel)
224       aPropertyPanel->cleanContent();
225
226     SessionPtr aMgr = ModelAPI_Session::get();
227     /// Set current feature and remeber old current feature
228     if (myIsEditing) {
229       DocumentPtr aDoc = aMgr->activeDocument();
230       bool aIsOp = aMgr->isOperation();
231       if (!aIsOp)
232         aMgr->startOperation();
233       aDoc->setCurrentFeature(myPreviousCurrentFeature, true);
234       if (!aIsOp)
235         aMgr->finishOperation();
236       myPreviousCurrentFeature = FeaturePtr();
237     }
238     commitOperation();
239     aMgr->finishOperation();
240
241     stopOperation();
242     emit stopped();
243     emit committed();
244
245     afterCommitOperation();
246     return true;
247   }
248   return false;
249 }
250
251 void ModuleBase_OperationFeature::activateByPreselection()
252 {
253   if (myPreSelection.empty())
254     return;
255
256   ModuleBase_ModelWidget* aFilledWgt = 0;
257   ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
258   if (aPropertyPanel) {
259     const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
260     if (!aWidgets.empty()) {
261       ModuleBase_ModelWidget* aWgt = 0;
262       QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
263       bool isSet = false;
264       // 1. apply the selection to controls
265       for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
266         aWgt = (*aWIt);
267         if (!aWgt->canSetValue())
268           continue;
269         aPropertyPanel->setPreselectionWidget(aWgt);
270         if (!aWgt->setSelection(myPreSelection, true)) {
271           isSet = false;
272           break;
273         } else {
274           isSet = true;
275           aFilledWgt = aWgt;
276         }
277       }
278       aPropertyPanel->setPreselectionWidget(NULL);
279       // in order to redisplay object in the viewer, the update/redisplay signals should be flushed
280       // it is better to perform it not in setSelection of each widget, but do it here,
281       // after the preselection is processed
282       ModuleBase_ModelWidget::updateObject(myFeature);
283
284       // 3. a signal should be emitted before the next widget activation
285       // because, the activation of the next widget will give a focus to the widget. As a result
286       // the value of the widget is initialized. And commit may happens until the value is entered.
287       if (aFilledWgt)
288         emit activatedByPreselection();
289     }
290     // 4. activate the next obligatory widget
291     aPropertyPanel->activateNextWidget(aFilledWgt);
292   }
293
294   clearPreselection();
295 }
296
297 void ModuleBase_OperationFeature::setParentFeature(CompositeFeaturePtr theParent)
298 {
299   myParentFeature = theParent;
300 }
301
302 CompositeFeaturePtr ModuleBase_OperationFeature::parentFeature() const
303 {
304   return myParentFeature;
305 }
306
307 void ModuleBase_OperationFeature::initSelection(ModuleBase_ISelection* theSelection,
308                                          ModuleBase_IViewer* theViewer)
309 {
310   clearPreselection();
311
312   QList<ModuleBase_ViewerPrs> aPreSelected;
313   // Check that the selected result are not results of operation feature
314   FeaturePtr aFeature = feature();
315   if (aFeature) {
316     QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
317
318     std::list<ResultPtr> aResults = aFeature->results();
319     QObjectPtrList aResList;
320     std::list<ResultPtr>::const_iterator aIt;
321     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
322       aResList.append(*aIt);
323
324     foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
325       if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
326         aPreSelected.append(aPrs);
327     }
328   } else
329     aPreSelected = theSelection->getSelected(ModuleBase_ISelection::AllControls);
330
331   myPreSelection = aPreSelected;
332 }
333
334 void ModuleBase_OperationFeature::clearPreselection()
335 {
336   myPreSelection.clear();
337 }
338
339 void ModuleBase_OperationFeature::setPropertyPanel(ModuleBase_IPropertyPanel* theProp) 
340 {
341   ModuleBase_Operation::setPropertyPanel(theProp);
342
343   theProp->setEditingMode(isEditOperation());
344
345   if (theProp) {
346     const QList<ModuleBase_ModelWidget*>& aWidgets = theProp->modelWidgets();
347     QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
348     for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
349       ModuleBase_ModelWidget* aWgt = (*aWIt);
350       connect(aWgt, SIGNAL(valuesChanged()), this, SLOT(onValuesChanged()));
351     }
352   }
353
354   // Do not activate widgets by default if the current operation is editing operation
355   // Because we don't know which widget is going to be edited. 
356   if (!isEditOperation()) {
357     // 4. activate the first obligatory widget
358     theProp->activateNextWidget(NULL);
359   }
360 }