]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_Operation.cpp
Salome HOME
4a980a1dc2b8b0dc534ca40968f0b57ca247bc57
[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   if (myFeature) myFeature->execute();
90 }
91
92 void ModuleBase_Operation::afterCommitOperation()
93 {
94 }
95
96 bool ModuleBase_Operation::canBeCommitted() const
97 {
98   if (ModuleBase_IOperation::canBeCommitted()) {
99 /*    FeaturePtr aFeature = feature();
100     std::string aId = aFeature->getKind();
101
102     PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
103     ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
104     std::list<ModelAPI_Validator*> aValidators;
105     aFactory->validators(aId, aValidators);
106     std::list<ModelAPI_Validator*>::const_iterator aIt;
107     for (aIt = aValidators.cbegin(); aIt != aValidators.cend(); ++aIt) {
108       const ModuleBase_FeatureValidator* aFValidator = 
109         dynamic_cast<const ModuleBase_FeatureValidator*>(*aIt);
110       if (aFValidator) {
111         if (!aFValidator->isValid(aFeature))
112           return false;
113       }
114     }*/
115     return true;
116   }
117   return false;
118 }
119
120 void ModuleBase_Operation::flushUpdated()
121 {
122   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
123 }
124
125 void ModuleBase_Operation::flushCreated()
126 {
127   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
128 }
129
130 FeaturePtr ModuleBase_Operation::createFeature(const bool theFlushMessage)
131 {
132   boost::shared_ptr<ModelAPI_Document> aDoc = document();
133   myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
134   if (myFeature) { // TODO: generate an error if feature was not created
135     myIsModified = true;
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 }