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