Salome HOME
Merge branch 'master' of newgeom:newgeom
[modules/shaper.git] / src / PartSet / PartSet_Module.cpp
index f82eee4b2249fe239766cb03c221710028381f39..bc383d3019f745a6025c688d96652a46685c3315 100644 (file)
@@ -1,5 +1,8 @@
 #include <PartSet_Module.h>
 #include <PartSet_OperationSketch.h>
+#include <PartSet_OperationSketchLine.h>
+#include <PartSet_Listener.h>
+#include <PartSet_Tools.h>
 
 #include <ModuleBase_Operation.h>
 
 #include <XGUI_Viewer.h>
 #include <XGUI_Workshop.h>
 #include <XGUI_OperationMgr.h>
+#include <XGUI_ViewWindow.h>
+#include <XGUI_SelectionMgr.h>
+#include <XGUI_ViewPort.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>
@@ -32,9 +42,19 @@ extern "C" PARTSET_EXPORT XGUI_Module* createModule(XGUI_Workshop* theWshop)
 PartSet_Module::PartSet_Module(XGUI_Workshop* theWshop)
 {
   myWorkshop = theWshop;
+  myListener = new PartSet_Listener(this);
+
   XGUI_OperationMgr* anOperationMgr = myWorkshop->operationMgr();
 
-  connect(anOperationMgr, SIGNAL(beforeOperationStart()), this, SLOT(onBeforeOperationStart()));
+  connect(anOperationMgr, SIGNAL(operationStarted()), this, SLOT(onOperationStarted()));
+  connect(anOperationMgr, SIGNAL(operationStopped(ModuleBase_Operation*)),
+          this, SLOT(onOperationStopped(ModuleBase_Operation*)));
+  if (!myWorkshop->isSalomeMode()) {
+    connect(myWorkshop->mainWindow()->viewer(), SIGNAL(mouseReleased(QPoint)),
+            this, SLOT(onMouseReleased(QPoint)));
+    connect(myWorkshop->mainWindow()->viewer(), SIGNAL(mouseMoved(QPoint)),
+            this, SLOT(onMouseMoved(QPoint)));
+  }
 }
 
 PartSet_Module::~PartSet_Module()
@@ -44,8 +64,8 @@ PartSet_Module::~PartSet_Module()
 void PartSet_Module::createFeatures()
 {
   Config_ModuleReader aXMLReader = Config_ModuleReader();
-  aXMLReader.setAutoImport(true);
   aXMLReader.readAll();
+  myFeaturesInFiles = aXMLReader.featuresInFiles();
 }
 
 void PartSet_Module::featureCreated(XGUI_Command* theFeature)
@@ -53,13 +73,14 @@ void PartSet_Module::featureCreated(XGUI_Command* theFeature)
   theFeature->connectTo(this, SLOT(onFeatureTriggered()));
 }
 
-std::string PartSet_Module::modulePlugin()
+QStringList PartSet_Module::nestedFeatures(QString)
 {
-  Config_ModuleReader aModuleReader = Config_ModuleReader();
-  aModuleReader.readAll();
-  std::map < std::string, std::string > aPluginMap = aModuleReader.plugins();
-  std::string aPluginName = aPluginMap["PartSetPlugin"];
-  return aPluginName;
+  return QStringList();
+}
+
+std::string PartSet_Module::featureFile(const std::string& theFeatureId)
+{
+  return myFeaturesInFiles[theFeatureId];
 }
 
 /*
@@ -67,72 +88,135 @@ std::string PartSet_Module::modulePlugin()
  */
 void PartSet_Module::onFeatureTriggered()
 {
-  std::string aPluginName = modulePlugin();
-  Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginName);
-  aWdgReader.readAll();
   XGUI_Command* aCmd = dynamic_cast<XGUI_Command*>(sender());
