Salome HOME
Issue #1477 Build Vertex - wrong selection in viewer
[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     sendOperation(aFOperation);
84   }
85 }
86
87
88 void ModuleBase_IModule::sendOperation(ModuleBase_Operation* theOperation)
89 {
90   static Events_ID aModuleEvent = Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED);
91   std::shared_ptr<Config_PointerMessage> aMessage =
92       std::shared_ptr<Config_PointerMessage>(new Config_PointerMessage(aModuleEvent, this));
93   aMessage->setPointer(theOperation);
94   Events_Loop::loop()->send(aMessage);
95 }
96
97 Handle(AIS_InteractiveObject) ModuleBase_IModule::createPresentation(const ResultPtr& theResult)
98 {
99   return Handle(AIS_InteractiveObject)();
100 }
101
102 bool ModuleBase_IModule::canBeShaded(Handle(AIS_InteractiveObject) theAIS) const
103 {
104   return true;
105 }
106
107 QString ModuleBase_IModule::getFeatureError(const FeaturePtr& theFeature)
108 {
109   return ModelAPI_Tools::getFeatureError(theFeature).c_str();
110 }
111
112 void ModuleBase_IModule::grantedOperationIds(ModuleBase_Operation* theOperation,
113                                              QStringList& theIds) const
114 {
115 }
116
117 ModuleBase_Operation* ModuleBase_IModule::getNewOperation(const std::string& theFeatureId)
118 {
119   return new ModuleBase_OperationFeature(theFeatureId.c_str(), this);
120 }
121
122 bool ModuleBase_IModule::customizeObject(ObjectPtr theObject,
123                                          const ModuleBase_IModule::ModuleBase_CustomizeFlag& theFlag,
124                                          const bool theUpdateViewer)
125 {
126   return false;
127 }
128
129 ModuleBase_Operation* ModuleBase_IModule::createOperation(const std::string& theFeatureId)
130 {
131   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
132                                                           (getNewOperation(theFeatureId));
133   // If the operation is launched as sub-operation of another then we have to initialize
134   // parent feature
135   ModuleBase_OperationFeature* aCurOperation = dynamic_cast<ModuleBase_OperationFeature*>
136                                                          (myWorkshop->currentOperation());
137   if (aCurOperation) {
138     FeaturePtr aFeature = aCurOperation->feature();
139     CompositeFeaturePtr aCompFeature =
140         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aFeature);
141     if (aCompFeature) {
142       aFOperation->setParentFeature(aCompFeature);
143     }
144   }
145
146   std::string aXmlCfg, aDescription;
147   getXMLRepresentation(theFeatureId, aXmlCfg, aDescription);
148   aFOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
149   aFOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
150
151   return aFOperation;
152 }
153
154 void ModuleBase_IModule::createFeatures()
155 {
156   registerValidators();
157   registerFilters();
158   registerProperties();
159
160   Config_ModuleReader aXMLReader = Config_ModuleReader();
161   aXMLReader.readAll();
162   myFeaturesInFiles = aXMLReader.featuresInFiles();
163 }
164
165
166 void ModuleBase_IModule::actionCreated(QAction* theFeature)
167 {
168   theFeature->setStatusTip(theFeature->text());
169   connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
170 }
171
172 bool ModuleBase_IModule::canEraseObject(const ObjectPtr& theObject) const
173 {
174   return true;
175 }
176
177 bool ModuleBase_IModule::canDisplayObject(const ObjectPtr& theObject) const
178 {
179   return true;
180 }
181
182 bool ModuleBase_IModule::canUndo() const
183 {
184   SessionPtr aMgr = ModelAPI_Session::get();
185   return aMgr->hasModuleDocument() && aMgr->canUndo() && !aMgr->isOperation();
186 }
187
188 bool ModuleBase_IModule::canRedo() const
189 {
190   SessionPtr aMgr = ModelAPI_Session::get();
191   return aMgr->hasModuleDocument() && aMgr->canRedo() && !aMgr->isOperation();
192 }
193
194 void ModuleBase_IModule::onFeatureTriggered()
195 {
196   QAction* aCmd = dynamic_cast<QAction*>(sender());
197   //Do nothing on uncheck
198   if (aCmd->isCheckable() && !aCmd->isChecked()) {
199     ModuleBase_Operation* anOperation = myWorkshop->findStartedOperation(aCmd->data().toString());
200     if (myWorkshop->canStopOperation(anOperation))
201       myWorkshop->abortOperation(anOperation);
202     else {
203       aCmd->setChecked(true);
204     }
205   }
206   else {
207     QString aCmdId = aCmd->data().toString();
208     std::shared_ptr<Config_FeatureMessage> aInfo = myWorkshop->featureInfo(aCmdId);
209     if (aInfo.get() && aInfo->isModal()) {
210       launchModal(aCmdId);
211     } else {
212       launchOperation(aCmdId);
213       emit operationLaunched();
214     }
215   }
216 }
217
218 void ModuleBase_IModule::editFeature(FeaturePtr theFeature)
219 {
220   std::string aFeatureId = theFeature->getKind();
221   if (!myWorkshop->canStartOperation(aFeatureId.c_str()))
222     return;
223
224   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
225                                                          (createOperation(aFeatureId));
226   if (aFOperation) {
227     aFOperation->setFeature(theFeature);
228     sendOperation(aFOperation);
229   }
230 }
231
232 bool ModuleBase_IModule::canActivateSelection(const ObjectPtr& theObject) const
233 {
234   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
235                                                      (myWorkshop->currentOperation());
236   return !aFOperation || !aFOperation->hasObject(theObject);
237 }
238
239 void ModuleBase_IModule::operationResumed(ModuleBase_Operation* theOperation) 
240 {
241   emit resumed(theOperation);
242 }
243
244 void ModuleBase_IModule::getXMLRepresentation(const std::string& theFeatureId,
245                                               std::string& theXmlCfg, std::string& theDescription)
246 {
247   std::string aPluginFileName = myFeaturesInFiles[theFeatureId];
248   Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
249   aWdgReader.readAll();
250
251   theXmlCfg = aWdgReader.featureWidgetCfg(theFeatureId);
252   theDescription = aWdgReader.featureDescription(theFeatureId);
253 }