Salome HOME
Copyright update 2021
[modules/gui.git] / src / Qtx / Qtx.cxx
old mode 100755 (executable)
new mode 100644 (file)
index e976086..28cf2c3
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2021  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
 #include <QApplication>
 #include <QDesktopWidget>
 #include <QtDebug>
-#if QT_VERSION > QT_VERSION_CHECK(5, 0, 0)
 #include <QSurfaceFormat>
-#endif
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdarg.h>
 #include <clocale>
 
+#ifdef WIN32
+#include <windows.h>
+#define MAX_VALUE_SIZE 32767 // Limit according to http://msdn.microsoft.com/en-us/library/ms683188.aspx
+#endif
+
+#include <iostream>
+
 #define BICOLOR_CHANGE_HUE
 
 /*!
@@ -484,18 +489,18 @@ QString Qtx::library( const QString& str )
 */
 QString Qtx::tmpDir()
 {
-  const char* tmpdir = ::getenv( "TEMP" );
-  if ( !tmpdir )
-    tmpdir = ::getenv ( "TMP" );
-  if ( !tmpdir )
+  QString tmpdir = getenv( "TEMP" );
+  if ( tmpdir.isEmpty() )
+    tmpdir = getenv ( "TMP" );
+  if ( tmpdir.isEmpty() )
   {
 #ifdef WIN32
-    tmpdir = "C:\\";
+    tmpdir = QString("C:\\");
 #else
-    tmpdir = "/tmp";
+    tmpdir = QString("/tmp");
 #endif
   }
-  return QString( tmpdir );
+  return tmpdir;
 }
 
 /*!
@@ -584,7 +589,7 @@ bool Qtx::dos2unix( const QString& absName )
     char inbuf[512], outbuf[512];
 
     /* convert buffer */
