Salome HOME
SIMAN removal
[modules/gui.git] / src / Qtx / QtxResourceMgr.cxx
index 51a9630df0d2bf81abb6b8a60bdf232b838c3242..68511cf35c7703a6cada46f0a6917b91bba74f4f 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2015  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
 //
 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
@@ -6,7 +6,7 @@
 // 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
 // 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.
+// 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
 //
 // This library is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 #include <stdlib.h>
 
 
 #include <stdlib.h>
 
+/* XPM for the default pixmap */
+static const char* pixmap_not_found_xpm[] = {
+"16 16 3 1",
+"       c None",
+".      c #000000",
+"+      c #A80000",
+"                ",
+"                ",
+"    .     .     ",
+"   .+.   .+.    ",
+"  .+++. .+++.   ",
+"   .+++.+++.    ",
+"    .+++++.     ",
+"     .+++.      ",
+"    .+++++.     ",
+"   .+++.+++.    ",
+"  .+++. .+++.   ",
+"   .+.   .+.    ",
+"    .     .     ",
+"                ",
+"                ",
+"                "};
+
 /*!
   \class QtxResourceMgr::Resources
   \internal
 /*!
   \class QtxResourceMgr::Resources
   \internal
@@ -334,15 +357,22 @@ const QtxResourceMgr::Section QtxResourceMgr::Resources::section( const QString&
 QString QtxResourceMgr::Resources::fileName( const QString& sect, const QString& prefix, const QString& name ) const
 {
   QString path;
 QString QtxResourceMgr::Resources::fileName( const QString& sect, const QString& prefix, const QString& name ) const
 {
   QString path;
-  if ( hasValue( sect, prefix ) )
+  if ( !QFileInfo( name ).isRelative() )
   {
   {
-    path = value( sect, prefix, true );
-    if ( !path.isEmpty() )
+    path = name;
+  }
+  else
+  {
+    if ( hasValue( sect, prefix ) )
     {
     {
-      if ( QFileInfo( path ).isRelative() )
-        path = Qtx::addSlash( Qtx::dir( myFileName, true ) ) + path;
-
-      path = Qtx::addSlash( path ) + name;
+      path = value( sect, prefix, true );
+      if ( !path.isEmpty() )
+      {
+       if ( QFileInfo( path ).isRelative() )
+         path = Qtx::addSlash( Qtx::dir( myFileName, true ) ) + path;
+       
+       path = Qtx::addSlash( path ) + name;
+      }
     }
   }
   if( !path.isEmpty() )
     }
   }
   if( !path.isEmpty() )
@@ -1379,11 +1409,14 @@ QtxResourceMgr::WorkingMode QtxResourceMgr::workingMode() const
   Note, that setValue() method always put the value to the user settings file.
   
   \param mode new working mode
   Note, that setValue() method always put the value to the user settings file.
   
   \param mode new working mode
+  \return previous working mode
   \sa workingMode(), value(), hasValue(), hasSection(), setValue()
 */
   \sa workingMode(), value(), hasValue(), hasSection(), setValue()
 */
