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>
36 ModuleBase_IModule::ModuleBase_IModule(ModuleBase_IWorkshop* theParent)
37 : QObject(theParent), myWorkshop(theParent)
39 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
42 //connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)), this,
43 // SLOT(onMousePressed(QMouseEvent*)));
44 //connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)), this,
45 // SLOT(onMouseReleased(QMouseEvent*)));
46 //connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)), this,
47 // SLOT(onMouseMoved(QMouseEvent*)));
48 //connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)), this,
49 // SLOT(onKeyRelease(QKeyEvent*)));
50 //connect(myWorkshop->viewer(), SIGNAL(mouseDoubleClick(QMouseEvent*)), this,
51 // SLOT(onMouseDoubleClick(QMouseEvent*)));
54 void ModuleBase_IModule::launchModal(const QString& theCmdId)
56 if (!myWorkshop->canStartOperation(theCmdId))
59 std::string aXmlCfg, aDescription;
60 getXMLRepresentation(theCmdId.toStdString(), aXmlCfg, aDescription);
62 SessionPtr aMgr = ModelAPI_Session::get();
63 aMgr->startOperation(theCmdId.toStdString());
65 ModuleBase_Dialog aDlg(myWorkshop, theCmdId, aXmlCfg);
66 if (aDlg.exec() == QDialog::Accepted)
67 aMgr->finishOperation();
69 aMgr->abortOperation();
70 myWorkshop->updateCommandStatus();
74 void ModuleBase_IModule::launchOperation(const QString& theCmdId)
76 if (!myWorkshop->canStartOperation(theCmdId))
79 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
80 (createOperation(theCmdId.toStdString()));
82 ModuleBase_ISelection* aSelection = myWorkshop->selection();
83 // Initialise operation with preliminary selection
84 aFOperation->initSelection(aSelection, myWorkshop->viewer());
85 sendOperation(aFOperation);
90 void ModuleBase_IModule::sendOperation(ModuleBase_Operation* theOperation)
92 static Events_ID aModuleEvent = Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED);
93 std::shared_ptr<Config_PointerMessage> aMessage =
94 std::shared_ptr<Config_PointerMessage>(new Config_PointerMessage(aModuleEvent, this));
95 aMessage->setPointer(theOperation);
96 Events_Loop::loop()->send(aMessage);
99 Handle(AIS_InteractiveObject) ModuleBase_IModule::createPresentation(const ResultPtr& theResult)
101 return Handle(AIS_InteractiveObject)();
104 bool ModuleBase_IModule::canBeShaded(Handle(AIS_InteractiveObject) theAIS) const
109 QString ModuleBase_IModule::getFeatureError(const FeaturePtr& theFeature)
111 QString aMsg = ModelAPI_Tools::getFeatureError(theFeature).c_str();
112 if (!aMsg.isEmpty()) {
113 std::string aStr = Config_Translator::translate(theFeature->getKind(), aMsg.toStdString());
114 std::string aCodec = Config_Translator::codec(theFeature->getKind());
115 aMsg = QTextCodec::codecForName(aCodec.c_str())->toUnicode(aStr.c_str());
120 void ModuleBase_IModule::grantedOperationIds(ModuleBase_Operation* theOperation,
121 QStringList& theIds) const
125 ModuleBase_Operation* ModuleBase_IModule::getNewOperation(const std::string& theFeatureId)
127 return new ModuleBase_OperationFeature(theFeatureId.c_str(), this);
130 bool ModuleBase_IModule::customizeObject(ObjectPtr theObject,
131 const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
132 const bool theUpdateViewer)
137 ModuleBase_Operation* ModuleBase_IModule::createOperation(const std::string& theFeatureId)
139 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
140 (getNewOperation(theFeatureId));
141 // If the operation is launched as sub-operation of another then we have to initialize
143 ModuleBase_OperationFeature* aCurOperation = dynamic_cast<ModuleBase_OperationFeature*>
144 (myWorkshop->currentOperation());
146 FeaturePtr aFeature = aCurOperation->feature();
147 CompositeFeaturePtr aCompFeature =
148 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
150 aFOperation->setParentFeature(aCompFeature);
154 std::string aXmlCfg, aDescription;
155 getXMLRepresentation(theFeatureId, aXmlCfg, aDescription);
156 aFOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
157 aFOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
162 void ModuleBase_IModule::createFeatures()
164 registerValidators();
166 registerProperties();
168 Config_ModuleReader aXMLReader = Config_ModuleReader();
169 aXMLReader.readAll();
170 myFeaturesInFiles = aXMLReader.featuresInFiles();
174 void ModuleBase_IModule::actionCreated(QAction* theFeature)
176 theFeature->setStatusTip(theFeature->text());
177 connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
180 bool ModuleBase_IModule::canEraseObject(const ObjectPtr& theObject) const
185 bool ModuleBase_IModule::canDisplayObject(const ObjectPtr& theObject) const
190 bool ModuleBase_IModule::canUndo() const
192 SessionPtr aMgr = ModelAPI_Session::get();
193 return aMgr->hasModuleDocument() && aMgr->canUndo() && !aMgr->isOperation();
196 bool ModuleBase_IModule::canRedo() const
198 SessionPtr aMgr = ModelAPI_Session::get();
199 return aMgr->hasModuleDocument() && aMgr->canRedo() && !aMgr->isOperation();
202 void ModuleBase_IModule::onFeatureTriggered()
204 QAction* aCmd = dynamic_cast<QAction*>(sender());
205 //Do nothing on uncheck
206 if (aCmd->isCheckable() && !aCmd->isChecked()) {
207 ModuleBase_Operation* anOperation = myWorkshop->findStartedOperation(aCmd->data().toString());
208 if (myWorkshop->canStopOperation(anOperation))
209 myWorkshop->abortOperation(anOperation);
211 aCmd->setChecked(true);
215 QString aCmdId = aCmd->data().toString();
216 std::shared_ptr<Config_FeatureMessage> aInfo = myWorkshop->featureInfo(aCmdId);
217 if (aInfo.get() && aInfo->isModal()) {
220 launchOperation(aCmdId);
221 emit operationLaunched();
226 void ModuleBase_IModule::editFeature(FeaturePtr theFeature)
228 std::string aFeatureId = theFeature->getKind();
229 if (!myWorkshop->canStartOperation(aFeatureId.c_str()))
232 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
233 (createOperation(aFeatureId));
235 aFOperation->setFeature(theFeature);
236 sendOperation(aFOperation);
240 bool ModuleBase_IModule::canActivateSelection(const ObjectPtr& theObject) const
242 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
243 (myWorkshop->currentOperation());
244 return !aFOperation || !aFOperation->hasObject(theObject);
247 void ModuleBase_IModule::operationResumed(ModuleBase_Operation* theOperation)
249 emit resumed(theOperation);
252 void ModuleBase_IModule::getXMLRepresentation(const std::string& theFeatureId,
253 std::string& theXmlCfg, std::string& theDescription)
255 std::string aPluginFileName = myFeaturesInFiles[theFeatureId];
256 Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
257 aWdgReader.readAll();
259 theXmlCfg = aWdgReader.featureWidgetCfg(theFeatureId);
260 theDescription = aWdgReader.featureDescription(theFeatureId);