]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_Module.cpp
Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / PartSet / PartSet_Module.cpp
1 #include <PartSet_Module.h>
2 #include <PartSet_OperationSketch.h>
3 #include <PartSet_OperationSketchLine.h>
4 #include <PartSet_OperationEditLine.h>
5 #include <ModuleBase_Operation.h>
6 #include <ModuleBase_OperationDescription.h>
7 #include <PartSet_Listener.h>
8
9 #include <ModuleBase_Operation.h>
10
11 #include <XGUI_MainWindow.h>
12 #include <XGUI_Displayer.h>
13 #include <XGUI_Viewer.h>
14 #include <XGUI_Workshop.h>
15 #include <XGUI_OperationMgr.h>
16 #include <XGUI_SelectionMgr.h>
17 #include <XGUI_ViewPort.h>
18 #include <XGUI_ActionsMgr.h>
19 #include <XGUI_ViewerProxy.h>
20
21 #include <Config_PointerMessage.h>
22 #include <Config_ModuleReader.h>
23 #include <Config_WidgetReader.h>
24 #include <Events_Loop.h>
25 #include <Events_Message.h>
26 #include <Events_Error.h>
27
28 #include <GeomAPI_Shape.h>
29
30 #include <AIS_ListOfInteractive.hxx>
31
32 #include <QObject>
33 #include <QMouseEvent>
34 #include <QString>
35
36 #ifdef _DEBUG
37 #include <QDebug>
38 #endif
39
40
41 /*!Create and return new instance of XGUI_Module*/
42 extern "C" PARTSET_EXPORT XGUI_Module* createModule(XGUI_Workshop* theWshop)
43 {
44   return new PartSet_Module(theWshop);
45 }
46
47 PartSet_Module::PartSet_Module(XGUI_Workshop* theWshop)
48 {
49   myWorkshop = theWshop;
50   myListener = new PartSet_Listener(this);
51
52   XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
53
54   connect(anOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
55           this, SLOT(onOperationStopped(ModuleBase_Operation*)));
56
57   connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)),
58           this, SLOT(onMousePressed(QMouseEvent*)));
59   connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)),
60           this, SLOT(onMouseReleased(QMouseEvent*)));
61   connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)),
62           this, SLOT(onMouseMoved(QMouseEvent*)));
63   connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)),
64           this, SLOT(onKeyRelease(QKeyEvent*)));
65 }
66
67 PartSet_Module::~PartSet_Module()
68 {
69 }
70
71 XGUI_Workshop* PartSet_Module::workshop() const
72 {
73   return myWorkshop;
74 }
75
76 void PartSet_Module::createFeatures()
77 {
78   Config_ModuleReader aXMLReader = Config_ModuleReader();
79   aXMLReader.readAll();
80   myFeaturesInFiles = aXMLReader.featuresInFiles();
81 }
82
83 void PartSet_Module::featureCreated(QAction* theFeature)
84 {
85   connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
86 }
87
88 QStringList PartSet_Module::nestedFeatures(QString)
89 {
90   return QStringList();
91 }
92
93 std::string PartSet_Module::featureFile(const std::string& theFeatureId)
94 {
95   return myFeaturesInFiles[theFeatureId];
96 }
97
98 /*
99  *
100  */
101 void PartSet_Module::onFeatureTriggered()
102 {
103   QAction* aCmd = dynamic_cast<QAction*>(sender());
104   //Do nothing on uncheck
105   if(aCmd->isCheckable() && !aCmd->isChecked())
106     return;
107   launchOperation(aCmd->data().toString());
108 }
109   
110 void PartSet_Module::launchOperation(const QString& theCmdId)
111 {
112   ModuleBase_Operation* anOperation = createOperation(theCmdId.toStdString());
113   sendOperation(anOperation);
114 }
115
116 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
117 {
118   if (!theOperation)
119     return;
120   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
121   if (aPreviewOp) {
122   }
123 }
124
125 void PartSet_Module::onMousePressed(QMouseEvent* theEvent)
126 {
127   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
128                                        myWorkshop->operationMgr()->currentOperation());
129   if (aPreviewOp)
130   {
131     std::list<XGUI_ViewerPrs> aPresentations = myWorkshop->displayer()->GetViewerPrs();
132     aPreviewOp->mousePressed(theEvent, myWorkshop->viewer()->activeView(), aPresentations);
133   }
134 }
135
136 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
137 {
138   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
139                                        myWorkshop->operationMgr()->currentOperation());
140   if (aPreviewOp)
141   {
142     std::list<XGUI_ViewerPrs> aPresentations = myWorkshop->displayer()->GetViewerPrs();
143     aPreviewOp->mouseReleased(theEvent, myWorkshop->viewer()->activeView(), aPresentations);
144   }
145 }
146
147 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
148 {
149   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
150                                        myWorkshop->operationMgr()->currentOperation());
151   if (aPreviewOp)
152     aPreviewOp->mouseMoved(theEvent, myWorkshop->viewer()->activeView());
153 }
154
155 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
156 {
157   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
158   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
159   if (aPreviewOp) {
160     aPreviewOp->keyReleased(theEvent->key());
161   }
162 }
163
164 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
165 {
166   myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
167   myWorkshop->actionsMgr()->setNestedActionsEnabled(true);
168 }
169
170 void PartSet_Module::onLaunchOperation(std::string theName, boost::shared_ptr<ModelAPI_Feature> theFeature)
171 {
172   ModuleBase_Operation* anOperation = createOperation(theName.c_str());
173   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
174   if (aPreviewOp)
175   {
176     std::list<XGUI_ViewerPrs> aPresentations = myWorkshop->displayer()->GetViewerPrs();
177     aPreviewOp->init(theFeature, aPresentations);
178   }
179   myWorkshop->actionsMgr()->setActionChecked(anOperation->getDescription()->operationId(), true);
180   sendOperation(anOperation);
181 }
182
183 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
184 {
185   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
186   aViewer->enableMultiselection(theEnabled);
187 }
188
189 void PartSet_Module::onStopSelection(const std::list<XGUI_ViewerPrs>& theFeatures, const bool isStop)
190 {
191   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
192   if (!isStop) {
193     std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
194     boost::shared_ptr<ModelAPI_Feature> aFeature;
195     for (; anIt != aLast; anIt++) {
196       activateFeature((*anIt).feature(), false);
197     }
198   }
199   aDisplayer->StopSelection(theFeatures, isStop, false);
200   aDisplayer->UpdateViewer();
201 }
202
203 void PartSet_Module::onSetSelection(const std::list<XGUI_ViewerPrs>& theFeatures)
204 {
205   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
206   aDisplayer->SetSelected(theFeatures, false);
207   aDisplayer->UpdateViewer();
208 }
209
210 void PartSet_Module::onCloseLocalContext()
211 {
212   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
213   aDisplayer->CloseLocalContexts();
214 }
215
216 void PartSet_Module::onFeatureConstructed(boost::shared_ptr<ModelAPI_Feature> theFeature,
217                                           int theMode)
218 {
219   bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
220   visualizePreview(theFeature, isDisplay, false);
221   if (!isDisplay) {
222     ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
223     boost::shared_ptr<ModelAPI_Feature> aSketch;
224     PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
225     if (aPrevOp) {
226       std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >
227                                                                          aList = aPrevOp->subPreview();
228       XGUI_Displayer* aDisplayer = myWorkshop->displayer();
229       std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
230
231       std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
232                                                              anIt = aList.begin(), aLast = aList.end();
233       for (; anIt != aLast; anIt++) {
234         boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).first;
235         visualizePreview(aFeature, false, false);
236       }
237       aDisplayer->UpdateViewer();
238     }
239   }
240
241   if (theMode == PartSet_OperationSketchBase::FM_Activation ||
242       theMode == PartSet_OperationSketchBase::FM_Deactivation)
243     activateFeature(theFeature, true);
244 }
245
246 ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId)
247 {
248   // get operation xml description
249   std::string aStdCmdId = theCmdId;
250   if (aStdCmdId == PartSet_OperationEditLine::Type())
251     aStdCmdId = PartSet_OperationSketchLine::Type();
252   std::string aPluginFileName = featureFile(aStdCmdId);
253   Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
254   aWdgReader.readAll();
255   std::string aXmlCfg = aWdgReader.featureWidgetCfg(aStdCmdId);
256   std::string aDescription = aWdgReader.featureDescription(aStdCmdId);
257
258   // create the operation
259   ModuleBase_Operation* anOperation;
260   if (theCmdId == PartSet_OperationSketch::Type()) {
261     anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
262   }
263   else if(theCmdId == PartSet_OperationSketchLine::Type() ||
264           theCmdId == PartSet_OperationEditLine::Type()) {
265     ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
266     boost::shared_ptr<ModelAPI_Feature> aSketch;
267     PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
268     if (aPrevOp)
269       aSketch = aPrevOp->sketch();
270     if (theCmdId == PartSet_OperationSketchLine::Type())
271       anOperation = new PartSet_OperationSketchLine(theCmdId.c_str(), this, aSketch);
272     else
273       anOperation = new PartSet_OperationEditLine(theCmdId.c_str(), this, aSketch);
274   }
275   else {
276     anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
277   }
278   anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
279   anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
280
281   // connect the operation
282   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
283   if (aPreviewOp) {
284     connect(aPreviewOp, SIGNAL(featureConstructed(boost::shared_ptr<ModelAPI_Feature>, int)),
285             this, SLOT(onFeatureConstructed(boost::shared_ptr<ModelAPI_Feature>, int)));
286     connect(aPreviewOp, SIGNAL(launchOperation(std::string, boost::shared_ptr<ModelAPI_Feature>)),
287             this, SLOT(onLaunchOperation(std::string, boost::shared_ptr<ModelAPI_Feature>)));
288     connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
289             this, SLOT(onMultiSelectionEnabled(bool)));
290
291     connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
292             this, SLOT(onMultiSelectionEnabled(bool)));
293     connect(aPreviewOp, SIGNAL(stopSelection(const std::list<XGUI_ViewerPrs>&, const bool)),
294             this, SLOT(onStopSelection(const std::list<XGUI_ViewerPrs>&, const bool)));
295     connect(aPreviewOp, SIGNAL(setSelection(const std::list<XGUI_ViewerPrs>&)),
296             this, SLOT(onSetSelection(const std::list<XGUI_ViewerPrs>&)));
297
298      connect(aPreviewOp, SIGNAL(closeLocalContext()),
299              this, SLOT(onCloseLocalContext()));
300
301     PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
302     if (aSketchOp) {
303       connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
304               this, SLOT(onPlaneSelected(double, double, double)));
305     }
306   }
307
308   return anOperation;
309 }
310
311 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
312 {
313   //TODO(sbh): Implement static method to extract event id [SEID]
314   static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetModuleEvent");
315   Config_PointerMessage aMessage(aModuleEvent, this);
316   aMessage.setPointer(theOperation);
317   Events_Loop::loop()->send(aMessage);
318 }
319
320 void PartSet_Module::visualizePreview(boost::shared_ptr<ModelAPI_Feature> theFeature, bool isDisplay,
321                                       const bool isUpdateViewer)
322 {
323   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
324   if (!anOperation)
325     return;
326
327   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
328   if (!aPreviewOp)
329     return;
330
331   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
332   if (isDisplay) {
333     boost::shared_ptr<GeomAPI_Shape> aPreview = aPreviewOp->preview(theFeature);
334     aDisplayer->Redisplay(theFeature,
335                           aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(), false);
336   }
337   else
338     aDisplayer->Erase(theFeature, false);
339
340   if (isUpdateViewer)
341     aDisplayer->UpdateViewer();
342 }
343
344 void PartSet_Module::activateFeature(boost::shared_ptr<ModelAPI_Feature> theFeature,
345                                      const bool isUpdateViewer)
346 {
347   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
348   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
349   if (aPreviewOp) {
350     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
351     aDisplayer->ActivateInLocalContext(theFeature, aPreviewOp->getSelectionModes(theFeature),
352                                        isUpdateViewer);
353   }
354 }
355
356 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
357 {
358   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
359   if (!anOperation)
360     return;
361
362   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
363   if (!aPreviewOp)
364     return;
365
366   boost::shared_ptr<ModelAPI_Feature> aFeature = aPreviewOp->feature();
367   if (!aFeature || aFeature->getKind() != theCmdId)
368     return;
369
370   std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >
371                                                                      aList = aPreviewOp->subPreview();
372   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
373   std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
374
375   std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
376                                                          anIt = aList.begin(), aLast = aList.end();
377   for (; anIt != aLast; anIt++) {
378     boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).first;
379     boost::shared_ptr<GeomAPI_Shape> aPreview = (*anIt).second;
380     aDisplayer->Redisplay(aFeature,
381                           aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(), false);
382     aDisplayer->ActivateInLocalContext(aFeature, aModes, false);
383   }
384   aDisplayer->UpdateViewer();
385 }
386