]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_IModule.cpp
Salome HOME
#1107 Tab key does not change focus to Apply in circle sketch feature.
[modules/shaper.git] / src / ModuleBase / ModuleBase_IModule.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3
4 #include "ModuleBase_IModule.h"
5 #include "ModuleBase_IViewer.h"
6 #include "ModuleBase_ViewerPrs.h"
7 #include "ModuleBase_Operation.h"
8 #include "ModuleBase_ISelection.h"
9 #include "ModuleBase_OperationDescription.h"
10 #include "ModuleBase_OperationFeature.h"
11 #include "ModuleBase_ModelWidget.h"
12
13 #include <Events_Loop.h>
14
15 #include <ModelAPI_Events.h>
16 #include <ModelAPI_CompositeFeature.h>
17 #include <ModelAPI_Session.h>
18 #include "ModelAPI_Tools.h"
19
20 #include <Config_PointerMessage.h>
21 #include <Config_WidgetReader.h>
22 #include <Config_ModuleReader.h>
23
24 #include <QAction>
25
26 ModuleBase_IModule::ModuleBase_IModule(ModuleBase_IWorkshop* theParent)
27   : QObject(theParent), myWorkshop(theParent) 
28 {
29   connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
30
31
32   //connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)), this,
33   //        SLOT(onMousePressed(QMouseEvent*)));
34   //connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)), this,
35   //        SLOT(onMouseReleased(QMouseEvent*)));
36   //connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)), this,
37   //        SLOT(onMouseMoved(QMouseEvent*)));
38   //connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)), this,
39   //        SLOT(onKeyRelease(QKeyEvent*)));
40   //connect(myWorkshop->viewer(), SIGNAL(mouseDoubleClick(QMouseEvent*)), this,
41   //        SLOT(onMouseDoubleClick(QMouseEvent*)));
42 }
43
44 void ModuleBase_IModule::launchOperation(const QString& theCmdId)
45 {
46   if (!myWorkshop->canStartOperation(theCmdId))
47     return;
48
49   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
50                                              (createOperation(theCmdId.toStdString()));
51   if (aFOperation) {
52     ModuleBase_ISelection* aSelection = myWorkshop->selection();
53     // Initialise operation with preliminary selection
54     aFOperation->initSelection(aSelection, myWorkshop->viewer());
55     sendOperation(aFOperation);
56   }
57 }
58
59
60 void ModuleBase_IModule::sendOperation(ModuleBase_Operation* theOperation)
61 {
62   static Events_ID aModuleEvent = Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED);
63   std::shared_ptr<Config_PointerMessage> aMessage =
64       std::shared_ptr<Config_PointerMessage>(new Config_PointerMessage(aModuleEvent, this));
65   aMessage->setPointer(theOperation);
66   Events_Loop::loop()->send(aMessage);
67 }
68
69 const char* toString(ModelAPI_ExecState theExecState) 
70 {
71 #define TO_STRING(__NAME__) case __NAME__: return #__NAME__;
72   switch (theExecState) {
73   TO_STRING(ModelAPI_StateDone)
74   TO_STRING(ModelAPI_StateMustBeUpdated)
75   TO_STRING(ModelAPI_StateExecFailed)
76   TO_STRING(ModelAPI_StateInvalidArgument)
77   TO_STRING(ModelAPI_StateNothing)
78   default: return "Unknown ExecState.";
79   }
80 #undef TO_STRING
81 }
82
83 QString ModuleBase_IModule::getFeatureError(const FeaturePtr& theFeature)
84 {
85   return ModelAPI_Tools::getFeatureError(theFeature).c_str();
86 }
87
88 void ModuleBase_IModule::grantedOperationIds(ModuleBase_Operation* theOperation,
89                                              QStringList& theIds) const
90 {
91 }
92
93 ModuleBase_Operation* ModuleBase_IModule::getNewOperation(const std::string& theFeatureId)
94 {
95   return new ModuleBase_OperationFeature(theFeatureId.c_str(), this);
96 }
97
98 bool ModuleBase_IModule::customizeObject(ObjectPtr theObject, const bool theUpdateViewer)
99 {
100   return false;
101 }
102
103 ModuleBase_Operation* ModuleBase_IModule::createOperation(const std::string& theFeatureId)
104 {
105   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
106                                                           (getNewOperation(theFeatureId));
107   // If the operation is launched as sub-operation of another then we have to initialize
108   // parent feature
109   ModuleBase_OperationFeature* aCurOperation = dynamic_cast<ModuleBase_OperationFeature*>
110                                                          (myWorkshop->currentOperation());
111   if (aCurOperation) {
112     FeaturePtr aFeature = aCurOperation->feature();
113     CompositeFeaturePtr aCompFeature =
114         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
115     if (aCompFeature) {
116       aFOperation->setParentFeature(aCompFeature);
117     }
118   }
119
120   std::string aPluginFileName = myFeaturesInFiles[theFeatureId];
121   Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
122   aWdgReader.readAll();
123   std::string aXmlCfg = aWdgReader.featureWidgetCfg(theFeatureId);
124   std::string aDescription = aWdgReader.featureDescription(theFeatureId);
125
126   aFOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
127   aFOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
128
129   return aFOperation;
130 }
131
132 void ModuleBase_IModule::createFeatures()
133 {
134   registerValidators();
135   registerFilters();
136   registerProperties();
137
138   Config_ModuleReader aXMLReader = Config_ModuleReader();
139   aXMLReader.readAll();
140   myFeaturesInFiles = aXMLReader.featuresInFiles();
141 }
142
143
144 void ModuleBase_IModule::actionCreated(QAction* theFeature)
145 {
146   connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
147 }
148
149 bool ModuleBase_IModule::canEraseObject(const ObjectPtr& theObject) const
150 {
151   return true;
152 }
153
154 bool ModuleBase_IModule::canDisplayObject(const ObjectPtr& theObject) const
155 {
156   return true;
157 }
158
159 bool ModuleBase_IModule::canUndo() const
160 {
161   SessionPtr aMgr = ModelAPI_Session::get();
162   return aMgr->hasModuleDocument() && aMgr->canUndo() && !aMgr->isOperation();
163 }
164
165 bool ModuleBase_IModule::canRedo() const
166 {
167   SessionPtr aMgr = ModelAPI_Session::get();
168   return aMgr->hasModuleDocument() && aMgr->canRedo() && !aMgr->isOperation();
169 }
170
171 void ModuleBase_IModule::onFeatureTriggered()
172 {
173   QAction* aCmd = dynamic_cast<QAction*>(sender());
174   //Do nothing on uncheck
175   if (aCmd->isCheckable() && !aCmd->isChecked()) {
176     ModuleBase_Operation* anOperation = myWorkshop->findStartedOperation(aCmd->data().toString());
177     if (myWorkshop->canStopOperation(anOperation))
178       myWorkshop->abortOperation(anOperation);
179     else {
180       aCmd->setChecked(true);
181     }
182   }
183   else {
184     launchOperation(aCmd->data().toString());
185     emit operationLaunched();
186   }
187 }
188
189 void ModuleBase_IModule::editFeature(FeaturePtr theFeature)
190 {
191   std::string aFeatureId = theFeature->getKind();
192   if (!myWorkshop->canStartOperation(aFeatureId.c_str()))
193     return;
194
195   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
196                                                          (createOperation(aFeatureId));
197   if (aFOperation) {
198     aFOperation->setFeature(theFeature);
199     sendOperation(aFOperation);
200   }
201 }
202
203 bool ModuleBase_IModule::canActivateSelection(const ObjectPtr& theObject) const
204 {
205   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
206                                                      (myWorkshop->currentOperation());
207   return !aFOperation || !aFOperation->hasObject(theObject);
208 }
209
210 void ModuleBase_IModule::operationResumed(ModuleBase_Operation* theOperation) 
211 {
212   emit resumed(theOperation);
213 }