]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_Operation.cpp
Salome HOME
Restore show trihedron on viewer creation
[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 FeaturePtr ModuleBase_Operation::createFeature(const bool theFlushMessage)
84 {
85   if (myParentFeature.get()) {
86     myFeature = myParentFeature->addFeature(getDescription()->operationId().toStdString());
87   } else {
88     std::shared_ptr<ModelAPI_Document> aDoc = ModelAPI_Session::get()->activeDocument();
89     myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
90   }
91   if (myFeature) {  // TODO: generate an error if feature was not created
92     myIsModified = true;
93     // Model update should call "execute" of a feature.
94     //myFeature->execute();
95     // Init default values
96     /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
97      QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
98      for (; anIt != aLast; anIt++) {
99      (*anIt)->storeValue(aFeature);
100      }*/
101   }
102
103   if (theFlushMessage)
104     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
105   return myFeature;
106 }
107
108 void ModuleBase_Operation::setFeature(FeaturePtr theFeature)
109 {
110   myFeature = theFeature;
111   myIsEditing = true;
112 }
113
114 bool ModuleBase_Operation::hasObject(ObjectPtr theObj) const
115 {
116   FeaturePtr aFeature = feature();
117   if (aFeature) {
118     if (aFeature == theObj)
119       return true;
120     std::list<ResultPtr> aResults = aFeature->results();
121     std::list<ResultPtr>::const_iterator aIt;
122     for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
123       if (theObj == (*aIt))
124         return true;
125     }
126   }
127   return false;
128 }
129
130 void ModuleBase_Operation::start()
131 {
132   QString anId = getDescription()->operationId();
133   if (myIsEditing) {
134     anId = anId.append(EditSuffix());
135   }
136   ModelAPI_Session::get()->startOperation(anId.toStdString());
137
138   if (!myIsEditing) {
139     FeaturePtr aFeature = createFeature();
140     // if the feature is not created, there is no sense to start the operation
141     if (aFeature.get() == NULL) {
142       // it is necessary to abor the operation in the session and emit the aborted signal
143       // in order to update commands status in the workshop, to be exact the feature action
144       // to be unchecked
145       abort();
146       return;
147     }
148   }
149   /// Set current feature and remeber old current feature
150   if (myIsEditing) {
151     SessionPtr aMgr = ModelAPI_Session::get();
152     DocumentPtr aDoc = aMgr->activeDocument();
153     myCurrentFeature = aDoc->currentFeature(true);
154     aDoc->setCurrentFeature(feature(), false);
155   }
156
157   startOperation();
158   emit started();
159
160 }
161
162 void ModuleBase_Operation::postpone()
163 {
164   postponeOperation();
165   emit postponed();
166 }
167
168 void ModuleBase_Operation::resume()
169 {
170   resumeOperation();
171   emit resumed();
172 }
173
174 void ModuleBase_Operation::abort()
175 {
176   if (myIsEditing) {
177     SessionPtr aMgr = ModelAPI_Session::get();
178     DocumentPtr aDoc = aMgr->activeDocument();
179     aDoc->setCurrentFeature(myCurrentFeature, true);
180     myCurrentFeature = FeaturePtr();
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     /// Set current feature and remeber old current feature
195     if (myIsEditing) {
196       SessionPtr aMgr = ModelAPI_Session::get();
197       DocumentPtr aDoc = aMgr->activeDocument();
198       aDoc->setCurrentFeature(myCurrentFeature, true);
199       myCurrentFeature = FeaturePtr();
200     }
201     commitOperation();
202     // check whether there are modifications performed during the current operation
203     // in the model
204     // in case if there are no modifications, do not increase the undo/redo stack
205     if (ModelAPI_Session::get()->isModified())
206       ModelAPI_Session::get()->finishOperation();
207     else
208       ModelAPI_Session::get()->abortOperation();
209
210     stopOperation();
211     emit stopped();
212     emit committed();
213
214     afterCommitOperation();
215     return true;
216   }
217   return false;
218 }
219
220 void ModuleBase_Operation::setRunning(bool theState)
221 {
222   emit triggered(theState);
223 }
224
225 void ModuleBase_Operation::activateByPreselection()
226 {
227   if (!myPropertyPanel || myPreSelection.empty()) {
228     myPropertyPanel->activateNextWidget(NULL);
229     return;
230   }
231   const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
232   if (aWidgets.empty()) {
233     myPropertyPanel->activateNextWidget(NULL);
234     return;
235   }
236   
237   ModuleBase_ModelWidget* aWgt, *aFilledWgt = 0;
238   QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
239   bool isSet = false;
240   // 1. apply the selection to controls
241   int aCurrentPosition = 0;
242   for (aWIt = aWidgets.constBegin(); aWIt != aWidgets.constEnd(); ++aWIt) {
243     aWgt = (*aWIt);
244     if (!aWgt->canSetValue())
245       continue;
246
247     if (!aWgt->setSelection(myPreSelection, aCurrentPosition/*aValue*/)) {
248       isSet = false;
249       break;
250     } else {
251       isSet = true;
252       aFilledWgt = aWgt;
253     }
254   }
255   // 2. ignore not obligatory widgets
256   /*for (; aWIt != aWidgets.constEnd(); ++aWIt) {
257     aWgt = (*aWIt);
258     if (aWgt && aWgt->isObligatory())
259       continue;
260     aFilledWgt = aWgt;
261   }*/
262
263   // 3. activate the next obligatory widget
264   myPropertyPanel->activateNextWidget(aFilledWgt);
265   if (aFilledWgt)
266     emit activatedByPreselection();
267
268 }
269
270 void ModuleBase_Operation::setParentFeature(CompositeFeaturePtr theParent)
271 {
272   myParentFeature = theParent;
273 }
274
275 CompositeFeaturePtr ModuleBase_Operation::parentFeature() const
276 {
277   return myParentFeature;
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
360   // Do not activate widgets by default if the current operation is editing operation
361   // Because we don't know which widget is going to be edited. 
362   if (!isEditOperation())
363     activateByPreselection();
364 }
365
366 bool ModuleBase_Operation::isGranted(QString theId) const
367 {
368   return myNestedFeatures.contains(theId);
369 }