-    int nbread = ::fread( inbuf, 1, sizeof( inbuf ), src );
+    int nbread = (int)::fread( inbuf, 1, sizeof( inbuf ), src ); //!< TODO: conversion from 'size_t' to 'int'
     for ( int incnt = 0; incnt < nbread; incnt++  )
     {
       if ( waitingLF )
@@ -605,7 +610,7 @@ bool Qtx::dos2unix( const QString& absName )
     waitingLF = ( inbuf[nbread - 1] == CR );
 
     /* write converted buffer to temp file */
-    int nbwri = ::fwrite( outbuf, 1, outcnt, tgt );
+    int nbwri = (int)::fwrite( outbuf, 1, outcnt, tgt ); //!< TODO: conversion from 'size_t' to 'int'
     if ( nbwri != outcnt )
     {
       ::fclose( src );
@@ -746,8 +751,8 @@ QString Qtx::makeEnvVarSubst( const QString& str, const SubstMode mode )
         break;
 
       QString newStr;
-      if ( ::getenv( envName.toUtf8() ) || mode == Always )
-        newStr = QString( ::getenv( envName.toUtf8() ) );
+      if ( getenv( envName ).isEmpty() || mode == Always )
+        newStr = QString( getenv( envName ) );
 
       if ( newStr.isNull() )
       {
@@ -1734,9 +1739,14 @@ Qtx::BackgroundData Qtx::stringToBackground( const QString& str )
   
   To use the Localizer class, just create a local variable in the beginning
   of the code where you need to read / write data from textual file(s).
-  The constructor of the class forces setting "C" locale temporariy.
+  The constructor of the class forces setting "C" locale temporarily.
   The destructor switches back to the initial locale.
 
+  There are two ways to create a localizer.
+  First constructor accepts category and locale value to be forced as parameters.
+  The second constructor does not take parameters, and is just a shortcut to the
+  first one, setting LC_NUMERIC as a category and "C" as a locale to force.
+  
   \code
   Qtx::Localizer loc;
   readSomething();
@@ -1745,12 +1755,30 @@ Qtx::BackgroundData Qtx::stringToBackground( const QString& str )
 */
 
 /*!
-  \brief Constructor. Forces "C" locale to be set.
+  \brief Default constructor. Forces "C" locale to be set as LC_NUMERIC.
 */
 Qtx::Localizer::Localizer()
 {
-  myCurLocale = setlocale( LC_NUMERIC, 0 );
-  setlocale( LC_NUMERIC, "C" );
+  init( LC_NUMERIC, "C" );
+}
+
+/*!
+  \brief Constructor. Forces \a locale to be set for \a category.
+*/
+Qtx::Localizer::Localizer( int category, const char* locale )
+{
+  init( category, locale );
+}
+
+/*!
+  \brief Internal initialization
+  \internal
+*/
+void Qtx::Localizer::init( int category, const char* locale )
+{
+  myCategory = category;
+  myOriginalLocale = setlocale( category, NULL );
+  setlocale( category, locale );
 }
 
 /*!
@@ -1758,7 +1786,7 @@ Qtx::Localizer::Localizer()
 */
 Qtx::Localizer::~Localizer()
 {
-  setlocale( LC_NUMERIC, myCurLocale.toLatin1().constData() );
+  setlocale( myCategory, myOriginalLocale.toLatin1().constData() );
 }
 
 /*!
@@ -2101,7 +2129,6 @@ long Qtx::versionToId( const QString& version )
   
   The function tries to detect qt installation directory by analyzing the system variables in the following order:
   - QT5_ROOT_DIR
-  - QT4_ROOT_DIR
   - QT_ROOT_DIR
   - QTDIR
 
@@ -2113,10 +2140,12 @@ long Qtx::versionToId( const QString& version )
 
 QString Qtx::qtDir( const QString& context )
 {
-  const char* vars[] = { "QT5_ROOT_DIR", "QT4_ROOT_DIR", "QT_ROOT_DIR", "QTDIR" };
+
+  QStringList vars = { "QT5_ROOT_DIR", "QT_ROOT_DIR", "QTDIR" };
   QString qtPath;
-  for (uint i = 0; i < sizeof(vars)/sizeof(vars[0]) && qtPath.isEmpty(); i++ )
-    qtPath = qgetenv( vars[i] );
+  for (int i = 0; i < vars.length() && qtPath.isEmpty(); i++ ) {
+    qtPath = getenv(vars[i]);
+  }
   if ( !qtPath.isEmpty() && !context.isEmpty() )
     qtPath = QDir( qtPath ).absoluteFilePath( context );
   return qtPath;
@@ -2133,6 +2162,36 @@ QFont Qtx::stringToFont( const QString& fontDescription )
   return font;
 }
 
+QString Qtx::getenv(const QString & envVar)
+{
+       QString value;
+#ifndef WIN32
+       value = qgetenv(envVar.toLocal8Bit().constData());
+#else
+       LPTSTR buff = new TCHAR[MAX_VALUE_SIZE];
+#ifdef UNICODE
+       LPTSTR anEnvVar = new TCHAR[envVar.length() + 1];       
+       anEnvVar[envVar.toWCharArray(anEnvVar)] = '\0';
+#else
+       const TCHAR* anEnvVar = envVar.toLocal8Bit(buff).constData();
+#endif 
+       const DWORD ret = GetEnvironmentVariable(anEnvVar, buff, MAX_VALUE_SIZE);
+       buff[ret] = '\0';
+       if (ret > 0) {
+#ifdef UNICODE
+               value = QString::fromWCharArray(buff);
+#else
+               value = QString::fromLocal8Bit(buff);
+#endif 
+       }
+       delete buff;
+#ifdef UNICODE
+       delete anEnvVar;
+#endif
+#endif
+       return value;
+}
+
 #if !defined WIN32 && !defined __APPLE__ 
 
 #include <X11/Xlib.h>
@@ -2203,8 +2262,6 @@ Qt::HANDLE Qtx::getVisual()
 
 #endif // WIN32
 
-
-#if QT_VERSION > QT_VERSION_CHECK(5, 0, 0)
 /*!
   \brief Set default QSurfaceFormat for an application.
 
@@ -2240,7 +2297,6 @@ void Qtx::initDefaultSurfaceFormat()
 
   QSurfaceFormat::setDefaultFormat(fmt);
 }
-#endif
 
 /*!
   \class Qtx::CmdLineArgs