Salome HOME
9f99fad519725bd389e2899e5df8288cefae98b1
[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
17 #include <ModelAPI_AttributeDouble.h>
18 #include <ModelAPI_Document.h>
19 #include <ModelAPI_Feature.h>
20 #include <ModelAPI_Data.h>
21 #include <ModelAPI_Document.h>
22 #include <ModelAPI_Events.h>
23 #include <ModelAPI_Result.h>
24 #include <ModelAPI_Validator.h>
25 #include <ModelAPI_Session.h>
26
27 #include <GeomAPI_Pnt2d.h>
28
29 #include <Events_Loop.h>
30
31 #ifdef _DEBUG
32 #include <QDebug>
33 #endif
34
35 ModuleBase_Operation::ModuleBase_Operation(const QString& theId, QObject* theParent)
36     : QObject(theParent),
37       myIsEditing(false),
38       myIsModified(false),
39       myPropertyPanel(NULL)
40 {
41   myDescription = new ModuleBase_OperationDescription(theId);
42 }
43
44 ModuleBase_Operation::~ModuleBase_Operation()
45 {
46   delete myDescription;
47 }
48
49 QString ModuleBase_Operation::id() const
50 {
51   return getDescription()->operationId();
52 }
53
54 FeaturePtr ModuleBase_Operation::feature() const
55 {
56   return myFeature;
57 }
58
59 bool ModuleBase_Operation::isValid() const
60 {
61   if (!myFeature)
62     return true; // rename operation
63   //Get validators for the Id
64   SessionPtr aMgr = ModelAPI_Session::get();
65   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
66   return aFactory->validate(myFeature);
67 }
68
69 bool ModuleBase_Operation::isNestedOperationsEnabled() const
70 {
71   return true;
72 }
73
74 void ModuleBase_Operation::storeCustomValue()
75 {
76   if (!myFeature) {
77 #ifdef _DEBUG
78     qDebug() << "ModuleBase_Operation::storeCustom: " <<
79     "trying to store value without opening a transaction.";
80 #endif
81     return;
82   }
83
84   ModuleBase_ModelWidget* aCustom = dynamic_cast<ModuleBase_ModelWidget*>(sender());
85   if (aCustom)
86     aCustom->storeValue();
87 }
88
89 void ModuleBase_Operation::startOperation()
90 {
91   if (!myIsEditing)
92     createFeature();
93 }
94
95 void ModuleBase_Operation::stopOperation()
96 {
97 }
98
99 void ModuleBase_Operation::abortOperation()
100 {
101 }
102
103 void ModuleBase_Operation::commitOperation()
104 {
105 }
106
107 void ModuleBase_Operation::afterCommitOperation()
108 {
109 }
110
111 bool ModuleBase_Operation::canBeCommitted() const
112 {
113   return true;
114 }
115
116 void ModuleBase_Operation::flushUpdated()
117 {
118   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
119 }
120
121 void ModuleBase_Operation::flushCreated()
122 {
123   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
124 }
125
126 FeaturePtr ModuleBase_Operation::createFeature(
127   const bool theFlushMessage, CompositeFeaturePtr theCompositeFeature)
128 {
129   if (theCompositeFeature) {
130     myFeature = theCompositeFeature->addFeature(getDescription()->operationId().toStdString());
131   } else {
132     boost::shared_ptr<ModelAPI_Document> aDoc = document();
133     myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
134   }
135   if (myFeature) {  // TODO: generate an error if feature was not created
136     myIsModified = true;
137     // Model update should call "execute" of a feature.
138     //myFeature->execute();
139     // Init default values
140     /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
141      QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
142      for (; anIt != aLast; anIt++) {
143      (*anIt)->storeValue(aFeature);
144      }*/
145   }
146
147   if (theFlushMessage)
148     flushCreated();
149   return myFeature;
150 }
151
152 void ModuleBase_Operation::setFeature(FeaturePtr theFeature)
153 {
154   myFeature = theFeature;
155   myIsEditing = true;
156 }
157
158 bool ModuleBase_Operation::hasObject(ObjectPtr theObj) const
159 {
160   FeaturePtr aFeature = feature();
161   if (aFeature) {
162     if (aFeature == theObj)
163       return true;
164     std::list<ResultPtr> aResults = aFeature->results();
165     std::list<ResultPtr>::const_iterator aIt;
166     for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
167       if ((*aIt) == theObj)
168         return true;
169     }
170   }
171   return false;
172 }
173
174
175 boost::shared_ptr<ModelAPI_Document> ModuleBase_Operation::document() const
176 {
177   return ModelAPI_Session::get()->moduleDocument();
178 }
179
180
181 void ModuleBase_Operation::start()
182 {
183   ModelAPI_Session::get()->startOperation();
184
185   startOperation();
186   emit started();
187 }
188
189 void ModuleBase_Operation::resume()
190 {
191   if (myPropertyPanel)
192     connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)),
193             this,            SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
194   emit resumed();
195 }
196
197 void ModuleBase_Operation::abort()
198 {
199   abortOperation();
200   emit aborted();
201   if (myPropertyPanel)
202     disconnect(myPropertyPanel, 0, this, 0);
203
204   stopOperation();
205
206   ModelAPI_Session::get()->abortOperation();
207   emit stopped();
208 }
209
210 bool ModuleBase_Operation::commit()
211 {
212   if (canBeCommitted()) {
213     commitOperation();
214     emit committed();
215
216   if (myPropertyPanel)
217     disconnect(myPropertyPanel, 0, this, 0);
218
219     stopOperation();
220     ModelAPI_Session::get()->finishOperation();
221
222     emit stopped();
223
224     afterCommitOperation();
225     return true;
226   }
227   return false;
228 }
229
230 void ModuleBase_Operation::setRunning(bool theState)
231 {
232   if (!theState) {
233     abort();
234   }
235 }
236
237 bool ModuleBase_Operation::activateByPreselection()
238 {
239   if (!myPropertyPanel)
240     return false;
241   if (myPreSelection.empty())
242     return false;
243   const QList<ModuleBase_ModelWidget*>& aWidgets = myPropertyPanel->modelWidgets();
244   if (aWidgets.empty())
245     return false;
246   
247   ModuleBase_ModelWidget* aWgt;
248   ModuleBase_ViewerPrs aPrs;
249   QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
250   QList<ModuleBase_ViewerPrs>::const_iterator aPIt;
251   for (aWIt = aWidgets.constBegin(), aPIt = myPreSelection.constBegin();
252        (aWIt != aWidgets.constEnd()) && (aPIt != myPreSelection.constEnd());
253        ++aWIt, ++aPIt) {
254     aWgt = (*aWIt);
255     aPrs = (*aPIt);
256     ModuleBase_WidgetValueFeature aValue;
257     aValue.setObject(aPrs.object());
258     if (!aWgt->setValue(&aValue))
259       break;
260   }
261   if (canBeCommitted()) {
262     // if all widgets are filled with selection
263     commit();
264     return true;
265   }
266
267   //ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
268   //if ((myPreSelection.size() > 0) && aActiveWgt) {
269   //  const ModuleBase_ViewerPrs& aPrs = myPreSelection.first();
270   //  ModuleBase_WidgetValueFeature aValue;
271   //  aValue.setObject(aPrs.object());
272   //  if (aActiveWgt->setValue(&aValue)) {
273   //    myPreSelection.removeOne(aPrs);
274   //    myPropertyPanel->activateNextWidget();
275   //  }
276   //  // If preselection is enough to make a valid feature - apply it immediately
277   //}
278   return false;
279 }
280
281 void ModuleBase_Operation::initSelection(ModuleBase_ISelection* theSelection)
282 {
283   myPreSelection = theSelection->getSelected();
284 }
285
286 void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
287 {
288   //activateByPreselection();
289   //if (theWidget && myPropertyPanel) {
290   //  myPropertyPanel->activateNextWidget();
291   ////  //emit activateNextWidget(myActiveWidget);
292   //}
293 }
294
295 bool ModuleBase_Operation::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
296 {
297   ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
298   if (!aActiveWgt)
299     return false;
300   ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
301   aValue->setObject(theFeature);
302   aValue->setPoint(boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
303   bool isApplyed = aActiveWgt->setValue(aValue);
304
305   delete aValue;
306   myIsModified = (myIsModified || isApplyed);
307   return isApplyed;
308 }
309
310
311 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp) 
312
313   myPropertyPanel = theProp; 
314   connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)), this,
315           SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
316 }