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