Salome HOME
Merge remote-tracking branch 'remotes/origin/master' into SketchSolver
[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), myIsEditing(false)
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 FeaturePtr 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::storeCustomValue()
51 {
52   if(!myFeature){
53     #ifdef _DEBUG
54     qDebug() << "ModuleBase_Operation::storeCustom: " <<
55         "trying to store value without opening a transaction.";
56     #endif
57     return;
58   }
59
60   ModuleBase_ModelWidget* aCustom = dynamic_cast<ModuleBase_ModelWidget*>(sender());
61   if (aCustom)
62     aCustom->storeValue(myFeature);
63 }
64
65 void ModuleBase_Operation::onWidgetActivated(ModuleBase_ModelWidget* theWidget)
66 {
67 }
68
69 void ModuleBase_Operation::startOperation()
70 {
71   if (!myIsEditing)
72     setFeature(createFeature());
73   //emit callSlot();
74   //commit();
75 }
76
77 void ModuleBase_Operation::stopOperation()
78 {
79 }
80
81 void ModuleBase_Operation::abortOperation()
82 {
83 }
84
85 void ModuleBase_Operation::commitOperation()
86 {
87   if (myFeature) myFeature->execute();
88 }
89
90 void ModuleBase_Operation::afterCommitOperation()
91 {
92 }
93
94 void ModuleBase_Operation::flushUpdated()
95 {
96   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
97 }
98
99 void ModuleBase_Operation::flushCreated()
100 {
101   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_FEATURE_CREATED));
102 }
103
104 FeaturePtr ModuleBase_Operation::createFeature(const bool theFlushMessage)
105 {
106   boost::shared_ptr<ModelAPI_Document> aDoc = document();
107   FeaturePtr aFeature = aDoc->addFeature(getDescription()->operationId().toStdString());
108   if (aFeature) { // TODO: generate an error if feature was not created
109     aFeature->execute();
110     // Init default values
111     /*QList<ModuleBase_ModelWidget*> aWidgets = getDescription()->modelWidgets();
112     QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
113     for (; anIt != aLast; anIt++) {
114       (*anIt)->storeValue(aFeature);
115     }*/
116   }
117
118   if (theFlushMessage)
119     flushCreated();
120   return aFeature;
121 }
122
123 void ModuleBase_Operation::setFeature(FeaturePtr theFeature)
124 {
125   myFeature = theFeature;
126 }
127
128 void ModuleBase_Operation::setEditingFeature(FeaturePtr theFeature)
129 {
130   setFeature(theFeature);
131   myIsEditing = true;
132 }