Salome HOME
#1112 tab key doesn't work on the last field of left panels
[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
90   std::string anError = ModelAPI_Tools::getFeatureError(myFeature);
91   return anError.empty();
92 }
93
94 void ModuleBase_OperationFeature::startOperation()
95 {
96   FeaturePtr aFeature = feature();
97   if (!aFeature.get() || !isEditOperation())
98     return;
99
100   if (aFeature.get() && isEditOperation())
101     aFeature->setStable(false);
102
103   myVisualizedObjects.clear();
104   // store hidden result features
105   std::list<ResultPtr> aResults = aFeature->results();
106   std::list<ResultPtr>::const_iterator aIt;
107   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
108     ObjectPtr anObject = *aIt;
109     if (anObject.get() && !anObject->isDisplayed()) {
110       myVisualizedObjects.insert(*aIt);
111       anObject->setDisplayed(true);
112     }
113   }
114   if (!aFeature->isDisplayed()) {
115     myVisualizedObjects.insert(aFeature);
116     aFeature->setDisplayed(true);
117   }
118   Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
119 }
120
121 void ModuleBase_OperationFeature::stopOperation()
122 {
123   FeaturePtr aFeature = feature();
124   if (!aFeature.get() || !isEditOperation())
125     return;
126
127   // store hidden result features
128   std::list<ResultPtr> aResults = aFeature->results();
129   std::list<ResultPtr>::const_iterator aIt;
130   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
131     ObjectPtr anObject = *aIt;
132     if (anObject.get() && myVisualizedObjects.find(anObject) != myVisualizedObjects.end()) {
133       anObject->setDisplayed(false);
134     }
135   }
136   if (myVisualizedObjects.find(aFeature) != myVisualizedObjects.end()) {
137     aFeature->setDisplayed(false);
138   }
139   if (myVisualizedObjects.size() > 0)
140     Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
141 }
142
143 FeaturePtr ModuleBase_OperationFeature::createFeature(const bool theFlushMessage)
144 {
145   if (myParentFeature.get()) {
146     myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
147   } else {
148     std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
149     myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
150   }
151   if (myFeature) {  // TODO: generate an error if feature was not created
152     setIsModified(true);
153     // Model update should call "execute" of a feature.
154     //myFeature->execute();
155     // Init default values
156     /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
157      QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
158      for (; anIt != aLast; anIt++) {
159      (*anIt)->storeValue(aFeature);
160      }*/
161   }
162
163   if (theFlushMessage)
164     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
165   return myFeature;
166 }
167
168 void ModuleBase_OperationFeature::setFeature(FeaturePtr theFeature)
169 {
170   myFeature = theFeature;
171   myIsEditing = true;
172 }
173
174 bool ModuleBase_OperationFeature::hasObject(ObjectPtr theObj) const
175 {
176   FeaturePtr aFeature = feature();
177   if (aFeature) {
178     if (aFeature == theObj)
179       return true;
180     std::list<ResultPtr> aResults = aFeature->results();
181     std::list<ResultPtr>::const_iterator aIt;
182     for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
183       if (theObj == (*aIt))
184         return true;
185     }
186   }
187   return false;
188 }
189
190 bool ModuleBase_OperationFeature::isDisplayedOnStart(ObjectPtr theObject)
191 {
192   return myVisualizedObjects.find(theObject) != myVisualizedObjects.end();
193 }
194
195 bool ModuleBase_OperationFeature::start()
196 {
197   setIsModified(false);
198   QString anId = getDescription()->operationId();
199   if (myIsEditing) {
200     anId = anId.append(EditSuffix());
201   }
202   ModelAPI_Session::get()->startOperation(anId.toStdString());
203
204   emit beforeStarted();
205   startOperation();
206
207   if (!myIsEditing) {
208     FeaturePtr aFeature = createFeature();
209     // if the feature is not created, there is no sense to start the operation
210     if (aFeature.get() == NULL) {
211       // it is necessary to abor the operation in the session and emit the aborted signal
212       // in order to update commands status in the workshop, to be exact the feature action
213       // to be unchecked
214       abort();
215       return false;
216     }
217   }
218   //Already called startOperation();
219   emit started();
220   return true;
221 }
222
223 void ModuleBase_OperationFeature::abort()
224 {
225   emit beforeAborted();
226
227   // the viewer update should be blocked in order to avoid the features blinking before they are
228   // hidden
229   std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
230       new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
231   Events_Loop::loop()->send(aMsg);
232
233   // the widgets of property panel should not process any events come from data mode
234   // after abort clicked. Some signal such as redisplay/create influence on content
235   // of the object browser and viewer context. Therefore it influence to the current
236   // selection and if the active widget listens it, the attribute value is errnoneous
237   // changed.
238   ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
239   if (aPropertyPanel)
240     aPropertyPanel->cleanContent();
241
242   if (myFeature.get())
243     myFeature->setStable(true);
244
245   abortOperation();
246   stopOperation();
247
248   SessionPtr aMgr = ModelAPI_Session::get();
249   aMgr->abortOperation();
250   emit stopped();
251   // the viewer update should be unblocked in order to avoid the features blinking before they are
252   // hidden
253   aMsg = std::shared_ptr<Events_Message>(
254                 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
255
256   Events_Loop::loop()->send(aMsg);
257
258   emit aborted();
259 }
260
261 bool ModuleBase_OperationFeature::commit()
262 {
263   ModuleBase_IPropertyPanel* aPanel = propertyPanel();
264   if (aPanel) {
265     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
266     if (anActiveWidget && anActiveWidget->getValueState() == ModuleBase_ModelWidget::ModifiedInPP) {
267       anActiveWidget->storeValue();
268     }
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 }