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