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