Salome HOME
Merge branch 'Dev_GroupsRevision'
[modules/shaper.git] / src / ModuleBase / ModuleBase_OperationFeature.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "ModuleBase_OperationFeature.h"
22
23 #include "ModuleBase_OperationDescription.h"
24 #include "ModuleBase_ModelWidget.h"
25 #include "ModuleBase_ViewerPrs.h"
26 #include "ModuleBase_IPropertyPanel.h"
27 #include "ModuleBase_ISelection.h"
28 #include "ModuleBase_IViewer.h"
29 #include "ModuleBase_Tools.h"
30
31 #include <ModelAPI_AttributeDouble.h>
32 #include <ModelAPI_Document.h>
33 #include <ModelAPI_Feature.h>
34 #include <ModelAPI_Data.h>
35 #include <ModelAPI_Document.h>
36 #include <ModelAPI_Events.h>
37 #include <ModelAPI_Result.h>
38 #include <ModelAPI_Object.h>
39 #include <ModelAPI_Validator.h>
40 #include <ModelAPI_Session.h>
41 #include <ModelAPI_Tools.h>
42
43 #include <GeomAPI_Pnt2d.h>
44
45 #include <Events_Loop.h>
46
47 #include <QTimer>
48
49 // the define to check the activated object as a sub-feature by argument of
50 // the operation feature. E.g. rectangle feature(operation), line(in argument) to be not activated
51 #define DEBUG_DO_NOT_ACTIVATE_SUB_FEATURE
52 #ifdef DEBUG_DO_NOT_ACTIVATE_SUB_FEATURE
53 #include <ModelAPI_AttributeRefList.h>
54 #endif
55
56 //#define DEBUG_OPERATION_START
57
58 #ifdef _DEBUG
59 #include <QDebug>
60 #endif
61
62 ModuleBase_OperationFeature::ModuleBase_OperationFeature(const QString& theId, QObject* theParent)
63 : ModuleBase_Operation(theId, theParent),
64   myIsEditing(false)
65 {
66 }
67
68 ModuleBase_OperationFeature::~ModuleBase_OperationFeature()
69 {
70   clearPreselection();
71 }
72
73 void ModuleBase_OperationFeature::setEditOperation(const bool& isEditState
74                                                    /*const bool theRestartTransaction*/)
75 {
76   bool isCurrentEditState = isEditOperation();
77   if (isCurrentEditState == isEditState)
78     return;
79
80   /*
81   // this case is obsolete as it was not approved for reentrant sketch operation
82   // it was implemented when isEditState did not exist and only edit operation can be set
83   if (theRestartTransaction) {
84     // finsh previous create operation
85     emit beforeCommitted();
86     SessionPtr aMgr = ModelAPI_Session::get();
87     ModelAPI_Session::get()->finishOperation();
88
89     // start new edit operation
90     myIsEditing = true;
91     QString anId = getDescription()->operationId();
92     if (myIsEditing) {
93       anId = anId.append(EditSuffix());
94     }
95     ModelAPI_Session::get()->startOperation(anId.toStdString());
96     emit beforeStarted();
97   } else*/
98   myIsEditing = isEditState;
99
100   propertyPanel()->setEditingMode(isEditOperation());
101 }
102
103 FeaturePtr ModuleBase_OperationFeature::feature() const
104 {
105   return myFeature;
106 }
107
108 bool ModuleBase_OperationFeature::isValid() const
109 {
110   if (!myFeature || !myFeature->data()->isValid())
111     return true; // rename operation
112   if (myFeature->isAction())
113     return true;
114
115   std::string anError = ModelAPI_Tools::getFeatureError(myFeature);
116   //ModuleBase_Tools::translate(myFeature->getKind(), anError);
117   return anError.empty();
118 }
119
120 void ModuleBase_OperationFeature::startOperation()
121 {
122   FeaturePtr aFeature = feature();
123   if (!aFeature.get() || !isEditOperation())
124     return;
125
126   if (aFeature.get() && isEditOperation())
127     aFeature->setStable(false);
128
129   myVisualizedObjects.clear();
130   // store hidden result features
131   std::list<ResultPtr> aResults;
132   ModelAPI_Tools::allResults(aFeature, aResults);
133   std::list<ResultPtr>::const_iterator aIt;
134   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
135     ObjectPtr anObject = *aIt;
136     if (anObject.get() && !anObject->isDisplayed()) {
137       myVisualizedObjects.insert(*aIt);
138       anObject->setDisplayed(true);
139     }
140   }
141   if (!aFeature->isDisplayed()) {
142     myVisualizedObjects.insert(aFeature);
143     aFeature->setDisplayed(true);
144   }
145   Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
146 }
147
148 void ModuleBase_OperationFeature::stopOperation()
149 {
150   FeaturePtr aFeature = feature();
151   if (!aFeature.get() || !isEditOperation())
152     return;
153
154   // store hidden result features
155   std::list<ResultPtr> aResults;
156   ModelAPI_Tools::allResults(aFeature, aResults);
157   std::list<ResultPtr>::const_iterator aIt;
158   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
159     ObjectPtr anObject = *aIt;
160     if (anObject.get() && myVisualizedObjects.find(anObject) != myVisualizedObjects.end()) {
161       anObject->setDisplayed(false);
162     }
163   }
164   if (myVisualizedObjects.find(aFeature) != myVisualizedObjects.end()) {
165     aFeature->setDisplayed(false);
166   }
167   if (myVisualizedObjects.size() > 0)
168     Events_Loop::loop()->flush(Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
169 }
170
171 FeaturePtr ModuleBase_OperationFeature::createFeature(const bool theFlushMessage)
172 {
173   if (myParentFeature.get()) {
174     myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
175   } else {
176     std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
177     myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
178   }
179   if (myFeature) {  // TODO: generate an error if feature was not created
180     setIsModified(true);
181     // Model update should call "execute" of a feature.
182     //myFeature->execute();
183     // Init default values
184     /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
185      QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
186      for (; anIt != aLast; anIt++) {
187      (*anIt)->storeValue(aFeature);
188      }*/
189   }
190
191   if (theFlushMessage) {
192     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
193     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
194   }
195   return myFeature;
196 }
197
198 void ModuleBase_OperationFeature::setFeature(FeaturePtr theFeature)
199 {
200   myFeature = theFeature;
201   myIsEditing = true;
202 }
203
204 bool ModuleBase_OperationFeature::hasObject(ObjectPtr theObj) const
205 {
206   FeaturePtr aFeature = feature();
207   if (aFeature) {
208     if (aFeature == theObj)
209       return true;
210     std::list<ResultPtr> aResults;
211     ModelAPI_Tools::allResults(aFeature, aResults);
212     std::list<ResultPtr>::const_iterator aIt;
213     for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
214       ResultPtr aResult = *aIt;
215       if (theObj == aResult)
216          return true;
217     }
218 #ifdef DEBUG_DO_NOT_ACTIVATE_SUB_FEATURE
219     if (aFeature->isMacro()) {
220       // macro feature may refers to sub-features,
221       // which also should be deactivated when the operation
222       // is active, e.g. rectangle'lines.
223       FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObj);
224       std::list<AttributePtr> anAttributes = aFeature->data()->attributes(
225                                               ModelAPI_AttributeRefList::typeId());
226       std::list<AttributePtr>::const_iterator
227         anIt = anAttributes.begin(), aLast = anAttributes.end();
228       bool aFoundObject = false;
229       for (; anIt != aLast && !aFoundObject; anIt++) {
230         std::shared_ptr<ModelAPI_AttributeRefList> aCurSelList =
231                                std::dynamic_pointer_cast<ModelAPI_AttributeRefList>(*anIt);
232         for (int i = 0, aNb = aCurSelList->size(); i < aNb && !aFoundObject; i++) {
233           ObjectPtr anObject = aCurSelList->object(i);
234           FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(anObject);
235           if (aFeature.get()) {
236             aFoundObject = anObjectFeature == aFeature;
237           }
238         }
239       }
240       return aFoundObject;
241     }
242 #endif
243   }
244   return false;
245 }
246
247 bool ModuleBase_OperationFeature::isDisplayedOnStart(ObjectPtr theObject)
248 {
249   return myVisualizedObjects.find(theObject) != myVisualizedObjects.end();
250 }
251
252 bool ModuleBase_OperationFeature::start()
253 {
254 #ifdef DEBUG_OPERATION_START
255   qDebug("ModuleBase_OperationFeature::start -- begin");
256 #endif
257   setIsModified(false);
258   QString anId = getDescription()->operationId();
259   if (myIsEditing) {
260     anId = anId.append(EditSuffix());
261   }
262   ModelAPI_Session::get()->startOperation(anId.toStdString());
263
264   emit beforeStarted();
265   startOperation();
266
267   if (!myIsEditing) {
268     FeaturePtr aFeature = createFeature();
269     // if the feature is not created, there is no sense to start the operation
270     if (aFeature.get() == NULL) {
271       // it is necessary to abor the operation in the session and emit the aborted signal
272       // in order to update commands status in the workshop, to be exact the feature action
273       // to be unchecked
274       abort();
275 #ifdef DEBUG_OPERATION_START
276   qDebug("ModuleBase_OperationFeature::start -- end : IMPOSSIBLE to start");
277 #endif
278       return false;
279     }
280   }
281   //Already called startOperation();
282   emit started();
283 #ifdef DEBUG_OPERATION_START
284   qDebug("ModuleBase_OperationFeature::start -- end");
285 #endif
286   return true;
287 }
288
289 void ModuleBase_OperationFeature::abort()
290 {
291 #ifdef DEBUG_OPERATION_START
292   qDebug("ModuleBase_OperationFeature::abort -- begin");
293 #endif
294
295   emit beforeAborted();
296
297   // the viewer update should be blocked in order to avoid the features blinking before they are
298   // hidden
299   std::shared_ptr<Events_Message> aMsg = std::shared_ptr<Events_Message>(
300       new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_BLOCKED)));
301   Events_Loop::loop()->send(aMsg);
302
303   // the widgets of property panel should not process any events come from data mode
304   // after abort clicked. Some signal such as redisplay/create influence on content
305   // of the object browser and viewer context. Therefore it influence to the current
306   // selection and if the active widget listens it, the attribute value is errnoneous
307   // changed.
308   ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
309   if (aPropertyPanel)
310     aPropertyPanel->cleanContent();
311
312   if (myFeature.get())
313     myFeature->setStable(true);
314
315   abortOperation();
316   stopOperation();
317
318   SessionPtr aMgr = ModelAPI_Session::get();
319   aMgr->abortOperation();
320   emit stopped();
321   // the viewer update should be unblocked in order to avoid the features blinking before they are
322   // hidden
323   aMsg = std::shared_ptr<Events_Message>(
324                 new Events_Message(Events_Loop::eventByName(EVENT_UPDATE_VIEWER_UNBLOCKED)));
325
326   Events_Loop::loop()->send(aMsg);
327
328   emit aborted();
329 #ifdef DEBUG_OPERATION_START
330   qDebug("ModuleBase_OperationFeature::abort -- end");
331 #endif
332 }
333
334 bool ModuleBase_OperationFeature::commit()
335 {
336 #ifdef DEBUG_OPERATION_START
337   qDebug("ModuleBase_OperationFeature::commit -- begin");
338 #endif
339   ModuleBase_IPropertyPanel* aPanel = propertyPanel();
340   if (aPanel) {
341     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
342     if (anActiveWidget && anActiveWidget->getValueState() == ModuleBase_ModelWidget::ModifiedInPP) {
343       anActiveWidget->storeValue();
344     }
345   }
346   if (canBeCommitted()) {
347     emit beforeCommitted();
348     // the widgets of property panel should not process any events come from data mode
349     // after commit clicked. Some signal such as redisplay/create influence on content
350     // of the object browser and viewer context. Therefore it influence to the current
351     // selection and if the active widget listens it, the attribute value is errnoneous
352     // changed.
353     ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
354     if (aPropertyPanel)
355       aPropertyPanel->cleanContent();
356
357     myFeature->setStable(true);
358
359     SessionPtr aMgr = ModelAPI_Session::get();
360     /// Set current feature and remeber old current feature
361
362     commitOperation();
363     aMgr->finishOperation();
364
365     stopOperation();
366     emit stopped();
367     emit committed();
368
369     afterCommitOperation();
370 #ifdef DEBUG_OPERATION_START
371   qDebug("ModuleBase_OperationFeature::commit -- end");
372 #endif
373     return true;
374   }
375 #ifdef DEBUG_OPERATION_START
376   qDebug("ModuleBase_OperationFeature::commit -- end : IMPOSSIBLE to commit");
377 #endif
378   return false;
379 }
380
381 ModuleBase_ModelWidget* ModuleBase_OperationFeature::activateByPreselection(
382                                               const std::string& theGreedAttributeId)
383 {
384   ModuleBase_ModelWidget* aWidget = 0;
385   if (myPreSelection.empty())
386     return aWidget;
387
388   ModuleBase_IPropertyPanel* aPropertyPanel = propertyPanel();
389   ModuleBase_ModelWidget* aFilledWgt = 0;
390   if (aPropertyPanel) {
391     const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
392     QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
393     ModuleBase_ModelWidget* aWgt = 0;
394     if (!aWidgets.empty()) {
395       // equal vertices should not be used here
396       ModuleBase_ISelection::filterSelectionOnEqualPoints(myPreSelection);
397
398       if (!theGreedAttributeId.empty()) {
399         // set preselection to greed widget
400         for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
401           aWgt = (*aWIt);
402           if (aWgt->attributeID() == theGreedAttributeId) {
403             aPropertyPanel->setPreselectionWidget(aWgt);
404             if (aWgt->setSelection(myPreSelection, true)) {
405               aPropertyPanel->setPreselectionWidget(NULL);
406               aFilledWgt = aWgt;
407               break;
408             }
409             else { // do not process invalid for greed widget selection
410               break;
411             }
412           }
413         }
414       }
415       else {
416         bool isSet = false;
417         // 1. apply the selection to controls
418         for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
419           aWgt = (*aWIt);
420           if (!aWgt->canAcceptFocus())
421             continue;
422           aPropertyPanel->setPreselectionWidget(aWgt);
423           if (myPreSelection.empty() || !aWgt->setSelection(myPreSelection, true)) {
424             isSet = false;
425             break;
426           } else {
427             isSet = true;
428             aFilledWgt = aWgt;
429           }
430         }
431       }
432       aPropertyPanel->setPreselectionWidget(NULL);
433       // in order to redisplay object in the viewer, the update/redisplay signals should be flushed
434       // it is better to perform it not in setSelection of each widget, but do it here,
435       // after the preselection is processed
436       ModuleBase_Tools::flushUpdated(myFeature);
437     }
438   }
439   clearPreselection();
440
441   return aFilledWgt;
442 }
443
444 void ModuleBase_OperationFeature::setParentFeature(CompositeFeaturePtr theParent)
445 {
446   myParentFeature = theParent;
447 }
448
449 CompositeFeaturePtr ModuleBase_OperationFeature::parentFeature() const
450 {
451   return myParentFeature;
452 }
453
454 void ModuleBase_OperationFeature::setPreviousCurrentFeature(const FeaturePtr& theFeature)
455 {
456   myPreviousCurrentFeature = theFeature;
457 }
458
459 FeaturePtr ModuleBase_OperationFeature::previousCurrentFeature()
460 {
461   return myPreviousCurrentFeature;
462 }
463
464 void ModuleBase_OperationFeature::initSelection(
465   const QList<ModuleBase_ViewerPrsPtr>& thePreSelected)
466 {
467   QObjectPtrList aCurrentFeatureResults;
468
469   // Check that the selected result are not results of operation feature
470   FeaturePtr aFeature = feature();
471   if (aFeature) {
472     std::list<ResultPtr> aResults;
473     ModelAPI_Tools::allResults(aFeature, aResults);
474     std::list<ResultPtr>::const_iterator aIt;
475     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
476       aCurrentFeatureResults.append(*aIt);
477   }
478
479   if (aCurrentFeatureResults.empty()) /// filtering of selection is not necessary
480     setPreselection(thePreSelected);
481   else { // create preselection list without results of current feature
482     QList<ModuleBase_ViewerPrsPtr> aPreSelected;
483     foreach (ModuleBase_ViewerPrsPtr aPrs, thePreSelected) {
484       if ((!aCurrentFeatureResults.contains(aPrs->object())) && (aPrs->object() != aFeature))
485         aPreSelected.append(aPrs);
486     }
487     setPreselection(aPreSelected);
488   }
489 }
490
491 void ModuleBase_OperationFeature::setPreselection(const QList<ModuleBase_ViewerPrsPtr>& theValues)
492 {
493   clearPreselection();
494   myPreSelection = theValues;
495 }
496
497 void ModuleBase_OperationFeature::clearPreselection()
498 {
499   myPreSelection.clear();
500 }
501
502 void ModuleBase_OperationFeature::setPropertyPanel(ModuleBase_IPropertyPanel* theProp)
503 {
504   ModuleBase_Operation::setPropertyPanel(theProp);
505
506   theProp->setEditingMode(isEditOperation());
507
508   if (theProp) {
509     const QList<ModuleBase_ModelWidget*>& aWidgets = theProp->modelWidgets();
510     QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
511     for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
512       ModuleBase_ModelWidget* aWgt = (*aWIt);
513       connect(aWgt, SIGNAL(valuesChanged()), this, SLOT(onValuesChanged()));
514       connect(aWgt, SIGNAL(valueStateChanged(int)), this, SLOT(onValueStateChanged(int)));
515     }
516   }
517
518   // Do not activate widgets by default if the current operation is editing operation
519   // Because we don't know which widget is going to be edited.
520   if (!isEditOperation()) {
521     // 4. activate the first obligatory widget
522     theProp->activateNextWidget(NULL);
523   }
524   else {
525     // set focus on Ok button in order to operation manager could process Enter press
526     if (theProp)
527       theProp->setFocusOnOkButton();
528   }
529 }