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