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