]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_Operation.cpp
Salome HOME
5ee3daa6e6f8d88dc18427c03ed76542a05c4d52
[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, ++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 - commit
263     // in order to commit the operation outside of starting procedure - use timer event
264     QTimer::singleShot(50, this, SLOT(commit()));
265     return true;
266   }
267   else {
268     //activate next widget
269     if (aFilledWgt) {
270       myPropertyPanel->activateNextWidget(aFilledWgt);
271       return true;
272     }
273   }
274   return false;
275 }
276
277 void ModuleBase_Operation::initSelection(ModuleBase_ISelection* theSelection,
278                                          ModuleBase_IViewer* theViewer)
279 {
280   clearPreselection();
281
282   QList<ModuleBase_ViewerPrs> aPreSelected;
283   // Check that the selected result are not results of operation feature
284   FeaturePtr aFeature = feature();
285   if (aFeature) {
286     QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
287
288     std::list<ResultPtr> aResults = aFeature->results();
289     QObjectPtrList aResList;
290     std::list<ResultPtr>::const_iterator aIt;
291     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
292       aResList.append(*aIt);
293
294     foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
295       if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
296         aPreSelected.append(aPrs);
297     }
298   } else
299     aPreSelected = theSelection->getSelected();
300
301   // convert the selection values to the values, which are set to the operation widgets
302
303   Handle(V3d_View) aView = theViewer->activeView();
304   foreach (ModuleBase_ViewerPrs aPrs, aPreSelected) {
305     ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
306     aValue->setObject(aPrs.object());
307
308     double aX, anY;
309     if (getViewerPoint(aPrs, theViewer, aX, anY))
310       aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
311     myPreSelection.append(aValue);
312   }
313 }
314
315 //void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
316 //{
317 //  //activateByPreselection();
318 //  //if (theWidget && myPropertyPanel) {
319 //  //  myPropertyPanel->activateNextWidget();
320 //  ////  //emit activateNextWidget(myActiveWidget);
321 //  //}
322 //}
323
324 //bool ModuleBase_Operation::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
325 //{
326 //  ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
327 //  if (!aActiveWgt)
328 //    return false;
329 //  ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
330 //  aValue->setObject(theFeature);
331 //  aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
332 //  bool isApplyed = aActiveWgt->setValue(aValue);
333 //
334 //  delete aValue;
335 //  myIsModified = (myIsModified || isApplyed);
336 //  return isApplyed;
337 //}
338
339 bool ModuleBase_Operation::getViewerPoint(ModuleBase_ViewerPrs thePrs,
340                                                ModuleBase_IViewer* theViewer,
341                                                double& theX, double& theY)
342 {
343   return false;
344 }
345
346 void ModuleBase_Operation::clearPreselection()
347 {
348   while (!myPreSelection.isEmpty()) {
349     delete myPreSelection.takeFirst();
350   }
351 }
352
353 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp) 
354
355   myPropertyPanel = theProp; 
356   myPropertyPanel->setEditingMode(isEditOperation());
357   //connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)), this,
358   //        SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
359 }
360
361 bool ModuleBase_Operation::isGranted(QString theId) const
362 {
363   return myNestedFeatures.contains(theId);
364 }