]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_Operation.cpp
Salome HOME
Helper methods, aliases for data()->method()
[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
16 #include <ModelAPI_AttributeDouble.h>
17 #include <ModelAPI_Document.h>
18 #include <ModelAPI_Feature.h>
19 #include <ModelAPI_Data.h>
20 #include <ModelAPI_Document.h>
21 #include <ModelAPI_Events.h>
22 #include <ModelAPI_Result.h>
23 #include <ModelAPI_Validator.h>
24 #include <ModelAPI_Session.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 void ModuleBase_Operation::activateByPreselection()
237 {
238   if (!myPropertyPanel)
239     return;
240   ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
241   if ((myPreSelection.size() > 0) && aActiveWgt) {
242     const ModuleBase_ViewerPrs& aPrs = myPreSelection.front();
243     ModuleBase_WidgetValueFeature aValue;
244     aValue.setObject(aPrs.object());
245     if (aActiveWgt->setValue(&aValue)) {
246       myPreSelection.remove(aPrs);
247       myPropertyPanel->activateNextWidget();
248     }
249     // If preselection is enough to make a valid feature - apply it immediately
250   }
251 }
252
253 void ModuleBase_Operation::initSelection(
254     const std::list<ModuleBase_ViewerPrs>& theSelected,
255     const std::list<ModuleBase_ViewerPrs>& /*theHighlighted*/)
256 {
257   myPreSelection = theSelected;
258 }
259
260 void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
261 {
262   activateByPreselection();
263   //if (theWidget && myPropertyPanel) {
264   //  myPropertyPanel->activateNextWidget();
265   ////  //emit activateNextWidget(myActiveWidget);
266   //}
267 }
268
269 bool ModuleBase_Operation::setWidgetValue(ObjectPtr theFeature, double theX, double theY)
270 {
271   ModuleBase_ModelWidget* aActiveWgt = myPropertyPanel->activeWidget();
272   if (!aActiveWgt)
273     return false;
274   ModuleBase_WidgetValueFeature* aValue = new ModuleBase_WidgetValueFeature();
275   aValue->setObject(theFeature);
276   aValue->setPoint(boost::shared_ptr<GeomAPI_Pnt2d>(new GeomAPI_Pnt2d(theX, theY)));
277   bool isApplyed = aActiveWgt->setValue(aValue);
278
279   delete aValue;
280   myIsModified = (myIsModified || isApplyed);
281   return isApplyed;
282 }
283
284
285 void ModuleBase_Operation::setPropertyPanel(ModuleBase_IPropertyPanel* theProp) 
286
287   myPropertyPanel = theProp; 
288   connect(myPropertyPanel, SIGNAL(widgetActivated(ModuleBase_ModelWidget*)), this,
289           SLOT(onWidgetActivated(ModuleBase_ModelWidget*)));
290 }