Salome HOME
Patch for MacOS (from SALOME forum)
[modules/gui.git] / src / Qtx / Qtx.cxx
index f43ec8504d21a651c65f86fe7cd7feb71cb34ad6..d596874ce2988a8647757aed20e31913fb0f7b8e 100755 (executable)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2016  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
@@ -429,13 +429,15 @@ QString Qtx::library( const QString& str )
     name = QString( "lib" ) + name;
 #endif
 
-#ifdef WIN32
+#if defined(WIN32)
   QString libExt( "dll" );
+#elif defined(__APPLE__)
+  QString libExt( "dylib" );
 #else
   QString libExt( "so" );
 #endif
 
-  if ( ext.toLower() != QString( "so" ) && ext.toLower() != QString( "dll" ) )
+  if ( ext.toLower() != QString( "so" ) && ext.toLower() != QString( "dll" ) && ext.toLower() != QString( "dylib" ) )
   {
     if ( !name.isEmpty() && !ext.isEmpty() )
       name += QString( "." );
@@ -651,11 +653,17 @@ QCompleter* Qtx::pathCompleter( const PathType type, const QString& filter )
 /*!
   \brief Parse given string to retrieve environment variable.
 
-  Looks through the string for the patterns: ${name} or $(name) or %name%.
+  Looks through the string for the environment variable patterns.
   If string contains variable satisfying any pattern, the variable name
   is returned, start index of the variable is returned in the \a start parameter,
   and length of the variable is returned in the \a len parameter.
 
+  Supported environment variables definitions:
+  - ${name} or $name : Linux shell variable
+  - $(name)          : GNU make substitution
+  - %name%           : Windows shell variable
+  - %(name)s         : Python substitutions:
+
   \param str string being processed
   \param start if variable is found, this parameter contains its starting 
          position in the \a str
@@ -667,30 +675,22 @@ QString Qtx::findEnvVar( const QString& str, int& start, int& len )
   QString varName;
   len = 0;
 
-  QRegExp rx( "(^\\$\\{|[^\\$]\\$\\{)([a-zA-Z]+[a-zA-Z0-9_]*)(\\})|(^\\$\\(|[^\\$]\\$\\()([a-zA-Z]+[a-zA-Z0-9_]*)(\\))|(^\\$|[^\\$]\\$)([a-zA-Z]+[a-zA-Z0-9_]*)|(^%|[^%]%)([a-zA-Z]+[a-zA-Z0-9_]*)(%[^%]|%$)" );
-
-  int pos = rx.indexIn( str, start );
-  if ( pos != -1 )
+  QStringList rxList;
+  rxList << "\\$\\{([a-zA-Z][a-zA-Z_0-9]*)\\}"; // ${name}
+  rxList << "\\$([a-zA-Z][a-zA-Z_0-9]*)";       // $name
+  rxList << "\\$\\(([a-zA-Z][a-zA-Z_0-9]*)\\)"; // $(name)
+  rxList << "%([a-zA-Z][a-zA-Z0-9_]*)%";        // %name%
+  rxList << "%\\(([a-zA-Z][a-zA-Z_0-9]*)\\)s";  // %(name)s
+  
+  for ( int i = 0; i < rxList.count() && varName.isEmpty(); ++i ) 
   {
-    int i = 1;
-    while ( i <= rx.captureCount() && varName.isEmpty() )
+    QRegExp rx(rxList[i]);
+    int pos = rx.indexIn( str, start );
+    if ( pos != -1 )
     {
-      QString capStr = rx.cap( i );
-      if ( !capStr.contains( "%" ) && !capStr.contains( "$" ) )
-        varName = capStr;
-      i++;
-    }
-
-    if ( !varName.isEmpty() )
-    {
-      int capIdx = i - 1;
-      start = rx.pos( capIdx );
-      int end = start + varName.length();
-      if ( capIdx > 1 && rx.cap( capIdx - 1 ).contains( QRegExp( "\\$|%" ) ) )
-        start = rx.pos( capIdx - 1 ) + rx.cap( capIdx - 1 ).indexOf( QRegExp( "\\$|%" ) );
-      if ( capIdx < rx.captureCount() && !rx.cap( capIdx - 1 ).isEmpty() )
-        end++;
-      len = end - start;
+      varName = rx.cap( 1 );
+      start = pos;
+      len = rx.matchedLength();
     }
   }
   return varName;
@@ -2095,7 +2095,18 @@ QString Qtx::qtDir( const QString& context )
   return qtPath;
 }
 
-#ifndef WIN32
+/*!
+  Creates font from string description
+*/
+QFont Qtx::stringToFont( const QString& fontDescription )
+{
+  QFont font;
+  if ( fontDescription.trimmed().isEmpty() || !font.fromString( fontDescription ) )
+    font = QFont( "Courier", 11 );
+  return font;
+}
+
+#if !defined WIN32 && !defined __APPLE__ 
 
 #include <X11/Xlib.h>
 #include <GL/glx.h>
@@ -2164,3 +2175,61 @@ Qt::HANDLE Qtx::getVisual()
 }
 
 #endif // WIN32
+
+/*!
+  \class Qtx::CmdLineArgs
+  \brief Get access to the command line arguments in the C-like manner.
+
+  This class translates command line arguments stored in QApplication in form of QStrlingList
+  to the char* array, in the same way as they specified to main() function.
+
+  Constructor of class allocates required memory to store arguments; destructor deallocates it,
+  This allows using this class as a local variable:
+
+  \code
+  Qtx::CmdLineArgs args;
+  some_function(args.argc(), args.argv()); // function that has main()-like syntax.
+  \endcode
+*/
+
+/*!
+  \brief Default constructor.
+*/
+Qtx::CmdLineArgs::CmdLineArgs()
+{
+  QStringList args = QCoreApplication::arguments();
+  myArgc = args.size();
+  myArgv = new char*[myArgc];
+  for ( int i = 0; i < myArgc; i++ ) {
+    QByteArray ba = args[i].toUtf8();
+    myArgv[i] = qstrdup(ba.constData());
+  }
+}
+
+/*!
+  \brief Destructor. Deallocates the array with command line arguments
+*/
+Qtx::CmdLineArgs::~CmdLineArgs()
+{
+  for ( int i = 0; i < myArgc; i++ )
+    delete[] myArgv[i];
+  delete[] myArgv;
+}
+
+/*!
+  \brief Get number of command line arguments
+  \return number of arguments
+*/
+int Qtx::CmdLineArgs::argc() const
+{
+  return myArgc;
+}
+
+/*!
+  \brief Get command line arguments
+  \return command line arguments
+*/
+char** Qtx::CmdLineArgs::argv() const
+{
+  return myArgv;
+}