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