Salome HOME
Updated copyright comment
[modules/gui.git] / src / GLViewer / GLViewer_Widget.cxx
index b29a9fb5230e559be84dcc84eee62e7c6da2169d..e01435daabbacf50456f84939f74b4ef14e519f8 100644 (file)
@@ -1,59 +1,47 @@
-//  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
 //
 
-// File:      GLViewer_Widget.cxx
-// Created:   November, 2004
-
-//================================================================
-// Class       : GLViewer_Widget
-// Description : OpenGL QWidget for GLViewer
-//================================================================
-
 #include "GLViewer_Widget.h"
 #include "GLViewer_ViewPort2d.h"
 #include "GLViewer_Viewer2d.h"
-#include "GLViewer_Compass.h"
 #include "GLViewer_Grid.h"
-#include "GLViewer_Object.h"
 #include "GLViewer_CoordSystem.h"
+#include "GLViewer_ViewFrame.h"
 
 #include <cmath>
-using namespace std;
-
-#include <qevent.h>
-#include <qrect.h>
-
-#include <qpixmap.h>
-#include <qimage.h>
-#include <qapplication.h>
-#include <qintdict.h>
-#include <qpaintdevicemetrics.h>
-#include <qsize.h>
-#include <qtooltip.h>
-
-//=======================================================================
-// Function: GLViewer_Widget
-// Purpose :
-//=======================================================================
-GLViewer_Widget::GLViewer_Widget( QWidget* parent, const char* name ):
+
+#include <QEvent>
+#include <QPaintEvent>
+#include <QRect>
+#include <QFile>
+#include <QImage>
+#include <QApplication>
+#include <QToolTip>
+
+/*!
+  A constructor
+  Parameters using for QOGLWidget as is 
+*/
+GLViewer_Widget::GLViewer_Widget( QWidget* parent, const char* /*name*/ ):
 QGLWidget( parent, 0/*, WRepaintNoErase | WResizeNoErase*/ )
 {
   myViewPort = ( GLViewer_ViewPort2d* )parent;
@@ -78,18 +66,16 @@ QGLWidget( parent, 0/*, WRepaintNoErase | WResizeNoErase*/ )
   setMouseTracking( true );
 }
 
-//=======================================================================
-// Function: GLViewer_Widget
-// Purpose :
-//=======================================================================
+/*!
+  Destructor
+*/
 GLViewer_Widget::~GLViewer_Widget()
 {
 }
 
-//=======================================================================
-// Function: ~GLViewer_Widget
-// Purpose :
-//=======================================================================
+/*!
+  \return offset parameters of Window in OpenGL global scene
+*/
 void GLViewer_Widget::getPan( GLfloat& xPan, GLfloat& yPan, GLfloat& zPan )
 {
   xPan = myXPan;
@@ -97,10 +83,9 @@ void GLViewer_Widget::getPan( GLfloat& xPan, GLfloat& yPan, GLfloat& zPan )
   zPan = myZPan;
 }
 
-//=======================================================================
-// Function: setPan
-// Purpose :
-//=======================================================================
+/*!
+  A function for installing the  offset parameters of Window in OpenGL global scene
+*/
 void GLViewer_Widget::setPan( GLfloat xPan, GLfloat yPan, GLfloat zPan )
 {
   myXPan = xPan;
@@ -108,10 +93,9 @@ void GLViewer_Widget::setPan( GLfloat xPan, GLfloat yPan, GLfloat zPan )
   myZPan = zPan;
 }
 
-//=======================================================================
-// Function: getScale
-// Purpose :
-//=======================================================================
+/*!
+  \return scales on OpenGL scene along 3 directions in 2d scene zScale = 1.0
+*/
 void GLViewer_Widget::getScale( GLfloat& xScale, GLfloat& yScale, GLfloat& zScale )
 {
   xScale = myXScale;
@@ -119,10 +103,9 @@ void GLViewer_Widget::getScale( GLfloat& xScale, GLfloat& yScale, GLfloat& zScal
   zScale = myZScale;
 }
 
-//=======================================================================
-// Function: setScale
-// Purpose :
-//=======================================================================
+/*!
+  A function for installing the scales of OpenGL scene
+*/
 void GLViewer_Widget::setScale( GLfloat xScale, GLfloat yScale, GLfloat zScale )
 {
   if ( xScale > 0 && yScale > 0 && zScale > 0 )
@@ -133,12 +116,11 @@ void GLViewer_Widget::setScale( GLfloat xScale, GLfloat yScale, GLfloat zScale )
   }
 }
 
