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