Salome HOME
Issue #236: Buttons 'N' and 'P' change selection of object under cursor to Next or...
[modules/shaper.git] / src / XGUI / XGUI_Viewer.cpp
index 8053c9712bd8f74a8c74f967ce548611a51a6721..47100f7c463e0e33c472f6c372d8978e939ce7f1 100644 (file)
@@ -2,9 +2,15 @@
 #include "XGUI_MainWindow.h"
 #include "XGUI_ViewWindow.h"
 #include "XGUI_ViewPort.h"
+#include "XGUI_Preferences.h"
+
+#include <SUIT_ResourceMgr.h>
 
 #include <QMdiArea>
 #include <QMdiSubWindow>
+#include <QApplication>
+#include <QMouseEvent>
+#include <QMenu>
 
 #include <V3d_View.hxx>
 
@@ -17,6 +23,9 @@
 #include <Prs3d_LineAspect.hxx>
 #include <V3d_View.hxx>
 #include <Visual3d_View.hxx>
+#include <AIS_ListOfInteractive.hxx>
+#include <AIS_ListIteratorOfListOfInteractive.hxx>
+#include <AIS_Shape.hxx>
 
 #ifdef WIN32
 #include <WNT_Window.hxx>
@@ -61,23 +70,30 @@ Handle(V3d_Viewer) CreateViewer(const Standard_ExtString name, const Standard_CS
  */
 QString XGUI_Viewer::backgroundData(QStringList& gradList, QIntList& idList, QIntList& txtList)
 {
-  gradList << tr("GT_HORIZONTALGRADIENT") << tr("GT_VERTICALGRADIENT")
-      << tr("GT_FIRSTDIAGONALGRADIENT") << tr("GT_SECONDDIAGONALGRADIENT")
-      << tr("GT_FIRSTCORNERGRADIENT") << tr("GT_SECONDCORNERGRADIENT")
-      << tr("GT_THIRDCORNERGRADIENT") << tr("GT_FORTHCORNERGRADIENT");
+  gradList << tr("Horizontal gradient") << tr("Vertical gradient") << tr("First diagonal gradient")
+           << tr("Second diagonal gradient") << tr("First corner gradient")
+           << tr("Second corner gradient") << tr("Third corner gradient")
+           << tr("Fourth corner gradient");
   idList << XGUI::HorizontalGradient << XGUI::VerticalGradient << XGUI::Diagonal1Gradient
-      << XGUI::Diagonal2Gradient << XGUI::Corner1Gradient << XGUI::Corner2Gradient
-      << XGUI::Corner3Gradient << XGUI::Corner4Gradient;
+         << XGUI::Diagonal2Gradient << XGUI::Corner1Gradient << XGUI::Corner2Gradient
+         << XGUI::Corner3Gradient << XGUI::Corner4Gradient;
 #ifdef OCC_ENABLE_TEXTURED_BACKGROUND
   txtList << XGUI::CenterTexture << XGUI::TileTexture << XGUI::StretchTexture;
 #endif
-  return tr("BG_IMAGE_FILES");
+  return tr("Image files (*.bmp *.gif *.pix *.xwd *.rgb *.rs)");
 }
 
 XGUI_Viewer::XGUI_Viewer(XGUI_MainWindow* theParent, bool DisplayTrihedron)
-    : QObject(theParent), myMainWindow(theParent), myPreselectionEnabled(true), mySelectionEnabled(
-        true), myMultiSelectionEnabled(true), myIsRelative(true), myInteractionStyle(
-        XGUI::STANDARD), myTrihedronSize(100)
+    : QObject(theParent),
+      myMainWindow(theParent),
+      myPreselectionEnabled(true),
+      mySelectionEnabled(true),
+      myMultiSelectionEnabled(true),
+      myIsRelative(true),
+      myInteractionStyle(XGUI::STANDARD),
+      myTrihedronSize(100),
+      myActiveView(0),
+      myWndIdCount(0)
 {
   if (!isInitialized) {
     isInitialized = true;
@@ -105,8 +121,8 @@ XGUI_Viewer::XGUI_Viewer(XGUI_MainWindow* theParent, bool DisplayTrihedron)
     XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::ROTATE] = Qt::NoModifier;
     XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::ROTATE] = Qt::LeftButton;
 
-    XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::FIT_AREA] = Qt::NoModifier; // unused
-    XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::FIT_AREA] = Qt::NoButton; // unused
+    XGUI_Viewer::myStateMap[XGUI::KEY_FREE][XGUI::FIT_AREA] = Qt::NoModifier;  // unused
+    XGUI_Viewer::myButtonMap[XGUI::KEY_FREE][XGUI::FIT_AREA] = Qt::NoButton;  // unused
   }
 
   // init CasCade viewers