-  QString aCmdId = aCmd->id();
-  std::string aXmlCfg = aWdgReader.featureWidgetCfg(aCmdId.toStdString());
-  std::string aDescription = aWdgReader.featureDescription(aCmdId.toStdString());
+  //Do nothing on uncheck
+  if(aCmd->isCheckable() && !aCmd->isChecked())
+    return;
+  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 (aCmdId == "Sketch" ) {
-    aPartSetOp = new PartSet_OperationSketch(aCmdId, this);
-  } else {
-    aPartSetOp = new ModuleBase_PropPanelOperation(aCmdId, this);
+  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");
+  static Events_ID aModuleEvent = Events_Loop::eventByName("PartSetModuleEvent");
   Config_PointerMessage aMessage(aModuleEvent, this);
   aMessage.setPointer(aPartSetOp);
-  Event_Loop::loop()->send(aMessage);
+  Events_Loop::loop()->send(aMessage);
 }
 
-/**
- * Slot that is called by the operation requiring of preview display or erase
- * \param isDisplay the display or erase state
-*/
-void PartSet_Module::onVisualizePreview(bool isDisplay)
+void PartSet_Module::onOperationStarted()
 {
   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
-  if (!anOperation)
-    return;
 
   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
-  if (!aPreviewOp)
-    return;
+  if (aPreviewOp) {
+    visualizePreview(true);
+    connect(aPreviewOp, SIGNAL(viewerProjectionChange(double, double, double)),
+            this, SLOT(onViewerProjectionChange(double, double, double)));
+  }
+}
 
-  if (isDisplay)
-    myWorkshop->displayer()->Display(anOperation->feature(), aPreviewOp->preview());
-  else
-    myWorkshop->displayer()->Erase(anOperation->feature(), aPreviewOp->preview());
+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::onBeforeOperationStart()
+void PartSet_Module::onMouseReleased(QPoint thePoint)
 {
   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);
+      XGUI_ViewWindow* aWindow = myWorkshop->mainWindow()->viewer()->activeViewWindow();
+      if (aWindow) {
+        Handle(V3d_View) aView3d = aWindow->viewPort()->getView();
+        if ( !aView3d.IsNull() ) {
+          gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(thePoint, aView3d);
+          aPreviewOp->setSelectedShapes(aList, aPoint);
+        }
+      }
+    }
+  }
+}
 
+void PartSet_Module::onMouseMoved(QPoint thePoint)
+{
+  ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
   PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
   if (aPreviewOp) {
-    connect(anOperation, SIGNAL(stopped()), this, SLOT(onOperationStopped()));
-    connect(aPreviewOp, SIGNAL(visualizePreview(bool)), this, SLOT(onVisualizePreview(bool)));
-    connect(myWorkshop->mainWindow()->viewer(), SIGNAL(selectionChanged()),
-            aPreviewOp, SLOT(onViewSelectionChanged()));
+    XGUI_Viewer* aViewer = myWorkshop->mainWindow()->viewer();
+    if (aViewer) {
+      XGUI_ViewWindow* aWindow = aViewer->activeViewWindow();
+      if (aWindow) {
+        Handle(V3d_View) aView3d = aWindow->viewPort()->getView();
+        if ( !aView3d.IsNull() ) {
+          gp_Pnt aPoint = PartSet_Tools::ConvertClickToPoint(thePoint, aView3d);
+          aPreviewOp->setMouseMovePoint(aPoint);
+        }
+      }
+    }
   }
 }
 
-void PartSet_Module::onOperationStopped()
+void PartSet_Module::onViewerProjectionChange(double theX, double theY, double theZ)
 {
-  ModuleBase_PropPanelOperation* anOperation = dynamic_cast<ModuleBase_PropPanelOperation*>(sender());
+  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) {
-    disconnect(aPreviewOp, SIGNAL(visualizePreview(bool)), this, SLOT(onVisualizePreview(bool)));
-    disconnect(myWorkshop->mainWindow()->viewer(), SIGNAL(selectionChanged()),
-               aPreviewOp, SLOT(onViewSelectionChanged()));
+  if (!aPreviewOp)
+    return;
+
+  XGUI_Displayer* aDisplayer = myWorkshop->displayer();
+  if (isDisplay) {
+    boost::shared_ptr<GeomAPI_Shape> aPreview = aPreviewOp->preview();
+    if (aPreview) {
+      aDisplayer->LocalSelection(anOperation->feature(),
+                                   aPreview->impl<TopoDS_Shape>(), aPreviewOp->getSelectionMode());
+    }
+  }
+  else {
+    aDisplayer->GlobalSelection(false);
+    aDisplayer->Erase(anOperation->feature());
   }
 }