Salome HOME
Constriction type for all sketch entities
[modules/shaper.git] / src / ModuleBase / ModuleBase_Operation.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 /*
4  * ModuleBase_Operation.cpp
5  *
6  *  Created on: Apr 2, 2014
7  *      Author: sbh
8  */
9
10 #include "ModuleBase_Operation.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_Operation::ModuleBase_Operation(const QString& theId, QObject* theParent)
41     : QObject(theParent),
42       myIsEditing(false),
43       myIsModified(false),
44       myPropertyPanel(NULL)
45 {
46   myDescription = new ModuleBase_OperationDescription(theId);
47 }
48
49 ModuleBase_Operation::~ModuleBase_Operation()
50 {
51   delete myDescription;
52   clearPreselection();
53 }
54
55 QString ModuleBase_Operation::id() const
56 {
57   return getDescription()->operationId();
58 }
59
60 FeaturePtr ModuleBase_Operation::feature() const
61 {
62   return myFeature;
63 }
64
65 bool ModuleBase_Operation::isValid() const
66 {
67   if (!myFeature)
68     return true; // rename operation
69   if (myFeature->isAction())
70     return true;
71   //Get validators for the Id
72   SessionPtr aMgr = ModelAPI_Session::get();
73   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
74   return aFactory->validate(myFeature);
75 }
76
77
78 bool ModuleBase_Operation::canBeCommitted() const
79 {
80   return isValid();
81 }
82
83 FeaturePtr ModuleBase_Operation::createFeature(const bool theFlushMessage)
84 {
85   if (myParentFeature.get()) {
86     myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
87   } else {
88     std::shared_ptr<ModelAPI_Document> aDoc = document();
89     myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
90   }
91   if (myFeature) {  // TODO: generate an error if feature was not created
92     myIsModified = true;
93     // Model update should call "execute" of a feature.
94     //myFeature->execute();
95     // Init default values
96     /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
97      QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
98      for (; anIt != aLast; anIt++) {
99      (*anIt)->storeValue(aFeature);
100      }*/
101   }
102
103   if (theFlushMessage)
104     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
105   return myFeature;
106 }
107
108 void ModuleBase_Operation::setFeature(FeaturePtr theFeature)
109 {
110   myFeature = theFeature;
111   myIsEditing = true;
112 }
113
114 bool ModuleBase_Operation::hasObject(ObjectPtr theObj) const
115 {
116   FeaturePtr aFeature = feature();
117   if (aFeature) {
118     if (aFeature == theObj)
119       return true;
120     std::list<ResultPtr> aResults = aFeature->results();
121     std::list<ResultPtr>::const_iterator aIt;
122     for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
123       if (theObj == (*aIt))
124         return true;
125     }
126   }
127   return false;
128 }
129
130
131 std::shared_ptr<ModelAPI_Document> ModuleBase_Operation::document() const
132 {
133   return ModelAPI_Session::get()->moduleDocument();
134 }
135
136
137 void ModuleBase_Operation::start()
138 {
139   QString anId = getDescription()->operationId();
140   if (myIsEditing) {
141       anId = anId.append(EditSuffix());
142   }
143   ModelAPI_Session::get()->startOperation(anId.toStdString());
144
145   if (!myIsEditing)
146     createFeature();
147
148   startOperation();
149   emit started();
150
151 }
152
153 void ModuleBase_Operation::postpone()
154 {
155   postponeOperation();
156   emit postponed();
157 }
158
159 void ModuleBase_Operation::resume()
160 {
161   resumeOperation();
162   emit resumed();
163 }
164
165 void ModuleBase_Operation::abort()
166 {
167   abortOperation();
168   emit aborted();
169
170   stopOperation();
171
172   ModelAPI_Session::get()->abortOperation();
173   emit stopped();
174 }
175
176 bool ModuleBase_Operation::commit()
177 {
178   if (canBeCommitted()) {
179     commitOperation();
180     // check whether there are modifications performed during the current operation
181     // in the model
182     // in case if there are no modifications, do not increase the undo/redo stack
183     if (ModelAPI_Session::get()->isModified())
184       ModelAPI_Session::get()->finishOperation();
185     else
186       ModelAPI_Session::get()->abortOperation();
187
188     stopOperation();
189     emit stopped();
190     emit committed();
191
192     afterCommitOperation();
193     return true;
194   }
195   return false;
196 }
197
198 void ModuleBase_Operation::setRunning(bool theState)
199 {
200   if (!theState) {
201     abort();
202   }
203 }
204
205 void ModuleBase_Operation::activateByPreselection()
206 {
207   if (!myPropertyPanel || myPreSelection.empty()) {
208     myPropertyPanel->activateNextWidget(NULL);
209     return;
210   }
211   const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
212   if (aWidgets.empty()) {
213     myPropertyPanel->activateNextWidget(NULL);
214     return;
215   }
216   
217   ModuleBase_ModelWidget* aWgt, *aFilledWgt = 0;
218   QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
219   QList<ModuleBase_ViewerPrs>::const_iterator aPIt;
220   bool isSet = false;
221   for (aWIt = aWidgets.constBegin(), aPIt = myPreSelection.constBegin();
222        (aWIt != aWidgets.constEnd()) && (aPIt != myPreSelection.constEnd());
223        ++aWIt) {
224     aWgt = (*aWIt);
225     ModuleBase_ViewerPrs aValue = (*aPIt);
226     if (!aWgt->canSetValue())
227       continue;
228
229     ++aPIt;
230     if (!aWgt->setSelection(aValue)) {
231       isSet = false;
232       break;
233     } else {
234       isSet = true;
235       aFilledWgt = aWgt;
236     }
237   }
238
239   myPropertyPanel->activateNextWidget(aFilledWgt);
240   if (aFilledWgt)
241     emit activatedByPreselection();
242
243 }
244
245 void ModuleBase_Operation::setParentFeature(CompositeFeaturePtr theParent)
246 {
247   myParentFeature = theParent;
248 }
249
250 CompositeFeaturePtr ModuleBase_Operation::parentFeature() const
251 {
252   return myParentFeature;
253 }
254
255 void ModuleBase_Operation::initSelection(ModuleBase_ISelection* theSelection,
256                                          ModuleBase_IViewer* theViewer)
257 {
258   clearPreselection();
259
260   QList<ModuleBase_ViewerPrs> aPreSelected;
261   // Check that the selected result are not results of operation feature
262   FeaturePtr aFeature = feature();
263   if (aFeature) {
264     QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
265
266     std::list<ResultPtr> aResults = aFeature->results();
267     QObjectPtrList aResList;
268     std::list<ResultPtr>::const_iterator aIt;
269     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
270       aResList.append(*aIt);
271
272     foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
273       if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
274         aPreSelected.append(aPrs);
275     }
276   } else
277     aPreSelected = theSelection->getSelected();
278
279   // convert the selection values to the values, which are set to the operation widgets
280
281   //Handle(V3d_View) aView = theViewer->activeView();
282   //foreach (ModuleBase_ViewerPrs aPrs, aPreSelected) {
283   //  ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
284   //  aValue->setObject(aPrs.object());
285
286   //  double aX, anY;
287   //  if (getViewerPoint(aPrs, theViewer, aX, anY))
288   //    aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
289   //  myPreSelection.append(aValue);
290   //}
291   myPreSelection = aPreSelected;
292 }
293
294 //void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
295 //{
296 //  //activateByPreselection();
297 //  //if (theWidget && myPropertyPanel) {
298 //  //  myPropertyPanel->activateNextWidget();
299 //  ////  //emit activateNextWidget(myActiveWidget);
300 //  //}
301 //}
302
303 //bool ModuleBase_Operation::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
304 //{
305 //  ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
306 //  if (!aActiveWgt)
307 //    return false;
308 //  ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
309 //  aValue->setObject(theFeature);
310 //  aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
311 //  bool isApplyed = aActiveWgt->setValue(aValue);
312 //
313 //  delete aValue;
314 //  myIsModified = (myIsModified || isApplyed);
315 //  return isApplyed;
316 //}
317
318 bool ModuleBase_Operation::getViewerPoint(ModuleBase_ViewerPrs thePrs,
319                                                ModuleBase_IViewer* theViewer,
320                                                double& theX, double& theY)
321 {
322   return false;
323 }
324
325 void ModuleBase_Operation::clearPreselection()
326 {
327   myPreSelection.clear();
328 }
329
330 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp) 
331
332   myPropertyPanel = theProp; 
333   myPropertyPanel->setEditingMode(isEditOperation());
334
335   // Do not activate widgets by default if the current operation is editing operation
336   // Because we don't know which widget is going to be edited. 
337   if (!isEditOperation())
338     activateByPreselection();
339 }
340
341 bool ModuleBase_Operation::isGranted(QString theId) const
342 {
343   return myNestedFeatures.contains(theId);
344 }