-//=======================================================================
-// Function: getRotationStart
-// Purpose :
-//=======================================================================
+/*!
+  \return start point of curren rotation of Window in OpenGL global scene
+*/
 void GLViewer_Widget::getRotationStart( GLfloat& rotationStartX,
-                                       GLfloat& rotationStartY,
+                                        GLfloat& rotationStartY,
                                         GLfloat& rotationStartZ )
 {
     rotationStartX = myRotationStartX;
@@ -146,12 +128,11 @@ void GLViewer_Widget::getRotationStart( GLfloat& rotationStartX,
     rotationStartZ = myRotationStartZ;
 }
 
-//=======================================================================
-// Function: setRotationStart
-// Purpose :
-//=======================================================================
+/*!
+  A function for installing the rotation angle of Window in OpenGL global scene in degree (Only in 2D)
+*/
 void GLViewer_Widget::setRotationStart( GLfloat rotationStartX,
-                                       GLfloat rotationStartY,
+                                        GLfloat rotationStartY,
                                         GLfloat rotationStartZ )
 {
     myRotationStartX = rotationStartX;
@@ -159,14 +140,17 @@ void GLViewer_Widget::setRotationStart( GLfloat rotationStartX,
     myRotationStartZ = rotationStartZ;
 }
 
-//=======================================================================
-// Function: getRotation
-// Purpose :
-//=======================================================================
+/*!
+  \return parameters of rotation
+  \param rotationAngle - angle
+  \param rotationCenterX - center x
+  \param rotationCenterY - center y
+  \param rotationCenterZ - center z
+*/
 void GLViewer_Widget::getRotation( GLfloat& rotationAngle,
-                                  GLfloat& rotationCenterX,
-                                  GLfloat& rotationCenterY,
-                                  GLfloat& rotationCenterZ )
+                                   GLfloat& rotationCenterX,
+                                   GLfloat& rotationCenterY,
+                                   GLfloat& rotationCenterZ )
 {
     rotationAngle = myRotationAngle;
     rotationCenterX = myRotationCenterX;
@@ -174,14 +158,17 @@ void GLViewer_Widget::getRotation( GLfloat& rotationAngle,
     rotationCenterZ = myRotationCenterZ;
 }
 
-//=======================================================================
-// Function: setRotation
-// Purpose :
-//=======================================================================
+/*!
+  Sets parameters of rotation
+  \param rotationAngle - angle
+  \param rotationCenterX - center x
+  \param rotationCenterY - center y
+  \param rotationCenterZ - center z
+*/
 void GLViewer_Widget::setRotation( GLfloat rotationAngle,
-                                  GLfloat rotationCenterX,
-                                  GLfloat rotationCenterY,
-                                  GLfloat rotationCenterZ )
+                                   GLfloat rotationCenterX,
+                                   GLfloat rotationCenterY,
+                                   GLfloat rotationCenterZ )
 {
     myRotationAngle = rotationAngle;
     myRotationCenterX = rotationCenterX;
@@ -189,10 +176,11 @@ void GLViewer_Widget::setRotation( GLfloat rotationAngle,
     myRotationCenterZ = rotationCenterZ;
 }
 
-//=======================================================================
-// Function: setBackground
-// Purpose :
-//=======================================================================
+
+/*!
+  Sets image as background
+  \param filename - name of file
+*/
 void GLViewer_Widget::setBackground( QString filename )
 {
     
@@ -245,29 +233,30 @@ void GLViewer_Widget::setBackground( QString filename )
     }
 }
 
-//=======================================================================
-// Function: addToolTip
-// Purpose :
-//=======================================================================
+/*!
+  Adds tooltip
+  \param theString - tooltip text
+  \param theRect - tooltip rectangle
+*/
 void GLViewer_Widget::addToolTip( QString theString, QRect theRect )
 {
     myToolTipRect = theRect;
-    QToolTip::add( this, myToolTipRect, theString );
+    setToolTip(theString);
+    //QToolTip::add( this, myToolTipRect, theString );
 }
 
-//=======================================================================
-// Function: removeToolTip
-// Purpose :
-//=======================================================================
+/*!
+  Removes tooltip
+*/
 void GLViewer_Widget::removeToolTip()
 {
-    QToolTip::remove( this, myToolTipRect );
+    setToolTip("");
+    //QToolTip::remove( this, myToolTipRect );
 }
 
-//=======================================================================
-// Function: initializeGL
-// Purpose :
-//=======================================================================
+/*!
+  Initialization (redefined virtual from QGLWidget)
+*/
 void GLViewer_Widget::initializeGL()
 {
     setAutoBufferSwap( true );
@@ -276,7 +265,7 @@ void GLViewer_Widget::initializeGL()
     
     //get image
     QImage buf; 
-    QString aPicturePath = getenv("GLViewer__Background_Picture");
+    QString aPicturePath = Qtx::getenv("GLViewer__Background_Picture");
     
     if ( !aPicturePath.isEmpty() && buf.load( aPicturePath ) ) 
     {  // Load first image from file
@@ -289,10 +278,9 @@ void GLViewer_Widget::initializeGL()
         isLoadBackground = false;
 }
 
-//=======================================================================
-// Function: paintGL
-// Purpose :
-//=======================================================================
+/*!
+  Paints content
+*/
 void GLViewer_Widget::paintGL()
 {
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@@ -335,10 +323,11 @@ void GLViewer_Widget::paintGL()
         v->repaintView( getViewPort()->getViewFrame() );
 }
 
-//=======================================================================
-// Function: resizeGL
-// Purpose :
-//=======================================================================
+/*!
+  Resets OpenGl parameters after resize
+  \param w - new width
+  \param h - new height
+*/
 void GLViewer_Widget::resizeGL( int w, int h )
 {
 
@@ -365,10 +354,9 @@ void GLViewer_Widget::resizeGL( int w, int h )
   glLoadIdentity(); 
 }
 
-//=======================================================================
-// Function: exportRepaint
-// Purpose :
-//=======================================================================
+/*!
+  Provides repaint in export mode
+*/
 void GLViewer_Widget::exportRepaint()
 {
     isExportMode = true;
@@ -378,65 +366,71 @@ void GLViewer_Widget::exportRepaint()
     isExportMode = false;
 }
 
-//=======================================================================
-// Function: paintEvent
-// Purpose :
-//=======================================================================
+/*!
+  Custom paint event handler
+*/
 void GLViewer_Widget::paintEvent( QPaintEvent* e )
 {
   QApplication::sendEvent( myViewPort, e );
 }
 
-//=======================================================================
-// Function: mouseMoveEvent
-// Purpose :
-//=======================================================================
+/*!
+  Custom mouse move event handler
+*/
 void GLViewer_Widget::mouseMoveEvent( QMouseEvent* e )
 {
   QApplication::sendEvent( myViewPort, e );
 }
 
-//=======================================================================
-// Function: mousePressEvent
-// Purpose :
-//=======================================================================
+/*!
+  Custom mouse press event handler
+*/
 void GLViewer_Widget::mousePressEvent( QMouseEvent* e )
 {
   QApplication::sendEvent( myViewPort, e );
 }
 
-//=======================================================================
-// Function: mouseReleaseEvent
-// Purpose :
-//=======================================================================
+/*!
+  Custom mouse release event handler
+*/
 void GLViewer_Widget::mouseReleaseEvent( QMouseEvent* e )
 {
   QApplication::sendEvent( myViewPort, e );
 }
 
-//=======================================================================
-// Function: enterEvent
-// Purpose :
-//=======================================================================
-void GLViewer_Widget::enterEvent( QEvent* e )
+/*!
+  Custom enter event handler
+*/
+void GLViewer_Widget::enterEvent( QEvent* /*e*/ )
 {
   updateGL();
 }
 
-//=======================================================================
-// Function: leaveEvent
-// Purpose :
-//=======================================================================
-void GLViewer_Widget::leaveEvent( QEvent* e )
+/*!
+  Custom leave event handler
+*/
+void GLViewer_Widget::leaveEvent( QEvent* /*e*/ )
 {
   updateGL();
 }
 
+/*!
+  Custom leave event handler
+*/
+bool GLViewer_Widget::event ( QEvent* e )
+{
+  if (e->type() == QEvent::ToolTip) {
+    QHelpEvent *helpEvent = static_cast<QHelpEvent *>(e);
+    if ( myToolTipRect.contains(helpEvent->pos()) )
+      QToolTip::showText(helpEvent->globalPos(), toolTip());
+  }
+  return QGLWidget::event(e);
+}
 
-//=======================================================================
-//! Function: hex
-//! Purpose : Returns the hex code of digit < 16
-//=======================================================================
+/*!
+  \return the hex code of digit < 16
+  \param c - digit
+*/
 inline char hex( uchar c )
 {
   if( c<=9 )
@@ -447,11 +441,23 @@ inline char hex( uchar c )
   return ' ';
 }
 
-//=======================================================================
-//! Function: AddImagePart
-//! Purpose : Translates path of image to PS format
-/*! Image inside rectangle from w1 to w2 and from h2 to h1*/
-//=======================================================================
+/*!
+  Translates part of image inside rectangle from w1 to w2 and from h2 to h1 to PS format
+  \param hFile - PostScript file 
+  \param image - image to be tarnslated
+  \param w1 - x start position
+  \param w2 - x end position
+  \param h1 - y start position
+  \param h2 - y end position
+  \param aViewerCS - viewer co-ordinate system
+  \param aPSCS - paper co-ordinate system
+  \param a
+  \param b
+  \param c
+  \param d
+  \param dw
+  \param dh
+*/
 void AddImagePart( QFile& hFile, QImage& image, int w1, int w2, int h1, int h2, 
                    GLViewer_CoordSystem* aViewerCS, GLViewer_CoordSystem* aPSCS, 
                    double a, double b, double c, double d, double dw, double dh )
@@ -471,42 +477,43 @@ void AddImagePart( QFile& hFile, QImage& image, int w1, int w2, int h1, int h2,
     {           
       uchar* theCurLine = image.scanLine( i ), cur;
       for( int j=w1; j<=w2; j++ )
-       for( int k=0; k<3; k++ )
-       {
-         cur = *(theCurLine+4*j+2-k);
-         *(line+cur_index) = hex( cur/16 ); //HI
-         *(line+cur_index+1) = hex( cur%16 ); //LO
-         full++;
-         cur_index+=2;
-         if( cur_index>=80 )
-         {
-           aBuffer += line;
-           aBuffer += "\n";
-           cur_index = 0;
-         }
-       }           
+        for( int k=0; k<3; k++ )
+        {
+          cur = *(theCurLine+4*j+2-k);
+          *(line+cur_index) = hex( cur/16 ); //HI
+          *(line+cur_index+1) = hex( cur%16 ); //LO
+          full++;
+          cur_index+=2;
+          if( cur_index>=80 )
+          {
+            aBuffer += line;
+            aBuffer += "\n";
+            cur_index = 0;
+          }
+        }           
     }
     
     aBuffer += "> false 3 colorimage\n\n";
 
-    hFile.writeBlock( aBuffer.ascii(), aBuffer.length() );
+    hFile.write( aBuffer.toLatin1() );
   }
 }
 
-//=======================================================================
-// Function: getBackgroundRectInViewerCS
-// Purpose :
-//=======================================================================
+/*!
+  \return background rectangle in viewer CS
+*/
 void GLViewer_Widget::getBackgroundRectInViewerCS( double& left, double& top, double& right, double& bottom )
 {
   left = -myIW/2; right = myIW/2; 
   top = myIH/2; bottom = -myIH/2;
 }
 
-//=======================================================================
-// Function: translateBackgroundToPS
-// Purpose :
-//=======================================================================
+/*!
+  Translates background to PostScript
+  \param hFile - PostScript file 
+  \param aViewerCS - viewer co-ordinate system
+  \param aPSCS - paper co-ordinate system
+*/
 void GLViewer_Widget::translateBackgroundToPS( QFile& hFile, GLViewer_CoordSystem* aViewerCS, GLViewer_CoordSystem* aPSCS )
 {
     QImage buf; 
@@ -548,15 +555,14 @@ void GLViewer_Widget::translateBackgroundToPS( QFile& hFile, GLViewer_CoordSyste
         const int max = 133000; //The maximum length of string in PS
         int dh = int( floor( double( max ) / ( 3.0*2.0*width ) ) );
         for( int k=buf.height()-1; k>=0; k-=dh )
-            AddImagePart( hFile, buf, 0, buf.width()-1, QMAX( k-dh+1, 0 ), k,
+            AddImagePart( hFile, buf, 0, buf.width()-1, qMax( k-dh+1, 0 ), k,
                           aViewerCS, aPSCS, a, b, c, d, dx, dy-(buf.height()-1-k) );
     }
 }
 
-//=======================================================================
-//! Function: DecodeScanLine
-//! Purpose : Translate image line with one color depth to line wiht other depth
-//=======================================================================
+/*!
+  Translates image line with one color depth to line with other depth
+*/
 void DecodeScanLine( int width, uchar* dest, int dest_depth, uchar* source, int source_depth )
 {
 #ifndef WIN32
@@ -573,7 +579,7 @@ typedef unsigned int WORD;
         memcpy( dest, source, aSize/8 );
     else
     {
-        double r, g, b; WORD color;
+        double r=0, g=0, b=0; WORD color;
         for( int i=0; i<width; i++ )
         {
             color = 0;
@@ -620,11 +626,13 @@ typedef unsigned int WORD;
     }
 }
 
-//=======================================================================
-// Function: translateBackgroundToEMF
-// Purpose :
-//=======================================================================
 #ifdef WIN32
+/*!
+  Translates background to EMF
+  \param dc - descriptor of EMF
+  \param aViewerCS - viewer co-ordinate system
+  \param aPSCS - paper co-ordinate system
+*/
 void GLViewer_Widget::translateBackgroundToEMF( HDC dc, GLViewer_CoordSystem* aViewerCS, GLViewer_CoordSystem* aEMFCS )
 {
     QImage buf;