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