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