Salome HOME
Updated copyright comment
[modules/gui.git] / src / GLViewer / GLViewer_Widget.cxx
index 2f35e0819cd82754e7a5535bbfeeb057dbdc4c39..e01435daabbacf50456f84939f74b4ef14e519f8 100644 (file)
-// File:      GLViewer_Widget.cxx
-// Created:   November, 2004
-// Author:    OCC team
-// Copyright (C) CEA 2004
-
-/***************************************************************************
-**  Class:   GLViewer_Widget
-**  Descr:   OpenGL QWidget for GLViewer
-**  Module:  GLViewer
-**  Created: UI team, 27.03.03
-****************************************************************************/
-
-//#include <GLViewerAfx.h>
+// Copyright (C) 2007-2024  CEA, EDF, OPEN CASCADE
+//
+// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+//
+// 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.
+//
+// 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.
+//
+// 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
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
+
 #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 <math.h>
-//#include <stdlib.h>
-//#include <iostream.h>
-
-#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>
-
-//-----------
-#include <qfontdialog.h>
-#include <qfontmetrics.h>
-#include <qpainter.h>
-//-----------
-
-//static GLuint texFont;
-//static int fontW;
-//static int fontH;
-
-/*
-static void genFont()
-{
-    bool ok;
-    QFont aFont = QFontDialog::getFont( &ok );
-    QFontMetrics aFM( aFont );
-    QString aStr;
-    for( int k = 32; k <= 127; k++ )
-    {
-        char aLetter = (char)k;
-        aStr += aLetter;
-    }
-    
-    int pixelsWidth = aFM.width( aStr );
-    int pixelsHigh = aFM.height();
-    int aFontX = 64;
-    int aFontY = 64;
-
-    while( aFontX < pixelsWidth )
-        aFontX = aFontX * 2;
-    while( aFontY < pixelsHigh )
-        aFontY = aFontY * 2;
-
-    QPixmap* aPixmap = new QPixmap( aFontX, aFontY );
-    aPixmap->fill( QColor( 255, 255, 255) );
-    QPainter aPainter( aPixmap );
-    aPainter.setFont( aFont );    
-    aPainter.drawText ( 0, pixelsHigh, aStr );
-    QImage aImage = aPixmap->convertToImage();
-
-    aImage.save( "W:\\Temp\\pic.jpg", "JPEG" );
-
-    char* pixels = new char[aFontX * aFontY * 2];
-
-    for( int i = 0; i < aFontY; i++ )
-    {            
-        for( int j = 0; j < aFontX;  j++ )
-        {
-            if( qRed( aImage.pixel( j, aFontY - i - 1 ) ) == 0 )
-            {
-                pixels[i * aFontX * 2 + j * 2] = (char) 255;
-                pixels[i * aFontX * 2 + j * 2 + 1]= (char) 255;
-            }
-            else
-            {
-                pixels[i * aFontX * 2 + j * 2] = (char) 0;
-                pixels[i * aFontX * 2 + j * 2 + 1]= (char) 0;
-            }                
-        }
-    }
-
-    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
-    glGenTextures(1, &texFont);
-    glBindTexture(GL_TEXTURE_2D, texFont);  
-    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
-    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
-    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
-    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
-    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
-    glTexImage2D(GL_TEXTURE_2D, 0, 2, aFontX,
-        aFontY, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, pixels);
-
-    fontW = aFontX;
-    fontH = aFontY;
-
-    delete[] pixels;
-}
 
-static void showFont()
-{
-    glEnable(GL_TEXTURE_2D);
-    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
-    glAlphaFunc(GL_GEQUAL, 0.005F);
-    glEnable(GL_ALPHA_TEST);
-    glColor3f( 1.0, 1.0, 1.0);
-    glBindTexture(GL_TEXTURE_2D, texFont);
-    glBegin(GL_QUADS);
-
-    glTexCoord2f( 0.0, 0.0 ); glVertex3f( -fontW/2, -fontH/2, 1.0 );    
-    glTexCoord2f( 0.0, 1.0 ); glVertex3f( -fontW/2, fontH/2, 1.0 );
-    glTexCoord2f( 1.0, 1.0 ); glVertex3f( fontW/2, fontH/2, 1.0 );
-    glTexCoord2f( 1.0, 0.0 ); glVertex3f( fontW/2, -fontH/2, 1.0 );
-
-    glEnd();
-    glFlush();
-    glDisable(GL_ALPHA_TEST);
-    glDisable(GL_TEXTURE_2D);
-}
+#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 ):
+GLViewer_Widget::GLViewer_Widget( QWidget* parent, const char* /*name*/ ):
 QGLWidget( parent, 0/*, WRepaintNoErase | WResizeNoErase*/ )
 {
   myViewPort = ( GLViewer_ViewPort2d* )parent;
@@ -161,10 +66,16 @@ QGLWidget( parent, 0/*, WRepaintNoErase | WResizeNoErase*/ )
   setMouseTracking( true );
 }
 
