1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
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 #include "ModuleBase_WidgetFactory.h"
13 #include "ModuleBase_PageWidget.h"
14 #include "ModuleBase_Dialog.h"
16 #include <Events_Loop.h>
18 #include <ModelAPI_Events.h>
19 #include <ModelAPI_CompositeFeature.h>
20 #include <ModelAPI_Session.h>
21 #include "ModelAPI_Tools.h"
23 #include <Config_PointerMessage.h>
24 #include <Config_WidgetReader.h>
25 #include <Config_ModuleReader.h>
26 #include <Config_Translator.h>
29 #include <QMainWindow>
32 #include <QDialogButtonBox>
33 #include <QPushButton>
35 ModuleBase_IModule::ModuleBase_IModule(ModuleBase_IWorkshop* theParent)
36 : QObject(theParent), myWorkshop(theParent)
38 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
41 //connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)), this,
42 // SLOT(onMousePressed(QMouseEvent*)));
43 //connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)), this,
44 // SLOT(onMouseReleased(QMouseEvent*)));
45 //connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)), this,
46 // SLOT(onMouseMoved(QMouseEvent*)));
47 //connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)), this,
48 // SLOT(onKeyRelease(QKeyEvent*)));
49 //connect(myWorkshop->viewer(), SIGNAL(mouseDoubleClick(QMouseEvent*)), this,
50 // SLOT(onMouseDoubleClick(QMouseEvent*)));
53 void ModuleBase_IModule::launchModal(const QString& theCmdId)
55 if (!myWorkshop->canStartOperation(theCmdId))
58 std::string aXmlCfg, aDescription;
59 getXMLRepresentation(theCmdId.toStdString(), aXmlCfg, aDescription);
61 SessionPtr aMgr = ModelAPI_Session::get();
62 aMgr->startOperation(theCmdId.toStdString());
64 ModuleBase_Dialog aDlg(myWorkshop, theCmdId, aXmlCfg);
65 if (aDlg.exec() == QDialog::Accepted)
66 aMgr->finishOperation();
68 aMgr->abortOperation();
69 myWorkshop->updateCommandStatus();
73 void ModuleBase_IModule::launchOperation(const QString& theCmdId)
75 if (!myWorkshop->canStartOperation(theCmdId))
78 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
79 (createOperation(theCmdId.toStdString()));
81 ModuleBase_ISelection* aSelection = myWorkshop->selection();
82 // Initialise operation with preliminary selection
83 aFOperation->initSelection(aSelection, myWorkshop->viewer());
84 sendOperation(aFOperation);
89 void ModuleBase_IModule::sendOperation(ModuleBase_Operation* theOperation)
91 static Events_ID aModuleEvent = Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED);
92 std::shared_ptr<Config_PointerMessage> aMessage =
93 std::shared_ptr<Config_PointerMessage>(new Config_PointerMessage(aModuleEvent, this));
94 aMessage->setPointer(theOperation);
95 Events_Loop::loop()->send(aMessage);
98 Handle(AIS_InteractiveObject) ModuleBase_IModule::createPresentation(const ResultPtr& theResult)
100 return Handle(AIS_InteractiveObject)();
103 bool ModuleBase_IModule::canBeShaded(Handle(AIS_InteractiveObject) theAIS) const
108 QString ModuleBase_IModule::getFeatureError(const FeaturePtr& theFeature)
110 QString aMsg = ModelAPI_Tools::getFeatureError(theFeature).c_str();
112 aMsg = Config_Translator::translate(theFeature->getKind(), aMsg.toStdString()).c_str();
116 void ModuleBase_IModule::grantedOperationIds(ModuleBase_Operation* theOperation,
117 QStringList& theIds) const
121 ModuleBase_Operation* ModuleBase_IModule::getNewOperation(const std::string& theFeatureId)
123 return new ModuleBase_OperationFeature(theFeatureId.c_str(), this);
126 bool ModuleBase_IModule::customizeObject(ObjectPtr theObject,
127 const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
128 const bool theUpdateViewer)
133 ModuleBase_Operation* ModuleBase_IModule::createOperation(const std::string& theFeatureId)
135 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
136 (getNewOperation(theFeatureId));
137 // If the operation is launched as sub-operation of another then we have to initialize
139 ModuleBase_OperationFeature* aCurOperation = dynamic_cast<ModuleBase_OperationFeature*>
140 (myWorkshop->currentOperation());
142 FeaturePtr aFeature = aCurOperation->feature();
143 CompositeFeaturePtr aCompFeature =
144 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
146 aFOperation->setParentFeature(aCompFeature);
150 std::string aXmlCfg, aDescription;
151 getXMLRepresentation(theFeatureId, aXmlCfg, aDescription);
152 aFOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
153 aFOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
158 void ModuleBase_IModule::createFeatures()
160 registerValidators();
162 registerProperties();
164 Config_ModuleReader aXMLReader = Config_ModuleReader();
165 aXMLReader.readAll();
166 myFeaturesInFiles = aXMLReader.featuresInFiles();
170 void ModuleBase_IModule::actionCreated(QAction* theFeature)
172 theFeature->setStatusTip(theFeature->text());
173 connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
176 bool ModuleBase_IModule::canEraseObject(const ObjectPtr& theObject) const
181 bool ModuleBase_IModule::canDisplayObject(const ObjectPtr& theObject) const
186 bool ModuleBase_IModule::canUndo() const
188 SessionPtr aMgr = ModelAPI_Session::get();
189 return aMgr->hasModuleDocument() && aMgr->canUndo() && !aMgr->isOperation();
192 bool ModuleBase_IModule::canRedo() const
194 SessionPtr aMgr = ModelAPI_Session::get();
195 return aMgr->hasModuleDocument() && aMgr->canRedo() && !aMgr->isOperation();
198 void ModuleBase_IModule::onFeatureTriggered()
200 QAction* aCmd = dynamic_cast<QAction*>(sender());
201 //Do nothing on uncheck
202 if (aCmd->isCheckable() && !aCmd->isChecked()) {
203 ModuleBase_Operation* anOperation = myWorkshop->findStartedOperation(aCmd->data().toString());
204 if (myWorkshop->canStopOperation(anOperation))
205 myWorkshop->abortOperation(anOperation);
207 aCmd->setChecked(true);
211 QString aCmdId = aCmd->data().toString();
212 std::shared_ptr<Config_FeatureMessage> aInfo = myWorkshop->featureInfo(aCmdId);
213 if (aInfo.get() && aInfo->isModal()) {
216 launchOperation(aCmdId);
217 emit operationLaunched();
222 void ModuleBase_IModule::editFeature(FeaturePtr theFeature)
224 std::string aFeatureId = theFeature->getKind();
225 if (!myWorkshop->canStartOperation(aFeatureId.c_str()))
228 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
229 (createOperation(aFeatureId));
231 aFOperation->setFeature(theFeature);
232 sendOperation(aFOperation);
236 bool ModuleBase_IModule::canActivateSelection(const ObjectPtr& theObject) const
238 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
239 (myWorkshop->currentOperation());
240 return !aFOperation || !aFOperation->hasObject(theObject);
243 void ModuleBase_IModule::operationResumed(ModuleBase_Operation* theOperation)
245 emit resumed(theOperation);
248 void ModuleBase_IModule::getXMLRepresentation(const std::string& theFeatureId,
249 std::string& theXmlCfg, std::string& theDescription)
251 std::string aPluginFileName = myFeaturesInFiles[theFeatureId];
252 Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
253 aWdgReader.readAll();
255 theXmlCfg = aWdgReader.featureWidgetCfg(theFeatureId);
256 theDescription = aWdgReader.featureDescription(theFeatureId);