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>
28 #include <QMainWindow>
31 #include <QDialogButtonBox>
32 #include <QPushButton>
34 ModuleBase_IModule::ModuleBase_IModule(ModuleBase_IWorkshop* theParent)
35 : QObject(theParent), myWorkshop(theParent)
37 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
40 //connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)), this,
41 // SLOT(onMousePressed(QMouseEvent*)));
42 //connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)), this,
43 // SLOT(onMouseReleased(QMouseEvent*)));
44 //connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)), this,
45 // SLOT(onMouseMoved(QMouseEvent*)));
46 //connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)), this,
47 // SLOT(onKeyRelease(QKeyEvent*)));
48 //connect(myWorkshop->viewer(), SIGNAL(mouseDoubleClick(QMouseEvent*)), this,
49 // SLOT(onMouseDoubleClick(QMouseEvent*)));
52 void ModuleBase_IModule::launchModal(const QString& theCmdId)
54 if (!myWorkshop->canStartOperation(theCmdId))
57 std::string aXmlCfg, aDescription;
58 getXMLRepresentation(theCmdId.toStdString(), aXmlCfg, aDescription);
60 SessionPtr aMgr = ModelAPI_Session::get();
61 aMgr->startOperation(theCmdId.toStdString());
63 ModuleBase_Dialog aDlg(myWorkshop, theCmdId, aXmlCfg);
64 if (aDlg.exec() == QDialog::Accepted)
65 aMgr->finishOperation();
67 aMgr->abortOperation();
68 myWorkshop->updateCommandStatus();
72 void ModuleBase_IModule::launchOperation(const QString& theCmdId)
74 if (!myWorkshop->canStartOperation(theCmdId))
77 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
78 (createOperation(theCmdId.toStdString()));
80 ModuleBase_ISelection* aSelection = myWorkshop->selection();
81 // Initialise operation with preliminary selection
82 aFOperation->initSelection(aSelection, myWorkshop->viewer());
83 sendOperation(aFOperation);
88 void ModuleBase_IModule::sendOperation(ModuleBase_Operation* theOperation)
90 static Events_ID aModuleEvent = Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED);
91 std::shared_ptr<Config_PointerMessage> aMessage =
92 std::shared_ptr<Config_PointerMessage>(new Config_PointerMessage(aModuleEvent, this));
93 aMessage->setPointer(theOperation);
94 Events_Loop::loop()->send(aMessage);
97 Handle(AIS_InteractiveObject) ModuleBase_IModule::createPresentation(const ResultPtr& theResult)
99 return Handle(AIS_InteractiveObject)();
102 bool ModuleBase_IModule::canBeShaded(Handle(AIS_InteractiveObject) theAIS) const
107 QString ModuleBase_IModule::getFeatureError(const FeaturePtr& theFeature)
109 std::string aMsg = ModelAPI_Tools::getFeatureError(theFeature);
110 ModuleBase_Tools::translate(theFeature->getKind(), aMsg);
115 void ModuleBase_IModule::grantedOperationIds(ModuleBase_Operation* theOperation,
116 QStringList& theIds) const
120 ModuleBase_Operation* ModuleBase_IModule::getNewOperation(const std::string& theFeatureId)
122 return new ModuleBase_OperationFeature(theFeatureId.c_str(), this);
125 bool ModuleBase_IModule::customizeObject(ObjectPtr theObject,
126 const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
127 const bool theUpdateViewer)
132 ModuleBase_Operation* ModuleBase_IModule::createOperation(const std::string& theFeatureId)
134 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
135 (getNewOperation(theFeatureId));
136 // If the operation is launched as sub-operation of another then we have to initialize
138 ModuleBase_OperationFeature* aCurOperation = dynamic_cast<ModuleBase_OperationFeature*>
139 (myWorkshop->currentOperation());
141 FeaturePtr aFeature = aCurOperation->feature();
142 CompositeFeaturePtr aCompFeature =
143 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
145 aFOperation->setParentFeature(aCompFeature);
149 std::string aXmlCfg, aDescription;
150 getXMLRepresentation(theFeatureId, aXmlCfg, aDescription);
151 aFOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
152 aFOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
157 void ModuleBase_IModule::createFeatures()
159 registerValidators();
161 registerProperties();
163 Config_ModuleReader aXMLReader = Config_ModuleReader();
164 aXMLReader.readAll();
165 myFeaturesInFiles = aXMLReader.featuresInFiles();
169 void ModuleBase_IModule::actionCreated(QAction* theFeature)
171 theFeature->setStatusTip(theFeature->text());
172 connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
175 bool ModuleBase_IModule::canEraseObject(const ObjectPtr& theObject) const
180 bool ModuleBase_IModule::canDisplayObject(const ObjectPtr& theObject) const
185 bool ModuleBase_IModule::canUndo() const
187 SessionPtr aMgr = ModelAPI_Session::get();
188 return aMgr->hasModuleDocument() && aMgr->canUndo() && !aMgr->isOperation();
191 bool ModuleBase_IModule::canRedo() const
193 SessionPtr aMgr = ModelAPI_Session::get();
194 return aMgr->hasModuleDocument() && aMgr->canRedo() && !aMgr->isOperation();
197 void ModuleBase_IModule::onFeatureTriggered()
199 QAction* aCmd = dynamic_cast<QAction*>(sender());
200 //Do nothing on uncheck
201 if (aCmd->isCheckable() && !aCmd->isChecked()) {
202 ModuleBase_Operation* anOperation = myWorkshop->findStartedOperation(aCmd->data().toString());
203 if (myWorkshop->canStopOperation(anOperation))
204 myWorkshop->stopOperation(anOperation);
206 aCmd->setChecked(true);
210 QString aCmdId = aCmd->data().toString();
211 std::shared_ptr<Config_FeatureMessage> aInfo = myWorkshop->featureInfo(aCmdId);
212 if (aInfo.get() && aInfo->isModal()) {
215 launchOperation(aCmdId);
216 emit operationLaunched();
221 void ModuleBase_IModule::editFeature(FeaturePtr theFeature)
223 std::string aFeatureId = theFeature->getKind();
224 if (!myWorkshop->canStartOperation(aFeatureId.c_str()))
227 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
228 (createOperation(aFeatureId));
230 aFOperation->setFeature(theFeature);
231 sendOperation(aFOperation);
235 bool ModuleBase_IModule::canActivateSelection(const ObjectPtr& theObject) const
237 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
238 (myWorkshop->currentOperation());
239 return !aFOperation || !aFOperation->hasObject(theObject);
242 void ModuleBase_IModule::operationResumed(ModuleBase_Operation* theOperation)
244 emit resumed(theOperation);
247 void ModuleBase_IModule::getXMLRepresentation(const std::string& theFeatureId,
248 std::string& theXmlCfg, std::string& theDescription)
250 std::string aPluginFileName = myFeaturesInFiles[theFeatureId];
251 Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
252 aWdgReader.readAll();
254 theXmlCfg = aWdgReader.featureWidgetCfg(theFeatureId);
255 theDescription = aWdgReader.featureDescription(theFeatureId);