Salome HOME
Using of SelectionMgr instead of using Viewer directly in Sketcher starting operation
[modules/shaper.git] / src / PartSet / PartSet_Module.cpp
index 7f87997da0b2c0c3b1ad50f5bba8f66445f7da28..4d0b8867e05f7b09761547a7703032ecae08f3d2 100644 (file)
@@ -1,10 +1,25 @@
 #include <PartSet_Module.h>
-#include <Config_WidgetMessage.h>
+#include <PartSet_OperationSketch.h>
+#include <PartSet_OperationSketchLine.h>
 
+#include <ModuleBase_Operation.h>
+
+#include <XGUI_MainWindow.h>
+#include <XGUI_Displayer.h>
+#include <XGUI_Viewer.h>
+#include <XGUI_Workshop.h>
+#include <XGUI_OperationMgr.h>
+#include <XGUI_SelectionMgr.h>
+
+#include <Config_PointerMessage.h>
 #include <Config_ModuleReader.h>
 #include <Config_WidgetReader.h>
-#include <Event_Loop.h>
-#include <Event_Message.h>
+#include <Events_Loop.h>
+#include <Events_Message.h>
+
+#include <GeomAPI_Shape.h>
+
+#include <AIS_ListOfInteractive.hxx>
 
 #include <QObject>
 #include <QString>
@@ -23,6 +38,13 @@ extern "C" PARTSET_EXPORT XGUI_Module* createModule(XGUI_Workshop* theWshop)
 PartSet_Module::PartSet_Module(XGUI_Workshop* theWshop)
 {
   myWorkshop = theWshop;
+  XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
+
+  connect(anOperationMgr, SIGNAL(operationStarted()), this, SLOT(onOperationStarted()));
+  connect(anOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
+          this, SLOT(onOperationStopped(ModuleBase_Operation*)));
+  connect(myWorkshop->selector(), SIGNAL(selectionChanged()),
+            this, SLOT(onViewSelectionChanged()));
 }
 
 PartSet_Module::~PartSet_Module()
@@ -31,35 +53,124 @@ PartSet_Module::~PartSet_Module()
 
 void PartSet_Module::createFeatures()
 {
-  Config_ModuleReader* aXMLReader = new Config_ModuleReader();
-  aXMLReader->setAutoImport(true);
-  aXMLReader->readAll();
-  delete aXMLReader;
+  Config_ModuleReader aXMLReader = Config_ModuleReader();
+  aXMLReader.readAll();
+  myFeaturesInFiles = aXMLReader.featuresInFiles();
 }
 
 void PartSet_Module::featureCreated(XGUI_Command* theFeature)
 {
-  QString aFtId = theFeature->getId();
-  if (aFtId == "new_point") {
-    theFeature->connectTo(this, SLOT(onCommandTriggered()));
-  }
+  theFeature->connectTo(this, SLOT(onFeatureTriggered()));
 }
 
-void PartSet_Module::onCommandTriggered()
+std::string PartSet_Module::featureFile(const std::string& theFeatureId)
+{
+  return myFeaturesInFiles[theFeatureId];
+}
+
+/*
+ *
+ */
+void PartSet_Module::onFeatureTriggered()
 {
-  Config_ModuleReader aModuleReader = Config_ModuleReader();
-  aModuleReader.readAll();
-  std::map<std::string, std::string> aPluginMap = aModuleReader.plugins();
-  std::string aPluginName = aPluginMap["PartSetPlugin"];
-  Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginName);
-  aWdgReader.readAll();
   XGUI_Command* aCmd = dynamic_cast<XGUI_Command*>(sender());
-  std::string aCmdId = aCmd->getId().toStdString();
-  std::string aXMLWidgetCfg = aWdgReader.featureWidgetCfg(aCmdId);
+  launchOperation(aCmd->id());
+}
+  
+void PartSet_Module::launchOperation(const QString& theCmdId)
+{
+  std::string aStdCmdId = theCmdId.toStdString();
+  std::string aPluginFileName = featureFile(aStdCmdId);
+  Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
+  aWdgReader.readAll();
+  std::string aXmlCfg = aWdgReader.featureWidgetCfg(aStdCmdId);
+  std::string aDescription = aWdgReader.featureDescription(aStdCmdId);
+  ModuleBase_PropPanelOperation* aPartSetOp;
+  if (theCmdId == "Sketch" ) {
+    aPartSetOp = new PartSet_OperationSketch(theCmdId, this);
+  }
+  else if(theCmdId == "SketchLine") {
+    ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
+    boost::shared_ptr<ModelAPI_Feature> aSketchFeature;
+    if (anOperation)
+      aSketchFeature = anOperation->feature();
+    aPartSetOp = new PartSet_OperationSketchLine(theCmdId, this, aSketchFeature);
+  }
+  else {
+    aPartSetOp = new ModuleBase_PropPanelOperation(theCmdId, this);
+  }
+  aPartSetOp->setXmlRepresentation(QString::fromStdString(aXmlCfg));
+  aPartSetOp->setDescription(QString::fromStdString(aDescription));
+
   //TODO(sbh): Implement static method to extract event id [SEID]
-  static Event_ID aModuleEvent = Event_Loop::eventByName("PartSetModuleEvent");
-  Config_WidgetMessage aMessage(aModuleEvent, this);
-  aMessage.setFeatureId(aCmdId);
-  aMessage.setXmlRepresentation(aXMLWidgetCfg);
-  Event_Loop::loop()->send(aMessage);
+  static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetModuleEvent");
+  Config_PointerMessage aMessage(aModuleEvent, this);
+  aMessage.setPointer(aPartSetOp);
+  Events_Loop::loop()->send(aMessage);
+}
+
+void PartSet_Module::onOperationStarted()
+{
+  ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
+
+  PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
+  if (aPreviewOp) {
+    visualizePreview(true);
+    connect(aPreviewOp, SIGNAL(viewerProjectionChange(double, double, double)),
+            this, SLOT(onViewerProjectionChange(double, double, double)));
+  }
+}
+
+void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
+{
+  ModuleBase_PropPanelOperation* anOperation = dynamic_cast<ModuleBase_PropPanelOperation*>(theOperation);
+  if (!anOperation)
+    return;
+  PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
+  if (aPreviewOp)
+    visualizePreview(false);
+}
+
+void PartSet_Module::onViewSelectionChanged()
+{
+  ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
+  PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
+  if (aPreviewOp) {
+    XGUI_SelectionMgr* aSelector = myWorkshop->selector();
+    if (aSelector) {
+      NCollection_List<TopoDS_Shape> aList;
+      aSelector->selectedShapes(aList);
+      aPreviewOp->setSelectedShapes(aList);
+    }
+  }
+}
+
+void PartSet_Module::onViewerProjectionChange(double theX, double theY, double theZ)
+{
+  XGUI_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
+  if (aViewer) {
+    aViewer->setViewProjection(theX, theY, theZ);
+  }
+}
+
+void PartSet_Module::visualizePreview(bool isDisplay)
+{
+  ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
+  if (!anOperation)
+    return;
+
+  PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
+  if (!aPreviewOp)
+    return;
+
+  if (isDisplay) {
+    boost::shared_ptr<GeomAPI_Shape> aPreview = aPreviewOp->preview();
+    if (aPreview)
+      myWorkshop->displayer()->LocalSelection(anOperation->feature(),
+                                   aPreview->impl<TopoDS_Shape>(), aPreviewOp->getSelectionMode());
+  }
+  else {
+    myWorkshop->displayer()->GlobalSelection(false);
+    myWorkshop->displayer()->Erase(anOperation->feature());
+  }
 }