@@ -139,6 +155,11 @@ XGUI_Viewer::XGUI_Viewer(XGUI_MainWindow* theParent, bool DisplayTrihedron)
   }
   // set zooming style to standard
   //myZoomingStyle = 0;
+
+  QMdiArea* aMDI = myMainWindow->mdiArea();
+  connect(aMDI, SIGNAL(subWindowActivated(QMdiSubWindow*)), this,
+          SLOT(onWindowActivated(QMdiSubWindow*)));
+
 }
 
 XGUI_Viewer::~XGUI_Viewer(void)
@@ -157,19 +178,72 @@ QMdiSubWindow* XGUI_Viewer::createView(V3d_TypeOfView theType)
   //initView( vw );
   // set default background for view window
   //vw->setBackground( background(0) ); // 0 means MAIN_VIEW (other views are not yet created here)
-  //// connect signal from viewport
-  connect(view->viewPort(), SIGNAL(vpClosed()), this, SLOT(onViewClosed()));
-  connect(view->viewPort(), SIGNAL(vpMapped()), this, SLOT(onViewMapped()));
+  // connect signal from viewport
+  //connect(view->viewPort(), SIGNAL(vpClosed()), this, SLOT(onViewClosed()));
+  //connect(view->viewPort(), SIGNAL(vpMapped()), this, SLOT(onViewMapped()));
+  if (myViews.size() == 0)
+    setTrihedronShown(true);
 
-  view->setBackground(XGUI_ViewBackground(XGUI::VerticalGradient, Qt::green, Qt::blue));
+  Qtx::BackgroundData aBk = XGUI_Preferences::resourceMgr()->backgroundValue("Viewer",
+                                                                             "background");
+  view->setBackground(aBk);
+  view->updateEnabledDrawMode();
 
   QMdiArea* aMDI = myMainWindow->mdiArea();
   QMdiSubWindow* aWnd = aMDI->addSubWindow(view, Qt::FramelessWindowHint);
+  addView(aWnd);
   aWnd->setGeometry(0, 0, aMDI->width() / 2, aMDI->height() / 2);
   aWnd->show();
+  aWnd->setWindowTitle(QString("Viewer #%1").arg(++myWndIdCount));
+  emit viewCreated(view);
   return aWnd;
 }
 
+void XGUI_Viewer::updateFromResources()
+{
+  Qtx::BackgroundData aBk = XGUI_Preferences::resourceMgr()->backgroundValue("Viewer",
+                                                                             "background");
+  foreach (QMdiSubWindow* aWnd, myViews)
+  {
+    XGUI_ViewWindow* aView = dynamic_cast<XGUI_ViewWindow*>(aWnd->widget());
+    if (aView)
+      aView->setBackground(aBk);
+  }
+}
+
+XGUI_ViewWindow* XGUI_Viewer::activeViewWindow() const
+{
+  if (myActiveView)
+    return dynamic_cast<XGUI_ViewWindow*>(myActiveView->widget());
+  return 0;
+}
+
+void XGUI_Viewer::getSelectedObjects(AIS_ListOfInteractive& theList)
+{
+  theList.Clear();
+  for (myAISContext->InitSelected(); myAISContext->MoreSelected(); myAISContext->NextSelected())
+    theList.Append(myAISContext->SelectedInteractive());
+}
+
+void XGUI_Viewer::getSelectedShapes(NCollection_List<TopoDS_Shape>& theList)
+{
+  Handle(AIS_InteractiveContext) ic = AISContext();
+
+  for (ic->InitSelected(); ic->MoreSelected(); ic->NextSelected()) {
+    TopoDS_Shape aShape = ic->SelectedShape();
+    if (!aShape.IsNull())
+      theList.Append(aShape);
+  }
+}
+
+void XGUI_Viewer::setObjectsSelected(const AIS_ListOfInteractive& theList)
+{
+  AIS_ListIteratorOfListOfInteractive aIt;
+  for (aIt.Initialize(theList); aIt.More(); aIt.Next())
+    myAISContext->AddOrRemoveSelected(aIt.Value(), false);
+  myAISContext->UpdateCurrentViewer();
+}
+
 /*! Sets hot button
  *\param theOper - hot operation
  *\param theState - adding state to state map operations.
@@ -217,7 +291,7 @@ bool XGUI_Viewer::isTrihedronVisible() const
  \param on - new state
  */
 
