Salome HOME
Merge branch 'V9_2_BR'
[modules/gui.git] / src / Session / SALOME_Session_Server.cxx
index 0b10352e26c4082e90241f15c7a93b86dc1e78d9..7e1c32f05a40fbab8db4e6a250019330fb5ff553 100755 (executable)
 //  Author : Paul RASCLE, EDF
 //  Module : SALOME
 
+#include <Container_init_python.hxx>
 #include <SALOME_NamingService.hxx>
 #include <SALOME_ModuleCatalog_impl.hxx>
 #include <SALOME_LifeCycleCORBA.hxx>
 #include <SALOME_Event.h>
 
-#include <Container_init_python.hxx>
 #include <ConnectionManager_i.hxx>
 #include <RegistryService.hxx>
 
-#ifdef ENABLE_TESTRECORDER
-  #include <TestApplication.h>
-#endif
-
 #include <OpUtil.hxx>
 #include <Utils_ORB_INIT.hxx>
 #include <Utils_SINGLETON.hxx>
@@ -50,6 +46,7 @@
 #include "Session_Session_i.hxx"
 
 #include <Qtx.h>
+#include <QtxMsgHandler.h>
 #include <QtxSplash.h>
 
 #ifdef USE_SALOME_STYLE
  * - stop Session ( must be idle )
  * - get session state
  */
-#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
-void MessageOutput( QtMsgType type, const char* msg )
-#else
-void MessageOutput( QtMsgType type, const QMessageLogContext &context, const QString &msg )
-#endif
+
+class SessionMsgHandler: public QtxMsgHandlerCallback
 {
-  switch ( type )
+public:
+  SessionMsgHandler() {}
+  void qtMessage(QtMsgType type, const QMessageLogContext& context, const QString& message)
   {
-  case QtDebugMsg:
+    switch ( type )
+    {
+    case QtDebugMsg:
 #ifdef QT_DEBUG_MESSAGE
-    MESSAGE( "Debug: " << qPrintable( QString(msg) ) );
+      MESSAGE( "Debug: " << qPrintable( message ) );
 #endif
-    break;
-  case QtWarningMsg:
-    MESSAGE( "Warning: " << qPrintable( QString(msg) ) );
-    break;
-  case QtFatalMsg:
-    MESSAGE( "Fatal: " << qPrintable( QString(msg) ) );
-    break;
+      break;
+    case QtWarningMsg:
+      MESSAGE( "Warning: " << qPrintable( message ) );
+      break;
+    case QtCriticalMsg:
+      MESSAGE( "Critical: " << qPrintable( message ) );
+      break;
+    case QtFatalMsg:
+      MESSAGE( "Fatal: " << qPrintable( message ) );
+      break;
+    case QtInfoMsg:
+    default:
+      MESSAGE( "Information: " << qPrintable( message ) );
+      break;
+    }
   }
-}
+};
 
 QString salomeVersion()
 {
@@ -217,8 +223,8 @@ protected:
 #endif
       QString fname = QFileInfo( _fname ).fileName();
       if ( exp.exactMatch( fname ) ) {
-       long fid = Qtx::versionToId( exp.cap( 1 ) );
-       if ( fid > 0 ) id = fid;
+        long fid = Qtx::versionToId( exp.cap( 1 ) );
+        if ( fid > 0 ) id = fid;
       }
     }
     return id;
@@ -247,19 +253,12 @@ public:
   }
 };
 
