Salome HOME
Merge branch 'master' of newgeom:newgeom
[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 <Model_Events.h>
19
20 #include <Events_Loop.h>
21
22 #ifdef _DEBUG
23 #include <QDebug>
24 #endif
25
26 ModuleBase_Operation::ModuleBase_Operation(const QString& theId, QObject* theParent)
27 : ModuleBase_IOperation(theId, theParent)
28 {
29 }
30
31 ModuleBase_Operation::~ModuleBase_Operation()
32 {
33 }
34
35 QString ModuleBase_Operation::id() const
36 {
37   return getDescription()->operationId();
38 }
39
40 boost::shared_ptr<ModelAPI_Feature> ModuleBase_Operation::feature() const
41 {
42   return myFeature;
43 }
44
45 bool ModuleBase_Operation::isNestedOperationsEnabled() const
46 {
47   return true;
48 }
49
50 void ModuleBase_Operation::storeReal(double theValue)
51 {
52   if(!myFeature){
53     #ifdef _DEBUG
54     qDebug() << "ModuleBase_Operation::storeReal: " <<
55         "trying to store value without opening a transaction.";
56     #endif
57     return;
58   }
59   QString anId = sender()->objectName();
60   boost::shared_ptr<ModelAPI_Data> aData = myFeature->data();
61   boost::shared_ptr<ModelAPI_AttributeDouble> aReal = aData->real(anId.toStdString());
62   aReal->setValue(theValue);
63   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
64 }
65
66 void ModuleBase_Operation::storeCustomValue()
67 {
68   if(!myFeature){
69     #ifdef _DEBUG
70     qDebug() << "ModuleBase_Operation::storeCustom: " <<
71         "trying to store value without opening a transaction.";
72     #endif
73     return;
74   }
75
76   ModuleBase_ModelWidget* aCustom = dynamic_cast<ModuleBase_ModelWidget*>(sender());
77   if (aCustom)
78     aCustom->storeValue(myFeature);
79 }
80
81 void ModuleBase_Operation::startOperation()
82 {
83   setFeature(createFeature());
84   //emit callSlot();
85   //commit();
86 }
87
88 void ModuleBase_Operation::stopOperation()
89 {
90 }
91
92 void ModuleBase_Operation::abortOperation()
93 {
94 }
95
96 void ModuleBase_Operation::commitOperation()
97 {
98   if (myFeature) myFeature->execute();
99 }
100
101 void ModuleBase_Operation::flushUpdated()
102 {
103   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
104 }
105
106 void ModuleBase_Operation::flushCreated()
107 {
108   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_CREATED));
109 }
110
111 boost::shared_ptr<ModelAPI_Feature> ModuleBase_Operation::createFeature(const bool theFlushMessage)
112 {
113   boost::shared_ptr<ModelAPI_Document> aDoc = document();
114   boost::shared_ptr<ModelAPI_Feature> aFeature = aDoc->addFeature(
115                                            getDescription()->operationId().toStdString());
116   if (aFeature) // TODO: generate an error if feature was not created
117     aFeature->execute();
118
119   if (theFlushMessage)
120     flushCreated();
121   return aFeature;
122 }
123
124 void ModuleBase_Operation::setFeature(boost::shared_ptr<ModelAPI_Feature> theFeature)
125 {
126   myFeature = theFeature;
127 }