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