Salome HOME
Updated copyright comment
[modules/gui.git] / src / GLViewer / GLViewer_Viewer.cxx
index 78a16398fc29c9804cbe6ffa677a95ecd1aab625..ac137a2bec3b9c9658348d467f3d5cdfa9583959 100644 (file)
@@ -1,40 +1,46 @@
-//  Copyright (C) 2005 OPEN CASCADE
+// Copyright (C) 2007-2024  CEA, EDF, OPEN CASCADE
 //
-//  This library is free software; you can redistribute it and/or
-//  modify it under the terms of the GNU Lesser General Public
-//  License as published by the Free Software Foundation; either
-//  version 2.1 of the License.
+// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
 //
-//  This library is distributed in the hope that it will be useful,
-//  but WITHOUT ANY WARRANTY; without even the implied warranty of
-//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-//  Lesser General Public License for more details.
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
 //
-//  You should have received a copy of the GNU Lesser General Public
-//  License along with this library; if not, write to the Free Software
-//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
 //
-//  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-//  Author : OPEN CASCADE
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 
+//  Author : OPEN CASCADE
 // File:      GLViewer_Viewer.cxx
 // Created:   November, 2004
-
 //#include <GLViewerAfx.h>
+//
 #include "GLViewer_Viewer.h"
 #include "GLViewer_Selector.h"
 #include "GLViewer_ViewPort.h"
 #include "GLViewer_ViewFrame.h"
 
-#include "SUIT_Desktop.h"
+//#include "SUIT_Desktop.h"
 #include "SUIT_ViewWindow.h"
+#include "SUIT_ViewManager.h"
 
-#include <qapplication.h>
-#include <qpainter.h>
-#include <qpopupmenu.h>
-#include <qcolordialog.h>
+#include <QApplication>
+#include <QMenu>
+#include <QColorDialog>
+#include <QMouseEvent>
+#include <QWheelEvent>
+#include <QRect>
+#include <QRubberBand>
 
 /* used for sketching */
 static QEvent* l_mbPressEvent = 0;
@@ -42,12 +48,12 @@ static QEvent* l_mbPressEvent = 0;
 /*!
     Constructor
 */
-GLViewer_Viewer::GLViewer_Viewer( const QString& title )
+GLViewer_Viewer::GLViewer_Viewer( const QString& /*title*/ )
 : SUIT_ViewModel(),
 mySelector( 0 ),
+mySelMode( NoSelection ),
 mySketcher( 0 ),
-myTransformer( 0 ),
-mySelMode( NoSelection )
+myTransformer( 0 )
 {
 }
 
@@ -85,12 +91,12 @@ void GLViewer_Viewer::setViewManager(SUIT_ViewManager* theViewManager)
 /*!
   Builds popup for GL viewer
 */
-void GLViewer_Viewer::contextMenuPopup( QPopupMenu* thePopup )
+void GLViewer_Viewer::contextMenuPopup( QMenu* thePopup )
 {
-  if( thePopup->count() > 0 )
-      thePopup->insertSeparator();
+  if( thePopup->actions().count() > 0 )
+      thePopup->addSeparator();
 
-  thePopup->insertItem( tr( "CHANGE_BGCOLOR" ), this, SLOT( onChangeBgColor() ) );
+  thePopup->addAction( tr( "CHANGE_BGCOLOR" ), this, SLOT( onChangeBgColor() ) );
 }
 
 /*!
@@ -327,7 +333,7 @@ bool GLViewer_Viewer::eventFilter( QObject* o, QEvent* e )
 /*!
     Called when smth is selected in this viewer. [ virtual protected slot ]
 */
-void GLViewer_Viewer::onSelectionDone( bool bAdded, SelectionChangeStatus status  )
+void GLViewer_Viewer::onSelectionDone( bool /*bAdded*/, SelectionChangeStatus status  )
 {
     emit selectionChanged( status );
 }
