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