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