]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_Operation.cpp
Salome HOME
Do not start create operation if the feature is not created.
[modules/shaper.git] / src / ModuleBase / ModuleBase_Operation.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 /*
4  * ModuleBase_Operation.cpp
5  *
6  *  Created on: Apr 2, 2014
7  *      Author: sbh
8  */
9
10 #include "ModuleBase_Operation.h"
11
12 #include "ModuleBase_OperationDescription.h"
13 #include "ModuleBase_ModelWidget.h"
14 #include "ModuleBase_ViewerPrs.h"
15 #include "ModuleBase_IPropertyPanel.h"
16 #include "ModuleBase_ISelection.h"
17 #include "ModuleBase_IViewer.h"
18
19 #include <ModelAPI_AttributeDouble.h>
20 #include <ModelAPI_Document.h>
21 #include <ModelAPI_Feature.h>
22 #include <ModelAPI_Data.h>
23 #include <ModelAPI_Document.h>
24 #include <ModelAPI_Events.h>
25 #include <ModelAPI_Result.h>
26 #include <ModelAPI_Object.h>
27 #include <ModelAPI_Validator.h>
28 #include <ModelAPI_Session.h>
29
30 #include <GeomAPI_Pnt2d.h>
31
32 #include <Events_Loop.h>
33
34 #include <QTimer>
35
36 #ifdef _DEBUG
37 #include <QDebug>
38 #endif
39
40 ModuleBase_Operation::ModuleBase_Operation(const QString& theId, QObject* theParent)
41     : QObject(theParent),
42       myIsEditing(false),
43       myIsModified(false),
44       myPropertyPanel(NULL)
45 {
46   myDescription = new ModuleBase_OperationDescription(theId);
47 }
48
49 ModuleBase_Operation::~ModuleBase_Operation()
50 {
51   delete myDescription;
52   clearPreselection();
53 }
54
55 QString ModuleBase_Operation::id() const
56 {
57   return getDescription()->operationId();
58 }
59
60 FeaturePtr ModuleBase_Operation::feature() const
61 {
62   return myFeature;
63 }
64
65 bool ModuleBase_Operation::isValid() const
66 {
67   if (!myFeature)
68     return true; // rename operation
69   if (myFeature->isAction())
70     return true;
71   //Get validators for the Id
72   SessionPtr aMgr = ModelAPI_Session::get();
73   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
74   return aFactory->validate(myFeature);
75 }
76
77
78 bool ModuleBase_Operation::canBeCommitted() const
79 {
80   return isValid();
81 }
82
83 void ModuleBase_Operation::flushUpdated()
84 {
85   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
86 }
87
88 void ModuleBase_Operation::flushCreated()
89 {
90   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
91 }
92
93 FeaturePtr ModuleBase_Operation::createFeature(const bool theFlushMessage)
94 {
95   if (myParentFeature.get()) {
96     myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
97   } else {
98     std::shared_ptr<ModelAPI_Document> aDoc = document();
99     myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
100   }
101   if (myFeature) {  // TODO: generate an error if feature was not created
102     myIsModified = true;
103     // Model update should call "execute" of a feature.
104     //myFeature->execute();
105     // Init default values
106     /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
107      QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
108      for (; anIt != aLast; anIt++) {
109      (*anIt)->storeValue(aFeature);
110      }*/
111   }
112
113   if (theFlushMessage)
114     flushCreated();
115   return myFeature;
116 }
117
118 void ModuleBase_Operation::setFeature(FeaturePtr theFeature)
119 {
120   myFeature = theFeature;
121   myIsEditing = true;
122 }
123
124 bool ModuleBase_Operation::hasObject(ObjectPtr theObj) const
125 {
126   FeaturePtr aFeature = feature();
127   if (aFeature) {
128     if (aFeature == theObj)
129       return true;
130     std::list<ResultPtr> aResults = aFeature->results();
131     std::list<ResultPtr>::const_iterator aIt;
132     for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
133       if (theObj == (*aIt))
134         return true;
135     }
136   }
137   return false;
138 }
139
140
141 std::shared_ptr<ModelAPI_Document> ModuleBase_Operation::document() const
142 {
143   return ModelAPI_Session::get()->moduleDocument();
144 }
145
146
147 void ModuleBase_Operation::start()
148 {
149   ModelAPI_Session::get()->startOperation();
150
151   if (!myIsEditing) {
152     FeaturePtr aFeature = createFeature();
153     // if the feature is not created, there is no sense to start the operation
154     if (aFeature.get() == NULL) {
155       // it is necessary to abor the operation in the session and emit the aborted signal
156       // in order to update commands status in the workshop, to be exact the feature action
157       // to be unchecked
158       abort();
159       return;
160     }
161   }
162
163   startOperation();
164   emit started();
165
166 }
167
168 void ModuleBase_Operation::postpone()
169 {
170   postponeOperation();
171   emit postponed();
172 }
173
174 void ModuleBase_Operation::resume()
175 {
176   resumeOperation();
177   emit resumed();
178 }
179
180 void ModuleBase_Operation::abort()
181 {
182   abortOperation();
183   emit aborted();
184
185   stopOperation();
186
187   ModelAPI_Session::get()->abortOperation();
188   emit stopped();
189 }
190
191 bool ModuleBase_Operation::commit()
192 {
193   if (canBeCommitted()) {
194     commitOperation();
195     // check whether there are modifications performed during the current operation
196     // in the model
197     // in case if there are no modifications, do not increase the undo/redo stack
198     if (ModelAPI_Session::get()->isModified())
199       ModelAPI_Session::get()->finishOperation();
200     else
201       ModelAPI_Session::get()->abortOperation();
202
203     stopOperation();
204     emit stopped();
205     emit committed();
206
207     afterCommitOperation();
208     return true;
209   }
210   return false;
211 }
212
213 void ModuleBase_Operation::setRunning(bool theState)
214 {
215   if (!theState) {
216     abort();
217   }
218 }
219
220 void ModuleBase_Operation::activateByPreselection()
221 {
222   if (!myPropertyPanel || myPreSelection.empty()) {
223     myPropertyPanel->activateNextWidget(NULL);
224     return;
225   }
226   const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
227   if (aWidgets.empty()) {
228     myPropertyPanel->activateNextWidget(NULL);
229     return;
230   }
231   
232   ModuleBase_ModelWidget* aWgt, *aFilledWgt = 0;
233   QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
234   QList<ModuleBase_ViewerPrs>::const_iterator aPIt;
235   bool isSet = false;
236   for (aWIt = aWidgets.constBegin(), aPIt = myPreSelection.constBegin();
237        (aWIt != aWidgets.constEnd()) && (aPIt != myPreSelection.constEnd());
238        ++aWIt) {
239     aWgt = (*aWIt);
240     ModuleBase_ViewerPrs aValue = (*aPIt);
241     if (!aWgt->canSetValue())
242       continue;
243
244     ++aPIt;
245     if (!aWgt->setSelection(aValue)) {
246       isSet = false;
247       break;
248     } else {
249       isSet = true;
250       aFilledWgt = aWgt;
251     }
252   }
253
254   myPropertyPanel->activateNextWidget(aFilledWgt);
255   if (aFilledWgt)
256     emit activatedByPreselection();
257
258 }
259
260 void ModuleBase_Operation::setParentFeature(CompositeFeaturePtr theParent)
261 {
262   myParentFeature = theParent;
263 }
264
265 CompositeFeaturePtr ModuleBase_Operation::parentFeature() const
266 {
267   return myParentFeature;
268 }
269
270 void ModuleBase_Operation::initSelection(ModuleBase_ISelection* theSelection,
271                                          ModuleBase_IViewer* theViewer)
272 {
273   clearPreselection();
274
275   QList<ModuleBase_ViewerPrs> aPreSelected;
276   // Check that the selected result are not results of operation feature
277   FeaturePtr aFeature = feature();
278   if (aFeature) {
279     QList<ModuleBase_ViewerPrs> aSelected = theSelection->getSelected();
280
281     std::list<ResultPtr> aResults = aFeature->results();
282     QObjectPtrList aResList;
283     std::list<ResultPtr>::const_iterator aIt;
284     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
285       aResList.append(*aIt);
286
287     foreach (ModuleBase_ViewerPrs aPrs, aSelected) {
288       if ((!aResList.contains(aPrs.object())) && (aPrs.object() != aFeature))
289         aPreSelected.append(aPrs);
290     }
291   } else
292     aPreSelected = theSelection->getSelected();
293
294   // convert the selection values to the values, which are set to the operation widgets
295
296   //Handle(V3d_View) aView = theViewer->activeView();
297   //foreach (ModuleBase_ViewerPrs aPrs, aPreSelected) {
298   //  ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
299   //  aValue->setObject(aPrs.object());
300
301   //  double aX, anY;
302   //  if (getViewerPoint(aPrs, theViewer, aX, anY))
303   //    aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(aX, anY)));
304   //  myPreSelection.append(aValue);
305   //}
306   myPreSelection = aPreSelected;
307 }
308
309 //void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
310 //{
311 //  //activateByPreselection();
312 //  //if (theWidget && myPropertyPanel) {
313 //  //  myPropertyPanel->activateNextWidget();
314 //  ////  //emit activateNextWidget(myActiveWidget);
315 //  //}
316 //}
317
318 //bool ModuleBase_Operation::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
319 //{
320 //  ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
321 //  if (!aActiveWgt)
322 //    return false;
323 //  ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
324 //  aValue->setObject(theFeature);
325 //  aValue->setPoint(std::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
326 //  bool isApplyed = aActiveWgt->setValue(aValue);
327 //
328 //  delete aValue;
329 //  myIsModified = (myIsModified || isApplyed);
330 //  return isApplyed;
331 //}
332
333 bool ModuleBase_Operation::getViewerPoint(ModuleBase_ViewerPrs thePrs,
334                                                ModuleBase_IViewer* theViewer,
335                                                double& theX, double& theY)
336 {
337   return false;
338 }
339
340 void ModuleBase_Operation::clearPreselection()
341 {
342   myPreSelection.clear();
343 }
344
345 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp) 
346
347   myPropertyPanel = theProp; 
348   myPropertyPanel->setEditingMode(isEditOperation());
349
350   // Do not activate widgets by default if the current operation is editing operation
351   // Because we don't know which widget is going to be edited. 
352   if (!isEditOperation())
353     activateByPreselection();
354 }
355
356 bool ModuleBase_Operation::isGranted(QString theId) const
357 {
358   return myNestedFeatures.contains(theId);
359 }