1 #include <PartSet_Module.h>
2 #include <PartSet_OperationSketch.h>
3 #include <PartSet_OperationFeatureCreate.h>
4 #include <PartSet_OperationFeatureEditMulti.h>
5 #include <PartSet_OperationFeatureEdit.h>
6 #include <ModuleBase_Operation.h>
7 #include <ModuleBase_OperationDescription.h>
8 #include <ModuleBase_WidgetFactory.h>
9 #include <PartSet_Listener.h>
10 #include <PartSet_TestOCC.h>
11 #include <PartSet_WidgetSketchLabel.h>
13 #include <ModuleBase_Operation.h>
14 #include <ModelAPI_Object.h>
15 #include <ModelAPI_Events.h>
17 #include <ModelAPI_Data.h>
18 #include <GeomDataAPI_Point2D.h>
19 #include <PartSet_Tools.h>
21 #include <XGUI_MainWindow.h>
22 #include <XGUI_Displayer.h>
23 #include <XGUI_Viewer.h>
24 #include <XGUI_Workshop.h>
25 #include <XGUI_OperationMgr.h>
26 #include <XGUI_SelectionMgr.h>
27 #include <XGUI_ViewPort.h>
28 #include <XGUI_ActionsMgr.h>
29 #include <XGUI_ViewerProxy.h>
30 #include <XGUI_ContextMenuMgr.h>
31 #include <XGUI_PropertyPanel.h>
32 #include <XGUI_ModuleConnector.h>
33 #include <XGUI_Tools.h>
35 #include <SketchPlugin_Line.h>
37 #include <Config_PointerMessage.h>
38 #include <Config_ModuleReader.h>
39 #include <Config_WidgetReader.h>
40 #include <Events_Loop.h>
41 #include <Events_Message.h>
42 #include <Events_Error.h>
44 #include <GeomAPI_Shape.h>
45 #include <GeomAPI_AISObject.h>
48 #include <QMouseEvent>
55 /*!Create and return new instance of XGUI_Module*/
56 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(XGUI_Workshop* theWshop)
58 return new PartSet_Module(theWshop);
61 PartSet_Module::PartSet_Module(XGUI_Workshop* theWshop)
63 myWorkshop = theWshop;
64 myListener = new PartSet_Listener(this);
66 XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
68 connect(anOperationMgr, SIGNAL(operationStarted()),
69 this, SLOT(onOperationStarted()));
71 connect(anOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
72 this, SLOT(onOperationStopped(ModuleBase_Operation*)));
74 XGUI_ContextMenuMgr* aContextMenuMgr = myWorkshop->contextMenuMgr();
75 connect(aContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)),
76 this, SLOT(onContextMenuCommand(const QString&, bool)));
78 connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)),
79 this, SLOT(onMousePressed(QMouseEvent*)));
80 connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)),
81 this, SLOT(onMouseReleased(QMouseEvent*)));
82 connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)),
83 this, SLOT(onMouseMoved(QMouseEvent*)));
84 connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)),
85 this, SLOT(onKeyRelease(QKeyEvent*)));
86 connect(myWorkshop->viewer(), SIGNAL(mouseDoubleClick(QMouseEvent*)),
87 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 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
143 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
144 // Initialise operation with preliminary selection
145 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
146 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
147 aPreviewOp->initSelection(aSelected, aHighlighted);
149 sendOperation(anOperation);
152 void PartSet_Module::onOperationStarted()
154 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
155 myWorkshop->operationMgr()->currentOperation());
157 XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
158 connect(aPropPanel, SIGNAL(storedPoint2D(FeaturePtr, const std::string&)),
159 this, SLOT(onStorePoint2D(FeaturePtr, const std::string&)), Qt::UniqueConnection);
163 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
167 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
169 XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
170 //disconnect(aPropPanel, SIGNAL(storedPoint2D(FeaturePtr, const std::string&)),
171 // this, SLOT(onStorePoint2D(FeaturePtr, const std::string&)));
175 void PartSet_Module::onContextMenuCommand(const QString& theId, bool isChecked)
177 QFeatureList aFeatures = myWorkshop->selector()->selectedFeatures();
178 if (theId == "EDIT_CMD" && (aFeatures.size() > 0)) {
179 editFeature(aFeatures.first());
183 void PartSet_Module::onMousePressed(QMouseEvent* theEvent)
185 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
186 myWorkshop->operationMgr()->currentOperation());
188 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
189 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
190 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
192 aPreviewOp->mousePressed(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
196 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
198 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
199 myWorkshop->operationMgr()->currentOperation());
202 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
203 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
204 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
206 aPreviewOp->mouseReleased(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
210 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
212 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
213 myWorkshop->operationMgr()->currentOperation());
215 aPreviewOp->mouseMoved(theEvent, myWorkshop->viewer()->activeView());
218 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
220 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
221 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
223 aPreviewOp->keyReleased(theEvent->key());
227 void PartSet_Module::onMouseDoubleClick(QMouseEvent* theEvent)
229 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
230 myWorkshop->operationMgr()->currentOperation());
233 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
234 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
235 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
236 aPreviewOp->mouseDoubleClick(theEvent, myWorkshop->viewer()->activeView(), aSelected,
241 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
243 myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
244 myWorkshop->actionsMgr()->update();
246 //PartSet_TestOCC::testSelection(myWorkshop);
249 void PartSet_Module::onFitAllView()
251 myWorkshop->viewer()->fitAll();
254 void PartSet_Module::onLaunchOperation(std::string theName, FeaturePtr theFeature)
256 ModuleBase_Operation* anOperation = createOperation(theName.c_str(),
257 theFeature ? theFeature->getKind() : "");
258 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
261 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
262 // refill the features list with avoiding of the features, obtained only by vertex shape (TODO)
263 std::list<XGUI_ViewerPrs> aSelected = aDisplayer->getSelected();
264 std::list<XGUI_ViewerPrs> aHighlighted = aDisplayer->getHighlighted();
265 aPreviewOp->initFeature(theFeature);
266 aPreviewOp->initSelection(aSelected, aHighlighted);
268 anOperation->setEditingFeature(theFeature);
270 sendOperation(anOperation);
271 myWorkshop->actionsMgr()->updateCheckState();
274 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
276 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
277 aViewer->enableMultiselection(theEnabled);
280 void PartSet_Module::onStopSelection(const QFeatureList& theFeatures, const bool isStop)
282 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
284 QFeatureList::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
285 for (; anIt != aLast; anIt++) {
286 activateFeature((*anIt), false);
289 aDisplayer->stopSelection(theFeatures, isStop, false);
291 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
292 aViewer->enableSelection(!isStop);
294 aDisplayer->updateViewer();
297 void PartSet_Module::onSetSelection(const QFeatureList& theFeatures)
299 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
300 aDisplayer->setSelected(theFeatures, false);
301 aDisplayer->updateViewer();
304 void PartSet_Module::onCloseLocalContext()
306 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
307 aDisplayer->closeLocalContexts();
310 void PartSet_Module::onFeatureConstructed(FeaturePtr theFeature, int theMode)
312 bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
313 visualizePreview(theFeature, isDisplay, false);
315 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
317 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
319 std::list<FeaturePtr> aList = aPrevOp->subFeatures();
320 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
321 std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
323 std::list<FeaturePtr>::const_iterator anIt = aList.begin(),
325 for (; anIt != aLast; anIt++)
326 visualizePreview(*anIt, false, false);
327 aDisplayer->updateViewer();
331 if (theMode == PartSet_OperationSketchBase::FM_Activation ||
332 theMode == PartSet_OperationSketchBase::FM_Deactivation)
333 activateFeature(theFeature, true);
336 ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId,
337 const std::string& theFeatureKind)
339 // create the operation
340 ModuleBase_Operation* anOperation = 0;
341 if (theCmdId == PartSet_OperationSketch::Type()) {
342 anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
345 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
347 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
349 aSketch = aPrevOp->sketch();
350 if (PartSet_OperationFeatureCreate::canProcessKind(theCmdId))
351 anOperation = new PartSet_OperationFeatureCreate(theCmdId.c_str(), this, aSketch);
352 else if (theCmdId == PartSet_OperationFeatureEditMulti::Type())
353 anOperation = new PartSet_OperationFeatureEditMulti(theCmdId.c_str(), this, aSketch);
354 else if (theCmdId == PartSet_OperationFeatureEdit::Type())
355 anOperation = new PartSet_OperationFeatureEdit(theCmdId.c_str(), 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(stopSelection(const QFeatureList&, const bool)),
394 this, SLOT(onStopSelection(const QFeatureList&, const bool)));
395 connect(aPreviewOp, SIGNAL(setSelection(const QFeatureList&)),
396 this, SLOT(onSetSelection(const QFeatureList&)));
398 connect(aPreviewOp, SIGNAL(closeLocalContext()),
399 this, SLOT(onCloseLocalContext()));
401 PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
403 connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
404 this, SLOT(onPlaneSelected(double, double, double)));
405 connect(aSketchOp, SIGNAL(fitAllView()),
406 this, SLOT(onFitAllView()));
413 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
415 static Events_ID aModuleEvent = Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED);
416 Config_PointerMessage aMessage(aModuleEvent, this);
417 aMessage.setPointer(theOperation);
418 Events_Loop::loop()->send(aMessage);
421 void PartSet_Module::visualizePreview(FeaturePtr theFeature, bool isDisplay,
422 const bool isUpdateViewer)
424 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
428 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
432 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
434 boost::shared_ptr<SketchPlugin_Feature> aSPFeature =
435 boost::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature);
438 boost::shared_ptr<GeomAPI_AISObject> anAIS =
439 aSPFeature->getAISObject(aDisplayer->getAISObject(theFeature));
440 aDisplayer->redisplay(theFeature, anAIS, false);
444 aDisplayer->erase(theFeature, false);
447 aDisplayer->updateViewer();
450 void PartSet_Module::activateFeature(FeaturePtr theFeature, const bool isUpdateViewer)
452 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
453 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
455 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
456 aDisplayer->activateInLocalContext(theFeature, aPreviewOp->getSelectionModes(theFeature),
461 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
463 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
467 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
471 FeaturePtr aFeature = aPreviewOp->feature();
472 if (!aFeature || aFeature->getKind() != theCmdId)
475 std::list<FeaturePtr> aList = aPreviewOp->subFeatures();
476 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
477 std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
479 std::list<FeaturePtr>::const_iterator anIt = aList.begin(),
481 for (; anIt != aLast; anIt++) {
482 boost::shared_ptr<SketchPlugin_Feature> aSPFeature =
483 boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
486 visualizePreview(*anIt, true, false);
487 aDisplayer->activateInLocalContext(*anIt, aModes, false);
489 aDisplayer->updateViewer();
492 void PartSet_Module::editFeature(FeaturePtr theFeature)
497 // if (theFeature->getKind() == SKETCH_KIND) {
498 FeaturePtr aFeature = theFeature;
499 if (XGUI_Tools::isModelObject(aFeature)) {
500 ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
501 aFeature = aObject->featureRef();
505 onLaunchOperation(aFeature->getKind(), aFeature);
506 updateCurrentPreview(aFeature->getKind());
511 void PartSet_Module::onStorePoint2D(FeaturePtr theFeature, const std::string& theAttribute)
513 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
514 myWorkshop->operationMgr()->currentOperation());
518 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
519 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theFeature->data()->attribute(theAttribute));
521 PartSet_Tools::setConstraints(aPreviewOp->sketch(), theFeature, theAttribute,
522 aPoint->x(), aPoint->y());
525 /*bool PartSet_Module::isFeatureEnabled(const QString& theCmdId) const
527 XGUI_OperationMgr* aOpMgr = myWorkshop->operationMgr();
528 XGUI_ActionsMgr* aActMgr = myWorkshop->actionsMgr();
530 ModuleBase_Operation* aOperation = aOpMgr->currentOperation();
532 return !aActMgr->isNested(theCmdId);
534 PartSet_OperationFeatureEdit* aSketchEdtOp = dynamic_cast<PartSet_OperationFeatureEdit*>(aOperation);
536 QStringList aConstraintList;
537 aConstraintList<<"SketchConstraintDistance"<<"SketchConstraintLength"
538 <<"SketchConstraintRadius"<<"SketchConstraintParallel"<<"SketchConstraintPerpendicular";
539 return aConstraintList.contains(theCmdId);
541 QStringList aList = aActMgr->nestedCommands(aOperation->id());
542 return aList.contains(theCmdId);
545 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
546 Config_WidgetAPI* theWidgetApi, QList<ModuleBase_ModelWidget*>& theModelWidgets)
548 if (theType == "sketch-start-label") {
549 PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi);
550 aWgt->setOperationsMgr(myWorkshop->operationMgr());
551 theModelWidgets.append(aWgt);
552 return aWgt->getControl();