-#ifdef ENABLE_TESTRECORDER
-  class SALOME_QApplication : public TestApplication
-#else
-  class SALOME_QApplication : public QApplication
-#endif
+class SALOME_QApplication : public QApplication
 {
 public:
-#ifdef ENABLE_TESTRECORDER
-  SALOME_QApplication( int& argc, char** argv ) : TestApplication( argc, argv ), myHandler ( 0 ) {}
-#else
   SALOME_QApplication( int& argc, char** argv )
 // TODO (QT5 PORTING) Below is a temporary solution, to allow compiling with Qt 5
-#if !defined WIN32 && QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
+#if !defined(WIN32) && !defined(__APPLE__) && (QT_VERSION < QT_VERSION_CHECK(5, 0, 0))
   // san: Opening an X display and choosing a visual most suitable for 3D visualization
   // in order to make SALOME viewers work with non-native X servers
   : QApplication( (Display*)Qtx::getDisplay(), argc, argv, Qtx::getVisual() ),
@@ -267,34 +266,33 @@ public:
   : QApplication( argc, argv ), 
 #endif
     myHandler ( 0 ) {}
-#endif
 
   virtual bool notify( QObject* receiver, QEvent* e )
   {
-
-#ifdef ENABLE_TESTRECORDER
-    return myHandler ? myHandler->handle( receiver, e ) :
-      TestApplication::notify( receiver, e );
-#else
-    try {
-      return myHandler ? myHandler->handle( receiver, e ) : QApplication::notify( receiver, e );
-    }
-    catch (std::exception& e) {
-      std::cerr << e.what()  << std::endl;
-    }
-    catch (CORBA::Exception& e) {
-      std::cerr << "Caught CORBA::Exception"  << std::endl;
-      CORBA::Any tmp;
-      tmp<<= e;
-      CORBA::TypeCode_var tc = tmp.type();
-      const char *p = tc->name();
-      std::cerr << "notify(): CORBA exception of the kind : " << p << " is caught" << std::endl;
+    QString debug_exceptions = ::getenv("SALOME_DEBUG_EXCEPTIONS");
+    if ( debug_exceptions.length() > 0 ) {
+      return QApplication::notify( receiver, e );
     }
-    catch (...) {
-      std::cerr << "Unknown exception caught in Qt handler: it's probably a bug in SALOME platform" << std::endl;
+    else {
+      try {
+        return myHandler ? myHandler->handle( receiver, e ) : QApplication::notify( receiver, e );
+      }
+      catch (std::exception& e) {
+        std::cerr << e.what()  << std::endl;
+      }
+      catch (CORBA::Exception& e) {
+        std::cerr << "Caught CORBA::Exception"  << std::endl;
+        CORBA::Any tmp;
+        tmp<<= e;
+        CORBA::TypeCode_var tc = tmp.type();
+        const char *p = tc->name();
+        std::cerr << "notify(): CORBA exception of the kind : " << p << " is caught" << std::endl;
+      }
+      catch (...) {
+        std::cerr << "Unknown exception caught in Qt handler: it's probably a bug in SALOME platform" << std::endl;
+      }
+      return false;  // return false when exception is caught
     }
-    return false;  // return false when exception is caught
-#endif
   }
   SUIT_ExceptionHandler* handler() const { return myHandler; }
   void setHandler( SUIT_ExceptionHandler* h ) { myHandler = h; }
@@ -349,11 +347,8 @@ void shutdownServers( SALOME_NamingService* theNS )
 int main( int argc, char **argv )
 {
   // Install Qt debug messages handler
-#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
-  qInstallMsgHandler( MessageOutput );
-#else
-  qInstallMessageHandler( MessageOutput );
-#endif
+  SessionMsgHandler msgHandler;
+  qInstallMessageHandler(QtxMsgHandler);
 
 // TODO (QT5 PORTING) Below is a temporary solution, to allow compiling with Qt 5
 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
@@ -383,6 +378,16 @@ int main( int argc, char **argv )
       QLocale::setDefault( QLocale::system() );
     }
   }
+  
+#if QT_VERSION > QT_VERSION_CHECK(5, 0, 0)
+
+  // RNV: setup the default format:
+  // QSurfaceFormat should be set before creation of QApplication,  
+  // so to avoid conflicts beetween SALOME and ParaView QSurfaceFormats we should merge theirs formats
+  // (see void Qtx::initDefaultSurfaceFormat()) and set the resultant format here.
+  Qtx::initDefaultSurfaceFormat(); 
+
+#endif
 
   // Create Qt application instance;
   // this should be done the very first!
@@ -567,8 +572,8 @@ int main( int argc, char **argv )
       SALOME::StatSession stat = session->GetStatSession();
       shutdownSession = stat.state == SALOME::shutdown;
       if ( shutdownSession ) {
-       _SessionMutex.lock(); // lock mutex before leaving loop - it will be unlocked later
-       break;
+        _SessionMutex.lock(); // lock mutex before leaving loop - it will be unlocked later
+        break;
       }
 
       // SUIT_Session creation
@@ -606,10 +611,15 @@ int main( int argc, char **argv )
         }
         else {
           // desktop might be closed from:
-          // - StopSesion() /temporarily/ or
-          // - Shutdown() /permanently/
+          // - StopSesion() (temporarily) or
+          // - Shutdown() (permanently)
           stat = session->GetStatSession();
           shutdownSession = stat.state == SALOME::shutdown;
+          // normally "shutdown standalone servers" flag should be false here, if we come from
+          // StopSesion() or from Shutdown();
+          // but we also have to check if somebody explicitly programmatically closed session,
+          // asking to kill servers also
+          shutdownAll = aGUISession->exitFlags();
         }
         if ( shutdownAll || shutdownSession ) {
           _SessionMutex.lock(); // lock mutex before leaving loop - it will be unlocked later