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