Salome HOME
A modification to commit the Lenght and the Radius constraints by the preselection.
[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 #include <QTimer>
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   clearPreselection();
52 }
53
54 QString ModuleBase_Operation::id() const
55 {
56   return getDescription()->operationId();
57 }
58
59 FeaturePtr ModuleBase_Operation::feature() const
60 {
61   return myFeature;
62 }
63
64 bool ModuleBase_Operation::isValid() const
65 {
66   if (!myFeature)
67     return true; // rename operation
68   //Get validators for the Id
69   SessionPtr aMgr = ModelAPI_Session::get();
70   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
71   return aFactory->validate(myFeature);
72 }
73
74 bool ModuleBase_Operation::isNestedOperationsEnabled() const
75 {
76   return true;
77 }
78
79 //void ModuleBase_Operation::storeCustomValue()
80 //{
81 //  if (!myFeature) {
82 //#ifdef _DEBUG
83 //    qDebug() << "ModuleBase_Operation::storeCustom: " <<
84 //    "trying to store value without opening a transaction.";
85 //#endif
86 //    return;
87 //  }
88 //
89 //  ModuleBase_ModelWidget* aCustom = dynamic_cast<ModuleBase_ModelWidget*>(sender());
90 //  if (aCustom)
91 //    aCustom->storeValue();
92 //}
93
94
95 bool ModuleBase_Operation::canBeCommitted() const
96 {
97   return isValid();
98 }
99
100 void ModuleBase_Operation::flushUpdated()
101 {
102   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
103 }
104
105 void ModuleBase_Operation::flushCreated()
106 {
107   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
108 }
109
110 FeaturePtr ModuleBase_Operation::createFeature(const bool theFlushMessage)
111 {
112   if (myParentFeature) {
113     myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
114   } else {
115     std::shared_ptr<ModelAPI_Document> aDoc = document();
116     myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
117   }
118   if (myFeature) {  // TODO: generate an error if feature was not created
119     myIsModified = true;
120     // Model update should call "execute" of a feature.
121     //myFeature->execute();
122     // Init default values
123     /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
124      QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
125      for (; anIt != aLast; anIt++) {
126      (*anIt)->storeValue(aFeature);
127      }*/
128   }
129
130   if (theFlushMessage)
131     flushCreated();
132   return myFeature;
133 }
134
135 void ModuleBase_Operation::setFeature(FeaturePtr theFeature)
136 {
137   myFeature = theFeature;
138   myIsEditing = true;
139 }
140
141 bool ModuleBase_Operation::hasObject(ObjectPtr theObj) const
142 {
143   FeaturePtr aFeature = feature();
144   if (aFeature) {
145     if (aFeature == theObj)
146       return true;
147     std::list<ResultPtr> aResults = aFeature->results();
148     std::list<ResultPtr>::const_iterator aIt;
149     for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
150       if (theObj == (*aIt))
151         return true;
152     }
153   }
154   return false;
155 }
156
157
158 std::shared_ptr<ModelAPI_Document> ModuleBase_Operation::document() const
159 {
160   return ModelAPI_Session::get()->moduleDocument();
161 }
162
163
164 void ModuleBase_Operation::start()
165 {
166   ModelAPI_Session::get()->startOperation();
167
168   if (!myIsEditing)
169     createFeature();
170
171   startOperation();
172   emit started();
173 }
174
175 void ModuleBase_Operation::postpone()
176 {
177   if (myPropertyPanel)
178     disconnect(myPropertyPanel, 0, this, 0);
179   emit postponed();
180 }
181
182 void ModuleBase_Operation::resume()
183 {
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) {
251     aWgt = (*aWIt);
252     ModuleBase_WidgetValueFeature* aValue = (*aPIt);
253     if (!aWgt->canSetValue())
254       continue;
255
256     ++aPIt;
257     if (!aWgt->setValue(aValue)) {
258       isSet = false;
259       break;
260     } else {
261       isSet = true;
262       aFilledWgt = aWgt;
263     }
264   }
265   if (isSet && canBeCommitted()) {
266     // if all widgets are filled with selection - commit
267     // in order to commit the operation outside of starting procedure - use timer event
268     QTimer::singleShot(50, this, SLOT(commit()));
269     return true;
270   }
271   else {
272     //activate next widget
273     if (aFilledWgt) {
274       myPropertyPanel->activateNextWidget(aFilledWgt);
275       return true;
276     }
277   }
278   return false;
279 }
280
281 void ModuleBase_Operation::initSelection(ModuleBase_ISelection* theSelection,
282                                          ModuleBase_IViewer* theViewer)
283 {
284   clearPreselection();
285
286   QList<ModuleBase_ViewerPrs> aPreSelected;
287   // Check that the selected result are not results of operation feature
288   FeaturePtr aFeature = feature();
289   if (aFeature) {
290     QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
291
292     std::list<ResultPtr> aResults = aFeature->results();
293     QObjectPtrList aResList;
294     std::list<ResultPtr>::const_iterator aIt;
295     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
296       aResList.append(*aIt);
297
298     foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
299       if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
300         aPreSelected.append(aPrs);
301     }
302   } else
303     aPreSelected = theSelection->getSelected();
304
305   // convert the selection values to the values, which are set to the operation widgets
306
307   Handle(V3d_View) aView = theViewer->activeView();
308   foreach (ModuleBase_ViewerPrs aPrs, aPreSelected) {
309     ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
310     aValue->setObject(aPrs.object());
311
312     double aX, anY;
313     if (getViewerPoint(aPrs, theViewer, aX, anY))
314       aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
315     myPreSelection.append(aValue);
316   }
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   while (!myPreSelection.isEmpty()) {
353     delete myPreSelection.takeFirst();
354   }
355 }
356
357 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp) 
358
359   myPropertyPanel = theProp; 
360   myPropertyPanel->setEditingMode(isEditOperation());
361   //connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)), this,
362   //        SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
363 }
364
365 bool ModuleBase_Operation::isGranted(QString theId) const
366 {
367   return myNestedFeatures.contains(theId);
368 }