-void QtxResourceMgr::setWorkingMode( WorkingMode mode )
+QtxResourceMgr::WorkingMode QtxResourceMgr::setWorkingMode( WorkingMode mode )
 {
 {
+  WorkingMode m = myWorkingMode;
   myWorkingMode = mode;
   myWorkingMode = mode;
+  return m;
 }
 
 /*!
 }
 
 /*!
@@ -2289,7 +2322,11 @@ bool QtxResourceMgr::save()
   if ( myResources.isEmpty() || !myHasUserValues )
     return true;
 
   if ( myResources.isEmpty() || !myHasUserValues )
     return true;
 
-  return fmt->save( myResources[0] );
+  bool result = fmt->save( myResources[0] );
+
+  saved();
+
+  return result;
 }
 
 /*!
 }
 
 /*!
@@ -2512,10 +2549,11 @@ QString QtxResourceMgr::sectionsToken() const
 */
 QPixmap QtxResourceMgr::defaultPixmap() const
 {
 */
 QPixmap QtxResourceMgr::defaultPixmap() const
 {
-  QPixmap res;
-  if ( myDefaultPix && !myDefaultPix->isNull() )
-    res = *myDefaultPix;
-  return res;
+  static QPixmap* defpx = 0;
+  if ( !defpx ) 
+    defpx = new QPixmap( pixmap_not_found_xpm );
+
+  return myDefaultPix ? *myDefaultPix : *defpx;
 }
 
 /*!
 }
 
 /*!
@@ -2659,15 +2697,16 @@ void QtxResourceMgr::loadLanguage( const QString& pref, const QString& l )
   if ( pref.isEmpty() && lang != "en" ) {
     // load Qt resources
     QString qt_translations = QLibraryInfo::location( QLibraryInfo::TranslationsPath );
   if ( pref.isEmpty() && lang != "en" ) {
     // load Qt resources
     QString qt_translations = QLibraryInfo::location( QLibraryInfo::TranslationsPath );
-    QString qt_dir_trpath;
-    if ( ::getenv( "QTDIR" ) )
-      qt_dir_trpath = QString( ::getenv( "QTDIR" ) );
+    QString qt_dir_trpath = qgetenv( "QT_ROOT_DIR" );
+    if ( qt_dir_trpath.isEmpty() )
+      qt_dir_trpath = qgetenv( "QTDIR" );
     if ( !qt_dir_trpath.isEmpty() )
       qt_dir_trpath = QDir( qt_dir_trpath ).absoluteFilePath( "translations" );
 
     QTranslator* trans = new QtxTranslator( 0 );
     if ( !qt_dir_trpath.isEmpty() )
       qt_dir_trpath = QDir( qt_dir_trpath ).absoluteFilePath( "translations" );
 
     QTranslator* trans = new QtxTranslator( 0 );
-    if ( trans->load( QString("qt_%1").arg( lang ), qt_translations ) || trans->load( QString("qt_%1").arg( lang ), qt_dir_trpath ) )
-      QApplication::instance()->installTranslator( trans );
+    if ( trans->load( QString("qt_%1").arg( lang ), qt_translations ) || trans->load( QString("qt_%1").arg( lang ), qt_dir_trpath ) ) {
+      if ( QApplication::instance() ) QApplication::instance()->installTranslator( trans );
+    }
   }
 
   for ( QStringList::ConstIterator iter = prefixList.begin(); iter != prefixList.end(); ++iter )
   }
 
   for ( QStringList::ConstIterator iter = prefixList.begin(); iter != prefixList.end(); ++iter )
@@ -2713,7 +2752,7 @@ void QtxResourceMgr::loadTranslators( const QString& prefix, const QStringList&
       {
         if ( !myTranslator[prefix].contains( trans ) )
           myTranslator[prefix].append( trans );
       {
         if ( !myTranslator[prefix].contains( trans ) )
           myTranslator[prefix].append( trans );
-        QApplication::instance()->installTranslator( trans );
+        if ( QApplication::instance() ) QApplication::instance()->installTranslator( trans );
       }
     }
   }
       }
     }
   }
@@ -2745,7 +2784,7 @@ void QtxResourceMgr::loadTranslator( const QString& prefix, const QString& name
     {
       if ( !myTranslator[prefix].contains( trans ) )
         myTranslator[prefix].append( trans );
     {
       if ( !myTranslator[prefix].contains( trans ) )
         myTranslator[prefix].append( trans );
-      QApplication::instance()->installTranslator( trans );
+      if ( QApplication::instance() ) QApplication::instance()->installTranslator( trans );
     }
   }
 }
     }
   }
 }
@@ -2761,7 +2800,7 @@ void QtxResourceMgr::removeTranslators( const QString& prefix )
 
   for ( TransList::Iterator it = myTranslator[prefix].begin(); it != myTranslator[prefix].end(); ++it )
   {
 
   for ( TransList::Iterator it = myTranslator[prefix].begin(); it != myTranslator[prefix].end(); ++it )
   {
-    QApplication::instance()->removeTranslator( *it );
+    if ( QApplication::instance() ) QApplication::instance()->removeTranslator( *it );
     delete *it;
   }
 
     delete *it;
   }
 
@@ -2780,8 +2819,10 @@ void QtxResourceMgr::raiseTranslators( const QString& prefix )
 
   for ( TransList::Iterator it = myTranslator[prefix].begin(); it != myTranslator[prefix].end(); ++it )
   {
 
   for ( TransList::Iterator it = myTranslator[prefix].begin(); it != myTranslator[prefix].end(); ++it )
   {
-    QApplication::instance()->removeTranslator( *it );
-    QApplication::instance()->installTranslator( *it );
+    if ( QApplication::instance() ) {
+      QApplication::instance()->removeTranslator( *it );
+      QApplication::instance()->installTranslator( *it );
+    }
   }
 }
 
   }
 }
 
@@ -2852,7 +2893,6 @@ QString QtxResourceMgr::userFileName( const QString& appName, const bool /*for_l
 {
   QString fileName;
   QString pathName = QDir::homePath();
 {
   QString fileName;
   QString pathName = QDir::homePath();
-
   QString cfgAppName = QApplication::applicationName();
   if ( !cfgAppName.isEmpty() )
     pathName = Qtx::addSlash( Qtx::addSlash( pathName ) + QString( ".config" ) ) + cfgAppName;
   QString cfgAppName = QApplication::applicationName();
   if ( !cfgAppName.isEmpty() )
     pathName = Qtx::addSlash( Qtx::addSlash( pathName ) + QString( ".config" ) ) + cfgAppName;
@@ -2890,6 +2930,14 @@ QString QtxResourceMgr::globalFileName( const QString& appName ) const
   return QString( "%1.%2" ).arg( appName ).arg( currentFormat() );
 }
 
   return QString( "%1.%2" ).arg( appName ).arg( currentFormat() );
 }
 
+/*!
+  \brief This function is called after user configuration file is saved.
+  Can be redefined in the successor classes, default implementation does nothing.
+*/
+void QtxResourceMgr::saved()
+{
+}
+
 /*!
   \brief Perform substitution of the patterns like \%A, \%B, etc by values from the map.
 
 /*!
   \brief Perform substitution of the patterns like \%A, \%B, etc by values from the map.