]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_Operation.cpp
Salome HOME
305b810c6b008193818b51e20ffe364e99fbee37
[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)
90     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      std::list<ModelAPI_Validator*> aValidators;
106      aFactory->validators(aId, aValidators);
107      std::list<ModelAPI_Validator*>::const_iterator aIt;
108      for (aIt = aValidators.cbegin(); aIt != aValidators.cend(); ++aIt) {
109      const ModuleBase_FeatureValidator* aFValidator = 
110      dynamic_cast<const ModuleBase_FeatureValidator*>(*aIt);
111      if (aFValidator) {
112      if (!aFValidator->isValid(aFeature))
113      return false;
114      }
115      }*/
116     return true;
117   }
118   return false;
119 }
120
121 void ModuleBase_Operation::flushUpdated()
122 {
123   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
124 }
125
126 void ModuleBase_Operation::flushCreated()
127 {
128   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_CREATED));
129 }
130
131 FeaturePtr ModuleBase_Operation::createFeature(const bool theFlushMessage)
132 {
133   boost::shared_ptr<ModelAPI_Document> aDoc = document();
134   myFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
135   if (myFeature) {  // TODO: generate an error if feature was not created
136     myIsModified = true;
137     myFeature->execute();
138     // Init default values
139     /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
140      QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
141      for (; anIt != aLast; anIt++) {
142      (*anIt)->storeValue(aFeature);
143      }*/
144   }
145
146   if (theFlushMessage)
147     flushCreated();
148   return myFeature;
149 }
150
151 void ModuleBase_Operation::setFeature(FeaturePtr theFeature)
152 {
153   myFeature = theFeature;
154 }
155
156 void ModuleBase_Operation::setEditingFeature(FeaturePtr theFeature)
157 {
158   setFeature(theFeature);
159   myIsEditing = true;
160 }
161
162 bool ModuleBase_Operation::hasObject(ObjectPtr theObj) const
163 {
164   FeaturePtr aFeature = feature();
165   if (aFeature) {
166     if (aFeature == theObj)
167       return true;
168     std::list<ResultPtr> aResults = aFeature->results();
169     std::list<ResultPtr>::const_iterator aIt;
170     for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
171       if ((*aIt) == theObj)
172         return true;
173     }
174   }
175   return false;
176 }