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