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 QList<ObjectPtr> aFeatures = myWorkshop->selector()->selection()->selectedObjects();
190 if (theId == "EDIT_CMD" && (aFeatures.size() > 0)) {
191 FeaturePtr aFeature = boost::dynamic_pointer_cast<ModelAPI_Feature>(aFeatures.first());
193 editFeature(aFeature);
197 void PartSet_Module::onMousePressed(QMouseEvent* theEvent)
199 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
200 myWorkshop->operationMgr()->currentOperation());
202 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
203 // Initialise operation with preliminary selection
204 std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
205 std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
207 aPreviewOp->mousePressed(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
211 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
213 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
214 myWorkshop->operationMgr()->currentOperation());
217 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
218 // Initialise operation with preliminary selection
219 std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
220 std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
222 aPreviewOp->mouseReleased(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
226 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
228 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
229 myWorkshop->operationMgr()->currentOperation());
231 aPreviewOp->mouseMoved(theEvent, myWorkshop->viewer()->activeView());
234 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
236 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
237 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
239 aPreviewOp->keyReleased(theEvent->key());
243 void PartSet_Module::onMouseDoubleClick(QMouseEvent* theEvent)
245 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
246 myWorkshop->operationMgr()->currentOperation());
249 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
250 // Initialise operation with preliminary selection
251 std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
252 std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
253 aPreviewOp->mouseDoubleClick(theEvent, myWorkshop->viewer()->activeView(), aSelected,
258 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
260 myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
261 myWorkshop->actionsMgr()->update();
263 //PartSet_TestOCC::testSelection(myWorkshop);
266 void PartSet_Module::onFitAllView()
268 myWorkshop->viewer()->fitAll();
271 void PartSet_Module::onLaunchOperation(std::string theName, FeaturePtr theFeature)
273 ModuleBase_Operation* anOperation = createOperation(theName.c_str(),
274 theFeature ? theFeature->getKind() : "");
275 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
278 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
279 // Initialise operation with preliminary selection
280 std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
281 std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
282 aPreviewOp->initFeature(theFeature);
283 aPreviewOp->initSelection(aSelected, aHighlighted);
285 anOperation->setEditingFeature(theFeature);
287 sendOperation(anOperation);
288 myWorkshop->actionsMgr()->updateCheckState();
291 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
293 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
294 aViewer->enableMultiselection(theEnabled);
297 void PartSet_Module::onStopSelection(const QFeatureList& theFeatures, const bool isStop)
299 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
301 QFeatureList::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
302 for (; anIt != aLast; anIt++) {
303 activateFeature((*anIt), false);
306 QResultList aResults;
307 foreach(FeaturePtr aFeature, theFeatures) {
308 if (aFeature->results().size() > 0) {
309 std::list<ResultPtr>& aResList = aFeature->results();
310 std::list<ResultPtr>::iterator aIt;
311 for (aIt = aResList.begin(); aIt != aResList.end(); ++aIt)
312 aResults.append(*aIt);
315 aDisplayer->stopSelection(aResults, isStop, false);
317 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
318 aViewer->enableSelection(!isStop);
320 aDisplayer->updateViewer();
323 void PartSet_Module::onSetSelection(const QResultList& theFeatures)
325 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
326 aDisplayer->setSelected(theFeatures, false);
327 aDisplayer->updateViewer();
330 void PartSet_Module::onCloseLocalContext()
332 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
333 aDisplayer->closeLocalContexts();
336 void PartSet_Module::onFeatureConstructed(FeaturePtr theFeature, int theMode)
338 bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
339 // TODO visualizePreview(theFeature, isDisplay, false);
341 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
343 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
345 std::list<FeaturePtr> aList = aPrevOp->subFeatures();
346 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
347 std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
349 std::list<FeaturePtr>::const_iterator anIt = aList.begin(),
351 for (; anIt != aLast; anIt++)
352 visualizePreview((*anIt)->firstResult(), false, false);
353 aDisplayer->updateViewer();
357 if (theMode == PartSet_OperationSketchBase::FM_Activation ||
358 theMode == PartSet_OperationSketchBase::FM_Deactivation)
359 activateFeature(theFeature, true);
362 ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId,
363 const std::string& theFeatureKind)
365 // create the operation
366 ModuleBase_Operation* anOperation = 0;
367 if (theCmdId == PartSet_OperationSketch::Type()) {
368 anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
371 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
373 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
375 aSketch = aPrevOp->sketch();
376 if (PartSet_OperationFeatureCreate::canProcessKind(theCmdId))
377 anOperation = new PartSet_OperationFeatureCreate(theCmdId.c_str(), this, aSketch);
378 else if (theCmdId == PartSet_OperationFeatureEditMulti::Type())
379 anOperation = new PartSet_OperationFeatureEditMulti(theCmdId.c_str(), this, aSketch);
380 else if (theCmdId == PartSet_OperationFeatureEdit::Type())
381 anOperation = new PartSet_OperationFeatureEdit(theCmdId.c_str(), this, aSketch);
385 anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
388 // set operation description and list of widgets corresponding to the feature xml definition
389 std::string aFeatureKind = theFeatureKind.empty() ? theCmdId : theFeatureKind;
391 std::string aPluginFileName = featureFile(aFeatureKind);
392 Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
393 aWdgReader.readAll();
394 std::string aXmlCfg = aWdgReader.featureWidgetCfg(aFeatureKind);
395 std::string aDescription = aWdgReader.featureDescription(aFeatureKind);
397 //QString aXmlRepr = QString::fromStdString(aXmlCfg);
398 //ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(),
399 // myWorkshop->moduleConnector());
400 //QWidget* aContent = myWorkshop->propertyPanel()->contentWidget();
401 //qDeleteAll(aContent->children());
402 //aFactory.createWidget(aContent);
404 anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
405 anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
407 //anOperation->setModelWidgets(aXmlRepr.toStdString(), aFactory.getModelWidgets());
409 // connect the operation
410 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
412 connect(aPreviewOp, SIGNAL(featureConstructed(FeaturePtr, int)),
413 this, SLOT(onFeatureConstructed(FeaturePtr, int)));
414 connect(aPreviewOp, SIGNAL(launchOperation(std::string, FeaturePtr)),
415 this, SLOT(onLaunchOperation(std::string, FeaturePtr)));
416 connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
417 this, SLOT(onMultiSelectionEnabled(bool)));
419 connect(aPreviewOp, SIGNAL(stopSelection(const QFeatureList&, const bool)),
420 this, SLOT(onStopSelection(const QFeatureList&, const bool)));
421 connect(aPreviewOp, SIGNAL(setSelection(const QFeatureList&)),
422 this, SLOT(onSetSelection(const QFeatureList&)));
424 connect(aPreviewOp, SIGNAL(closeLocalContext()),
425 this, SLOT(onCloseLocalContext()));
427 PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
429 connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
430 this, SLOT(onPlaneSelected(double, double, double)));
431 connect(aSketchOp, SIGNAL(fitAllView()),
432 this, SLOT(onFitAllView()));
439 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
441 static Events_ID aModuleEvent = Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED);
442 Config_PointerMessage aMessage(aModuleEvent, this);
443 aMessage.setPointer(theOperation);
444 Events_Loop::loop()->send(aMessage);
447 void PartSet_Module::visualizePreview(ResultPtr theFeature, bool isDisplay,
448 const bool isUpdateViewer)
450 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
454 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
458 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
460 boost::shared_ptr<SketchPlugin_Feature> aSPFeature =
461 boost::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature);
464 boost::shared_ptr<GeomAPI_AISObject> anAIS =
465 aSPFeature->getAISObject(aDisplayer->getAISObject(theFeature));
466 aDisplayer->redisplay(theFeature, anAIS, false);
470 aDisplayer->erase(theFeature, false);
473 aDisplayer->updateViewer();
476 void PartSet_Module::activateFeature(FeaturePtr theFeature, const bool isUpdateViewer)
478 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
479 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
481 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
482 aDisplayer->activateInLocalContext(theFeature->firstResult(), aPreviewOp->getSelectionModes(theFeature),
487 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
489 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
493 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
497 FeaturePtr aFeature = aPreviewOp->feature();
498 if (!aFeature || aFeature->getKind() != theCmdId)
501 std::list<FeaturePtr> aList = aPreviewOp->subFeatures();
502 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
503 std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
505 std::list<FeaturePtr>::const_iterator anIt = aList.begin(),
507 for (; anIt != aLast; anIt++) {
508 boost::shared_ptr<SketchPlugin_Feature> aSPFeature =
509 boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
512 visualizePreview((*anIt)->firstResult(), true, false);
513 aDisplayer->activateInLocalContext((*anIt)->firstResult(), aModes, false);
515 aDisplayer->updateViewer();
518 void PartSet_Module::editFeature(FeaturePtr theFeature)
523 // if (theFeature->getKind() == SKETCH_KIND) {
524 //FeaturePtr aFeature = theFeature;
525 //if (XGUI_Tools::isModelObject(aFeature)) {
526 // ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
527 // aFeature = aObject->featureRef();
531 onLaunchOperation(theFeature->getKind(), theFeature);
532 updateCurrentPreview(theFeature->getKind());
537 void PartSet_Module::onStorePoint2D(FeaturePtr theFeature, const std::string& theAttribute)
539 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
540 myWorkshop->operationMgr()->currentOperation());
544 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
545 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theFeature->data()->attribute(theAttribute));
547 PartSet_Tools::setConstraints(aPreviewOp->sketch(), theFeature, theAttribute,
548 aPoint->x(), aPoint->y());
551 /*bool PartSet_Module::isFeatureEnabled(const QString& theCmdId) const
553 XGUI_OperationMgr* aOpMgr = myWorkshop->operationMgr();
554 XGUI_ActionsMgr* aActMgr = myWorkshop->actionsMgr();
556 ModuleBase_Operation* aOperation = aOpMgr->currentOperation();
558 return !aActMgr->isNested(theCmdId);
560 PartSet_OperationFeatureEdit* aSketchEdtOp = dynamic_cast<PartSet_OperationFeatureEdit*>(aOperation);
562 QStringList aConstraintList;
563 aConstraintList<<"SketchConstraintDistance"<<"SketchConstraintLength"
564 <<"SketchConstraintRadius"<<"SketchConstraintParallel"<<"SketchConstraintPerpendicular";
565 return aConstraintList.contains(theCmdId);
567 QStringList aList = aActMgr->nestedCommands(aOperation->id());
568 return aList.contains(theCmdId);
571 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
572 Config_WidgetAPI* theWidgetApi, QList<ModuleBase_ModelWidget*>& theModelWidgets)
574 if (theType == "sketch-start-label") {
575 PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi);
576 aWgt->setOperationsMgr(myWorkshop->operationMgr());
577 theModelWidgets.append(aWgt);
578 return aWgt->getControl();