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