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