]> SALOME platform Git repositories - modules/shaper.git/blob - src/ModuleBase/ModuleBase_IModule.cpp
Salome HOME
Preparations for Split operation. It includes:
[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 #include "ModuleBase_OperationFeature.h"
11 #include "ModuleBase_ModelWidget.h"
12 #include "ModuleBase_WidgetFactory.h"
13 #include "ModuleBase_PageWidget.h"
14 #include "ModuleBase_Dialog.h"
15
16 #include <Events_Loop.h>
17
18 #include <ModelAPI_Events.h>
19 #include <ModelAPI_CompositeFeature.h>
20 #include <ModelAPI_Session.h>
21 #include "ModelAPI_Tools.h"
22
23 #include <Config_PointerMessage.h>
24 #include <Config_WidgetReader.h>
25 #include <Config_ModuleReader.h>
26
27 #include <QAction>
28 #include <QMainWindow>
29 #include <QDialog>
30 #include <QLayout>
31 #include <QDialogButtonBox>
32 #include <QPushButton>
33
34 ModuleBase_IModule::ModuleBase_IModule(ModuleBase_IWorkshop* theParent)
35   : QObject(theParent), myWorkshop(theParent) 
36 {
37   connect(myWorkshop, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
38
39
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*)));
50 }
51
52 void ModuleBase_IModule::launchModal(const QString& theCmdId)
53 {
54   if (!myWorkshop->canStartOperation(theCmdId))
55     return;
56
57   std::string aXmlCfg, aDescription;
58   getXMLRepresentation(theCmdId.toStdString(), aXmlCfg, aDescription);
59
60   SessionPtr aMgr = ModelAPI_Session::get();
61   aMgr->startOperation(theCmdId.toStdString());
62
63   ModuleBase_Dialog aDlg(myWorkshop, theCmdId, aXmlCfg);
64   if (aDlg.exec() == QDialog::Accepted)
65     aMgr->finishOperation();
66   else
67     aMgr->abortOperation();
68   myWorkshop->updateCommandStatus();
69 }
70
71
72 void ModuleBase_IModule::launchOperation(const QString& theCmdId)
73 {
74   if (!myWorkshop->canStartOperation(theCmdId))
75     return;
76
77   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
78                                              (createOperation(theCmdId.toStdString()));
79   if (aFOperation) {
80     ModuleBase_ISelection* aSelection = myWorkshop->selection();
81     // Initialise operation with preliminary selection
82     aFOperation->initSelection(aSelection, myWorkshop->viewer());
83     workshop()->processLaunchOperation(aFOperation);
84   }
85 }
86
87 Handle(AIS_InteractiveObject) ModuleBase_IModule::createPresentation(const ResultPtr& theResult)
88 {
89   return Handle(AIS_InteractiveObject)();
90 }
91
92 bool ModuleBase_IModule::canBeShaded(Handle(AIS_InteractiveObject) theAIS) const
93 {
94   return true;
95 }
96
97 QString ModuleBase_IModule::getFeatureError(const FeaturePtr& theFeature)
98 {
99   std::string aMsg = ModelAPI_Tools::getFeatureError(theFeature);
100   ModuleBase_Tools::translate(theFeature->getKind(), aMsg);
101
102   return aMsg.c_str();
103 }
104
105 void ModuleBase_IModule::grantedOperationIds(ModuleBase_Operation* theOperation,
106                                              QStringList& theIds) const
107 {
108 }
109
110 ModuleBase_Operation* ModuleBase_IModule::getNewOperation(const std::string& theFeatureId)
111 {
112   return new ModuleBase_OperationFeature(theFeatureId.c_str(), this);
113 }
114
115 bool ModuleBase_IModule::customizeObject(ObjectPtr theObject,
116                                          const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
117                                          const bool theUpdateViewer)
118 {
119   return false;
120 }
121
122 ModuleBase_Operation* ModuleBase_IModule::createOperation(const std::string& theFeatureId)
123 {
124   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
125                                                           (getNewOperation(theFeatureId));
126   // If the operation is launched as sub-operation of another then we have to initialize
127   // parent feature
128   ModuleBase_OperationFeature* aCurOperation = dynamic_cast<ModuleBase_OperationFeature*>
129                                                          (myWorkshop->currentOperation());
130   if (aCurOperation) {
131     FeaturePtr aFeature = aCurOperation->feature();
132     CompositeFeaturePtr aCompFeature =
133         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
134     if (aCompFeature) {
135       aFOperation->setParentFeature(aCompFeature);
136     }
137   }
138
139   std::string aXmlCfg, aDescription;
140   getXMLRepresentation(theFeatureId, aXmlCfg, aDescription);
141   aFOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
142   aFOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
143
144   return aFOperation;
145 }
146
147 void ModuleBase_IModule::createFeatures()
148 {
149   registerValidators();
150   registerFilters();
151   registerProperties();
152
153   Config_ModuleReader aXMLReader = Config_ModuleReader();
154   aXMLReader.readAll();
155   myFeaturesInFiles = aXMLReader.featuresInFiles();
156 }
157
158
159 void ModuleBase_IModule::actionCreated(QAction* theFeature)
160 {
161   connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
162 }
163
164 bool ModuleBase_IModule::canEraseObject(const ObjectPtr& theObject) const
165 {
166   return true;
167 }
168
169 bool ModuleBase_IModule::canDisplayObject(const ObjectPtr& theObject) const
170 {
171   return true;
172 }
173
174 bool ModuleBase_IModule::canUndo() const
175 {
176   SessionPtr aMgr = ModelAPI_Session::get();
177   return aMgr->hasModuleDocument() && aMgr->canUndo() && !aMgr->isOperation();
178 }
179
180 bool ModuleBase_IModule::canRedo() const
181 {
182   SessionPtr aMgr = ModelAPI_Session::get();
183   return aMgr->hasModuleDocument() && aMgr->canRedo() && !aMgr->isOperation();
184 }
185
186 void ModuleBase_IModule::onFeatureTriggered()
187 {
188   QAction* aCmd = dynamic_cast<QAction*>(sender());
189   //Do nothing on uncheck
190   if (aCmd->isCheckable() && !aCmd->isChecked()) {
191     ModuleBase_Operation* anOperation = myWorkshop->findStartedOperation(aCmd->data().toString());
192     if (myWorkshop->canStopOperation(anOperation))
193       myWorkshop->stopOperation(anOperation);
194     else {
195       aCmd->setChecked(true);
196     }
197   }
198   else {
199     QString aCmdId = aCmd->data().toString();
200     std::shared_ptr<Config_FeatureMessage> aInfo = myWorkshop->featureInfo(aCmdId);
201     if (aInfo.get() && aInfo->isModal()) {
202       launchModal(aCmdId);
203     } else {
204       launchOperation(aCmdId);
205       emit operationLaunched();
206     }
207   }
208 }
209
210 void ModuleBase_IModule::editFeature(FeaturePtr theFeature)
211 {
212   std::string aFeatureId = theFeature->getKind();
213   if (!myWorkshop->canStartOperation(aFeatureId.c_str()))
214     return;
215
216   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
217                                                          (createOperation(aFeatureId));
218   if (aFOperation) {
219     aFOperation->setFeature(theFeature);
220     workshop()->processLaunchOperation(aFOperation);
221   }
222 }
223
224 bool ModuleBase_IModule::canActivateSelection(const ObjectPtr& theObject) const
225 {
226   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
227                                                      (myWorkshop->currentOperation());
228   return !aFOperation || !aFOperation->hasObject(theObject);
229 }
230
231 void ModuleBase_IModule::operationResumed(ModuleBase_Operation* theOperation) 
232 {
233   emit resumed(theOperation);
234 }
235
236 void ModuleBase_IModule::getXMLRepresentation(const std::string& theFeatureId,
237                                               std::string& theXmlCfg, std::string& theDescription)
238 {
239   std::string aPluginFileName = myFeaturesInFiles[theFeatureId];
240   Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
241   aWdgReader.readAll();
242
243   theXmlCfg = aWdgReader.featureWidgetCfg(theFeatureId);
244   theDescription = aWdgReader.featureDescription(theFeatureId);
245 }