Salome HOME
Merge modifications for HYDRO project (origin/hydro/imps_2017 branch)
[modules/gui.git] / src / GLViewer / GLViewer_Drawer.cxx
index 1b6912b8163865d00784c79ee03c66547e2f6e0c..13d5e8acbef14f0b6bbaffb3abc2e4e93ceba5ec 100644 (file)
@@ -1,43 +1,49 @@
-//  Copyright (C) 2005 OPEN CASCADE
+// Copyright (C) 2007-2016  CEA/DEN, EDF R&D, 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_Drawer.cxx
 // Created:   November, 2004
-
 //#include <GLViewerAfx.h>
+//
 #include "GLViewer_Drawer.h"
 #include "GLViewer_Object.h"
 #include "GLViewer_Text.h"
 #include "GLViewer_ViewFrame.h"
 #include "GLViewer_ViewPort2d.h"
 
-#ifndef WIN32
+#include <QApplication>
+#include <QImage>
+#include <QPainter>
+#include <QFile>
+
+#if defined(__APPLE__)
+#include <OpenGL/CGLCurrent.h>
+#elif !defined(WIN32)
 #include <GL/glx.h>
 #endif
 
 #include <gp_Pnt2d.hxx>
 
-#include <qimage.h>
-#include <qpainter.h>
-
 #define TEXT_GAP    5
 // Two texture components for texmapped fonts: luminance and alpha
 #define NB_TEX_COMP 2
@@ -73,7 +79,7 @@ void GLViewer_TexFont::clearTextBases()
 GLViewer_TexFont::GLViewer_TexFont()
 : myMaxRowWidth( 0 ), myFontHeight( 0 )
 {
-    myQFont = QFont::defaultFont();
+    myQFont = QApplication::font();//QFont::defaultFont();
     mySeparator = 2;
     myIsResizeable = false;
     myMinMagFilter = GL_LINEAR;
@@ -204,7 +210,7 @@ bool GLViewer_TexFont::generateTexture()
             aPainter.drawText( myPositions[l], ( row + 1 ) * aRowPixelHeight - aDescent, aLetter );
         }
     
-        QImage aImage = aPixmap.convertToImage();
+        QImage aImage = aPixmap.toImage();
 
         //int qqq = 0;
         //if (qqq)
