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");
119 void PartSet_Module::featureCreated(QAction* theFeature)
121 connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
124 QStringList PartSet_Module::nestedFeatures(QString)
126 return QStringList();
129 std::string PartSet_Module::featureFile(const std::string& theFeatureId)
131 return myFeaturesInFiles[theFeatureId];
137 void PartSet_Module::onFeatureTriggered()
139 //PartSet_TestOCC::local_selection_change_shape(myWorkshop->viewer()->AISContext(),
140 // myWorkshop->viewer()->activeView());
142 //PartSet_TestOCC::local_selection_erase(myWorkshop->viewer()->AISContext(),
143 // myWorkshop->viewer()->activeView());
144 QAction* aCmd = dynamic_cast<QAction*>(sender());
145 //Do nothing on uncheck
146 if(aCmd->isCheckable() && !aCmd->isChecked())
148 launchOperation(aCmd->data().toString());
151 void PartSet_Module::launchOperation(const QString& theCmdId)
153 ModuleBase_Operation* anOperation = createOperation(theCmdId.toStdString());
154 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
156 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
157 // Initialise operation with preliminary selection
158 std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
159 std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
160 aPreviewOp->initSelection(aSelected, aHighlighted);
162 sendOperation(anOperation);
165 void PartSet_Module::onOperationStarted()
167 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
168 myWorkshop->operationMgr()->currentOperation());
170 XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
171 connect(aPropPanel, SIGNAL(storedPoint2D(FeaturePtr, const std::string&)),
172 this, SLOT(onStorePoint2D(FeaturePtr, const std::string&)), Qt::UniqueConnection);
176 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
180 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
182 XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
183 //disconnect(aPropPanel, SIGNAL(storedPoint2D(FeaturePtr, const std::string&)),
184 // this, SLOT(onStorePoint2D(FeaturePtr, const std::string&)));
188 void PartSet_Module::onContextMenuCommand(const QString& theId, bool isChecked)
190 QFeatureList aFeatures = myWorkshop->selector()->selection()->selectedFeatures();
191 if (theId == "EDIT_CMD" && (aFeatures.size() > 0)) {
192 editFeature(aFeatures.first());
196 void PartSet_Module::onMousePressed(QMouseEvent* theEvent)
198 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
199 myWorkshop->operationMgr()->currentOperation());
201 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
202 // Initialise operation with preliminary selection
203 std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
204 std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
206 aPreviewOp->mousePressed(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
210 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
212 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
213 myWorkshop->operationMgr()->currentOperation());
216 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
217 // Initialise operation with preliminary selection
218 std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
219 std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
221 aPreviewOp->mouseReleased(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
225 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
227 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
228 myWorkshop->operationMgr()->currentOperation());
230 aPreviewOp->mouseMoved(theEvent, myWorkshop->viewer()->activeView());
233 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
235 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
236 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
238 aPreviewOp->keyReleased(theEvent->key());
242 void PartSet_Module::onMouseDoubleClick(QMouseEvent* theEvent)
244 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
245 myWorkshop->operationMgr()->currentOperation());
248 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
249 // Initialise operation with preliminary selection
250 std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
251 std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
252 aPreviewOp->mouseDoubleClick(theEvent, myWorkshop->viewer()->activeView(), aSelected,
257 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
259 myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
260 myWorkshop->actionsMgr()->update();
262 //PartSet_TestOCC::testSelection(myWorkshop);
265 void PartSet_Module::onFitAllView()
267 myWorkshop->viewer()->fitAll();
270 void PartSet_Module::onLaunchOperation(std::string theName, FeaturePtr theFeature)
272 ModuleBase_Operation* anOperation = createOperation(theName.c_str(),
273 theFeature ? theFeature->getKind() : "");
274 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
277 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
278 // Initialise operation with preliminary selection
279 std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
280 std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
281 aPreviewOp->initFeature(theFeature);
282 aPreviewOp->initSelection(aSelected, aHighlighted);
284 anOperation->setEditingFeature(theFeature);
286 sendOperation(anOperation);
287 myWorkshop->actionsMgr()->updateCheckState();
290 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
292 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
293 aViewer->enableMultiselection(theEnabled);
296 void PartSet_Module::onStopSelection(const QFeatureList& theFeatures, const bool isStop)
298 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
300 QFeatureList::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
301 for (; anIt != aLast; anIt++) {
302 activateFeature((*anIt), false);
305 aDisplayer->stopSelection(theFeatures, isStop, false);
307 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
308 aViewer->enableSelection(!isStop);
310 aDisplayer->updateViewer();
313 void PartSet_Module::onSetSelection(const QFeatureList& theFeatures)
315 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
316 aDisplayer->setSelected(theFeatures, false);
317 aDisplayer->updateViewer();
320 void PartSet_Module::onCloseLocalContext()
322 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
323 aDisplayer->closeLocalContexts();
326 void PartSet_Module::onFeatureConstructed(FeaturePtr theFeature, int theMode)
328 bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
329 visualizePreview(theFeature, isDisplay, false);
331 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
333 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
335 std::list<FeaturePtr> aList = aPrevOp->subFeatures();
336 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
337 std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
339 std::list<FeaturePtr>::const_iterator anIt = aList.begin(),
341 for (; anIt != aLast; anIt++)
342 visualizePreview(*anIt, false, false);
343 aDisplayer->updateViewer();
347 if (theMode == PartSet_OperationSketchBase::FM_Activation ||
348 theMode == PartSet_OperationSketchBase::FM_Deactivation)
349 activateFeature(theFeature, true);
352 ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId,
353 const std::string& theFeatureKind)
355 // create the operation
356 ModuleBase_Operation* anOperation = 0;
357 if (theCmdId == PartSet_OperationSketch::Type()) {
358 anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
361 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
363 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
365 aSketch = aPrevOp->sketch();
366 if (PartSet_OperationFeatureCreate::canProcessKind(theCmdId))
367 anOperation = new PartSet_OperationFeatureCreate(theCmdId.c_str(), this, aSketch);
368 else if (theCmdId == PartSet_OperationFeatureEditMulti::Type())
369 anOperation = new PartSet_OperationFeatureEditMulti(theCmdId.c_str(), this, aSketch);
370 else if (theCmdId == PartSet_OperationFeatureEdit::Type())
371 anOperation = new PartSet_OperationFeatureEdit(theCmdId.c_str(), this, aSketch);
375 anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
378 // set operation description and list of widgets corresponding to the feature xml definition
379 std::string aFeatureKind = theFeatureKind.empty() ? theCmdId : theFeatureKind;
381 std::string aPluginFileName = featureFile(aFeatureKind);
382 Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
383 aWdgReader.readAll();
384 std::string aXmlCfg = aWdgReader.featureWidgetCfg(aFeatureKind);
385 std::string aDescription = aWdgReader.featureDescription(aFeatureKind);
387 //QString aXmlRepr = QString::fromStdString(aXmlCfg);
388 //ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(),
389 // myWorkshop->moduleConnector());
390 //QWidget* aContent = myWorkshop->propertyPanel()->contentWidget();
391 //qDeleteAll(aContent->children());
392 //aFactory.createWidget(aContent);
394 anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
395 anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
397 //anOperation->setModelWidgets(aXmlRepr.toStdString(), aFactory.getModelWidgets());
399 // connect the operation
400 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
402 connect(aPreviewOp, SIGNAL(featureConstructed(FeaturePtr, int)),
403 this, SLOT(onFeatureConstructed(FeaturePtr, int)));
404 connect(aPreviewOp, SIGNAL(launchOperation(std::string, FeaturePtr)),
405 this, SLOT(onLaunchOperation(std::string, FeaturePtr)));
406 connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
407 this, SLOT(onMultiSelectionEnabled(bool)));
409 connect(aPreviewOp, SIGNAL(stopSelection(const QFeatureList&, const bool)),
410 this, SLOT(onStopSelection(const QFeatureList&, const bool)));
411 connect(aPreviewOp, SIGNAL(setSelection(const QFeatureList&)),
412 this, SLOT(onSetSelection(const QFeatureList&)));
414 connect(aPreviewOp, SIGNAL(closeLocalContext()),
415 this, SLOT(onCloseLocalContext()));
417 PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
419 connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
420 this, SLOT(onPlaneSelected(double, double, double)));
421 connect(aSketchOp, SIGNAL(fitAllView()),
422 this, SLOT(onFitAllView()));
429 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
431 static Events_ID aModuleEvent = Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED);
432 Config_PointerMessage aMessage(aModuleEvent, this);
433 aMessage.setPointer(theOperation);
434 Events_Loop::loop()->send(aMessage);
437 void PartSet_Module::visualizePreview(FeaturePtr theFeature, bool isDisplay,
438 const bool isUpdateViewer)
440 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
444 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
448 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
450 boost::shared_ptr<SketchPlugin_Feature> aSPFeature =
451 boost::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature);
454 boost::shared_ptr<GeomAPI_AISObject> anAIS =
455 aSPFeature->getAISObject(aDisplayer->getAISObject(theFeature));
456 aDisplayer->redisplay(theFeature, anAIS, false);
460 aDisplayer->erase(theFeature, false);
463 aDisplayer->updateViewer();
466 void PartSet_Module::activateFeature(FeaturePtr theFeature, const bool isUpdateViewer)
468 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
469 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
471 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
472 aDisplayer->activateInLocalContext(theFeature, aPreviewOp->getSelectionModes(theFeature),
477 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
479 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
483 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
487 FeaturePtr aFeature = aPreviewOp->feature();
488 if (!aFeature || aFeature->getKind() != theCmdId)
491 std::list<FeaturePtr> aList = aPreviewOp->subFeatures();
492 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
493 std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
495 std::list<FeaturePtr>::const_iterator anIt = aList.begin(),
497 for (; anIt != aLast; anIt++) {
498 boost::shared_ptr<SketchPlugin_Feature> aSPFeature =
499 boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
502 visualizePreview(*anIt, true, false);
503 aDisplayer->activateInLocalContext(*anIt, aModes, false);
505 aDisplayer->updateViewer();
508 void PartSet_Module::editFeature(FeaturePtr theFeature)
513 // if (theFeature->getKind() == SketchPlugin_Sketch::ID()) {
514 FeaturePtr aFeature = theFeature;
515 if (XGUI_Tools::isModelObject(aFeature)) {
516 ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
517 aFeature = aObject->featureRef();
521 onLaunchOperation(aFeature->getKind(), aFeature);
522 updateCurrentPreview(aFeature->getKind());
527 void PartSet_Module::onStorePoint2D(FeaturePtr theFeature, const std::string& theAttribute)
529 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
530 myWorkshop->operationMgr()->currentOperation());
534 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
535 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theFeature->data()->attribute(theAttribute));
537 PartSet_Tools::setConstraints(aPreviewOp->sketch(), theFeature, theAttribute,
538 aPoint->x(), aPoint->y());
541 /*bool PartSet_Module::isFeatureEnabled(const QString& theCmdId) const
543 XGUI_OperationMgr* aOpMgr = myWorkshop->operationMgr();
544 XGUI_ActionsMgr* aActMgr = myWorkshop->actionsMgr();
546 ModuleBase_Operation* aOperation = aOpMgr->currentOperation();
548 return !aActMgr->isNested(theCmdId);
550 PartSet_OperationFeatureEdit* aSketchEdtOp = dynamic_cast<PartSet_OperationFeatureEdit*>(aOperation);
552 QStringList aConstraintList;
553 aConstraintList<<"SketchConstraintDistance"<<"SketchConstraintLength"
554 <<"SketchConstraintRadius"<<"SketchConstraintParallel"<<"SketchConstraintPerpendicular";
555 return aConstraintList.contains(theCmdId);
557 QStringList aList = aActMgr->nestedCommands(aOperation->id());
558 return aList.contains(theCmdId);
561 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
562 Config_WidgetAPI* theWidgetApi, QList<ModuleBase_ModelWidget*>& theModelWidgets)
564 if (theType == "sketch-start-label") {
565 PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi);
566 aWgt->setOperationsMgr(myWorkshop->operationMgr());
567 theModelWidgets.append(aWgt);
568 return aWgt->getControl();