1 #include <PartSet_Module.h>
2 #include <PartSet_OperationSketch.h>
3 #include <PartSet_OperationCreateFeature.h>
4 #include <PartSet_OperationEditConstraint.h>
5 #include <PartSet_OperationCreateConstraint.h>
6 #include <PartSet_OperationFeatureCreate.h>
7 #include <PartSet_OperationFeatureEditMulti.h>
8 #include <PartSet_OperationFeatureEdit.h>
9 #include <ModuleBase_Operation.h>
10 #include <ModuleBase_OperationDescription.h>
11 #include <ModuleBase_WidgetFactory.h>
12 #include <PartSet_Listener.h>
13 #include <PartSet_TestOCC.h>
15 #include <ModuleBase_Operation.h>
16 #include <ModelAPI_Object.h>
18 #include <XGUI_MainWindow.h>
19 #include <XGUI_Displayer.h>
20 #include <XGUI_Viewer.h>
21 #include <XGUI_Workshop.h>
22 #include <XGUI_OperationMgr.h>
23 #include <XGUI_SelectionMgr.h>
24 #include <XGUI_ViewPort.h>
25 #include <XGUI_ActionsMgr.h>
26 #include <XGUI_ViewerProxy.h>
27 #include <XGUI_ContextMenuMgr.h>
28 #include <XGUI_PropertyPanel.h>
29 #include <XGUI_ModuleConnector.h>
30 #include <XGUI_Tools.h>
32 #include <SketchPlugin_Line.h>
34 #include <Config_PointerMessage.h>
35 #include <Config_ModuleReader.h>
36 #include <Config_WidgetReader.h>
37 #include <Events_Loop.h>
38 #include <Events_Message.h>
39 #include <Events_Error.h>
41 #include <GeomAPI_Shape.h>
43 #include <AIS_ListOfInteractive.hxx>
44 //#include <AIS_DimensionSelectionMode.hxx>
47 #include <QMouseEvent>
54 /*!Create and return new instance of XGUI_Module*/
55 extern "C" PARTSET_EXPORT XGUI_Module* createModule(XGUI_Workshop* theWshop)
57 return new PartSet_Module(theWshop);
60 PartSet_Module::PartSet_Module(XGUI_Workshop* theWshop)
62 myWorkshop = theWshop;
63 myListener = new PartSet_Listener(this);
65 XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
67 connect(anOperationMgr, SIGNAL(operationStarted()),
68 this, SLOT(onOperationStarted()));
70 connect(anOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
71 this, SLOT(onOperationStopped(ModuleBase_Operation*)));
73 XGUI_ContextMenuMgr* aContextMenuMgr = myWorkshop->contextMenuMgr();
74 connect(aContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)),
75 this, SLOT(onContextMenuCommand(const QString&, bool)));
77 connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)),
78 this, SLOT(onMousePressed(QMouseEvent*)));
79 connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)),
80 this, SLOT(onMouseReleased(QMouseEvent*)));
81 connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)),
82 this, SLOT(onMouseMoved(QMouseEvent*)));
83 connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)),
84 this, SLOT(onKeyRelease(QKeyEvent*)));
85 connect(myWorkshop->viewer(), SIGNAL(mouseDoubleClick(QMouseEvent*)),
86 this, SLOT(onMouseDoubleClick(QMouseEvent*)));
90 PartSet_Module::~PartSet_Module()
94 XGUI_Workshop* PartSet_Module::workshop() const
99 void PartSet_Module::createFeatures()
101 Config_ModuleReader aXMLReader = Config_ModuleReader();
102 aXMLReader.readAll();
103 myFeaturesInFiles = aXMLReader.featuresInFiles();
106 void PartSet_Module::featureCreated(QAction* theFeature)
108 connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
111 QStringList PartSet_Module::nestedFeatures(QString)
113 return QStringList();
116 std::string PartSet_Module::featureFile(const std::string& theFeatureId)
118 return myFeaturesInFiles[theFeatureId];
124 void PartSet_Module::onFeatureTriggered()
126 //PartSet_TestOCC::local_selection_change_shape(myWorkshop->viewer()->AISContext(),
127 // myWorkshop->viewer()->activeView());
129 //PartSet_TestOCC::local_selection_erase(myWorkshop->viewer()->AISContext(),
130 // myWorkshop->viewer()->activeView());
131 QAction* aCmd = dynamic_cast<QAction*>(sender());
132 //Do nothing on uncheck
133 if(aCmd->isCheckable() && !aCmd->isChecked())
135 launchOperation(aCmd->data().toString());
138 void PartSet_Module::launchOperation(const QString& theCmdId)
140 ModuleBase_Operation* anOperation = createOperation(theCmdId.toStdString());
141 sendOperation(anOperation);
144 void PartSet_Module::onOperationStarted()
146 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
147 myWorkshop->operationMgr()->currentOperation());
149 XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
150 connect(aPreviewOp, SIGNAL(focusActivated(const std::string&)),
151 aPropPanel, SLOT(onFocusActivated(const std::string&)));
155 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
159 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
161 XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
162 disconnect(aPreviewOp, SIGNAL(focusActivated(const std::string&)),
163 aPropPanel, SLOT(onFocusActivated(const std::string&)));
167 void PartSet_Module::onContextMenuCommand(const QString& theId, bool isChecked)
169 QFeatureList aFeatures = myWorkshop->selector()->selectedFeatures();
170 if (theId == "EDIT_CMD" && (aFeatures.size() > 0)) {
171 editFeature(aFeatures.first());
175 void PartSet_Module::onMousePressed(QMouseEvent* theEvent)
177 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
178 myWorkshop->operationMgr()->currentOperation());
181 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
182 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
183 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
185 aPreviewOp->mousePressed(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
189 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
191 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
192 myWorkshop->operationMgr()->currentOperation());
195 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
196 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
197 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
199 aPreviewOp->mouseReleased(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
203 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
205 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
206 myWorkshop->operationMgr()->currentOperation());
208 aPreviewOp->mouseMoved(theEvent, myWorkshop->viewer()->activeView());
211 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
213 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
214 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
216 aPreviewOp->keyReleased(theEvent->key());
220 void PartSet_Module::onMouseDoubleClick(QMouseEvent* theEvent)
222 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
223 myWorkshop->operationMgr()->currentOperation());
226 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
227 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
228 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
229 aPreviewOp->mouseDoubleClick(theEvent, myWorkshop->viewer()->activeView(), aSelected,
234 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
236 myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
237 myWorkshop->actionsMgr()->update();
239 //PartSet_TestOCC::testSelection(myWorkshop);
242 void PartSet_Module::onFitAllView()
244 myWorkshop->viewer()->fitAll();
247 void PartSet_Module::onLaunchOperation(std::string theName, FeaturePtr theFeature)
249 ModuleBase_Operation* anOperation = createOperation(theName.c_str(),
250 theFeature ? theFeature->getKind() : "");
251 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
254 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
255 // refill the features list with avoiding of the features, obtained only by vertex shape (TODO)
256 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
257 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
258 aPreviewOp->init(theFeature, aSelected, aHighlighted);
260 anOperation->setEditingFeature(theFeature);
262 sendOperation(anOperation);
263 myWorkshop->actionsMgr()->updateCheckState();
266 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
268 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
269 aViewer->enableMultiselection(theEnabled);
272 void PartSet_Module::onStopSelection(const QFeatureList& theFeatures, const bool isStop)
274 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
276 QFeatureList::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
277 for (; anIt != aLast; anIt++) {
278 activateFeature((*anIt), false);
281 aDisplayer->stopSelection(theFeatures, isStop, false);
283 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
284 aViewer->enableSelection(!isStop);
286 aDisplayer->updateViewer();
289 void PartSet_Module::onSetSelection(const QFeatureList& theFeatures)
291 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
292 aDisplayer->setSelected(theFeatures, false);
293 aDisplayer->updateViewer();
296 void PartSet_Module::onCloseLocalContext()
298 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
299 aDisplayer->closeLocalContexts();
302 void PartSet_Module::onFeatureConstructed(FeaturePtr theFeature, int theMode)
304 bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
305 visualizePreview(theFeature, isDisplay, false);
307 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
309 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
311 std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> > aList = aPrevOp->subPreview();
312 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
313 std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
315 std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
316 anIt = aList.begin(), aLast = aList.end();
317 for (; anIt != aLast; anIt++) {
318 FeaturePtr aFeature = (*anIt).first;
319 visualizePreview(aFeature, false, false);
321 aDisplayer->updateViewer();
325 if (theMode == PartSet_OperationSketchBase::FM_Activation ||
326 theMode == PartSet_OperationSketchBase::FM_Deactivation)
327 activateFeature(theFeature, true);
330 ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId,
331 const std::string& theFeatureKind)
333 // create the operation
334 ModuleBase_Operation* anOperation = 0;
335 if (theCmdId == PartSet_OperationSketch::Type()) {
336 anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
339 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
341 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
343 aSketch = aPrevOp->sketch();
344 if (PartSet_OperationFeatureCreate::canProcessKind(theCmdId))
345 anOperation = new PartSet_OperationFeatureCreate(theCmdId.c_str(), this, aSketch);
346 else if (PartSet_OperationCreateFeature::canProcessKind(theCmdId))
347 anOperation = new PartSet_OperationCreateFeature(theCmdId.c_str(), this, aSketch);
348 else if (theCmdId == PartSet_OperationFeatureEditMulti::Type())
349 anOperation = new PartSet_OperationFeatureEditMulti(theCmdId.c_str(), this, aSketch);
350 else if (theCmdId == PartSet_OperationFeatureEdit::Type())
351 anOperation = new PartSet_OperationFeatureEdit(theCmdId.c_str(), this, aSketch);
352 else if (PartSet_OperationCreateConstraint::canProcessKind(theCmdId))
353 anOperation = new PartSet_OperationCreateConstraint(theCmdId.c_str(), this, aSketch);
354 else if (theCmdId == PartSet_OperationEditConstraint::Type())
355 anOperation = new PartSet_OperationEditConstraint(theCmdId.c_str(), theFeatureKind, this, aSketch);
359 anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
362 // set operation description and list of widgets corresponding to the feature xml definition
363 std::string aFeatureKind = theFeatureKind.empty() ? theCmdId : theFeatureKind;
365 std::string aPluginFileName = featureFile(aFeatureKind);
366 Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
367 aWdgReader.readAll();
368 std::string aXmlCfg = aWdgReader.featureWidgetCfg(aFeatureKind);
369 std::string aDescription = aWdgReader.featureDescription(aFeatureKind);
371 //QString aXmlRepr = QString::fromStdString(aXmlCfg);
372 //ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(),
373 // myWorkshop->moduleConnector());
374 //QWidget* aContent = myWorkshop->propertyPanel()->contentWidget();
375 //qDeleteAll(aContent->children());
376 //aFactory.createWidget(aContent);
378 anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
379 anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
381 //anOperation->setModelWidgets(aXmlRepr.toStdString(), aFactory.getModelWidgets());
383 // connect the operation
384 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
386 connect(aPreviewOp, SIGNAL(featureConstructed(FeaturePtr, int)),
387 this, SLOT(onFeatureConstructed(FeaturePtr, int)));
388 connect(aPreviewOp, SIGNAL(launchOperation(std::string, FeaturePtr)),
389 this, SLOT(onLaunchOperation(std::string, FeaturePtr)));
390 connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
391 this, SLOT(onMultiSelectionEnabled(bool)));
393 connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
394 this, SLOT(onMultiSelectionEnabled(bool)));
395 connect(aPreviewOp, SIGNAL(stopSelection(const QFeatureList&, const bool)),
396 this, SLOT(onStopSelection(const QFeatureList&, const bool)));
397 connect(aPreviewOp, SIGNAL(setSelection(const QFeatureList&)),
398 this, SLOT(onSetSelection(const QFeatureList&)));
400 connect(aPreviewOp, SIGNAL(closeLocalContext()),
401 this, SLOT(onCloseLocalContext()));
403 PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
405 connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
406 this, SLOT(onPlaneSelected(double, double, double)));
407 connect(aSketchOp, SIGNAL(fitAllView()),
408 this, SLOT(onFitAllView()));
415 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
417 //TODO(sbh): Implement static method to extract event id [SEID]
418 static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetModuleEvent");
419 Config_PointerMessage aMessage(aModuleEvent, this);
420 aMessage.setPointer(theOperation);
421 Events_Loop::loop()->send(aMessage);
424 void PartSet_Module::visualizePreview(FeaturePtr theFeature, bool isDisplay,
425 const bool isUpdateViewer)
427 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
431 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
435 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
437 boost::shared_ptr<SketchPlugin_Feature> aSPFeature =
438 boost::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature);
441 Handle(AIS_InteractiveObject) anAIS = aSPFeature->getAISShape(aDisplayer->getAISObject(theFeature));
442 aDisplayer->redisplay(theFeature, anAIS, false);
446 aDisplayer->erase(theFeature, false);
449 aDisplayer->updateViewer();
452 void PartSet_Module::activateFeature(FeaturePtr theFeature, const bool isUpdateViewer)
454 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
455 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
457 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
458 aDisplayer->activateInLocalContext(theFeature, aPreviewOp->getSelectionModes(theFeature),
463 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
465 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
469 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
473 FeaturePtr aFeature = aPreviewOp->feature();
474 if (!aFeature || aFeature->getKind() != theCmdId)
477 std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> > aList = aPreviewOp->subPreview();
478 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
479 std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
481 std::map<FeaturePtr, boost::shared_ptr<GeomAPI_Shape> >::const_iterator
482 anIt = aList.begin(), aLast = aList.end();
483 for (; anIt != aLast; anIt++) {
484 FeaturePtr aFeature = (*anIt).first;
485 boost::shared_ptr<SketchPlugin_Feature> aSPFeature =
486 boost::dynamic_pointer_cast<SketchPlugin_Feature>((*anIt).first);
490 Handle(AIS_InteractiveObject) anAIS = aSPFeature->getAISShape(aDisplayer->getAISObject(aFeature));
492 aDisplayer->redisplay(aFeature, anAIS, false);
493 aDisplayer->activateInLocalContext(aFeature, aModes, false);
495 aDisplayer->updateViewer();
498 void PartSet_Module::editFeature(FeaturePtr theFeature)
503 // if (theFeature->getKind() == SKETCH_KIND) {
504 FeaturePtr aFeature = theFeature;
505 if (XGUI_Tools::isModelObject(aFeature)) {
506 ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
507 aFeature = aObject->featureRef();
511 onLaunchOperation(aFeature->getKind(), aFeature);
512 updateCurrentPreview(aFeature->getKind());