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_Selection.h>
28 #include <XGUI_ViewPort.h>
29 #include <XGUI_ActionsMgr.h>
30 #include <XGUI_ViewerProxy.h>
31 #include <XGUI_ContextMenuMgr.h>
32 #include <XGUI_PropertyPanel.h>
33 #include <XGUI_ModuleConnector.h>
34 #include <XGUI_Tools.h>
36 #include <SketchPlugin_Line.h>
38 #include <Config_PointerMessage.h>
39 #include <Config_ModuleReader.h>
40 #include <Config_WidgetReader.h>
41 #include <Events_Loop.h>
42 #include <Events_Message.h>
43 #include <Events_Error.h>
45 #include <GeomAPI_Shape.h>
46 #include <GeomAPI_AISObject.h>
49 #include <QMouseEvent>
56 /*!Create and return new instance of XGUI_Module*/
57 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(XGUI_Workshop* theWshop)
59 return new PartSet_Module(theWshop);
62 PartSet_Module::PartSet_Module(XGUI_Workshop* theWshop)
64 myWorkshop = theWshop;
65 myListener = new PartSet_Listener(this);
67 XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
69 connect(anOperationMgr, SIGNAL(operationStarted()),
70 this, SLOT(onOperationStarted()));
72 connect(anOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
73 this, SLOT(onOperationStopped(ModuleBase_Operation*)));
75 XGUI_ContextMenuMgr* aContextMenuMgr = myWorkshop->contextMenuMgr();
76 connect(aContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)),
77 this, SLOT(onContextMenuCommand(const QString&, bool)));
79 connect(myWorkshop->viewer(), SIGNAL(mousePress(QMouseEvent*)),
80 this, SLOT(onMousePressed(QMouseEvent*)));
81 connect(myWorkshop->viewer(), SIGNAL(mouseRelease(QMouseEvent*)),
82 this, SLOT(onMouseReleased(QMouseEvent*)));
83 connect(myWorkshop->viewer(), SIGNAL(mouseMove(QMouseEvent*)),
84 this, SLOT(onMouseMoved(QMouseEvent*)));
85 connect(myWorkshop->viewer(), SIGNAL(keyRelease(QKeyEvent*)),
86 this, SLOT(onKeyRelease(QKeyEvent*)));
87 connect(myWorkshop->viewer(), SIGNAL(mouseDoubleClick(QMouseEvent*)),
88 this, SLOT(onMouseDoubleClick(QMouseEvent*)));
91 PartSet_Module::~PartSet_Module()
95 XGUI_Workshop* PartSet_Module::workshop() const
100 void PartSet_Module::createFeatures()
102 Config_ModuleReader aXMLReader = Config_ModuleReader();
103 aXMLReader.readAll();
104 myFeaturesInFiles = aXMLReader.featuresInFiles();
107 void PartSet_Module::featureCreated(QAction* theFeature)
109 connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
112 QStringList PartSet_Module::nestedFeatures(QString)
114 return QStringList();
117 std::string PartSet_Module::featureFile(const std::string& theFeatureId)
119 return myFeaturesInFiles[theFeatureId];
125 void PartSet_Module::onFeatureTriggered()
127 //PartSet_TestOCC::local_selection_change_shape(myWorkshop->viewer()->AISContext(),
128 // myWorkshop->viewer()->activeView());
130 //PartSet_TestOCC::local_selection_erase(myWorkshop->viewer()->AISContext(),
131 // myWorkshop->viewer()->activeView());
132 QAction* aCmd = dynamic_cast<QAction*>(sender());
133 //Do nothing on uncheck
134 if(aCmd->isCheckable() && !aCmd->isChecked())
136 launchOperation(aCmd->data().toString());
139 void PartSet_Module::launchOperation(const QString& theCmdId)
141 ModuleBase_Operation* anOperation = createOperation(theCmdId.toStdString());
142 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
144 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
145 // Initialise operation with preliminary selection
146 std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
147 std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
148 aPreviewOp->initSelection(aSelected, aHighlighted);
150 sendOperation(anOperation);
153 void PartSet_Module::onOperationStarted()
155 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
156 myWorkshop->operationMgr()->currentOperation());
158 XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
159 connect(aPropPanel, SIGNAL(storedPoint2D(FeaturePtr, const std::string&)),
160 this, SLOT(onStorePoint2D(FeaturePtr, const std::string&)), Qt::UniqueConnection);
164 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
168 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
170 XGUI_PropertyPanel* aPropPanel = myWorkshop->propertyPanel();
171 //disconnect(aPropPanel, SIGNAL(storedPoint2D(FeaturePtr, const std::string&)),
172 // this, SLOT(onStorePoint2D(FeaturePtr, const std::string&)));
176 void PartSet_Module::onContextMenuCommand(const QString& theId, bool isChecked)
178 QFeatureList aFeatures = myWorkshop->selector()->selection()->selectedFeatures();
179 if (theId == "EDIT_CMD" && (aFeatures.size() > 0)) {
180 editFeature(aFeatures.first());
184 void PartSet_Module::onMousePressed(QMouseEvent* theEvent)
186 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
187 myWorkshop->operationMgr()->currentOperation());
189 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
190 // Initialise operation with preliminary selection
191 std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
192 std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
194 aPreviewOp->mousePressed(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
198 void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
200 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
201 myWorkshop->operationMgr()->currentOperation());
204 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
205 // Initialise operation with preliminary selection
206 std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
207 std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
209 aPreviewOp->mouseReleased(theEvent, myWorkshop->viewer()->activeView(), aSelected, aHighlighted);
213 void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
215 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
216 myWorkshop->operationMgr()->currentOperation());
218 aPreviewOp->mouseMoved(theEvent, myWorkshop->viewer()->activeView());
221 void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
223 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
224 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
226 aPreviewOp->keyReleased(theEvent->key());
230 void PartSet_Module::onMouseDoubleClick(QMouseEvent* theEvent)
232 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
233 myWorkshop->operationMgr()->currentOperation());
236 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
237 // Initialise operation with preliminary selection
238 std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
239 std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
240 aPreviewOp->mouseDoubleClick(theEvent, myWorkshop->viewer()->activeView(), aSelected,
245 void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
247 myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
248 myWorkshop->actionsMgr()->update();
250 //PartSet_TestOCC::testSelection(myWorkshop);
253 void PartSet_Module::onFitAllView()
255 myWorkshop->viewer()->fitAll();
258 void PartSet_Module::onLaunchOperation(std::string theName, FeaturePtr theFeature)
260 ModuleBase_Operation* anOperation = createOperation(theName.c_str(),
261 theFeature ? theFeature->getKind() : "");
262 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
265 XGUI_Selection* aSelection = myWorkshop->selector()->selection();
266 // Initialise operation with preliminary selection
267 std::list<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
268 std::list<ModuleBase_ViewerPrs> aHighlighted = aSelection->getHighlighted();
269 aPreviewOp->initFeature(theFeature);
270 aPreviewOp->initSelection(aSelected, aHighlighted);
272 anOperation->setEditingFeature(theFeature);
274 sendOperation(anOperation);
275 myWorkshop->actionsMgr()->updateCheckState();
278 void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
280 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
281 aViewer->enableMultiselection(theEnabled);
284 void PartSet_Module::onStopSelection(const QFeatureList& theFeatures, const bool isStop)
286 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
288 QFeatureList::const_iterator anIt = theFeatures.begin(), aLast = theFeatures.end();
289 for (; anIt != aLast; anIt++) {
290 activateFeature((*anIt), false);
293 aDisplayer->stopSelection(theFeatures, isStop, false);
295 XGUI_ViewerProxy* aViewer = myWorkshop->viewer();
296 aViewer->enableSelection(!isStop);
298 aDisplayer->updateViewer();
301 void PartSet_Module::onSetSelection(const QFeatureList& theFeatures)
303 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
304 aDisplayer->setSelected(theFeatures, false);
305 aDisplayer->updateViewer();
308 void PartSet_Module::onCloseLocalContext()
310 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
311 aDisplayer->closeLocalContexts();
314 void PartSet_Module::onFeatureConstructed(FeaturePtr theFeature, int theMode)
316 bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
317 visualizePreview(theFeature, isDisplay, false);
319 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
321 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
323 std::list<FeaturePtr> aList = aPrevOp->subFeatures();
324 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
325 std::list<int> aModes = aPrevOp->getSelectionModes(aPrevOp->feature());
327 std::list<FeaturePtr>::const_iterator anIt = aList.begin(),
329 for (; anIt != aLast; anIt++)
330 visualizePreview(*anIt, false, false);
331 aDisplayer->updateViewer();
335 if (theMode == PartSet_OperationSketchBase::FM_Activation ||
336 theMode == PartSet_OperationSketchBase::FM_Deactivation)
337 activateFeature(theFeature, true);
340 ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId,
341 const std::string& theFeatureKind)
343 // create the operation
344 ModuleBase_Operation* anOperation = 0;
345 if (theCmdId == PartSet_OperationSketch::Type()) {
346 anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
349 ModuleBase_Operation* aCurOperation = myWorkshop->operationMgr()->currentOperation();
351 PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
353 aSketch = aPrevOp->sketch();
354 if (PartSet_OperationFeatureCreate::canProcessKind(theCmdId))
355 anOperation = new PartSet_OperationFeatureCreate(theCmdId.c_str(), this, aSketch);
356 else if (theCmdId == PartSet_OperationFeatureEditMulti::Type())
357 anOperation = new PartSet_OperationFeatureEditMulti(theCmdId.c_str(), this, aSketch);
358 else if (theCmdId == PartSet_OperationFeatureEdit::Type())
359 anOperation = new PartSet_OperationFeatureEdit(theCmdId.c_str(), this, aSketch);
363 anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
366 // set operation description and list of widgets corresponding to the feature xml definition
367 std::string aFeatureKind = theFeatureKind.empty() ? theCmdId : theFeatureKind;
369 std::string aPluginFileName = featureFile(aFeatureKind);
370 Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
371 aWdgReader.readAll();
372 std::string aXmlCfg = aWdgReader.featureWidgetCfg(aFeatureKind);
373 std::string aDescription = aWdgReader.featureDescription(aFeatureKind);
375 //QString aXmlRepr = QString::fromStdString(aXmlCfg);
376 //ModuleBase_WidgetFactory aFactory = ModuleBase_WidgetFactory(aXmlRepr.toStdString(),
377 // myWorkshop->moduleConnector());
378 //QWidget* aContent = myWorkshop->propertyPanel()->contentWidget();
379 //qDeleteAll(aContent->children());
380 //aFactory.createWidget(aContent);
382 anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
383 anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
385 //anOperation->setModelWidgets(aXmlRepr.toStdString(), aFactory.getModelWidgets());
387 // connect the operation
388 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
390 connect(aPreviewOp, SIGNAL(featureConstructed(FeaturePtr, int)),
391 this, SLOT(onFeatureConstructed(FeaturePtr, int)));
392 connect(aPreviewOp, SIGNAL(launchOperation(std::string, FeaturePtr)),
393 this, SLOT(onLaunchOperation(std::string, FeaturePtr)));
394 connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)),
395 this, SLOT(onMultiSelectionEnabled(bool)));
397 connect(aPreviewOp, SIGNAL(stopSelection(const QFeatureList&, const bool)),
398 this, SLOT(onStopSelection(const QFeatureList&, const bool)));
399 connect(aPreviewOp, SIGNAL(setSelection(const QFeatureList&)),
400 this, SLOT(onSetSelection(const QFeatureList&)));
402 connect(aPreviewOp, SIGNAL(closeLocalContext()),
403 this, SLOT(onCloseLocalContext()));
405 PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
407 connect(aSketchOp, SIGNAL(planeSelected(double, double, double)),
408 this, SLOT(onPlaneSelected(double, double, double)));
409 connect(aSketchOp, SIGNAL(fitAllView()),
410 this, SLOT(onFitAllView()));
417 void PartSet_Module::sendOperation(ModuleBase_Operation* theOperation)
419 static Events_ID aModuleEvent = Events_Loop::eventByName(EVENT_OPERATION_LAUNCHED);
420 Config_PointerMessage aMessage(aModuleEvent, this);
421 aMessage.setPointer(theOperation);
422 Events_Loop::loop()->send(aMessage);
425 void PartSet_Module::visualizePreview(FeaturePtr theFeature, bool isDisplay,
426 const bool isUpdateViewer)
428 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
432 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
436 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
438 boost::shared_ptr<SketchPlugin_Feature> aSPFeature =
439 boost::dynamic_pointer_cast<SketchPlugin_Feature>(theFeature);
442 boost::shared_ptr<GeomAPI_AISObject> anAIS =
443 aSPFeature->getAISObject(aDisplayer->getAISObject(theFeature));
444 aDisplayer->redisplay(theFeature, anAIS, false);
448 aDisplayer->erase(theFeature, false);
451 aDisplayer->updateViewer();
454 void PartSet_Module::activateFeature(FeaturePtr theFeature, const bool isUpdateViewer)
456 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
457 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
459 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
460 aDisplayer->activateInLocalContext(theFeature, aPreviewOp->getSelectionModes(theFeature),
465 void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
467 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
471 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
475 FeaturePtr aFeature = aPreviewOp->feature();
476 if (!aFeature || aFeature->getKind() != theCmdId)
479 std::list<FeaturePtr> aList = aPreviewOp->subFeatures();
480 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
481 std::list<int> aModes = aPreviewOp->getSelectionModes(aPreviewOp->feature());
483 std::list<FeaturePtr>::const_iterator anIt = aList.begin(),
485 for (; anIt != aLast; anIt++) {
486 boost::shared_ptr<SketchPlugin_Feature> aSPFeature =
487 boost::dynamic_pointer_cast<SketchPlugin_Feature>(*anIt);
490 visualizePreview(*anIt, true, false);
491 aDisplayer->activateInLocalContext(*anIt, aModes, false);
493 aDisplayer->updateViewer();
496 void PartSet_Module::editFeature(FeaturePtr theFeature)
501 // if (theFeature->getKind() == SKETCH_KIND) {
502 FeaturePtr aFeature = theFeature;
503 if (XGUI_Tools::isModelObject(aFeature)) {
504 ObjectPtr aObject = boost::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
505 aFeature = aObject->featureRef();
509 onLaunchOperation(aFeature->getKind(), aFeature);
510 updateCurrentPreview(aFeature->getKind());
515 void PartSet_Module::onStorePoint2D(FeaturePtr theFeature, const std::string& theAttribute)
517 PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(
518 myWorkshop->operationMgr()->currentOperation());
522 boost::shared_ptr<GeomDataAPI_Point2D> aPoint =
523 boost::dynamic_pointer_cast<GeomDataAPI_Point2D>(theFeature->data()->attribute(theAttribute));
525 PartSet_Tools::setConstraints(aPreviewOp->sketch(), theFeature, theAttribute,
526 aPoint->x(), aPoint->y());
529 /*bool PartSet_Module::isFeatureEnabled(const QString& theCmdId) const
531 XGUI_OperationMgr* aOpMgr = myWorkshop->operationMgr();
532 XGUI_ActionsMgr* aActMgr = myWorkshop->actionsMgr();
534 ModuleBase_Operation* aOperation = aOpMgr->currentOperation();
536 return !aActMgr->isNested(theCmdId);
538 PartSet_OperationFeatureEdit* aSketchEdtOp = dynamic_cast<PartSet_OperationFeatureEdit*>(aOperation);
540 QStringList aConstraintList;
541 aConstraintList<<"SketchConstraintDistance"<<"SketchConstraintLength"
542 <<"SketchConstraintRadius"<<"SketchConstraintParallel"<<"SketchConstraintPerpendicular";
543 return aConstraintList.contains(theCmdId);
545 QStringList aList = aActMgr->nestedCommands(aOperation->id());
546 return aList.contains(theCmdId);
549 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
550 Config_WidgetAPI* theWidgetApi, QList<ModuleBase_ModelWidget*>& theModelWidgets)
552 if (theType == "sketch-start-label") {
553 PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi);
554 aWgt->setOperationsMgr(myWorkshop->operationMgr());
555 theModelWidgets.append(aWgt);
556 return aWgt->getControl();