Salome HOME
Issue #244 : do not allow to select null objects (coming from other documents) yet
[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     // check whether there are modifications performed during the current operation
225     // in the model
226     // in case if there are no modifications, do not increase the undo/redo stack
227     if (ModelAPI_Session::get()->isModified())
228       ModelAPI_Session::get()->finishOperation();
229     else
230       ModelAPI_Session::get()->abortOperation();
231
232     emit stopped();
233
234     afterCommitOperation();
235     return true;
236   }
237   return false;
238 }
239
240 void ModuleBase_Operation::setRunning(bool theState)
241 {
242   if (!theState) {
243     abort();
244   }
245 }
246
247 bool ModuleBase_Operation::activateByPreselection()
248 {
249   if (!myPropertyPanel)
250     return false;
251   if (myPreSelection.empty())
252     return false;
253   const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
254   if (aWidgets.empty())
255     return false;
256   
257   ModuleBase_ModelWidget* aWgt, *aFilledWgt = 0;
258   ModuleBase_ViewerPrs aPrs;
259   QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
260   QList<ModuleBase_ViewerPrs>::const_iterator aPIt;
261   bool isSet = false;
262   for (aWIt = aWidgets.constBegin(), aPIt = myPreSelection.constBegin();
263        (aWIt != aWidgets.constEnd()) && (aPIt != myPreSelection.constEnd());
264        ++aWIt, ++aPIt) {
265     aWgt = (*aWIt);
266     aPrs = (*aPIt);
267     ModuleBase_WidgetValueFeature aValue;
268     aValue.setObject(aPrs.object());
269     // Check if the selection has a selected point
270     // for today it is impossible to do because
271     // the selected point demands convertation to Sketch plane 2d
272     if (!aWgt->setValue(&aValue)) {
273       isSet = false;
274       break;
275     } else {
276       isSet = true;
277       aFilledWgt = aWgt;
278     }
279   }
280   if (isSet && canBeCommitted()) {
281     // if all widgets are filled with selection
282     commit();
283     return true;
284   }
285   else {
286     //activate next widget
287     if (aFilledWgt) {
288       myPropertyPanel->activateNextWidget(aFilledWgt);
289       return true;
290     }
291   }
292
293   //ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
294   //if ((myPreSelection.size() > 0) && aActiveWgt) {
295   //  const ModuleBase_ViewerPrs& aPrs = myPreSelection.first();
296   //  ModuleBase_WidgetValueFeature aValue;
297   //  aValue.setObject(aPrs.object());
298   //  if (aActiveWgt->setValue(&aValue)) {
299   //    myPreSelection.removeOne(aPrs);
300   //    myPropertyPanel->activateNextWidget();
301   //  }
302   //  // If preselection is enough to make a valid feature - apply it immediately
303   //}
304   return false;
305 }
306
307 void ModuleBase_Operation::initSelection(ModuleBase_ISelection* theSelection,
308                                          ModuleBase_IViewer* /*theViewer*/)
309 {
310   myPreSelection.clear();
311
312   // Check that the selected result are not results of operation feature
313   QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
314   FeaturePtr aFeature = feature();
315   if (aFeature) {
316     std::list<ResultPtr> aResults = aFeature->results();
317     QList<ObjectPtr> aResList;
318     std::list<ResultPtr>::const_iterator aIt;
319     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
320       aResList.append(*aIt);
321
322     foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
323       if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
324         myPreSelection.append(aPrs);
325     }
326   } else
327     myPreSelection = aSelected;
328 }
329
330 void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
331 {
332   //activateByPreselection();
333   //if (theWidget && myPropertyPanel) {
334   //  myPropertyPanel->activateNextWidget();
335   ////  //emit activateNextWidget(myActiveWidget);
336   //}
337 }
338
339 bool ModuleBase_Operation::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
340 {
341   ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
342   if (!aActiveWgt)
343     return false;
344   ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
345   aValue->setObject(theFeature);
346   aValue->setPoint(boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
347   bool isApplyed = aActiveWgt->setValue(aValue);
348
349   delete aValue;
350   myIsModified = (myIsModified || isApplyed);
351   return isApplyed;
352 }
353
354
355 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp) 
356
357   myPropertyPanel = theProp; 
358   connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)), this,
359           SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
360 }