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