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