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