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