+/*!
+  Destructor
+*/
 GLViewer_Widget::~GLViewer_Widget()
 {
 }
 
+/*!
+  \return offset parameters of Window in OpenGL global scene
+*/
 void GLViewer_Widget::getPan( GLfloat& xPan, GLfloat& yPan, GLfloat& zPan )
 {
   xPan = myXPan;
@@ -172,6 +83,9 @@ void GLViewer_Widget::getPan( GLfloat& xPan, GLfloat& yPan, GLfloat& zPan )
   zPan = myZPan;
 }
 
+/*!
+  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;
@@ -179,6 +93,9 @@ void GLViewer_Widget::setPan( GLfloat xPan, GLfloat yPan, GLfloat zPan )
   myZPan = zPan;
 }
 
+/*!
+  \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;
@@ -186,6 +103,9 @@ void GLViewer_Widget::getScale( GLfloat& xScale, GLfloat& yScale, GLfloat& zScal
   zScale = myZScale;
 }
 
+/*!
+  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 )
@@ -196,24 +116,41 @@ void GLViewer_Widget::setScale( GLfloat xScale, GLfloat yScale, GLfloat zScale )
   }
 }
 
-void GLViewer_Widget::getRotationStart( GLfloat& rotationStartX, GLfloat& rotationStartY,
-                                     GLfloat& rotationStartZ )
+/*!
+  \return start point of curren rotation of Window in OpenGL global scene
+*/
+void GLViewer_Widget::getRotationStart( GLfloat& rotationStartX,
+                                        GLfloat& rotationStartY,
+                                        GLfloat& rotationStartZ )
 {
     rotationStartX = myRotationStartX;
     rotationStartY = myRotationStartY;
     rotationStartZ = myRotationStartZ;
 }
 
-void GLViewer_Widget::setRotationStart( GLfloat rotationStartX, GLfloat rotationStartY,
-                                     GLfloat rotationStartZ )
+/*!
+  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 rotationStartZ )
 {
     myRotationStartX = rotationStartX;
     myRotationStartY = rotationStartY;
     myRotationStartZ = rotationStartZ;
 }
 
-void GLViewer_Widget::getRotation( GLfloat& rotationAngle, GLfloat& rotationCenterX,
-                                GLfloat& rotationCenterY, GLfloat& rotationCenterZ )
+/*!
+  \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 )
 {
     rotationAngle = myRotationAngle;
     rotationCenterX = myRotationCenterX;
@@ -221,8 +158,17 @@ void GLViewer_Widget::getRotation( GLfloat& rotationAngle, GLfloat& rotationCent
     rotationCenterZ = myRotationCenterZ;
 }
 
-void GLViewer_Widget::setRotation( GLfloat rotationAngle, GLfloat rotationCenterX,
-                                GLfloat rotationCenterY, GLfloat rotationCenterZ )
+/*!
+  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 )
 {
     myRotationAngle = rotationAngle;
     myRotationCenterX = rotationCenterX;
@@ -230,6 +176,11 @@ void GLViewer_Widget::setRotation( GLfloat rotationAngle, GLfloat rotationCenter
     myRotationCenterZ = rotationCenterZ;
 }
 
+
+/*!
+  Sets image as background
+  \param filename - name of file
+*/
 void GLViewer_Widget::setBackground( QString filename )
 {
     
@@ -273,8 +224,6 @@ void GLViewer_Widget::setBackground( QString filename )
         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
         glGenTextures(1, &texName);
         glBindTexture(GL_TEXTURE_2D, texName);
-        //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
-        //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, myBackgroundSize , myBackgroundSize, 0, GL_RGBA, GL_UNSIGNED_BYTE,
@@ -282,165 +231,58 @@ void GLViewer_Widget::setBackground( QString filename )
 
         delete[] pixels;        
     }
