1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "ModelAPI_IReentrant.h"
22 #include "ModelAPI_EventReentrantMessage.h"
24 #include "ModuleBase_IModule.h"
25 #include "ModuleBase_IViewer.h"
26 #include "ModuleBase_ViewerPrs.h"
27 #include "ModuleBase_Operation.h"
28 #include "ModuleBase_IPropertyPanel.h"
29 #include "ModuleBase_ISelection.h"
30 #include "ModuleBase_OperationDescription.h"
31 #include "ModuleBase_OperationFeature.h"
32 #include "ModuleBase_ModelWidget.h"
33 #include "ModuleBase_WidgetFactory.h"
34 #include "ModuleBase_PageWidget.h"
35 #include "ModuleBase_Dialog.h"
36 #include "ModuleBase_IErrorMgr.h"
38 #include <Events_Loop.h>
39 #include <Events_Message.h>
41 #include <ModelAPI_Events.h>
42 #include <ModelAPI_CompositeFeature.h>
43 #include <ModelAPI_Session.h>
44 #include "ModelAPI_Tools.h"
46 #include <Config_PointerMessage.h>
47 #include <Config_WidgetReader.h>
48 #include <Config_ModuleReader.h>
51 #include <QMainWindow>
54 #include <QDialogButtonBox>
55 #include <QPushButton>
57 ModuleBase_IModule::ModuleBase_IModule(ModuleBase_IWorkshop* theParent)
58 : QObject(theParent), myWorkshop(theParent)
60 connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
63 //connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)), this,
64 // SLOT(onMousePressed(QMouseEvent*)));
65 //connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)), this,
66 // SLOT(onMouseReleased(QMouseEvent*)));
67 //connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)), this,
68 // SLOT(onMouseMoved(QMouseEvent*)));
69 //connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)), this,
70 // SLOT(onKeyRelease(QKeyEvent*)));
71 //connect(myWorkshop->viewer(), SIGNAL(mouseDoubleClick(QMouseEvent*)), this,
72 // SLOT(onMouseDoubleClick(QMouseEvent*)));
75 void ModuleBase_IModule::launchModal(const QString& theCmdId)
78 if (!myWorkshop->canStartOperation(theCmdId, isCommitted))
81 std::string aXmlCfg, aDescription;
82 getXMLRepresentation(theCmdId.toStdString(), aXmlCfg, aDescription);
84 SessionPtr aMgr = ModelAPI_Session::get();
85 aMgr->startOperation(theCmdId.toStdString());
87 ModuleBase_Dialog aDlg(myWorkshop, theCmdId, aXmlCfg);
88 if (aDlg.exec() == QDialog::Accepted)
89 aMgr->finishOperation();
91 aMgr->abortOperation();
92 myWorkshop->updateCommandStatus();
96 void ModuleBase_IModule::launchOperation(const QString& theCmdId,
97 const bool& isStartAfterCommitOnly)
99 /// selection should be obtained from workshop before ask if the operation can be started as
100 /// the canStartOperation method performs commit/abort of previous operation.
101 /// Sometimes commit/abort may cause selection clear(Sketch operation) as a result
102 /// it will be lost and is not used for preselection.
103 ModuleBase_ISelection* aSelection = myWorkshop->selection();
104 QList<ModuleBase_ViewerPrsPtr> aPreSelected =
105 aSelection->getSelected(ModuleBase_ISelection::AllControls);
107 ModuleBase_OperationFeature* aCurOperation = dynamic_cast<ModuleBase_OperationFeature*>
108 (myWorkshop->currentOperation());
109 QString aCurOperationKind = aCurOperation ? aCurOperation->getDescription()->operationId() : "";
112 if (!myWorkshop->canStartOperation(theCmdId, isCommitted))
115 /// reentrant operation(Sketch Line) should not be started if operation is aborted
116 if (isStartAfterCommitOnly && !isCommitted)
119 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
120 (createOperation(theCmdId.toStdString()));
122 std::shared_ptr<Events_Message> aMessage = reentrantMessage();
123 if (aMessage.get()) {
124 setReentrantPreSelection(aMessage);
126 else if (canUsePreselection(aCurOperationKind, theCmdId)) {
127 // restore of previous opeation is absent or new launched operation has the same kind
128 aFOperation->initSelection(aPreSelected);
130 workshop()->processLaunchOperation(aFOperation);
133 FeaturePtr aFeature = aFOperation->feature();
134 ModelReentrantPtr aReentrantFeature =
135 std::dynamic_pointer_cast<ModelAPI_IReentrant>(aFeature);
136 if (aReentrantFeature.get()) {
137 if (aMessage.get()) {
138 ModuleBase_IPropertyPanel* aPanel = workshop()->propertyPanel();
139 std::string aPrevAttribute = aReentrantFeature->processEvent(aMessage);
140 if (!aPrevAttribute.empty()) {
141 workshop()->errorMgr()->updateActions(aFeature);
142 ModuleBase_ModelWidget* aPrevWidget = aPanel->modelWidget(aPrevAttribute);
143 aPanel->activateNextWidget(aPrevWidget);
151 Handle(AIS_InteractiveObject) ModuleBase_IModule::createPresentation(const ResultPtr& theResult)
153 return Handle(AIS_InteractiveObject)();
156 bool ModuleBase_IModule::canBeShaded(Handle(AIS_InteractiveObject) theAIS) const
161 QString ModuleBase_IModule::getFeatureError(const FeaturePtr& theFeature)
163 // Error already translated.
164 std::string aMsg = ModelAPI_Tools::getFeatureError(theFeature);
165 return QString::fromUtf8(aMsg.c_str());
168 void ModuleBase_IModule::grantedOperationIds(ModuleBase_Operation* theOperation,
169 QStringList& theIds) const
173 ModuleBase_Operation* ModuleBase_IModule::getNewOperation(const std::string& theFeatureId)
175 return new ModuleBase_OperationFeature(theFeatureId.c_str(), this);
178 bool ModuleBase_IModule::customizeObject(ObjectPtr theObject,
179 const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
180 const bool theUpdateViewer)
185 ModuleBase_Operation* ModuleBase_IModule::createOperation(const std::string& theFeatureId)
187 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
188 (getNewOperation(theFeatureId));
189 // If the operation is launched as sub-operation of another then we have to initialize
191 ModuleBase_OperationFeature* aCurOperation = dynamic_cast<ModuleBase_OperationFeature*>
192 (myWorkshop->currentOperation());
194 FeaturePtr aFeature = aCurOperation->feature();
195 CompositeFeaturePtr aCompFeature =
196 std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
198 aFOperation->setParentFeature(aCompFeature);
202 std::string aXmlCfg, aDescription;
203 getXMLRepresentation(theFeatureId, aXmlCfg, aDescription);
204 aFOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
205 aFOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
210 void ModuleBase_IModule::createFeatures()
212 registerValidators();
213 registerProperties();
215 Config_ModuleReader aXMLReader = Config_ModuleReader();
216 aXMLReader.readAll();
217 myFeaturesInFiles = aXMLReader.featuresInFiles();
221 void ModuleBase_IModule::actionCreated(QAction* theFeature)
223 connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
226 bool ModuleBase_IModule::canEraseObject(const ObjectPtr& theObject) const
231 bool ModuleBase_IModule::canDisplayObject(const ObjectPtr& theObject) const
236 bool ModuleBase_IModule::canUsePreselection(const QString& thePreviousOperationKind,
237 const QString& theStartedOperationKind)
239 // no previous operation
240 if (thePreviousOperationKind.isEmpty())
243 if (thePreviousOperationKind.endsWith(ModuleBase_OperationFeature::EditSuffix()))
246 // reentrant operation
247 if (thePreviousOperationKind == theStartedOperationKind)
253 bool ModuleBase_IModule::canUndo() const
255 SessionPtr aMgr = ModelAPI_Session::get();
256 return aMgr->hasModuleDocument() && aMgr->canUndo() && !aMgr->isOperation();
259 bool ModuleBase_IModule::canRedo() const
261 SessionPtr aMgr = ModelAPI_Session::get();
262 return aMgr->hasModuleDocument() && aMgr->canRedo() && !aMgr->isOperation();
265 void ModuleBase_IModule::onFeatureTriggered()
267 QAction* aCmd = dynamic_cast<QAction*>(sender());
268 //Do nothing on uncheck
269 if (aCmd->isCheckable() && !aCmd->isChecked()) {
270 ModuleBase_Operation* anOperation = myWorkshop->findStartedOperation(aCmd->data().toString());
271 if (myWorkshop->canStopOperation(anOperation)) {
273 myWorkshop->stopOperation(anOperation, isCommitted);
276 aCmd->setChecked(true);
280 QString aCmdId = aCmd->data().toString();
281 std::shared_ptr<Config_FeatureMessage> aInfo = myWorkshop->featureInfo(aCmdId);
282 if (aInfo.get() && aInfo->isModal()) {
285 launchOperation(aCmdId, false);
290 void ModuleBase_IModule::editFeature(FeaturePtr theFeature)
292 std::string aFeatureId = theFeature->getKind();
294 if (!myWorkshop->canStartOperation(aFeatureId.c_str(), isCommitted))
297 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
298 (createOperation(aFeatureId));
300 aFOperation->setFeature(theFeature);
301 workshop()->processLaunchOperation(aFOperation);
305 bool ModuleBase_IModule::canActivateSelection(const ObjectPtr& theObject) const
307 ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
308 (myWorkshop->currentOperation());
309 return !aFOperation || !aFOperation->hasObject(theObject);
312 void ModuleBase_IModule::operationResumed(ModuleBase_Operation* theOperation)
314 emit resumed(theOperation);
317 void ModuleBase_IModule::getXMLRepresentation(const std::string& theFeatureId,
318 std::string& theXmlCfg, std::string& theDescription)
320 std::string aPluginFileName = myFeaturesInFiles[theFeatureId];
321 Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
322 aWdgReader.readAll();
324 theXmlCfg = aWdgReader.featureWidgetCfg(theFeatureId);
325 theDescription = aWdgReader.featureDescription(theFeatureId);