-void XGUI_Viewer::setTrihedronShown(const bool on)
+void XGUI_Viewer::setTrihedronShown(bool on)
 {
   if (myTrihedron.IsNull())
     return;
@@ -314,27 +388,265 @@ bool XGUI_Viewer::computeTrihedronSize(double& theNewSize, double& theSize)
   if (aMaxSide < Precision::Confusion())
     return false;
 
-  static float EPS = 5.0E-3;
+  static float EPS = (float) 5.0E-3;
   theSize = trihedron()->Size();
   //theNewSize = aMaxSide*aSizeInPercents / 100.0;
 
   return fabs(theNewSize - theSize) > theSize * EPS || fabs(theNewSize - theSize) > theNewSize * EPS;
 }
 
-void XGUI_Viewer::onViewClosed()
+void XGUI_Viewer::onViewClosed(QMdiSubWindow* theView)
 {
-  Standard_Integer aViewsNb = 0;
-  for(myV3dViewer->InitActiveViews(); myV3dViewer->MoreActiveViews();
-      myV3dViewer->NextActiveViews())
-    ++aViewsNb;
-  if (aViewsNb < 2) {
-    //clean up presentations before last view is closed
-    myAISContext->RemoveAll(Standard_False);
+  if (!theView)
+    return;
+
+  emit deleteView(static_cast<XGUI_ViewWindow*>(theView->widget()));
+  removeView(theView);
+
+  // if this is last view
+  if (myViews.size() == 0) {
+    Standard_Integer aViewsNb = 0;
+    for (myV3dViewer->InitActiveViews(); myV3dViewer->MoreActiveViews();
+        myV3dViewer->NextActiveViews())
+      ++aViewsNb;
+    if (aViewsNb < 2) {
+      //clean up presentations before last view is closed
+      myAISContext->RemoveAll(Standard_False);
+    }
   }
 }
 
-void XGUI_Viewer::onViewMapped()
+/*!Remove view window \a theView from view manager.
+ *And close the last view, if it has \a theView.
+ */
+void XGUI_Viewer::removeView(QMdiSubWindow* theView)
+{
+  XGUI_ViewWindow* aWindow = static_cast<XGUI_ViewWindow*>(theView->widget());
+
+  aWindow->disconnect(this);
+  myViews.removeAt(myViews.indexOf(theView));
+  if (myActiveView == theView)
+    myActiveView = 0;
+  if (myViews.size() == 0)
+    emit lastViewClosed();
+}
+
+/*void XGUI_Viewer::onViewMapped()
+ {
+ setTrihedronShown(true);
+ }*/
+
+void XGUI_Viewer::addView(QMdiSubWindow* theView)
 {
-  setTrihedronShown(true);
+  XGUI_ViewWindow* aWindow = dynamic_cast<XGUI_ViewWindow*>(theView->widget());
+
+  connect(aWindow, SIGNAL(closed(QMdiSubWindow*)), this, SLOT(onViewClosed(QMdiSubWindow*)));
+
+  connect(aWindow, SIGNAL(tryClosing(XGUI_ViewWindow*)), this,
+          SIGNAL(tryCloseView(XGUI_ViewWindow*)));
+
+  connect(aWindow, SIGNAL(mousePressed(XGUI_ViewWindow*, QMouseEvent*)), this,
+          SLOT(onMousePressed(XGUI_ViewWindow*, QMouseEvent*)));
+
+  connect(aWindow, SIGNAL(mouseDoubleClicked(XGUI_ViewWindow*, QMouseEvent*)), this,
+          SIGNAL(mouseDoubleClick(XGUI_ViewWindow*, QMouseEvent*)));
+
+  connect(aWindow, SIGNAL(mouseMoving(XGUI_ViewWindow*, QMouseEvent*)), this,
+          SIGNAL(mouseMove(XGUI_ViewWindow*, QMouseEvent*)));
+
+  connect(aWindow, SIGNAL(keyPressed(XGUI_ViewWindow*, QKeyEvent*)), this,
+          SIGNAL(keyPress(XGUI_ViewWindow*, QKeyEvent*)));
+
+  connect(aWindow, SIGNAL(keyReleased(XGUI_ViewWindow*, QKeyEvent*)), this,
+          SLOT(onKeyRelease(XGUI_ViewWindow*, QKeyEvent*)));
+
+  //connect(aWindow, SIGNAL(contextMenuRequested( QContextMenuEvent* )),
+  //        this,    SLOT  (onContextMenuRequested( QContextMenuEvent* )));
+  connect(aWindow, SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
+          SIGNAL(contextMenuRequested(QContextMenuEvent*)));
+
+  connect(aWindow, SIGNAL(mouseMoving(XGUI_ViewWindow*, QMouseEvent*)), this,
+          SLOT(onMouseMove(XGUI_ViewWindow*, QMouseEvent*)));
+
+  connect(aWindow, SIGNAL(mouseReleased(XGUI_ViewWindow*, QMouseEvent*)), this,
+          SLOT(onMouseReleased(XGUI_ViewWindow*, QMouseEvent*)));
+
+  myViews.append(theView);
+}
+
+/*!
+ Emit activated for view \a view.
+ */
+void XGUI_Viewer::onWindowActivated(QMdiSubWindow* view)
+{
+  if (view && (view != myActiveView) && (!view->isMinimized())) {
+    myActiveView = view;
+    ((XGUI_ViewWindow*) myActiveView->widget())->windowActivated();
+    QList<QMdiSubWindow*>::iterator aIt;
+    for (aIt = myViews.begin(); aIt != myViews.end(); ++aIt) {
+      if ((*aIt) != myActiveView) {
+        ((XGUI_ViewWindow*) (*aIt)->widget())->windowDeactivated();
+      }
+    }
+  }
+}
+
+void XGUI_Viewer::onWindowMinimized(QMdiSubWindow* theWnd)
+{
+  if (myActiveView == theWnd) {
+    myActiveView = 0;
+    QList<QMdiSubWindow*>::iterator aIt;
+    for (aIt = myViews.begin(); aIt != myViews.end(); ++aIt) {
+      if (!(*aIt)->widget()->isMinimized()) {
+        (*aIt)->raise();
+        onWindowActivated(*aIt);
+        break;
+      }
+    }
+  }
+}
+
+/*!
+ SLOT: called on mouse button press, stores current mouse position as start point for transformations
+ */
+void XGUI_Viewer::onMousePressed(XGUI_ViewWindow* theWindow, QMouseEvent* theEvent)
+{
+  myStartPnt.setX(theEvent->x());
+  myStartPnt.setY(theEvent->y());
+  emit mousePress(theWindow, theEvent);
+}
+
+/*!
+ SLOT: called on mouse move, processes hilighting
+ */
+void XGUI_Viewer::onMouseMove(XGUI_ViewWindow* theWindow, QMouseEvent* theEvent)
+{
+  myCurPnt.setX(theEvent->x());
+  myCurPnt.setY(theEvent->y());
+  if (!mySelectionEnabled)
+    return;
+
+  Handle(V3d_View) aView3d = theWindow->viewPort()->getView();
+  if (!aView3d.IsNull()) {
+    myAISContext->MoveTo(theEvent->x(), theEvent->y(), aView3d);
+    if (myAISContext->HasDetected())
+      theWindow->viewPort()->setFocus(Qt::MouseFocusReason);
+  }
+}
+
+/*!
+ SLOT: called on mouse button release, finishes selection
+ */
+void XGUI_Viewer::onMouseReleased(XGUI_ViewWindow* theWindow, QMouseEvent* theEvent)
+{
+  if (!mySelectionEnabled || theEvent->button() != Qt::LeftButton) {
+    emit mouseRelease(theWindow, theEvent);
+    return;
+  }
+
+  myEndPnt.setX(theEvent->x());
+  myEndPnt.setY(theEvent->y());
+  bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
+
+  //if (!aHasShift) 
+  //  emit deselection();
+
+  if (myStartPnt == myEndPnt) {
+    // the MoveTo is necessary for the second click in the same point. Otherwise the selection is lost.
+    //Handle(V3d_View) aView3d = theWindow->viewPort()->getView();
+    //if (!aView3d.IsNull()) {
+    //  myAISContext->MoveTo(theEvent->x(), theEvent->y(), aView3d);
+    //}
+    if (aHasShift && myMultiSelectionEnabled)
+      myAISContext->ShiftSelect();
+    else
+      myAISContext->Select();
+  } else {
+    if (aHasShift && myMultiSelectionEnabled)
+      myAISContext->ShiftSelect(myStartPnt.x(), myStartPnt.y(), myEndPnt.x(), myEndPnt.y(),
+                                theWindow->viewPort()->getView(), false);
+    else
+      myAISContext->Select(myStartPnt.x(), myStartPnt.y(), myEndPnt.x(), myEndPnt.y(),
+                           theWindow->viewPort()->getView(), false);
+
+    int Nb = myAISContext->NbSelected();
+    if (Nb > 1 && !myMultiSelectionEnabled) {
+      myAISContext->InitSelected();
+      Handle( SelectMgr_EntityOwner ) anOwner = myAISContext->SelectedOwner();
+      if (!anOwner.IsNull()) {
+        myAISContext->ClearSelected( Standard_False);
+        myAISContext->AddOrRemoveSelected(anOwner, Standard_False);
+      }
+    }
+
+    myAISContext->UpdateCurrentViewer();
+  }
+  emit mouseRelease(theWindow, theEvent);
+  emit selectionChanged();
+}
+
+//******************************************************
+void XGUI_Viewer::setMultiSelectionEnabled(bool toEnable)
+{
+  myMultiSelectionEnabled = toEnable;
+  updateViewsDrawMode();
+}
+
+//******************************************************
+void XGUI_Viewer::setSelectionEnabled(bool toEnable)
+{
+  mySelectionEnabled = toEnable;
+  updateViewsDrawMode();
+}
+
+//******************************************************
+void XGUI_Viewer::updateViewsDrawMode() const
+{
+  foreach(QMdiSubWindow* aWnd, myViews)
+  {
+    XGUI_ViewWindow* aView = static_cast<XGUI_ViewWindow*>(aWnd->widget());
+    aView->updateEnabledDrawMode();
+  }
+}
+
+//******************************************************
+void XGUI_Viewer::onKeyRelease(XGUI_ViewWindow* theView, QKeyEvent* theKey)
+{
+  Handle(V3d_View) aView = theView->viewPort()->getView();
+  bool noModifiers = (theKey->modifiers() == Qt::NoModifier);
+  if ((theKey->key() == Qt::Key_N) && noModifiers) {
+    myAISContext->HilightNextDetected(aView);
+  } else if ((theKey->key() == Qt::Key_P) && noModifiers) {
+    myAISContext->HilightPreviousDetected(aView);
+  } else {
+    emit keyRelease(theView, theKey);
+  }
 }
 
+//******************************************************
+//void XGUI_Viewer::onContextMenuRequested(QContextMenuEvent* theEvent)
+//{
+//  XGUI_ViewWindow* aWnd = dynamic_cast<XGUI_ViewWindow*>(sender());
+//  if (!aWnd) return;
+//
+//  QMenu aMenu;
+//
+//  // Include Viewer actions
+//  if (myActions.size() > 0) {
+//    aMenu.addActions(myActions);
+//    aMenu.addSeparator();
+//  }
+//  if (aWnd->actions().size() > 0) {
+//    aMenu.addActions(aWnd->actions());
+//    aMenu.addSeparator();
+//  }
+//
+//  QMdiArea* aMDI = myMainWindow->mdiArea();
+//  if (aMenu.actions().size() > 0) {
+//    QMenu* aSubMenu = aMenu.addMenu(tr("Windows"));
+//    aSubMenu->addActions(aMDI->actions());
+//  } else {
+//    aMenu.addActions(aMDI->actions());
+//  }
+//  aMenu.exec(theEvent->globalPos());
+//}