-    
 }
 
+/*!
+  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 );
 }
 
+/*!
+  Removes tooltip
+*/
 void GLViewer_Widget::removeToolTip()
 {
-    QToolTip::remove( this, myToolTipRect );
+    setToolTip("");
+    //QToolTip::remove( this, myToolTipRect );
 }
 
+/*!
+  Initialization (redefined virtual from QGLWidget)
+*/
 void GLViewer_Widget::initializeGL()
 {
     setAutoBufferSwap( true );
 
-//  cout << "GLViewer_Widget::initializeGL" << endl;
-
-    //glClearColor( 0.0, 0.0, 0.0, 0.0 );
-    //-----------------
     glShadeModel(GL_FLAT);
     
     //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
         isLoadBackground = true;
-        setBackground( aPicturePath );
-       
-        // for test texture font
-        //genFont();
-/*
-        bool ok;
-        QFont aFont = QFontDialog::getFont( &ok, this );
-        QFontMetrics aFM( aFont );
-        int pixelsWidth = aFM.width( "What's the width of this text?" );
-        int pixelsHigh = aFM.height();
-        int aFontX = 64;
-        int aFontY = 64;
-
-        while( aFontX < pixelsWidth )
-            aFontX = aFontX * 2;
-        while( aFontY < pixelsHigh )
-            aFontY = aFontY * 2;
-
-        myIW = aFontX;
-        myIH = aFontY;
-
-        QPixmap* aPixmap = new QPixmap( aFontX, aFontY );
-        aPixmap->fill( QColor( 255, 255, 255) );
-        QPainter aPainter( aPixmap );
-        aPainter.setFont( aFont );
-        //Painter.setPen( QColor( 0, 0, 0 ) );
-        aPainter.drawText ( 0, pixelsHigh, "A" );
-        QImage aImage = aPixmap->convertToImage();
-
-        aImage.save( "W:\\Temp\\pic.jpg", "JPEG" );
-
-        GLubyte* pixels = new GLubyte[aFontX * aFontY * 2];
-
-        for( int i = 0; i < aFontY; i++ )
-        {            
-            for( int j = 0; j < aFontX;  j++ )
-            {
-                if( qRed( aImage.pixel( j, myIH - i - 1 ) ) == 0 )
-                {
-                    pixels[i * aFontX * 2 + j * 2] = (GLubyte) 255;
-                    pixels[i * aFontX * 2 + j * 2 + 1]= (GLubyte) 255;
-                }
-                else
-                {
-                    pixels[i * aFontX * 2 + j * 2] = (GLubyte) 0;
-                    pixels[i * aFontX * 2 + j * 2 + 1]= (GLubyte) 0;
-                }                
-            }
-        }
-
-
-        glBindTexture(GL_TEXTURE_2D, texName);  
-        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
-        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
-        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
-        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
-        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
-        glTexImage2D(GL_TEXTURE_2D, 0, 2, aFontX,
-            aFontY, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, pixels);
-
-        delete[] pixels;
-*/
-        
-        //tex2 = buf.convertDepth( 32, 0 );
-
-        //tex1 = QGLWidget::convertToGLFormat( tex2 );  // flipped 32bit RGBA
-
-        /*myIW = buf.width();
-        myIH = buf.height();
-
-        myBackgroundSize = 64;
-        while( myBackgroundSize < myIW || myBackgroundSize < myIH)
-            myBackgroundSize = myBackgroundSize * 2;
-
-        GLubyte* pixels = new GLubyte[myBackgroundSize * myBackgroundSize * 4];
-
-        //GLubyte pixels[512][512][3];
-        for( int i = 0; i < myBackgroundSize; i++ )
-        {            
-            for( int j = 0; j < myBackgroundSize; j++ )
-            {
-                if( j < myIW && i < myIH )
-                {
-                    pixels[i * myBackgroundSize * 4 + j * 4] = (GLubyte)qRed( buf.pixel(j,myIH - i - 1) );
-                    pixels[i * myBackgroundSize * 4 + j * 4 + 1]= (GLubyte)qGreen( buf.pixel(j,myIH - i - 1) );
-                    pixels[i * myBackgroundSize * 4 + j * 4 + 2] = (GLubyte)qBlue( buf.pixel(j,myIH - i - 1) );
-                }
-                else
-                {
-                    pixels[i * myBackgroundSize * 4 + j * 4] = (GLubyte)0;
-                    pixels[i * myBackgroundSize * 4 + j * 4 + 1] = (GLubyte)0;
-                    pixels[i * myBackgroundSize * 4 + j * 4 + 2] = (GLubyte)0;
-                }                
-                pixels[i * myBackgroundSize* 4 + j * 4 +  3] = (GLubyte)255;
-                //cout << "RED = " << qRed( buf.pixel(i,j) ) << " GREEN  = " << qGreen( buf.pixel(i,j) ) <<
-                //    " BLUE = " << qBlue( buf.pixel(i,j) ) << endl;
-            }
-        }
-
-        //initialize texture
-        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
-        glGenTextures(1, &texName);
-        glBindTexture(GL_TEXTURE_2D, texName);
-        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
-        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
-        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
-        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
-        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, myBackgroundSize , myBackgroundSize, 0, GL_RGBA, GL_UNSIGNED_BYTE,
-            pixels);
-
-        delete[] pixels;  
-        */
+        setBackground( aPicturePath );       
         
     }
     
     else
         isLoadBackground = false;
