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