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>
12 #include <PartSet_Validators.h>
14 #include <ModuleBase_Operation.h>
15 #include <ModelAPI_Object.h>
16 #include <ModelAPI_Events.h>
17 #include <ModelAPI_Validator.h>
19 #include <ModelAPI_Data.h>
20 #include <GeomDataAPI_Point2D.h>
21 #include <PartSet_Tools.h>
23 #include <XGUI_MainWindow.h>
24 #include <XGUI_Displayer.h>
25 #include <XGUI_Viewer.h>
26 #include <XGUI_Workshop.h>
27 #include <XGUI_OperationMgr.h>
28 #include <XGUI_SelectionMgr.h>
29 #include <XGUI_Selection.h>
30 #include <XGUI_ViewPort.h>
31 #include <XGUI_ActionsMgr.h>
32 #include <XGUI_ViewerProxy.h>
33 #include <XGUI_ContextMenuMgr.h>
34 #include <XGUI_PropertyPanel.h>
35 #include <XGUI_ModuleConnector.h>
36 #include <XGUI_Tools.h>
38 #include <SketchPlugin_Line.h>
40 #include <Config_PointerMessage.h>
41 #include <Config_ModuleReader.h>
42 #include <Config_WidgetReader.h>
43 #include <Events_Loop.h>
44 #include <Events_Message.h>
45 #include <Events_Error.h>
47 #include <GeomAPI_Shape.h>
48 #include <GeomAPI_AISObject.h>
51 #include <QMouseEvent>
58 /*!Create and return new instance of XGUI_Module*/
59 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(XGUI_Workshop* theWshop)
61 return new PartSet_Module(theWshop);
64 PartSet_Module::PartSet_Module(XGUI_Workshop* theWshop)
66 myWorkshop = theWshop;
67 myListener = new PartSet_Listener(this);
69 XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
71 connect(anOperationMgr, SIGNAL(operationStarted()),
72 this, SLOT(onOperationStarted()));
74 connect(anOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
75 this, SLOT(onOperationStopped(ModuleBase_Operation*)));
77 XGUI_ContextMenuMgr* aContextMenuMgr = myWorkshop->contextMenuMgr();
78 connect(aContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)),
79 this, SLOT(onContextMenuCommand(const QString&, bool)));
81 connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)),
82 this, SLOT(onMousePressed(QMouseEvent*)));
83 connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)),
84 this, SLOT(onMouseReleased(QMouseEvent*)));
85 connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)),
86 this, SLOT(onMouseMoved(QMouseEvent*)));
87 connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)),
88 this, SLOT(onKeyRelease(QKeyEvent*)));
89 connect(myWorkshop->viewer(), SIGNAL(mouseDoubleClick(QMouseEvent*)),
90 this, SLOT(onMouseDoubleClick(QMouseEvent*)));
93 PartSet_Module::~PartSet_Module()
97 XGUI_Workshop* PartSet_Module::workshop() const
102 void PartSet_Module::createFeatures()
104 Config_ModuleReader aXMLReader = Config_ModuleReader();
105 aXMLReader.readAll();
106 myFeaturesInFiles = aXMLReader.featuresInFiles();
108 //!! Test registering of validators
109 PluginManagerPtr aMgr = ModelAPI_PluginManager::get();
110 ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
111 aFactory->registerValidator("PartSet_DistanceValidator", new PartSet_DistanceValidator);
112 aFactory->registerValidator("PartSet_LengthValidator", new PartSet_LengthValidator);
113 aFactory->registerValidator("PartSet_PerpendicularValidator", new PartSet_PerpendicularValidator);
114 aFactory->registerValidator("PartSet_ParallelValidator", new PartSet_ParallelValidator);
115 aFactory->registerValidator("PartSet_RadiusValidator", new PartSet_RadiusValidator);
118 void PartSet_Module::featureCreated(QAction* theFeature)
120 connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
123 QStringList PartSet_Module::nestedFeatures(QString)
125 return QStringList();
128 std::string PartSet_Module::featureFile(const std::string& theFeatureId)
130 return myFeaturesInFiles[theFeatureId];
136 void PartSet_Module::onFeatureTriggered()
138 //PartSet_TestOCC::local_selection_change_shape(myWorkshop->viewer()->AISContext(),
139 // myWorkshop->viewer()->activeView());
141 //PartSet_TestOCC::local_selection_erase(myWorkshop->viewer()->AISContext(),
142 // myWorkshop->viewer()->activeView());
143 QAction* aCmd = dynamic_cast<QAction*>(sender());
144 //Do nothing on uncheck
145 if(aCmd->isCheckable() && !aCmd->isChecked())
147 launchOperation(aCmd->data().toString());
150 void PartSet_Module::launchOperation(const QString& theCmdId)
152 ModuleBase_Operation* anOperation = createOperation(theCmdId.toStdString());
153 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
155 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
156 // Initialise operation with preliminary selection
157 std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
158 std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
159 aPreviewOp->initSelection(aSelected, aHighlighted);
161 sendOperation(anOperation);
164 void PartSet_Module::onOperationStarted()
166 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
167 myWorkshop->operationMgr()->currentOperation());
169 XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
170 connect(aPropPanel, SIGNAL(storedPoint2D(FeaturePtr, const std::string&)),
171 this, SLOT(onStorePoint2D(FeaturePtr, const std::string&)), Qt::UniqueConnection);
175 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
179 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
181 XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
182 //disconnect(aPropPanel, SIGNAL(storedPoint2D(FeaturePtr, const std::string&)),
183 // this, SLOT(onStorePoint2D(FeaturePtr, const std::string&)));
187 void PartSet_Module::onContextMenuCommand(const QString& theId, bool isChecked)
189 QFeatureList aFeatures = myWorkshop->selector()->selection()->selectedFeatures();
190 if (theId == "EDIT_CMD" && (aFeatures.size() > 0)) {
191 editFeature(aFeatures.first());
195 void PartSet_Module::onMousePressed(QMouseEvent* theEvent)
197 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
198 myWorkshop->operationMgr()->currentOperation());
200 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
201 // Initialise operation with preliminary selection
202 std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
203 std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
205 aPreviewOp->mousePressed(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
209 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
211 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
212 myWorkshop->operationMgr()->currentOperation());
215 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
216 // Initialise operation with preliminary selection
217 std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
218 std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
220 aPreviewOp->mouseReleased(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
224 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
226 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
227 myWorkshop->operationMgr()->currentOperation());
229 aPreviewOp->mouseMoved(theEvent, myWorkshop->viewer()->activeView());
232 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
234 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
235 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
237 aPreviewOp->keyReleased(theEvent->key());
241 void PartSet_Module::onMouseDoubleClick(QMouseEvent* theEvent)
243 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
244 myWorkshop->operationMgr()->currentOperation());
247 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
248 // Initialise operation with preliminary selection
249 std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
250 std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
251 aPreviewOp->mouseDoubleClick(theEvent, myWorkshop->viewer()->activeView(), aSelected,
256 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
258 myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
259 myWorkshop->actionsMgr()->update();
261 //PartSet_TestOCC::testSelection(myWorkshop);
264 void PartSet_Module::onFitAllView()
266 myWorkshop->viewer()->fitAll();
269 void PartSet_Module::onLaunchOperation(std::string theName, FeaturePtr theFeature)
271 ModuleBase_Operation* anOperation = createOperation(theName.c_str(),
272 theFeature ? theFeature->getKind() : "");
273 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
276 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
277 // Initialise operation with preliminary selection
278 std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
279 std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
280 aPreviewOp->initFeature(theFeature);
281 aPreviewOp->initSelection(aSelected, aHighlighted);
283 anOperation->setEditingFeature(theFeature);
285 sendOperation(anOperation);
286 myWorkshop->actionsMgr()->updateCheckState();
289 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
291 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
292 aViewer->enableMultiselection(theEnabled);
295 void PartSet_Module::onStopSelection(const QFeatureList& theFeatures, const bool isStop)
297 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
299 QFeatureList::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
300 for (; anIt != aLast; anIt++) {
301 activateFeature((*anIt), false);
304 aDisplayer->stopSelection(theFeatures, isStop, false);
306 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
307 aViewer->enableSelection(!isStop);
309 aDisplayer->updateViewer();
312 void PartSet_Module::onSetSelection(const QFeatureList& theFeatures)
314 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
315 aDisplayer->setSelected(theFeatures, false);
316 aDisplayer->updateViewer();
319 void PartSet_Module::onCloseLocalContext()
321 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
322 aDisplayer->closeLocalContexts();
325 void PartSet_Module::onFeatureConstructed(FeaturePtr theFeature, int theMode)
327 bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
328 visualizePreview(theFeature, isDisplay, false);
330 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
332 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
334 std::list<FeaturePtr> aList = aPrevOp->subFeatures();
335 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
336 std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
338 std::list<FeaturePtr>::const_iterator anIt = aList.begin(),
340 for (; anIt != aLast; anIt++)
341 visualizePreview(*anIt, false, false);
342 aDisplayer->updateViewer();
346 if (theMode == PartSet_OperationSketchBase::FM_Activation ||
347 theMode == PartSet_OperationSketchBase::FM_Deactivation)
348 activateFeature(theFeature, true);
351 ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId,
352 const std::string& theFeatureKind)
354 // create the operation
355 ModuleBase_Operation* anOperation = 0;
356 if (theCmdId == PartSet_OperationSketch::Type()) {
357 anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
360 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
362 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
364 aSketch = aPrevOp->sketch();
365 if (PartSet_OperationFeatureCreate::canProcessKind(theCmdId))
366 anOperation = new PartSet_OperationFeatureCreate(theCmdId.c_str(), this, aSketch);
367 else if (theCmdId == PartSet_OperationFeatureEditMulti::Type())
368 anOperation = new PartSet_OperationFeatureEditMulti(theCmdId.c_str(), this, aSketch);
369 else if (theCmdId == PartSet_OperationFeatureEdit::Type())
370 anOperation = new PartSet_OperationFeatureEdit(theCmdId.c_str(), this, aSketch);
374 anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
377 // set operation description and list of widgets corresponding to the feature xml definition
378 std::string aFeatureKind = theFeatureKind.empty() ? theCmdId : theFeatureKind;
380 std::string aPluginFileName = featureFile(aFeatureKind);
381 Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
382 aWdgReader.readAll();
383 std::string aXmlCfg = aWdgReader.featureWidgetCfg(aFeatureKind);
384 std::string aDescription = aWdgReader.featureDescription(aFeatureKind);
386 //QString aXmlRepr = QString::fromStdString(aXmlCfg);
387 //ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(),
388 // myWorkshop->moduleConnector());
389 //QWidget* aContent = myWorkshop->propertyPanel()->contentWidget();
390 //qDeleteAll(aContent->children());
391 //aFactory.createWidget(aContent);
393 anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
394 anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
396 //anOperation->setModelWidgets(aXmlRepr.toStdString(), aFactory.getModelWidgets());
398 // connect the operation
399 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
401 connect(aPreviewOp, SIGNAL(featureConstructed(FeaturePtr, int)),
402 this, SLOT(onFeatureConstructed(FeaturePtr, int)));
403 connect(aPreviewOp, SIGNAL(launchOperation(std::string, FeaturePtr)),
404 this, SLOT(onLaunchOperation(std::string, FeaturePtr)));
405 connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
406 this, SLOT(onMultiSelectionEnabled(bool)));
408 connect(aPreviewOp, SIGNAL(stopSelection(const QFeatureList&, const bool)),
409 this, SLOT(onStopSelection(const QFeatureList&, const bool)));
410 connect(aPreviewOp, SIGNAL(setSelection(const QFeatureList&)),
411 this, SLOT(onSetSelection(const QFeatureList&)));
413 connect(aPreviewOp, SIGNAL(closeLocalContext()),
414 this, SLOT(onCloseLocalContext()));
416 PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
418 connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
419 this, SLOT(onPlaneSelected(double, double, double)));
420 connect(aSketchOp, SIGNAL(fitAllView()),
421 this, SLOT(onFitAllView()));
428 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
430 static Events_ID aModuleEvent = Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED);
431 Config_PointerMessage aMessage(aModuleEvent, this);
432 aMessage.setPointer(theOperation);
433 Events_Loop::loop()->send(aMessage);
436 void PartSet_Module::visualizePreview(FeaturePtr theFeature, bool isDisplay,
437 const bool isUpdateViewer)
439 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
443 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
447 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
449 boost::shared_ptr<SketchPlugin_Feature> aSPFeature =
450 boost::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature);
453 boost::shared_ptr<GeomAPI_AISObject> anAIS =
454 aSPFeature->getAISObject(aDisplayer->getAISObject(theFeature));
455 aDisplayer->redisplay(theFeature, anAIS, false);
459 aDisplayer->erase(theFeature, false);
462 aDisplayer->updateViewer();
465 void PartSet_Module::activateFeature(FeaturePtr theFeature, const bool isUpdateViewer)
467 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
468 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
470 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
471 aDisplayer->activateInLocalContext(theFeature, aPreviewOp->getSelectionModes(theFeature),
476 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
478 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
482 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
486 FeaturePtr aFeature = aPreviewOp->feature();
487 if (!aFeature || aFeature->getKind() != theCmdId)
490 std::list<FeaturePtr> aList = aPreviewOp->subFeatures();
491 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
492 std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
494 std::list<FeaturePtr>::const_iterator anIt = aList.begin(),
496 for (; anIt != aLast; anIt++) {
497 boost::shared_ptr<SketchPlugin_Feature> aSPFeature =
498 boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
501 visualizePreview(*anIt, true, false);
502 aDisplayer->activateInLocalContext(*anIt, aModes, false);
504 aDisplayer->updateViewer();
507 void PartSet_Module::editFeature(FeaturePtr theFeature)
512 // if (theFeature->getKind() == SKETCH_KIND) {
513 FeaturePtr aFeature = theFeature;
514 if (XGUI_Tools::isModelObject(aFeature)) {
515 ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
516 aFeature = aObject->featureRef();
520 onLaunchOperation(aFeature->getKind(), aFeature);
521 updateCurrentPreview(aFeature->getKind());
526 void PartSet_Module::onStorePoint2D(FeaturePtr theFeature, const std::string& theAttribute)
528 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
529 myWorkshop->operationMgr()->currentOperation());
533 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
534 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theFeature->data()->attribute(theAttribute));
536 PartSet_Tools::setConstraints(aPreviewOp->sketch(), theFeature, theAttribute,
537 aPoint->x(), aPoint->y());
540 /*bool PartSet_Module::isFeatureEnabled(const QString& theCmdId) const
542 XGUI_OperationMgr* aOpMgr = myWorkshop->operationMgr();
543 XGUI_ActionsMgr* aActMgr = myWorkshop->actionsMgr();
545 ModuleBase_Operation* aOperation = aOpMgr->currentOperation();
547 return !aActMgr->isNested(theCmdId);
549 PartSet_OperationFeatureEdit* aSketchEdtOp = dynamic_cast<PartSet_OperationFeatureEdit*>(aOperation);
551 QStringList aConstraintList;
552 aConstraintList<<"SketchConstraintDistance"<<"SketchConstraintLength"
553 <<"SketchConstraintRadius"<<"SketchConstraintParallel"<<"SketchConstraintPerpendicular";
554 return aConstraintList.contains(theCmdId);
556 QStringList aList = aActMgr->nestedCommands(aOperation->id());
557 return aList.contains(theCmdId);
560 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
561 Config_WidgetAPI* theWidgetApi, QList<ModuleBase_ModelWidget*>& theModelWidgets)
563 if (theType == "sketch-start-label") {
564 PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi);
565 aWgt->setOperationsMgr(myWorkshop->operationMgr());
566 theModelWidgets.append(aWgt);
567 return aWgt->getControl();