]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_Module.cpp
Salome HOME
Merge remote-tracking branch 'remotes/origin/master' into SolveSpace
[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::onContextMenuCommand(const QString& theId, bool isChecked)
126 {
127   QFeatureList aFeatures = myWorkshop->selector()->selectedFeatures();
128   if (theId == "EDIT_CMD" && (aFeatures.size() > 0)) {
129     editFeature(aFeatures.first());
130   }
131 }
132
133 void PartSet_Module::onMousePressed(QMouseEvent* theEvent)
134 {
135   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
136                                        myWorkshop->operationMgr()->currentOperation());
137   if (aPreviewOp)
138   {
139     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
140     std::list<XGUI_ViewerPrs> aSelected = aDisplayer->GetSelected();
141     std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->GetHighlighted();
142
143     aPreviewOp->mousePressed(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
144   }
145 }
146
147 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
148 {
149   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
150                                        myWorkshop->operationMgr()->currentOperation());
151   if (aPreviewOp)
152   {
153     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
154     std::list<XGUI_ViewerPrs> aSelected = aDisplayer->GetSelected();
155     std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->GetHighlighted();
156
157     aPreviewOp->mouseReleased(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
158   }
159 }
160
161 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
162 {
163   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
164                                        myWorkshop->operationMgr()->currentOperation());
165   if (aPreviewOp)
166     aPreviewOp->mouseMoved(theEvent, myWorkshop->viewer()->activeView());
167 }
168
169 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
170 {
171   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
172   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
173   if (aPreviewOp) {
174     aPreviewOp->keyReleased(theEvent->key());
175   }
176 }
177
178 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
179 {
180   myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
181   myWorkshop->actionsMgr()->update();
182 }
183
184 void PartSet_Module::onLaunchOperation(std::string theName, boost::shared_ptr<ModelAPI_Feature> theFeature)
185 {
186   ModuleBase_Operation* anOperation = createOperation(theName.c_str());
187   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
188   if (aPreviewOp)
189   {
190     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
191       // refill the features list with avoiding of the features, obtained only by vertex shape (TODO)
192     std::list<XGUI_ViewerPrs> aSelected = aDisplayer->GetSelected(TopAbs_VERTEX);
193     std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->GetHighlighted(TopAbs_VERTEX);
194     aPreviewOp->init(theFeature, aSelected, aHighlighted);
195   }
196   myWorkshop->actionsMgr()->updateCheckState();
197   sendOperation(anOperation);
198 }
199
200 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
201 {
202   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
203   aViewer->enableMultiselection(theEnabled);
204 }
205
206 void PartSet_Module::onStopSelection(const std::list<XGUI_ViewerPrs>& theFeatures, const bool isStop)
207 {
208   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
209   if (!isStop) {
210     std::list<XGUI_ViewerPrs>::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
211     boost::shared_ptr<ModelAPI_Feature> aFeature;
212     for (; anIt != aLast; anIt++) {
213       activateFeature((*anIt).feature(), false);
214     }
215   }
216   aDisplayer->StopSelection(theFeatures, isStop, false);
217
218   XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
219   aViewer->enableSelection(!isStop);
220
221   aDisplayer->UpdateViewer();
222 }
223
224 void PartSet_Module::onSetSelection(const std::list<XGUI_ViewerPrs>& theFeatures)
225 {
226   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
227   aDisplayer->SetSelected(theFeatures, false);
228   aDisplayer->UpdateViewer();
229 }
230
231 void PartSet_Module::onCloseLocalContext()
232 {
233   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
234   aDisplayer->CloseLocalContexts();
235 }
236
237 void PartSet_Module::onFeatureConstructed(boost::shared_ptr<ModelAPI_Feature> theFeature,
238                                           int theMode)
239 {
240   bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
241   visualizePreview(theFeature, isDisplay, false);
242   if (!isDisplay) {
243     ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
244     boost::shared_ptr<ModelAPI_Feature> aSketch;
245     PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
246     if (aPrevOp) {
247       std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >
248                                                                          aList = aPrevOp->subPreview();
249       XGUI_Displayer* aDisplayer = myWorkshop->displayer();
250       std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
251
252       std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
253                                                              anIt = aList.begin(), aLast = aList.end();
254       for (; anIt != aLast; anIt++) {
255         boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).first;
256         visualizePreview(aFeature, false, false);
257       }
258       aDisplayer->UpdateViewer();
259     }
260   }
261
262   if (theMode == PartSet_OperationSketchBase::FM_Activation ||
263       theMode == PartSet_OperationSketchBase::FM_Deactivation)
264     activateFeature(theFeature, true);
265 }
266
267 ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId)
268 {
269   // get operation xml description
270   std::string aStdCmdId = theCmdId;
271   if (aStdCmdId == PartSet_OperationEditLine::Type())
272     aStdCmdId = PartSet_OperationSketchLine::Type();
273   std::string aPluginFileName = featureFile(aStdCmdId);
274   Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
275   aWdgReader.readAll();
276   std::string aXmlCfg = aWdgReader.featureWidgetCfg(aStdCmdId);
277   std::string aDescription = aWdgReader.featureDescription(aStdCmdId);
278
279   // create the operation
280   ModuleBase_Operation* anOperation;
281   if (theCmdId == PartSet_OperationSketch::Type()) {
282     anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
283   }
284   else if(theCmdId == PartSet_OperationSketchLine::Type() ||
285           theCmdId == PartSet_OperationEditLine::Type()) {
286     ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
287     boost::shared_ptr<ModelAPI_Feature> aSketch;
288     PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
289     if (aPrevOp)
290       aSketch = aPrevOp->sketch();
291     if (theCmdId == PartSet_OperationSketchLine::Type())
292       anOperation = new PartSet_OperationSketchLine(theCmdId.c_str(), this, aSketch);
293     else
294       anOperation = new PartSet_OperationEditLine(theCmdId.c_str(), this, aSketch);
295   }
296   else {
297     anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
298   }
299   anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
300   anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
301
302   // connect the operation
303   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
304   if (aPreviewOp) {
305     connect(aPreviewOp, SIGNAL(featureConstructed(boost::shared_ptr<ModelAPI_Feature>, int)),
306             this, SLOT(onFeatureConstructed(boost::shared_ptr<ModelAPI_Feature>, int)));
307     connect(aPreviewOp, SIGNAL(launchOperation(std::string, boost::shared_ptr<ModelAPI_Feature>)),
308             this, SLOT(onLaunchOperation(std::string, boost::shared_ptr<ModelAPI_Feature>)));
309     connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
310             this, SLOT(onMultiSelectionEnabled(bool)));
311
312     connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
313             this, SLOT(onMultiSelectionEnabled(bool)));
314     connect(aPreviewOp, SIGNAL(stopSelection(const std::list<XGUI_ViewerPrs>&, const bool)),
315             this, SLOT(onStopSelection(const std::list<XGUI_ViewerPrs>&, const bool)));
316     connect(aPreviewOp, SIGNAL(setSelection(const std::list<XGUI_ViewerPrs>&)),
317             this, SLOT(onSetSelection(const std::list<XGUI_ViewerPrs>&)));
318
319      connect(aPreviewOp, SIGNAL(closeLocalContext()),
320              this, SLOT(onCloseLocalContext()));
321
322     PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
323     if (aSketchOp) {
324       connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
325               this, SLOT(onPlaneSelected(double, double, double)));
326     }
327   }
328
329   return anOperation;
330 }
331
332 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
333 {
334   //TODO(sbh): Implement static method to extract event id [SEID]
335   static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetModuleEvent");
336   Config_PointerMessage aMessage(aModuleEvent, this);
337   aMessage.setPointer(theOperation);
338   Events_Loop::loop()->send(aMessage);
339 }
340
341 void PartSet_Module::visualizePreview(boost::shared_ptr<ModelAPI_Feature> theFeature, bool isDisplay,
342                                       const bool isUpdateViewer)
343 {
344   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
345   if (!anOperation)
346     return;
347
348   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
349   if (!aPreviewOp)
350     return;
351
352   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
353   if (isDisplay) {
354     boost::shared_ptr<GeomAPI_Shape> aPreview = aPreviewOp->preview(theFeature);
355     aDisplayer->Redisplay(theFeature,
356                           aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(), false);
357   }
358   else
359     aDisplayer->Erase(theFeature, false);
360
361   if (isUpdateViewer)
362     aDisplayer->UpdateViewer();
363 }
364
365 void PartSet_Module::activateFeature(boost::shared_ptr<ModelAPI_Feature> theFeature,
366                                      const bool isUpdateViewer)
367 {
368   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
369   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
370   if (aPreviewOp) {
371     XGUI_Displayer* aDisplayer = myWorkshop->displayer();
372     aDisplayer->ActivateInLocalContext(theFeature, aPreviewOp->getSelectionModes(theFeature),
373                                        isUpdateViewer);
374   }
375 }
376
377 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
378 {
379   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
380   if (!anOperation)
381     return;
382
383   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
384   if (!aPreviewOp)
385     return;
386
387   boost::shared_ptr<ModelAPI_Feature> aFeature = aPreviewOp->feature();
388   if (!aFeature || aFeature->getKind() != theCmdId)
389     return;
390
391   std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >
392                                                                      aList = aPreviewOp->subPreview();
393   XGUI_Displayer* aDisplayer = myWorkshop->displayer();
394   std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
395
396   std::map<boost::shared_ptr<ModelAPI_Feature>, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
397                                                          anIt = aList.begin(), aLast = aList.end();
398   for (; anIt != aLast; anIt++) {
399     boost::shared_ptr<ModelAPI_Feature> aFeature = (*anIt).first;
400     boost::shared_ptr<GeomAPI_Shape> aPreview = (*anIt).second;
401     aDisplayer->Redisplay(aFeature,
402                           aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(), false);
403     aDisplayer->ActivateInLocalContext(aFeature, aModes, false);
404   }
405   aDisplayer->UpdateViewer();
406 }
407
408 void PartSet_Module::editFeature(FeaturePtr theFeature)
409 {
410   /*if (!theFeature)
411     return;
412
413   if (theFeature->getKind() == "Sketch") {
414     onLaunchOperation(theFeature->getKind(), theFeature);
415     visualizePreview(theFeature, true);
416   }*/
417 }