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();
112 aFactory->registerValidator("PartSet_DistanceValidator", new PartSet_DistanceValidator);
113 aFactory->assignValidator("PartSet_DistanceValidator", "SketchConstraintDistance");
115 aFactory->registerValidator("PartSet_LengthValidator", new PartSet_LengthValidator);
116 aFactory->assignValidator("PartSet_LengthValidator", "SketchConstraintLength");
118 aFactory->registerValidator("PartSet_PerpendicularValidator", new PartSet_PerpendicularValidator);
119 aFactory->assignValidator("PartSet_PerpendicularValidator", "SketchConstraintPerpendicular");
121 aFactory->registerValidator("PartSet_ParallelValidator", new PartSet_ParallelValidator);
122 aFactory->assignValidator("PartSet_ParallelValidator", "SketchConstraintParallel");
124 aFactory->registerValidator("PartSet_RadiusValidator", new PartSet_RadiusValidator);
125 aFactory->assignValidator("PartSet_RadiusValidator", "SketchConstraintRadius");
128 void PartSet_Module::featureCreated(QAction* theFeature)
130 connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
133 QStringList PartSet_Module::nestedFeatures(QString)
135 return QStringList();
138 std::string PartSet_Module::featureFile(const std::string& theFeatureId)
140 return myFeaturesInFiles[theFeatureId];
146 void PartSet_Module::onFeatureTriggered()
148 //PartSet_TestOCC::local_selection_change_shape(myWorkshop->viewer()->AISContext(),
149 // myWorkshop->viewer()->activeView());
151 //PartSet_TestOCC::local_selection_erase(myWorkshop->viewer()->AISContext(),
152 // myWorkshop->viewer()->activeView());
153 QAction* aCmd = dynamic_cast<QAction*>(sender());
154 //Do nothing on uncheck
155 if(aCmd->isCheckable() && !aCmd->isChecked())
157 launchOperation(aCmd->data().toString());
160 void PartSet_Module::launchOperation(const QString& theCmdId)
162 ModuleBase_Operation* anOperation = createOperation(theCmdId.toStdString());
163 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
165 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
166 // Initialise operation with preliminary selection
167 std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
168 std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
169 aPreviewOp->initSelection(aSelected, aHighlighted);
171 sendOperation(anOperation);
174 void PartSet_Module::onOperationStarted()
176 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
177 myWorkshop->operationMgr()->currentOperation());
179 XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
180 connect(aPropPanel, SIGNAL(storedPoint2D(FeaturePtr, const std::string&)),
181 this, SLOT(onStorePoint2D(FeaturePtr, const std::string&)), Qt::UniqueConnection);
185 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
189 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
191 XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
192 //disconnect(aPropPanel, SIGNAL(storedPoint2D(FeaturePtr, const std::string&)),
193 // this, SLOT(onStorePoint2D(FeaturePtr, const std::string&)));
197 void PartSet_Module::onContextMenuCommand(const QString& theId, bool isChecked)
199 QFeatureList aFeatures = myWorkshop->selector()->selection()->selectedFeatures();
200 if (theId == "EDIT_CMD" && (aFeatures.size() > 0)) {
201 editFeature(aFeatures.first());
205 void PartSet_Module::onMousePressed(QMouseEvent* theEvent)
207 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
208 myWorkshop->operationMgr()->currentOperation());
210 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
211 // Initialise operation with preliminary selection
212 std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
213 std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
215 aPreviewOp->mousePressed(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
219 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
221 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
222 myWorkshop->operationMgr()->currentOperation());
225 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
226 // Initialise operation with preliminary selection
227 std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
228 std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
230 aPreviewOp->mouseReleased(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
234 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
236 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
237 myWorkshop->operationMgr()->currentOperation());
239 aPreviewOp->mouseMoved(theEvent, myWorkshop->viewer()->activeView());
242 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
244 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
245 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
247 aPreviewOp->keyReleased(theEvent->key());
251 void PartSet_Module::onMouseDoubleClick(QMouseEvent* theEvent)
253 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
254 myWorkshop->operationMgr()->currentOperation());
257 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
258 // Initialise operation with preliminary selection
259 std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
260 std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
261 aPreviewOp->mouseDoubleClick(theEvent, myWorkshop->viewer()->activeView(), aSelected,
266 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
268 myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
269 myWorkshop->actionsMgr()->update();
271 //PartSet_TestOCC::testSelection(myWorkshop);
274 void PartSet_Module::onFitAllView()
276 myWorkshop->viewer()->fitAll();
279 void PartSet_Module::onLaunchOperation(std::string theName, FeaturePtr theFeature)
281 ModuleBase_Operation* anOperation = createOperation(theName.c_str(),
282 theFeature ? theFeature->getKind() : "");
283 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
286 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
287 // Initialise operation with preliminary selection
288 std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
289 std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
290 aPreviewOp->initFeature(theFeature);
291 aPreviewOp->initSelection(aSelected, aHighlighted);
293 anOperation->setEditingFeature(theFeature);
295 sendOperation(anOperation);
296 myWorkshop->actionsMgr()->updateCheckState();
299 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
301 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
302 aViewer->enableMultiselection(theEnabled);
305 void PartSet_Module::onStopSelection(const QFeatureList& theFeatures, const bool isStop)
307 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
309 QFeatureList::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
310 for (; anIt != aLast; anIt++) {
311 activateFeature((*anIt), false);
314 aDisplayer->stopSelection(theFeatures, isStop, false);
316 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
317 aViewer->enableSelection(!isStop);
319 aDisplayer->updateViewer();
322 void PartSet_Module::onSetSelection(const QFeatureList& theFeatures)
324 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
325 aDisplayer->setSelected(theFeatures, false);
326 aDisplayer->updateViewer();
329 void PartSet_Module::onCloseLocalContext()
331 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
332 aDisplayer->closeLocalContexts();
335 void PartSet_Module::onFeatureConstructed(FeaturePtr theFeature, int theMode)
337 bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
338 visualizePreview(theFeature, isDisplay, false);
340 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
342 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
344 std::list<FeaturePtr> aList = aPrevOp->subFeatures();
345 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
346 std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
348 std::list<FeaturePtr>::const_iterator anIt = aList.begin(),
350 for (; anIt != aLast; anIt++)
351 visualizePreview(*anIt, false, false);
352 aDisplayer->updateViewer();
356 if (theMode == PartSet_OperationSketchBase::FM_Activation ||
357 theMode == PartSet_OperationSketchBase::FM_Deactivation)
358 activateFeature(theFeature, true);
361 ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId,
362 const std::string& theFeatureKind)
364 // create the operation
365 ModuleBase_Operation* anOperation = 0;
366 if (theCmdId == PartSet_OperationSketch::Type()) {
367 anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
370 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
372 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
374 aSketch = aPrevOp->sketch();
375 if (PartSet_OperationFeatureCreate::canProcessKind(theCmdId))
376 anOperation = new PartSet_OperationFeatureCreate(theCmdId.c_str(), this, aSketch);
377 else if (theCmdId == PartSet_OperationFeatureEditMulti::Type())
378 anOperation = new PartSet_OperationFeatureEditMulti(theCmdId.c_str(), this, aSketch);
379 else if (theCmdId == PartSet_OperationFeatureEdit::Type())
380 anOperation = new PartSet_OperationFeatureEdit(theCmdId.c_str(), this, aSketch);
384 anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
387 // set operation description and list of widgets corresponding to the feature xml definition
388 std::string aFeatureKind = theFeatureKind.empty() ? theCmdId : theFeatureKind;
390 std::string aPluginFileName = featureFile(aFeatureKind);
391 Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
392 aWdgReader.readAll();
393 std::string aXmlCfg = aWdgReader.featureWidgetCfg(aFeatureKind);
394 std::string aDescription = aWdgReader.featureDescription(aFeatureKind);
396 //QString aXmlRepr = QString::fromStdString(aXmlCfg);
397 //ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(),
398 // myWorkshop->moduleConnector());
399 //QWidget* aContent = myWorkshop->propertyPanel()->contentWidget();
400 //qDeleteAll(aContent->children());
401 //aFactory.createWidget(aContent);
403 anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
404 anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
406 //anOperation->setModelWidgets(aXmlRepr.toStdString(), aFactory.getModelWidgets());
408 // connect the operation
409 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
411 connect(aPreviewOp, SIGNAL(featureConstructed(FeaturePtr, int)),
412 this, SLOT(onFeatureConstructed(FeaturePtr, int)));
413 connect(aPreviewOp, SIGNAL(launchOperation(std::string, FeaturePtr)),
414 this, SLOT(onLaunchOperation(std::string, FeaturePtr)));
415 connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
416 this, SLOT(onMultiSelectionEnabled(bool)));
418 connect(aPreviewOp, SIGNAL(stopSelection(const QFeatureList&, const bool)),
419 this, SLOT(onStopSelection(const QFeatureList&, const bool)));
420 connect(aPreviewOp, SIGNAL(setSelection(const QFeatureList&)),
421 this, SLOT(onSetSelection(const QFeatureList&)));
423 connect(aPreviewOp, SIGNAL(closeLocalContext()),
424 this, SLOT(onCloseLocalContext()));
426 PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
428 connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
429 this, SLOT(onPlaneSelected(double, double, double)));
430 connect(aSketchOp, SIGNAL(fitAllView()),
431 this, SLOT(onFitAllView()));
438 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
440 static Events_ID aModuleEvent = Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED);
441 Config_PointerMessage aMessage(aModuleEvent, this);
442 aMessage.setPointer(theOperation);
443 Events_Loop::loop()->send(aMessage);
446 void PartSet_Module::visualizePreview(FeaturePtr theFeature, bool isDisplay,
447 const bool isUpdateViewer)
449 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
453 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
457 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
459 boost::shared_ptr<SketchPlugin_Feature> aSPFeature =
460 boost::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature);
463 boost::shared_ptr<GeomAPI_AISObject> anAIS =
464 aSPFeature->getAISObject(aDisplayer->getAISObject(theFeature));
465 aDisplayer->redisplay(theFeature, anAIS, false);
469 aDisplayer->erase(theFeature, false);
472 aDisplayer->updateViewer();
475 void PartSet_Module::activateFeature(FeaturePtr theFeature, const bool isUpdateViewer)
477 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
478 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
480 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
481 aDisplayer->activateInLocalContext(theFeature, aPreviewOp->getSelectionModes(theFeature),
486 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
488 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
492 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
496 FeaturePtr aFeature = aPreviewOp->feature();
497 if (!aFeature || aFeature->getKind() != theCmdId)
500 std::list<FeaturePtr> aList = aPreviewOp->subFeatures();
501 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
502 std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
504 std::list<FeaturePtr>::const_iterator anIt = aList.begin(),
506 for (; anIt != aLast; anIt++) {
507 boost::shared_ptr<SketchPlugin_Feature> aSPFeature =
508 boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
511 visualizePreview(*anIt, true, false);
512 aDisplayer->activateInLocalContext(*anIt, aModes, false);
514 aDisplayer->updateViewer();
517 void PartSet_Module::editFeature(FeaturePtr theFeature)
522 // if (theFeature->getKind() == SKETCH_KIND) {
523 FeaturePtr aFeature = theFeature;
524 if (XGUI_Tools::isModelObject(aFeature)) {
525 ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
526 aFeature = aObject->featureRef();
530 onLaunchOperation(aFeature->getKind(), aFeature);
531 updateCurrentPreview(aFeature->getKind());
536 void PartSet_Module::onStorePoint2D(FeaturePtr theFeature, const std::string& theAttribute)
538 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
539 myWorkshop->operationMgr()->currentOperation());
543 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
544 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theFeature->data()->attribute(theAttribute));
546 PartSet_Tools::setConstraints(aPreviewOp->sketch(), theFeature, theAttribute,
547 aPoint->x(), aPoint->y());
550 /*bool PartSet_Module::isFeatureEnabled(const QString& theCmdId) const
552 XGUI_OperationMgr* aOpMgr = myWorkshop->operationMgr();
553 XGUI_ActionsMgr* aActMgr = myWorkshop->actionsMgr();
555 ModuleBase_Operation* aOperation = aOpMgr->currentOperation();
557 return !aActMgr->isNested(theCmdId);
559 PartSet_OperationFeatureEdit* aSketchEdtOp = dynamic_cast<PartSet_OperationFeatureEdit*>(aOperation);
561 QStringList aConstraintList;
562 aConstraintList<<"SketchConstraintDistance"<<"SketchConstraintLength"
563 <<"SketchConstraintRadius"<<"SketchConstraintParallel"<<"SketchConstraintPerpendicular";
564 return aConstraintList.contains(theCmdId);
566 QStringList aList = aActMgr->nestedCommands(aOperation->id());
567 return aList.contains(theCmdId);
570 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
571 Config_WidgetAPI* theWidgetApi, QList<ModuleBase_ModelWidget*>& theModelWidgets)
573 if (theType == "sketch-start-label") {
574 PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi);
575 aWgt->setOperationsMgr(myWorkshop->operationMgr());
576 theModelWidgets.append(aWgt);
577 return aWgt->getControl();