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