Salome HOME
ec6c0e1e680ca86147122b361047136981795d44
[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
13 #include <ModelAPI_AttributeDouble.h>
14 #include <ModelAPI_Document.h>
15 #include <ModelAPI_Feature.h>
16 #include <ModelAPI_Data.h>
17 #include <ModelAPI_Document.h>
18 #include <ModelAPI_Events.h>
19 #include <ModelAPI_Result.h>
20 #include <ModelAPI_Validator.h>
21
22 #include <Events_Loop.h>
23
24 #ifdef _DEBUG
25 #include <QDebug>
26 #endif
27
28 ModuleBase_Operation::ModuleBase_Operation(const QString& theId, QObject* theParent)
29     : QObject(theParent),
30       myIsEditing(false),
31       myIsModified(false)
32 {
33   myDescription = new ModuleBase_OperationDescription(theId);
34 }
35
36 ModuleBase_Operation::~ModuleBase_Operation()
37 {
38   delete myDescription;
39 }
40
41 QString ModuleBase_Operation::id() const
42 {
43   return getDescription()->operationId();
44 }
45
46 FeaturePtr ModuleBase_Operation::feature() const
47 {
48   return myFeature;
49 }
50
51 bool ModuleBase_Operation::isValid() const
52 {
53   if (!myFeature)
54     return true; // rename operation
55   //Get validators for the Id
56   SessionPtr aMgr = ModelAPI_Session::get();
57   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
58   return aFactory->validate(myFeature);
59 }
60
61 bool ModuleBase_Operation::isNestedOperationsEnabled() const
62 {
63   return true;
64 }
65
66 void ModuleBase_Operation::storeCustomValue()
67 {
68   if (!myFeature) {
69 #ifdef _DEBUG
70     qDebug() << "ModuleBase_Operation::storeCustom: " <<
71     "trying to store value without opening a transaction.";
72 #endif
73     return;
74   }
75
76   ModuleBase_ModelWidget* aCustom = dynamic_cast<ModuleBase_ModelWidget*>(sender());
77   if (aCustom)
78     aCustom->storeValue();
79 }
80
81 void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
82 {
83 }
84
85 void ModuleBase_Operation::startOperation()
86 {
87   if (!myIsEditing)
88     createFeature();
89   //emit callSlot();
90   //commit();
91 }
92
93 void ModuleBase_Operation::stopOperation()
94 {
95 }
96
97 void ModuleBase_Operation::abortOperation()
98 {
99 }
100
101 void ModuleBase_Operation::commitOperation()
102 {
103 }
104
105 void ModuleBase_Operation::afterCommitOperation()
106 {
107 }
108
109 bool ModuleBase_Operation::canBeCommitted() const
110 {
111   return true;
112 }
113
114 void ModuleBase_Operation::flushUpdated()
115 {
116   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
117 }
118
119 void ModuleBase_Operation::flushCreated()
120 {
121   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
122 }
123
124 FeaturePtr ModuleBase_Operation::createFeature(const bool theFlushMessage)
125 {
126   boost::shared_ptr<ModelAPI_Document> aDoc = document();
127   myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
128   if (myFeature) {  // TODO: generate an error if feature was not created
129     myIsModified = true;
130     // Model update should call "execute" of a feature.
131     //myFeature->execute();
132     // Init default values
133     /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
134      QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
135      for (; anIt != aLast; anIt++) {
136      (*anIt)->storeValue(aFeature);
137      }*/
138   }
139
140   if (theFlushMessage)
141     flushCreated();
142   return myFeature;
143 }
144
145 void ModuleBase_Operation::setFeature(FeaturePtr theFeature)
146 {
147   myFeature = theFeature;
148 }
149
150 void ModuleBase_Operation::setEditingFeature(FeaturePtr theFeature)
151 {
152   setFeature(theFeature);
153   myIsEditing = true;
154 }
155
156 bool ModuleBase_Operation::hasObject(ObjectPtr theObj) const
157 {
158   FeaturePtr aFeature = feature();
159   if (aFeature) {
160     if (aFeature == theObj)
161       return true;
162     std::list<ResultPtr> aResults = aFeature->results();
163     std::list<ResultPtr>::const_iterator aIt;
164     for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
165       if ((*aIt) == theObj)
166         return true;
167     }
168   }
169   return false;
170 }
171
172
173 boost::shared_ptr<ModelAPI_Document> ModuleBase_Operation::document() const
174 {
175   return ModelAPI_Session::get()->moduleDocument();
176 }
177
178
179 void ModuleBase_Operation::start()
180 {
181   ModelAPI_Session::get()->startOperation();
182
183   startOperation();
184   emit started();
185 }
186
187 void ModuleBase_Operation::resume()
188 {
189   emit resumed();
190 }
191
192 void ModuleBase_Operation::abort()
193 {
194   abortOperation();
195   emit aborted();
196
197   stopOperation();
198
199   ModelAPI_Session::get()->abortOperation();
200   emit stopped();
201 }
202
203 bool ModuleBase_Operation::commit()
204 {
205   if (canBeCommitted()) {
206     commitOperation();
207     emit committed();
208
209     stopOperation();
210
211     ModelAPI_Session::get()->finishOperation();
212     emit stopped();
213
214     afterCommitOperation();
215     return true;
216   }
217   return false;
218 }
219
220 void ModuleBase_Operation::setRunning(bool theState)
221 {
222   if (!theState) {
223     abort();
224   }
225 }