Salome HOME
Merge branch 'Dev_0.6' of newgeom:newgeom into Dev_0.6
[modules/shaper.git] / src / ModuleBase / ModuleBase_Operation.cpp
1 /*
2  * ModuleBase_Operation.cpp
3  *
4  *  Created on: Apr 2, 2014
5  *      Author: sbh
6  */
7
8 #include "ModuleBase_Operation.h"
9
10 #include "ModuleBase_OperationDescription.h"
11 #include "ModuleBase_ModelWidget.h"
12 #include "ModuleBase_WidgetValueFeature.h"
13 #include "ModuleBase_ViewerPrs.h"
14 #include "ModuleBase_IPropertyPanel.h"
15 #include "ModuleBase_ISelection.h"
16 #include "ModuleBase_IViewer.h"
17
18 #include <ModelAPI_AttributeDouble.h>
19 #include <ModelAPI_Document.h>
20 #include <ModelAPI_Feature.h>
21 #include <ModelAPI_Data.h>
22 #include <ModelAPI_Document.h>
23 #include <ModelAPI_Events.h>
24 #include <ModelAPI_Result.h>
25 #include <ModelAPI_Object.h>
26 #include <ModelAPI_Validator.h>
27 #include <ModelAPI_Session.h>
28
29 #include <GeomAPI_Pnt2d.h>
30
31 #include <Events_Loop.h>
32
33 #include <QTimer>
34
35 #ifdef _DEBUG
36 #include <QDebug>
37 #endif
38
39 ModuleBase_Operation::ModuleBase_Operation(const QString& theId, QObject* theParent)
40     : QObject(theParent),
41       myIsEditing(false),
42       myIsModified(false),
43       myPropertyPanel(NULL)
44 {
45   myDescription = new ModuleBase_OperationDescription(theId);
46 }
47
48 ModuleBase_Operation::~ModuleBase_Operation()
49 {
50   delete myDescription;
51   clearPreselection();
52 }
53
54 QString ModuleBase_Operation::id() const
55 {
56   return getDescription()->operationId();
57 }
58
59 FeaturePtr ModuleBase_Operation::feature() const
60 {
61   return myFeature;
62 }
63
64 bool ModuleBase_Operation::isValid() const
65 {
66   if (!myFeature)
67     return true; // rename operation
68   //Get validators for the Id
69   SessionPtr aMgr = ModelAPI_Session::get();
70   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
71   return aFactory->validate(myFeature);
72 }
73
74 bool ModuleBase_Operation::isNestedOperationsEnabled() const
75 {
76   return true;
77 }
78
79 //void ModuleBase_Operation::storeCustomValue()
80 //{
81 //  if (!myFeature) {
82 //#ifdef _DEBUG
83 //    qDebug() << "ModuleBase_Operation::storeCustom: " <<
84 //    "trying to store value without opening a transaction.";
85 //#endif
86 //    return;
87 //  }
88 //
89 //  ModuleBase_ModelWidget* aCustom = dynamic_cast<ModuleBase_ModelWidget*>(sender());
90 //  if (aCustom)
91 //    aCustom->storeValue();
92 //}
93
94
95 bool ModuleBase_Operation::canBeCommitted() const
96 {
97   return isValid();
98 }
99
100 void ModuleBase_Operation::flushUpdated()
101 {
102   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
103 }
104
105 void ModuleBase_Operation::flushCreated()
106 {
107   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
108 }
109
110 FeaturePtr ModuleBase_Operation::createFeature(const bool theFlushMessage)
111 {
112   if (myParentFeature) {
113     myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
114   } else {
115     std::shared_ptr<ModelAPI_Document> aDoc = document();
116     myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
117   }
118   if (myFeature) {  // TODO: generate an error if feature was not created
119     myIsModified = true;
120     // Model update should call "execute" of a feature.
121     //myFeature->execute();
122     // Init default values
123     /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
124      QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
125      for (; anIt != aLast; anIt++) {
126      (*anIt)->storeValue(aFeature);
127      }*/
128   }
129
130   if (theFlushMessage)
131     flushCreated();
132   return myFeature;
133 }
134
135 void ModuleBase_Operation::setFeature(FeaturePtr theFeature)
136 {
137   myFeature = theFeature;
138   myIsEditing = true;
139 }
140
141 bool ModuleBase_Operation::hasObject(ObjectPtr theObj) const
142 {
143   FeaturePtr aFeature = feature();
144   if (aFeature) {
145     if (aFeature == theObj)
146       return true;
147     std::list<ResultPtr> aResults = aFeature->results();
148     std::list<ResultPtr>::const_iterator aIt;
149     for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
150       if (theObj == (*aIt))
151         return true;
152     }
153   }
154   return false;
155 }
156
157
158 std::shared_ptr<ModelAPI_Document> ModuleBase_Operation::document() const
159 {
160   return ModelAPI_Session::get()->moduleDocument();
161 }
162
163
164 void ModuleBase_Operation::start()
165 {
166   ModelAPI_Session::get()->startOperation();
167
168   if (!myIsEditing)
169     createFeature();
170
171   startOperation();
172   emit started();
173 }
174
175 void ModuleBase_Operation::postpone()
176 {
177   if (myPropertyPanel)
178     disconnect(myPropertyPanel, 0, this, 0);
179   emit postponed();
180 }
181
182 void ModuleBase_Operation::resume()
183 {
184   if (myPropertyPanel)
185     connect(myPropertyPanel, SIGNAL(noMoreWidgets()), SLOT(commit()));
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 bool ModuleBase_Operation::activateByPreselection()
237 {
238   if (!myPropertyPanel)
239     return false;
240   if (myPreSelection.empty())
241     return false;
242   const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
243   if (aWidgets.empty())
244     return false;
245   
246   ModuleBase_ModelWidget* aWgt, *aFilledWgt = 0;
247   QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
248   QList<ModuleBase_WidgetValueFeature*>::const_iterator aPIt;
249   bool isSet = false;
250   for (aWIt = aWidgets.constBegin(), aPIt = myPreSelection.constBegin();
251        (aWIt != aWidgets.constEnd()) && (aPIt != myPreSelection.constEnd());
252        ++aWIt, ++aPIt) {
253     aWgt = (*aWIt);
254     ModuleBase_WidgetValueFeature* aValue = (*aPIt);
255     if (!aWgt->setValue(aValue)) {
256       isSet = false;
257       break;
258     } else {
259       isSet = true;
260       aFilledWgt = aWgt;
261     }
262   }
263   if (isSet && canBeCommitted()) {
264     // if all widgets are filled with selection - commit
265     // in order to commit the operation outside of starting procedure - use timer event
266     QTimer::singleShot(50, this, SLOT(commit()));
267     return true;
268   }
269   else {
270     //activate next widget
271     if (aFilledWgt) {
272       myPropertyPanel->activateNextWidget(aFilledWgt);
273       return true;
274     }
275   }
276   return false;
277 }
278
279 void ModuleBase_Operation::initSelection(ModuleBase_ISelection* theSelection,
280                                          ModuleBase_IViewer* theViewer)
281 {
282   clearPreselection();
283
284   QList<ModuleBase_ViewerPrs> aPreSelected;
285   // Check that the selected result are not results of operation feature
286   FeaturePtr aFeature = feature();
287   if (aFeature) {
288     QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
289
290     std::list<ResultPtr> aResults = aFeature->results();
291     QObjectPtrList aResList;
292     std::list<ResultPtr>::const_iterator aIt;
293     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
294       aResList.append(*aIt);
295
296     foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
297       if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
298         aPreSelected.append(aPrs);
299     }
300   } else
301     aPreSelected = theSelection->getSelected();
302
303   // convert the selection values to the values, which are set to the operation widgets
304
305   Handle(V3d_View) aView = theViewer->activeView();
306   foreach (ModuleBase_ViewerPrs aPrs, aPreSelected) {
307     ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
308     aValue->setObject(aPrs.object());
309
310     double aX, anY;
311     if (getViewerPoint(aPrs, theViewer, aX, anY))
312       aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
313     myPreSelection.append(aValue);
314   }
315 }
316
317 //void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
318 //{
319 //  //activateByPreselection();
320 //  //if (theWidget && myPropertyPanel) {
321 //  //  myPropertyPanel->activateNextWidget();
322 //  ////  //emit activateNextWidget(myActiveWidget);
323 //  //}
324 //}
325
326 //bool ModuleBase_Operation::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
327 //{
328 //  ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
329 //  if (!aActiveWgt)
330 //    return false;
331 //  ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
332 //  aValue->setObject(theFeature);
333 //  aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
334 //  bool isApplyed = aActiveWgt->setValue(aValue);
335 //
336 //  delete aValue;
337 //  myIsModified = (myIsModified || isApplyed);
338 //  return isApplyed;
339 //}
340
341 bool ModuleBase_Operation::getViewerPoint(ModuleBase_ViewerPrs thePrs,
342                                                ModuleBase_IViewer* theViewer,
343                                                double& theX, double& theY)
344 {
345   return false;
346 }
347
348 void ModuleBase_Operation::clearPreselection()
349 {
350   while (!myPreSelection.isEmpty()) {
351     delete myPreSelection.takeFirst();
352   }
353 }
354
355 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp) 
356
357   myPropertyPanel = theProp; 
358   myPropertyPanel->setEditingMode(isEditOperation());
359   //connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)), this,
360   //        SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
361   if (myPropertyPanel) {
362     connect(myPropertyPanel, SIGNAL(noMoreWidgets()), SLOT(commit()));
363   }
364 }
365
366 bool ModuleBase_Operation::isGranted(QString theId) const
367 {
368   return myNestedFeatures.contains(theId);
369 }