@@ -389,7 +395,7 @@ void GLViewer_Viewer::onWheelEvent( SUIT_ViewWindow*, QWheelEvent* e )
 void GLViewer_Viewer::onSelectionModeChanged()
 {
     bool enable = ( mySelMode == Multiple );    
-    QPtrVector<SUIT_ViewWindow> views = getViewManager()->getViews();
+    QVector<SUIT_ViewWindow*> views = getViewManager()->getViews();
     for ( int i = 0, n = views.count(); i < n; i++ )
     {
         GLViewer_ViewPort* vp = ((GLViewer_ViewFrame*)views[i])->getViewPort();
@@ -403,7 +409,7 @@ void GLViewer_Viewer::onSelectionModeChanged()
 */
 void GLViewer_Viewer::update( int flags )
 {
-    QPtrVector<SUIT_ViewWindow> views = getViewManager()->getViews();
+    QVector<SUIT_ViewWindow*> views = getViewManager()->getViews();
     for ( int i = 0, n = views.count(); i < n; i++ )
         ((GLViewer_ViewFrame*)views[i])->onUpdate( flags );
 }
@@ -424,12 +430,12 @@ void GLViewer_Viewer::unhilightDetected()
 void GLViewer_Viewer::handleMousePress( QMouseEvent* e )
 {
     /* test accel for transforms */
-    if ( e->state() & GLViewer_ViewTransformer::accelKey() )
+    if ( e->modifiers() & GLViewer_ViewTransformer::accelKey() )
     {
-        ButtonState bs = e->button();
-        if ( bs == GLViewer_ViewTransformer::zoomButton() )
+        Qt::MouseButton bs = e->button();
+        if ( (int)bs == GLViewer_ViewTransformer::zoomButton() ) // todo Qt::MouseButton is unsigned int: comparison of int with uint
             activateTransform( Zoom );
-        else if ( bs == GLViewer_ViewTransformer::panButton() )
+        else if ( (int)bs == GLViewer_ViewTransformer::panButton() ) // todo Qt::MouseButton is unsigned int: comparison of int with uint
             activateTransform( Pan );
     }
     else
@@ -458,7 +464,7 @@ void GLViewer_Viewer::handleMousePress( QMouseEvent* e )
 void GLViewer_Viewer::handleMouseMove( QMouseEvent* e )
 {
     /* Highlight for selection */
-    bool dragged = ( e->state() & ( LeftButton | MidButton | RightButton ) );
+    bool dragged = ( e->buttons() & ( Qt::LeftButton | Qt::MidButton | Qt::RightButton ) );
     if ( !dragged )
     {
         if ( getSelector() )
@@ -466,7 +472,7 @@ void GLViewer_Viewer::handleMouseMove( QMouseEvent* e )
     }
     /* Try to activate default sketching
     */
-    else if ( e->state() == GLViewer_ViewSketcher::sketchButton() )
+    else if ( (int)e->button() == GLViewer_ViewSketcher::sketchButton() ) // todo Qt::MouseButton is unsigned int: comparison of int with uint
     {
         activateSketching( Rect );
         if ( mySketcher )
@@ -495,11 +501,11 @@ void GLViewer_Viewer::handleMouseRelease( QMouseEvent* e )
 {
     /* selection */
     /* tmp - in handleMousePress*/  
-    if( e->button() == LeftButton && !(getActiveView()->getViewPort()->currentBlock() & BS_Selection) )
+    if( e->button() == Qt::LeftButton && !(getActiveView()->getViewPort()->currentBlock() & BS_Selection) )
     {
         if ( getSelector() )
         {
-            bool append = bool ( e->state() & GLViewer_Selector::appendKey() );
+            bool append = bool ( e->modifiers() & GLViewer_Selector::appendKey() );
             getSelector()->select( append );
         }
     }
@@ -526,7 +532,7 @@ int GLViewer_ViewTransformer::panBtn = Qt::MidButton;
 int GLViewer_ViewTransformer::zoomBtn = Qt::LeftButton;
 int GLViewer_ViewTransformer::fitRectBtn = Qt::LeftButton;
 int GLViewer_ViewTransformer::panGlobalBtn = Qt::LeftButton;
-int GLViewer_ViewTransformer::acccelKey = Qt::ControlButton;
+int GLViewer_ViewTransformer::acccelKey = Qt::ControlModifier;
 
 /*!
     Constructor
@@ -535,8 +541,9 @@ GLViewer_ViewTransformer::GLViewer_ViewTransformer( GLViewer_Viewer* v, int type
 : QObject( 0 ),
 myViewer( v ),
 myType( type ),
-myMajorBtn( NoButton ),
-myButtonState( 0 )
+myButtonState( 0 ),
+myMajorBtn( Qt::NoButton ),
+myRectBand( 0 )
 {
     if ( myType == GLViewer_Viewer::Pan ||
          myType == GLViewer_Viewer::Zoom ||
@@ -563,6 +570,8 @@ GLViewer_ViewTransformer::~GLViewer_ViewTransformer()
     }
 
     //QAD_Application::getDesktop()->clearInfo();
+
+    endDrawRect();
 }
 
 /*!
@@ -645,11 +654,14 @@ bool GLViewer_ViewTransformer::eventFilter( QObject* o, QEvent* e )
             TransformState state = EnTrain;
             QMouseEvent* me = ( QMouseEvent* )e;
 
-            myButtonState = me->state();
+            myButtonState = me->buttons();
             if ( e->type() == QEvent::MouseButtonPress )
                 myButtonState |= me->button();  /* add pressed button */
 
-            int mouseOnlyState = ( myButtonState & ( LeftButton | MidButton | RightButton ) );
+            if ( e->type() == QEvent::MouseButtonRelease )
+                myButtonState |= me->button();  /* add release button */
+
+            int mouseOnlyState = ( myButtonState & ( Qt::LeftButton | Qt::MidButton | Qt::RightButton ) );
             if ( myStart.isNull() )
             {
                 state = Debut;
@@ -657,7 +669,7 @@ bool GLViewer_ViewTransformer::eventFilter( QObject* o, QEvent* e )
                 myMajorBtn = mouseOnlyState;
             }
 
-            if ( e->type() == QEvent::MouseButtonRelease && mouseOnlyState == myMajorBtn )
+            if ( e->type() == QEvent::MouseButtonRelease )
             {
                 state = Fin;
             }
@@ -704,8 +716,8 @@ void GLViewer_ViewTransformer::onTransform( TransformState state )
         {
             if ( doTrsf )
             {
-                QRect rect( QMIN( myStart.x(), myCurr.x() ), QMIN( myStart.y(), myCurr.y() ),
-                            QABS( myStart.x() - myCurr.x() ), QABS( myStart.y() - myCurr.y() ) );
+                QRect rect( qMin( myStart.x(), myCurr.x() ), qMin( myStart.y(), myCurr.y() ),
+                            qAbs( myStart.x() - myCurr.x() ), qAbs( myStart.y() - myCurr.y() ) );
                 if ( !rect.isEmpty() )
                 {
                     switch ( state )
@@ -715,13 +727,7 @@ void GLViewer_ViewTransformer::onTransform( TransformState state )
                             break;
                         default:
                         {
-                            QPainter p( avp->getPaintDevice() ); // for QAD_GLWidget
-                            p.setPen( Qt::white );
-                            p.setRasterOp( Qt::XorROP );
-                            if ( !myDrawRect.isEmpty() )
-                                p.drawRect( myDrawRect );    /* erase */
-                            p.drawRect( rect );
-                            myDrawRect = rect;
+                            drawRect( rect );
                             break;
                         }
                     }
@@ -737,6 +743,34 @@ void GLViewer_ViewTransformer::onTransform( TransformState state )
         myViewer->activateTransform( GLViewer_Viewer::NoTransform );
 }
 
+/*!
+  Draws rectangle by starting and current points
+*/
+void GLViewer_ViewTransformer::drawRect(const QRect& theRect)
+{
+  if ( !myRectBand ) {
+    myRectBand = new QRubberBand( QRubberBand::Rectangle, myViewer->getActiveView()->getViewPort() );
+    QPalette palette;
+    palette.setColor(myRectBand->foregroundRole(), Qt::white);
+    myRectBand->setPalette(palette);
+  }
+  myRectBand->hide();
+
+  myRectBand->setGeometry( theRect );
+  myRectBand->setVisible( theRect.isValid() );
+}
+
+/*!
+  \brief Delete rubber band on the end on the dragging operation.
+*/
+void GLViewer_ViewTransformer::endDrawRect()
+{
+  if ( myRectBand ) myRectBand->hide();
+
+  delete myRectBand;
+  myRectBand = 0;
+}
+
 /*!
     Returns the type of the transformer. [ public ]
 */
@@ -746,7 +780,7 @@ int GLViewer_ViewTransformer::type() const
 }
 
 
-int GLViewer_ViewSketcher::sketchBtn = LeftButton;
+int GLViewer_ViewSketcher::sketchBtn = Qt::LeftButton;
 
 /*!
     Constructor
@@ -754,8 +788,9 @@ int GLViewer_ViewSketcher::sketchBtn = LeftButton;
 GLViewer_ViewSketcher::GLViewer_ViewSketcher( GLViewer_Viewer* viewer, int type )
 : QObject( 0 ),
 myViewer( viewer ),
+myType( type ),
 myData( 0 ),
-myType( type )
+myRectBand( 0 )
 {
     if( !myViewer )
         return;
@@ -782,6 +817,8 @@ GLViewer_ViewSketcher::~GLViewer_ViewSketcher()
 
     if ( myType == GLViewer_Viewer::Rect )
         delete ( QRect* ) myData;
+
+    endDrawRect();
 }
 
 /*!
@@ -798,7 +835,7 @@ bool GLViewer_ViewSketcher::eventFilter( QObject* o, QEvent* e )
             SketchState state = EnTrain;
             QMouseEvent* me = (QMouseEvent*)e;
 
-            myButtonState = me->state();
+            myButtonState = me->buttons();
             if ( e->type() == QEvent::MouseButtonPress )
                 myButtonState |= me->button();  /* add pressed button */
 
@@ -808,7 +845,7 @@ bool GLViewer_ViewSketcher::eventFilter( QObject* o, QEvent* e )
                 myStart = me->pos();
             }
 
-            int mouseOnlyState = ( myButtonState & ( LeftButton | MidButton | RightButton ) );
+            int mouseOnlyState = ( myButtonState & ( Qt::LeftButton | Qt::MidButton | Qt::RightButton ) );
             if ( e->type() == QEvent::MouseButtonRelease && mouseOnlyState == sketchButton() )
             {
                 state = Fin;
@@ -836,25 +873,49 @@ void GLViewer_ViewSketcher::onSketch( SketchState state )
         QRect* sketchRect = ( QRect* )data();
         if ( myButtonState & sketchButton() )
         {
-            QRect rect( QMIN( myStart.x(), myCurr.x() ), QMIN( myStart.y(), myCurr.y() ),
-                        QABS( myStart.x() - myCurr.x() ), QABS( myStart.y() - myCurr.y() ) );
+            QRect rect( qMin( myStart.x(), myCurr.x() ), qMin( myStart.y(), myCurr.y() ),
+                        qAbs( myStart.x() - myCurr.x() ), qAbs( myStart.y() - myCurr.y() ) );
             if ( !rect.isEmpty() )
             {
-                QPainter p( avp );
-                p.setPen( Qt::white );
-                p.setRasterOp( Qt::XorROP );
-                if ( !sketchRect->isEmpty() )
-                    p.drawRect( *sketchRect );    /* erase */
+                if ( !sketchRect->isEmpty() && myRectBand )
+                    myRectBand->hide();    /* erase */
                 *sketchRect = rect;
                 if ( state != Fin )
-                    p.drawRect( *sketchRect );
+                    drawRect( *sketchRect );
             }
         }
     }
 
     if ( state == Fin )
     {
-        QApplication::syncX();  /* force rectangle redrawing */
         myViewer->activateSketching( GLViewer_Viewer::NoSketching );
     }
 }
+
+/*!
+  Draws rectangle by starting and current points
+*/
+void GLViewer_ViewSketcher::drawRect(const QRect& theRect)
+{
+  if ( !myRectBand ) {
+    myRectBand = new QRubberBand( QRubberBand::Rectangle, myViewer->getActiveView()->getViewPort() );
+    QPalette palette;
+    palette.setColor(myRectBand->foregroundRole(), Qt::white);
+    myRectBand->setPalette(palette);
+  }
+  myRectBand->hide();
+
+  myRectBand->setGeometry( theRect );
+  myRectBand->setVisible( theRect.isValid() );
+}
+
+/*!
+  \brief Delete rubber band on the end on the dragging operation.
+*/
+void GLViewer_ViewSketcher::endDrawRect()
+{
+  if ( myRectBand ) myRectBand->hide();
+
+  delete myRectBand;
+  myRectBand = 0;
+}