-
-    //GLViewer_TexFont aTexFont;
-    //aTexFont.generateTexture();
-    //aTexFont.drawString( "hello" );
-    //-----------------
 }
 
+/*!
+  Paints content
+*/
 void GLViewer_Widget::paintGL()
 {
-//    cout << "GLViewer_Widget::paintGL" << endl;
-
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
     glMatrixMode( GL_MODELVIEW );
@@ -449,9 +291,8 @@ void GLViewer_Widget::paintGL()
     glRotatef( myRotationAngle, myRotationCenterX, myRotationCenterY, myRotationCenterZ );
     glScalef( myXScale, myYScale, myZScale );
     glTranslatef( myXPan, myYPan, myZPan );
-    //glRotatef( myRotationAngle, myRotationCenterX, myRotationCenterY, myRotationCenterZ );  
     
-        if( isLoadBackground )
+    if( isLoadBackground )
     {
         glEnable(GL_TEXTURE_2D);
         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
@@ -467,8 +308,6 @@ void GLViewer_Widget::paintGL()
         glFlush();
         glDisable(GL_TEXTURE_2D);
 
-        // for test texture font
-        // showFont();
         glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
     }
 
@@ -476,27 +315,21 @@ void GLViewer_Widget::paintGL()
     GLViewer_Grid* grid = myViewPort->getGrid();
     if( grid )
         grid->draw();
-    //myViewPort->drawCompass();
-
-    //glEnable( GL_POLYGON_OFFSET_FILL );
-    //glPolygonOffset( 0, 1 );
 
     GLViewer_Viewer2d* v = ( GLViewer_Viewer2d* )getViewPort()->getViewFrame()->getViewer();
     if( !isExportMode )
         v->updateDrawers( GL_FALSE, myXScale, myYScale );
     else
         v->repaintView( getViewPort()->getViewFrame() );
-
-    //glDisable( GL_POLYGON_OFFSET_FILL );
-
-//    GLViewer_TexFont aTexFont;
-//    aTexFont.generateTexture();
-//    aTexFont.drawString( "hello" );
 }
 
+/*!
+  Resets OpenGl parameters after resize
+  \param w - new width
+  \param h - new height
+*/
 void GLViewer_Widget::resizeGL( int w, int h )
 {
-//  cout << "GLViewer_Widget::resizeGL " << w << " " << h << endl;
 
   if( h < 1 ) h = 1;
   if( w < 1 ) w = 1;
@@ -511,9 +344,6 @@ void GLViewer_Widget::resizeGL( int w, int h )
 
   myViewPort->initResize( w, h );
 
-  //GLViewer_Viewer2d* v = ( GLViewer_Viewer2d* )getViewPort()->getViewFrame()->getViewer();
-  //v->updateDrawers( GL_FALSE, myXScale, myYScale );
-
   glMatrixMode( GL_PROJECTION );
   glLoadIdentity();
   GLfloat w_c = w / 2., h_c = h / 2.; 
@@ -524,16 +354,9 @@ void GLViewer_Widget::resizeGL( int w, int h )
   glLoadIdentity(); 
 }
 
