Salome HOME
fd0724f287ef8e32fbeb2045f18e0961f75beae3
[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     : ModuleBase_IOperation(theId, theParent)
30 {
31 }
32
33 ModuleBase_Operation::~ModuleBase_Operation()
34 {
35 }
36
37 QString ModuleBase_Operation::id() const
38 {
39   return getDescription()->operationId();
40 }
41
42 FeaturePtr ModuleBase_Operation::feature() const
43 {
44   return myFeature;
45 }
46
47 bool ModuleBase_Operation::isValid() const
48 {
49   if (!myFeature)
50     return true; // rename operation
51   //Get validators for the Id
52   SessionPtr aMgr = ModelAPI_Session::get();
53   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
54   return aFactory->validate(myFeature);
55 }
56
57 bool ModuleBase_Operation::isNestedOperationsEnabled() const
58 {
59   return true;
60 }
61
62 void ModuleBase_Operation::storeCustomValue()
63 {
64   if (!myFeature) {
65 #ifdef _DEBUG
66     qDebug() << "ModuleBase_Operation::storeCustom: " <<
67     "trying to store value without opening a transaction.";
68 #endif
69     return;
70   }
71
72   ModuleBase_ModelWidget* aCustom = dynamic_cast<ModuleBase_ModelWidget*>(sender());
73   if (aCustom)
74     aCustom->storeValue();
75 }
76
77 void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
78 {
79 }
80
81 void ModuleBase_Operation::startOperation()
82 {
83   if (!myIsEditing)
84     createFeature();
85   //emit callSlot();
86   //commit();
87 }
88
89 void ModuleBase_Operation::stopOperation()
90 {
91 }
92
93 void ModuleBase_Operation::abortOperation()
94 {
95 }
96
97 void ModuleBase_Operation::commitOperation()
98 {
99 }
100
101 void ModuleBase_Operation::afterCommitOperation()
102 {
103 }
104
105 bool ModuleBase_Operation::canBeCommitted() const
106 {
107   if (ModuleBase_IOperation::canBeCommitted()) {
108     /*    FeaturePtr aFeature = feature();
109      std::string aId = aFeature->getKind();
110
111      SessionPtr aMgr = ModelAPI_Session::get();
112      ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
113      std::list<ModelAPI_Validator*> aValidators;
114      aFactory->validators(aId, aValidators);
115      std::list<ModelAPI_Validator*>::const_iterator aIt;
116      for (aIt = aValidators.cbegin(); aIt != aValidators.cend(); ++aIt) {
117      const ModuleBase_FeatureValidator* aFValidator = 
118      dynamic_cast<const ModuleBase_FeatureValidator*>(*aIt);
119      if (aFValidator) {
120      if (!aFValidator->isValid(aFeature))
121      return false;
122      }
123      }*/
124     return true;
125   }
126   return false;
127 }
128
129 void ModuleBase_Operation::flushUpdated()
130 {
131   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
132 }
133
134 void ModuleBase_Operation::flushCreated()
135 {
136   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
137 }
138
139 FeaturePtr ModuleBase_Operation::createFeature(const bool theFlushMessage)
140 {
141   boost::shared_ptr<ModelAPI_Document> aDoc = document();
142   myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
143   if (myFeature) {  // TODO: generate an error if feature was not created
144     myIsModified = true;
145     // Model update should call "execute" of a feature.
146     //myFeature->execute();
147     // Init default values
148     /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
149      QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
150      for (; anIt != aLast; anIt++) {
151      (*anIt)->storeValue(aFeature);
152      }*/
153   }
154
155   if (theFlushMessage)
156     flushCreated();
157   return myFeature;
158 }
159
160 void ModuleBase_Operation::setFeature(FeaturePtr theFeature)
161 {
162   myFeature = theFeature;
163 }
164
165 void ModuleBase_Operation::setEditingFeature(FeaturePtr theFeature)
166 {
167   setFeature(theFeature);
168   myIsEditing = true;
169 }
170
171 bool ModuleBase_Operation::hasObject(ObjectPtr theObj) const
172 {
173   FeaturePtr aFeature = feature();
174   if (aFeature) {
175     if (aFeature == theObj)
176       return true;
177     std::list<ResultPtr> aResults = aFeature->results();
178     std::list<ResultPtr>::const_iterator aIt;
179     for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
180       if ((*aIt) == theObj)
181         return true;
182     }
183   }
184   return false;
185 }
186