@@ -306,9 +312,9 @@ void GLViewer_TexFont::drawString( QString theStr, GLdouble theX , GLdouble theY
     float aDY = ( aRowPixelHeight - 1 ) / aYScale, aDX;
     char aLetter;
     int aLettIndex, row;
-    for( int i = 0; i < theStr.length(); i++ )
+    for ( int i = 0; i < (int)theStr.length(); i++ )
     {
-        aLetter    = theStr.data()[i];
+        aLetter    = theStr.data()[i].toLatin1();
         aLettIndex = (int)aLetter - FirstSymbolNumber;
         row        = aLettIndex / TEX_ROW_LEN;
 
@@ -338,9 +344,9 @@ void GLViewer_TexFont::drawString( QString theStr, GLdouble theX , GLdouble theY
 int GLViewer_TexFont::getStringWidth( QString theStr )
 {
     int aWidth = 0;
-    for( int i = 0; i < theStr.length(); i ++ )
+    for ( int i = 0; i < (int)theStr.length(); i ++ )
     {
-        char aLetter = theStr.data()[i];
+        char aLetter = theStr.data()[i].toLatin1();
         int aLettIndex = (int)aLetter - FirstSymbolNumber;
         aWidth += myWidths[aLettIndex] + mySeparator;
     }
@@ -373,7 +379,7 @@ static GLuint displayListBase( QFont* theFont )
   aFindFont.myIsUndl = theFont->underline();
   aFindFont.myPointSize = theFont->pointSize();
 
-#ifdef WIN32
+#if defined(WIN32)
   HGLRC ctx = ::wglGetCurrentContext();
   if ( !ctx )
     return aList;  
@@ -388,18 +394,47 @@ static GLuint displayListBase( QFont* theFont )
     QMap<GLViewer_TexFindId, GLuint>::iterator it = GLViewer_TexFont::BitmapFontCache.begin();
     for ( ; it != GLViewer_TexFont::BitmapFontCache.end(); ++it )
     {
-      if ( it.key().myViewPortId == (int)ctx && it.data() > listBase )
-        listBase = it.data();
+      if ( it.key().myViewPortId == (int)ctx && it.value() > listBase )
+        listBase = it.value();
     }
     listBase += 256;
 
     HDC glHdc = ::wglGetCurrentDC();
+ #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
     ::SelectObject( glHdc, theFont->handle() );
+ #endif
     if ( !::wglUseFontBitmaps( glHdc, 0, 256, listBase ) )
       listBase = 0;
     aList = listBase;
     GLViewer_TexFont::BitmapFontCache[aFindFont] = aList;
   }
+#elif defined(__APPLE__)
+  CGLContextObj ctx = ::CGLGetCurrentContext();
+  if ( !ctx )
+    return aList;
+
+  aFindFont.myViewPortId = (long)ctx;
+
+  if ( GLViewer_TexFont::BitmapFontCache.contains( aFindFont ) )
+    aList = GLViewer_TexFont::BitmapFontCache[aFindFont];
+  else
+  {
+    GLuint listBase = 0;
+    QMap<GLViewer_TexFindId, GLuint>::iterator it = GLViewer_TexFont::BitmapFontCache.begin();
+    for ( ; it != GLViewer_TexFont::BitmapFontCache.end(); ++it )
+    {
+      if ( it.key().myViewPortId == (long)ctx && it.value() > listBase )
+        listBase = it.value();
+    }
+    listBase += 256;
+
+    //HDC glHdc = ::wglGetCurrentDC();
+    //::SelectObject( glHdc, theFont->handle() );
+    //if ( !::wglUseFontBitmaps( glHdc, 0, 256, listBase ) )
+    //  listBase = 0;
+    aList = listBase;
+    GLViewer_TexFont::BitmapFontCache[aFindFont] = aList;
+  }
 #else //X Window
   Display* aDisp = glXGetCurrentDisplay();
   if( !aDisp )
@@ -429,19 +464,22 @@ static GLuint displayListBase( QFont* theFont )
     QMap<GLViewer_TexFindId, GLuint>::iterator it = GLViewer_TexFont::BitmapFontCache.begin();
     for ( ; it != GLViewer_TexFont::BitmapFontCache.end(); ++it )
     {
-      if ( it.key().myViewPortId == size_t(aCont) && it.data() > listBase )
-        listBase = it.data();
+      if ( it.key().myViewPortId == size_t(aCont) && it.value() > listBase )
+        listBase = it.value();
     }
     listBase += 256;
     
     //glXUseXFont( (Font)(theFont->handle()), 0, 256, listBase );
     int aFontCont = 0;
     QString aFontDef = theFont->toString();
-    char** xFontList = XListFonts( aDisp, aFontDef.latin1()/*aFindFont.myFontString.data()*/, 1, &aFontCont  );
+    char** xFontList = XListFonts( aDisp, aFontDef.toLatin1()/*aFindFont.myFontString.data()*/, 1, &aFontCont  );
+// TODO (QT5 PORTING) Below is a temporary solution, to allow compiling with Qt 5
+#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
     if( !theFont->handle() )
-    {       
+    {
+#endif
 #ifdef _DEBUG_
-      printf( "Can't load font %s. loading default font....\n", aFontDef.latin1()/*aFindFont.myFontString.data()*/ );
+      printf( "Can't load font %s. loading default font....\n", aFontDef.toLatin1().data()/*aFindFont.myFontString.data()*/ );
 #endif
       QString aFontMask ("-*-*-*-r-*-*-");
       aFontMask += aFontDef/*aFindFont.myFontString*/.section( ',', 1, 1 );
@@ -449,19 +487,20 @@ static GLuint displayListBase( QFont* theFont )
       printf( "Height of Default font: %s\n", aFontDef/*aFindFont.myFontString*/.section( ',', 1, 1 ).data() );
 #endif
       aFontMask += "-*-*-*-m-*-*-*";
-      xFontList = XListFonts( aDisp, aFontMask.data()/*"-*-*-*-r-*-*-12-*-*-*-m-*-*-*"*/, 1, &aFontCont  );
+      xFontList = XListFonts( aDisp, aFontMask.toLatin1().constData()/*"-*-*-*-r-*-*-12-*-*-*-m-*-*-*"*/, 1, &aFontCont  );
       if( aFontCont == 0 )
-      {      
+      {
 #ifdef _DEBUG_
         printf( "Can't load default font\n" );
 #endif
         return 0;
       }
       glXUseXFont( (Font)(XLoadFont( aDisp,xFontList[0] )), 0, 256, listBase );
+#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
     }
     else
       glXUseXFont( (Font)(theFont->handle()), 0, 256, listBase );
-    
+#endif
     aList = listBase;
     GLViewer_TexFont::BitmapFontCache[aFindFont] = aList;
   }
@@ -504,7 +543,7 @@ void GLViewer_Drawer::destroyAllTextures()
     QMap<GLViewer_TexFindId,GLViewer_TexIdStored>::Iterator anEndIt= GLViewer_TexFont::TexFontBase.end();
 
     for( ; anIt != anEndIt; anIt++ )
-        glDeleteTextures( 1, &(anIt.data().myTexFontId) );
+        glDeleteTextures( 1, &(anIt.value().myTexFontId) );
 }
 
 /*!
@@ -513,25 +552,25 @@ void GLViewer_Drawer::destroyAllTextures()
 */
 void GLViewer_Drawer::setAntialiasing(const bool on)
 {
-       if (on)
-       {
+        if (on)
+        {
     glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
     glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
 
-               glEnable(GL_POINT_SMOOTH);
-               glEnable(GL_LINE_SMOOTH);
-               glEnable(GL_POLYGON_SMOOTH);
-               glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 
-               glEnable (GL_BLEND);
-       }
-       else
-       {
-               glDisable(GL_POINT_SMOOTH);
-               glDisable(GL_LINE_SMOOTH);
-               glDisable(GL_POLYGON_SMOOTH);
-               glBlendFunc (GL_ONE, GL_ZERO);
-               glDisable (GL_BLEND);
-       }
+                glEnable(GL_POINT_SMOOTH);
+                glEnable(GL_LINE_SMOOTH);
+                glEnable(GL_POLYGON_SMOOTH);
+                glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 
+                glEnable (GL_BLEND);
+        }
+        else
+        {
+                glDisable(GL_POINT_SMOOTH);
+                glDisable(GL_LINE_SMOOTH);
+                glDisable(GL_POLYGON_SMOOTH);
+                glBlendFunc (GL_ONE, GL_ZERO);
+                glDisable (GL_BLEND);
+        }
 }
 
 /*! Loads texture from file
@@ -785,7 +824,7 @@ void GLViewer_Drawer::drawText( const QString& text, GLfloat xPos, GLfloat yPos,
   {
     glRasterPos2f( xPos, yPos );
     glListBase( displayListBase( theFont ) );
-    glCallLists( text.length(), GL_UNSIGNED_BYTE, text.local8Bit().data() );
+    glCallLists( text.length(), GL_UNSIGNED_BYTE, text.toLocal8Bit().data() );
   }
 }
 
@@ -946,7 +985,7 @@ bool GLViewer_Drawer::translateToEMF( HDC hDC, GLViewer_CoordSystem* aViewerCS,
   \param fillingColor - color of filling
 */
 void GLViewer_Drawer::drawRectangle( GLViewer_Rect* rect, GLfloat lineWidth, GLfloat gap,
-                                    QColor color, bool filled, QColor fillingColor )
+                                     QColor color, bool filled, QColor fillingColor )
 {
   if( !rect )
     return;
@@ -996,7 +1035,7 @@ void GLViewer_Drawer::drawContour( const GLViewer_PntList& pntList, QColor color
   glLineWidth( lineWidth );
   
   glBegin( GL_LINES );
-  QValueList<GLViewer_Pnt>::const_iterator it = pntList.begin();
+  QList<GLViewer_Pnt>::const_iterator it = pntList.begin();
   for( ; it != pntList.end(); ++it )
     glVertex2f( (*it).x(), (*it).y() );
   glEnd();
@@ -1011,7 +1050,7 @@ void GLViewer_Drawer::drawContour( const GLViewer_PntList& pntList, QColor color
   \param isStripe - enables line stipple
 */
 void GLViewer_Drawer::drawContour( GLViewer_Rect* rect, QColor color, GLfloat lineWidth,
-                                  GLushort pattern, bool isStripe )
+                                   GLushort pattern, bool isStripe )
 {
   float x1 = rect->left();
   float x2 = rect->right();
@@ -1051,7 +1090,7 @@ void GLViewer_Drawer::drawPolygon( const GLViewer_PntList& pntList, QColor color
     ( GLfloat )color.green() / 255,
     ( GLfloat )color.blue() / 255 );
   glBegin( GL_POLYGON );
-  QValueList<GLViewer_Pnt>::const_iterator it = pntList.begin();
+  QList<GLViewer_Pnt>::const_iterator it = pntList.begin();
   for( ; it != pntList.end(); ++it )
     glVertex2f( (*it).x(), (*it).y() );
   glEnd();
@@ -1134,9 +1173,9 @@ void GLViewer_Drawer::drawCross( GLfloat x, GLfloat y, QColor color )
   \param filled - drawn as filled
 */
 void GLViewer_Drawer::drawArrow( const GLfloat red, const GLfloat green, const GLfloat blue,
-                                GLfloat lineWidth,
-                                GLfloat staff, GLfloat length, GLfloat width,
-                                GLfloat x, GLfloat y, GLfloat angle, GLboolean filled )
+                                 GLfloat lineWidth,
+                                 GLfloat staff, GLfloat length, GLfloat width,
+                                 GLfloat x, GLfloat y, GLfloat angle, GLboolean filled )
 {
   GLfloat vx1 = x;
   GLfloat vy1 = y + staff + length;