Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom.git into Dev_1.1.0
[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   QString anId = getDescription()->operationId();
150   ModelAPI_Session::get()->startOperation(anId.toStdString());
151
152   if (!myIsEditing)
153     createFeature();
154
155   startOperation();
156   emit started();
157
158 }
159
160 void ModuleBase_Operation::postpone()
161 {
162   postponeOperation();
163   emit postponed();
164 }
165
166 void ModuleBase_Operation::resume()
167 {
168   resumeOperation();
169   emit resumed();
170 }
171
172 void ModuleBase_Operation::abort()
173 {
174   abortOperation();
175   emit aborted();
176
177   stopOperation();
178
179   ModelAPI_Session::get()->abortOperation();
180   emit stopped();
181 }
182
183 bool ModuleBase_Operation::commit()
184 {
185   if (canBeCommitted()) {
186     commitOperation();
187     // check whether there are modifications performed during the current operation
188     // in the model
189     // in case if there are no modifications, do not increase the undo/redo stack
190     if (ModelAPI_Session::get()->isModified())
191       ModelAPI_Session::get()->finishOperation();
192     else
193       ModelAPI_Session::get()->abortOperation();
194
195     stopOperation();
196     emit stopped();
197     emit committed();
198
199     afterCommitOperation();
200     return true;
201   }
202   return false;
203 }
204
205 void ModuleBase_Operation::setRunning(bool theState)
206 {
207   if (!theState) {
208     abort();
209   }
210 }
211
212 void ModuleBase_Operation::activateByPreselection()
213 {
214   if (!myPropertyPanel || myPreSelection.empty()) {
215     myPropertyPanel->activateNextWidget(NULL);
216     return;
217   }
218   const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
219   if (aWidgets.empty()) {
220     myPropertyPanel->activateNextWidget(NULL);
221     return;
222   }
223   
224   ModuleBase_ModelWidget* aWgt, *aFilledWgt = 0;
225   QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
226   QList<ModuleBase_ViewerPrs>::const_iterator aPIt;
227   bool isSet = false;
228   for (aWIt = aWidgets.constBegin(), aPIt = myPreSelection.constBegin();
229        (aWIt != aWidgets.constEnd()) && (aPIt != myPreSelection.constEnd());
230        ++aWIt) {
231     aWgt = (*aWIt);
232     ModuleBase_ViewerPrs aValue = (*aPIt);
233     if (!aWgt->canSetValue())
234       continue;
235
236     ++aPIt;
237     if (!aWgt->setSelection(aValue)) {
238       isSet = false;
239       break;
240     } else {
241       isSet = true;
242       aFilledWgt = aWgt;
243     }
244   }
245
246   myPropertyPanel->activateNextWidget(aFilledWgt);
247   if (aFilledWgt)
248     emit activatedByPreselection();
249
250 }
251
252 void ModuleBase_Operation::setParentFeature(CompositeFeaturePtr theParent)
253 {
254   myParentFeature = theParent;
255 }
256
257 CompositeFeaturePtr ModuleBase_Operation::parentFeature() const
258 {
259   return myParentFeature;
260 }
261
262 void ModuleBase_Operation::initSelection(ModuleBase_ISelection* theSelection,
263                                          ModuleBase_IViewer* theViewer)
264 {
265   clearPreselection();
266
267   QList<ModuleBase_ViewerPrs> aPreSelected;
268   // Check that the selected result are not results of operation feature
269   FeaturePtr aFeature = feature();
270   if (aFeature) {
271     QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
272
273     std::list<ResultPtr> aResults = aFeature->results();
274     QObjectPtrList aResList;
275     std::list<ResultPtr>::const_iterator aIt;
276     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
277       aResList.append(*aIt);
278
279     foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
280       if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
281         aPreSelected.append(aPrs);
282     }
283   } else
284     aPreSelected = theSelection->getSelected();
285
286   // convert the selection values to the values, which are set to the operation widgets
287
288   //Handle(V3d_View) aView = theViewer->activeView();
289   //foreach (ModuleBase_ViewerPrs aPrs, aPreSelected) {
290   //  ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
291   //  aValue->setObject(aPrs.object());
292
293   //  double aX, anY;
294   //  if (getViewerPoint(aPrs, theViewer, aX, anY))
295   //    aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
296   //  myPreSelection.append(aValue);
297   //}
298   myPreSelection = aPreSelected;
299 }
300
301 //void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
302 //{
303 //  //activateByPreselection();
304 //  //if (theWidget && myPropertyPanel) {
305 //  //  myPropertyPanel->activateNextWidget();
306 //  ////  //emit activateNextWidget(myActiveWidget);
307 //  //}
308 //}
309
310 //bool ModuleBase_Operation::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
311 //{
312 //  ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
313 //  if (!aActiveWgt)
314 //    return false;
315 //  ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
316 //  aValue->setObject(theFeature);
317 //  aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
318 //  bool isApplyed = aActiveWgt->setValue(aValue);
319 //
320 //  delete aValue;
321 //  myIsModified = (myIsModified || isApplyed);
322 //  return isApplyed;
323 //}
324
325 bool ModuleBase_Operation::getViewerPoint(ModuleBase_ViewerPrs thePrs,
326                                                ModuleBase_IViewer* theViewer,
327                                                double& theX, double& theY)
328 {
329   return false;
330 }
331
332 void ModuleBase_Operation::clearPreselection()
333 {
334   myPreSelection.clear();
335 }
336
337 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp) 
338
339   myPropertyPanel = theProp; 
340   myPropertyPanel->setEditingMode(isEditOperation());
341
342   // Do not activate widgets by default if the current operation is editing operation
343   // Because we don't know which widget is going to be edited. 
344   if (!isEditOperation())
345     activateByPreselection();
346 }
347
348 bool ModuleBase_Operation::isGranted(QString theId) const
349 {
350   return myNestedFeatures.contains(theId);
351 }