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