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>
9 #include <ModuleBase_Operation.h>
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>
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>
30 #include <GeomAPI_Shape.h>
32 #include <AIS_ListOfInteractive.hxx>
35 #include <QMouseEvent>
43 /*!Create and return new instance of XGUI_Module*/
44 extern "C" PARTSET_EXPORT XGUI_Module* createModule(XGUI_Workshop* theWshop)
46 return new PartSet_Module(theWshop);
49 PartSet_Module::PartSet_Module(XGUI_Workshop* theWshop)
51 myWorkshop = theWshop;
52 myListener = new PartSet_Listener(this);
54 XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
56 connect(anOperationMgr, SIGNAL(operationStarted()),
57 this, SLOT(onOperationStarted()));
59 connect(anOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
60 this, SLOT(onOperationStopped(ModuleBase_Operation*)));
62 XGUI_ContextMenuMgr* aContextMenuMgr = myWorkshop->contextMenuMgr();
63 connect(aContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)),
64 this, SLOT(onContextMenuCommand(const QString&, bool)));
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*)));
76 PartSet_Module::~PartSet_Module()
80 XGUI_Workshop* PartSet_Module::workshop() const
85 void PartSet_Module::createFeatures()
87 Config_ModuleReader aXMLReader = Config_ModuleReader();
89 myFeaturesInFiles = aXMLReader.featuresInFiles();
92 void PartSet_Module::featureCreated(QAction* theFeature)
94 connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
97 QStringList PartSet_Module::nestedFeatures(QString)
102 std::string PartSet_Module::featureFile(const std::string& theFeatureId)
104 return myFeaturesInFiles[theFeatureId];
110 void PartSet_Module::onFeatureTriggered()
112 QAction* aCmd = dynamic_cast<QAction*>(sender());
113 //Do nothing on uncheck
114 if(aCmd->isCheckable() && !aCmd->isChecked())
116 launchOperation(aCmd->data().toString());
119 void PartSet_Module::launchOperation(const QString& theCmdId)
121 ModuleBase_Operation* anOperation = createOperation(theCmdId.toStdString());
122 sendOperation(anOperation);
125 void PartSet_Module::onOperationStarted()
127 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
128 myWorkshop->operationMgr()->currentOperation());
130 XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
131 connect(aPreviewOp, SIGNAL(focusActivated(const std::string&)),
132 aPropPanel, SLOT(onFocusActivated(const std::string&)));
136 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
140 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
142 XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
143 disconnect(aPreviewOp, SIGNAL(focusActivated(const std::string&)),
144 aPropPanel, SLOT(onFocusActivated(const std::string&)));
148 void PartSet_Module::onContextMenuCommand(const QString& theId, bool isChecked)
150 QFeatureList aFeatures = myWorkshop->selector()->selectedFeatures();
151 if (theId == "EDIT_CMD" && (aFeatures.size() > 0)) {
152 editFeature(aFeatures.first());
156 void PartSet_Module::onMousePressed(QMouseEvent* theEvent)
158 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
159 myWorkshop->operationMgr()->currentOperation());
162 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
163 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->GetSelected();
164 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->GetHighlighted();
166 aPreviewOp->mousePressed(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
170 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
172 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
173 myWorkshop->operationMgr()->currentOperation());
176 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
177 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->GetSelected();
178 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->GetHighlighted();
180 aPreviewOp->mouseReleased(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
184 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
186 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
187 myWorkshop->operationMgr()->currentOperation());
189 aPreviewOp->mouseMoved(theEvent, myWorkshop->viewer()->activeView());
192 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
194 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
195 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
197 aPreviewOp->keyReleased(theEvent->key());
201 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
203 myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
204 myWorkshop->actionsMgr()->update();
207 void PartSet_Module::onLaunchOperation(std::string theName, boost::shared_ptr<ModelAPI_Feature> theFeature)
209 ModuleBase_Operation* anOperation = createOperation(theName.c_str());
210 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
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);
219 sendOperation(anOperation);
220 myWorkshop->actionsMgr()->updateCheckState();
223 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
225 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
226 aViewer->enableMultiselection(theEnabled);
229 void PartSet_Module::onStopSelection(const std::list<XGUI_ViewerPrs>& theFeatures, const bool isStop)
231 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
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);
239 aDisplayer->StopSelection(theFeatures, isStop, false);
241 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
242 aViewer->enableSelection(!isStop);
244 aDisplayer->UpdateViewer();
247 void PartSet_Module::onSetSelection(const std::list<XGUI_ViewerPrs>& theFeatures)
249 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
250 aDisplayer->SetSelected(theFeatures, false);
251 aDisplayer->UpdateViewer();
254 void PartSet_Module::onCloseLocalContext()
256 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
257 aDisplayer->CloseLocalContexts();
260 void PartSet_Module::onFeatureConstructed(boost::shared_ptr<ModelAPI_Feature> theFeature,
263 bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
264 visualizePreview(theFeature, isDisplay, false);
266 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
267 boost::shared_ptr<ModelAPI_Feature> aSketch;
268 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
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());
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);
281 aDisplayer->UpdateViewer();
285 if (theMode == PartSet_OperationSketchBase::FM_Activation ||
286 theMode == PartSet_OperationSketchBase::FM_Deactivation)
287 activateFeature(theFeature, true);
290 ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId)
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);
302 // create the operation
303 ModuleBase_Operation* anOperation;
304 if (theCmdId == PartSet_OperationSketch::Type()) {
305 anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
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);
313 aSketch = aPrevOp->sketch();
314 if (theCmdId == PartSet_OperationSketchLine::Type())
315 anOperation = new PartSet_OperationSketchLine(theCmdId.c_str(), this, aSketch);
317 anOperation = new PartSet_OperationEditLine(theCmdId.c_str(), this, aSketch);
320 anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
322 anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
323 anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
325 // connect the operation
326 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
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)));
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>&)));
342 connect(aPreviewOp, SIGNAL(closeLocalContext()),
343 this, SLOT(onCloseLocalContext()));
345 PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
347 connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
348 this, SLOT(onPlaneSelected(double, double, double)));
355 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
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);
364 void PartSet_Module::visualizePreview(boost::shared_ptr<ModelAPI_Feature> theFeature, bool isDisplay,
365 const bool isUpdateViewer)
367 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
371 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
375 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
377 boost::shared_ptr<GeomAPI_Shape> aPreview = aPreviewOp->preview(theFeature);
378 aDisplayer->Redisplay(theFeature,
379 aPreview ? aPreview->impl<TopoDS_Shape>() : TopoDS_Shape(), false);
382 aDisplayer->Erase(theFeature, false);
385 aDisplayer->UpdateViewer();
388 void PartSet_Module::activateFeature(boost::shared_ptr<ModelAPI_Feature> theFeature,
389 const bool isUpdateViewer)
391 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
392 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
394 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
395 aDisplayer->ActivateInLocalContext(theFeature, aPreviewOp->getSelectionModes(theFeature),
400 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
402 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
406 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
410 boost::shared_ptr<ModelAPI_Feature> aFeature = aPreviewOp->feature();
411 if (!aFeature || aFeature->getKind() != theCmdId)
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());
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);
428 aDisplayer->UpdateViewer();
431 void PartSet_Module::editFeature(FeaturePtr theFeature)
436 if (theFeature->getKind() == "Sketch") {
437 onLaunchOperation(theFeature->getKind(), theFeature);
438 updateCurrentPreview(theFeature->getKind());