Salome HOME
Providing Action class to have a common approach to start/finish/abort model transact...
[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
12 #include <Events_Loop.h>
13
14 #include <ModelAPI_Events.h>
15 #include <ModelAPI_CompositeFeature.h>
16 #include <ModelAPI_Session.h>
17
18 #include <Config_PointerMessage.h>
19 #include <Config_WidgetReader.h>
20 #include <Config_ModuleReader.h>
21
22 #include <QAction>
23
24 ModuleBase_IModule::ModuleBase_IModule(ModuleBase_IWorkshop* theParent)
25   : QObject(theParent), myWorkshop(theParent) 
26 {
27   connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
28
29
30   //connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)), this,
31   //        SLOT(onMousePressed(QMouseEvent*)));
32   //connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)), this,
33   //        SLOT(onMouseReleased(QMouseEvent*)));
34   //connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)), this,
35   //        SLOT(onMouseMoved(QMouseEvent*)));
36   //connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)), this,
37   //        SLOT(onKeyRelease(QKeyEvent*)));
38   //connect(myWorkshop->viewer(), SIGNAL(mouseDoubleClick(QMouseEvent*)), this,
39   //        SLOT(onMouseDoubleClick(QMouseEvent*)));
40 }
41
42 void ModuleBase_IModule::launchOperation(const QString& theCmdId)
43 {
44   if (!myWorkshop->canStartOperation(theCmdId))
45     return;
46
47   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
48                                              (createOperation(theCmdId.toStdString()));
49   if (aFOperation) {
50     ModuleBase_ISelection* aSelection = myWorkshop->selection();
51     // Initialise operation with preliminary selection
52     aFOperation->initSelection(aSelection, myWorkshop->viewer());
53     sendOperation(aFOperation);
54   }
55 }
56
57
58 void ModuleBase_IModule::sendOperation(ModuleBase_Operation* theOperation)
59 {
60   static Events_ID aModuleEvent = Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED);
61   std::shared_ptr<Config_PointerMessage> aMessage =
62       std::shared_ptr<Config_PointerMessage>(new Config_PointerMessage(aModuleEvent, this));
63   aMessage->setPointer(theOperation);
64   Events_Loop::loop()->send(aMessage);
65 }
66
67 ModuleBase_Operation* ModuleBase_IModule::getNewOperation(const std::string& theFeatureId)
68 {
69   return new ModuleBase_OperationFeature(theFeatureId.c_str(), this);
70 }
71
72 ModuleBase_Operation* ModuleBase_IModule::createOperation(const std::string& theFeatureId)
73 {
74   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
75                                                           (getNewOperation(theFeatureId));
76   // If the operation is launched as sub-operation of another then we have to initialise
77   // parent feature
78   ModuleBase_OperationFeature* aCurOperation = dynamic_cast<ModuleBase_OperationFeature*>
79                                                          (myWorkshop->currentOperation());
80   if (aCurOperation) {
81     FeaturePtr aFeature = aCurOperation->feature();
82     CompositeFeaturePtr aCompFeature =
83         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
84     if (aCompFeature) {
85       aFOperation->setParentFeature(aCompFeature);
86     }
87   }
88
89   std::string aPluginFileName = myFeaturesInFiles[theFeatureId];
90   Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
91   aWdgReader.readAll();
92   std::string aXmlCfg = aWdgReader.featureWidgetCfg(theFeatureId);
93   std::string aDescription = aWdgReader.featureDescription(theFeatureId);
94
95   aFOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
96   aFOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
97
98   return aFOperation;
99 }
100
101 void ModuleBase_IModule::createFeatures()
102 {
103   registerValidators();
104   registerFilters();
105   registerProperties();
106
107   Config_ModuleReader aXMLReader = Config_ModuleReader();
108   aXMLReader.readAll();
109   myFeaturesInFiles = aXMLReader.featuresInFiles();
110 }
111
112
113 void ModuleBase_IModule::actionCreated(QAction* theFeature)
114 {
115   connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
116 }
117
118 bool ModuleBase_IModule::canDisplayObject(const ObjectPtr& theObject) const
119 {
120   return true;
121 }
122
123 bool ModuleBase_IModule::canUndo() const
124 {
125   SessionPtr aMgr = ModelAPI_Session::get();
126   return aMgr->hasModuleDocument() && aMgr->canUndo() && !aMgr->isOperation();
127 }
128
129 bool ModuleBase_IModule::canRedo() const
130 {
131   SessionPtr aMgr = ModelAPI_Session::get();
132   return aMgr->hasModuleDocument() && aMgr->canRedo() && !aMgr->isOperation();
133 }
134
135 bool ModuleBase_IModule::canCommitOperation() const
136 {
137   return true;
138 }
139
140 void ModuleBase_IModule::onFeatureTriggered()
141 {
142   QAction* aCmd = dynamic_cast<QAction*>(sender());
143   //Do nothing on uncheck
144   if (aCmd->isCheckable() && !aCmd->isChecked()) {
145     ModuleBase_Operation* anOperation = myWorkshop->findStartedOperation(aCmd->data().toString());
146     if (myWorkshop->canStopOperation())
147       myWorkshop->abortOperation(anOperation);
148     else {
149       aCmd->setChecked(true);
150     }
151   }
152   else {
153     launchOperation(aCmd->data().toString());
154     emit operationLaunched();
155   }
156 }
157
158 void ModuleBase_IModule::editFeature(FeaturePtr theFeature)
159 {
160   std::string aFeatureId = theFeature->getKind();
161   if (!myWorkshop->canStartOperation(aFeatureId.c_str()))
162     return;
163
164   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
165                                                          (createOperation(aFeatureId));
166   if (aFOperation) {
167     aFOperation->setFeature(theFeature);
168     sendOperation(aFOperation);
169   }
170 }
171
172 bool ModuleBase_IModule::canActivateSelection(const ObjectPtr& theObject) const
173 {
174   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
175                                                      (myWorkshop->currentOperation());
176   return !aFOperation || !aFOperation->hasObject(theObject);
177 }
178
179 void ModuleBase_IModule::onOperationResumed(ModuleBase_Operation* theOperation) 
180 {
181   emit operationResumed(theOperation);
182 }