-void GLViewer_Widget::copyBuffers()
-{
-//  cout << "GLViewer_Widget::copyBuffers" << endl;
-
-  GLint val[4]; 
-  glGetIntegerv( GL_VIEWPORT, val );
-  glReadBuffer( GL_FRONT );
-  glCopyPixels( val[0], val[1], val[2], val[3], GL_COLOR );
-}
-
+/*!
+  Provides repaint in export mode
+*/
 void GLViewer_Widget::exportRepaint()
 {
     isExportMode = true;
@@ -543,97 +366,154 @@ void GLViewer_Widget::exportRepaint()
     isExportMode = false;
 }
 
-
+/*!
+  Custom paint event handler
+*/
 void GLViewer_Widget::paintEvent( QPaintEvent* e )
 {
-//  cout << "GLViewer_Widget::paintEvent" << endl;
   QApplication::sendEvent( myViewPort, e );
 }
 
+/*!
+  Custom mouse move event handler
+*/
 void GLViewer_Widget::mouseMoveEvent( QMouseEvent* e )
 {
-//  cout << "GLViewer_Widget::mouseMoveEvent" << endl;
   QApplication::sendEvent( myViewPort, e );
 }
 
+/*!
+  Custom mouse press event handler
+*/
 void GLViewer_Widget::mousePressEvent( QMouseEvent* e )
 {
-//  cout << "GLViewer_Widget::mousePressEvent" << endl;
   QApplication::sendEvent( myViewPort, e );
 }
 
+/*!
+  Custom mouse release event handler
+*/
 void GLViewer_Widget::mouseReleaseEvent( QMouseEvent* e )
 {
-//  cout << "GLViewer_Widget::mouseReleaseEvent" << endl;
   QApplication::sendEvent( myViewPort, e );
 }
 
-void GLViewer_Widget::enterEvent( QEvent* e )
+/*!
+  Custom enter event handler
+*/
+void GLViewer_Widget::enterEvent( QEvent* /*e*/ )
 {
-//  cout << "GLViewer_Widget::enterEvent" << endl;
   updateGL();
 }
-void GLViewer_Widget::leaveEvent( QEvent* e )
+
+/*!
+  Custom leave event handler
+*/
+void GLViewer_Widget::leaveEvent( QEvent* /*e*/ )
 {
-//  cout << "GLViewer_Widget::leaveEvent" << endl;
   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);
+}
+
+/*!
+  \return the hex code of digit < 16
+  \param c - digit
+*/
 inline char hex( uchar c )
 {
   if( c<=9 )
     return '0'+c;
-  else
+  else if( c < 16 )
     return 'a' + c - 10;
-}
 
+  return ' ';
+}
+
+/*!
+  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 )
 {
-    if( aViewerCS && aPSCS )
-    {       
-        double width = w2-w1+1, height = h2-h1+1;
-        QString aBuffer = "", temp = "%1 %2 8 [ %3 %4 %5 %6 %7 %8 ]\n";
-        aBuffer += temp.arg( width ).arg( height ).
+  if( aViewerCS && aPSCS )
+  {       
+    double width = w2-w1+1, height = h2-h1+1;
+    QString aBuffer = "", temp = "%1 %2 8 [ %3 %4 %5 %6 %7 %8 ]\n";
+    aBuffer += temp.arg( width ).arg( height ).
                         arg( a ).arg( b ).arg( c ).arg( d ).
                         arg( dw ).arg( dh );
-        aBuffer += "<\n";   
-
-        char line[81]; line[80] = '\0'; int cur_index = 0;
-        int full = 0;
-        for( int i=h2; i>=h1; i-- )
-        {           
-            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;
-                    }
-                }           
-        }
-
-        aBuffer += "> false 3 colorimage\n\n";
-
-        hFile.writeBlock( aBuffer.ascii(), aBuffer.length() );
+    aBuffer += "<\n";   
+    
+    char line[81]; line[80] = '\0'; int cur_index = 0;
+    int full = 0;
+    for( int i=h2; i>=h1; i-- )
+    {           
+      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;
+          }
+        }           
     }
+    
+    aBuffer += "> false 3 colorimage\n\n";
+
+    hFile.write( aBuffer.toLatin1() );
+  }
 }
 
+/*!
+  \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;
+  left = -myIW/2; right = myIW/2; 
+  top = myIH/2; bottom = -myIH/2;
 }
 
+/*!
+  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; 
@@ -675,11 +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) );
     }
 }
 
+/*!
+  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
@@ -696,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;
@@ -744,6 +627,12 @@ typedef unsigned int WORD;
 }
 
 #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;