ROOT logo
// @(#)root/qt:$Id: TQtApplication.cxx 32595 2010-03-12 16:54:29Z pcanal $
// Author: Valeri Fine   21/01/2002

/*************************************************************************
 * Copyright (C) 1995-2004, Rene Brun and Fons Rademakers.               *
 * Copyright (C) 2002 by Valeri Fine.                                    *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// TQtApplication -  Instantiate the Qt system within ROOT environment  //
//                                                                      //
// Instantiate the Qt package by createing Qapplication object if any   //
//                                                                      //
//////////////////////////////////////////////////////////////////////////


#include <assert.h>
#include "TQtApplication.h"

#include <QApplication>
#include <QCoreApplication>
#include <QDebug>

#include "TSystem.h"

#include "TROOT.h"
#include "TEnv.h"

#include "qmessagebox.h"

//________________________________________________________________
static int QVersion(const char *ver) {
   // convert the Qversion string into the interger
    QString version = QString::fromLatin1(ver);
    return   (version.section('.',0,0).toInt()<<16)
          +  (version.section('.',1,1).toInt()<<8 )
          +  (version.section('.',2,2).toInt()    );
}

TQtApplication *TQtApplication::fgQtApplication = 0;

ClassImp(TQtApplication)
//
//______________________________________________________________________________
TQtApplication::TQtApplication(const char * /*appClassName*/, int &argc,char **argv)
                : fGUIThread(0)
{
   assert(!fgQtApplication);
   fgQtApplication  = this;
   CreateGUIThread(argc,argv);
}
//______________________________________________________________________________
TQtApplication::~TQtApplication()
{ }
//______________________________________________________________________________
void TQtApplication::CreateQApplication(int &argc, char ** argv, bool GUIenabled)
{
  //  Initialize the Qt package
  //  Check the QT_BATCH environment variable to disactivate Qt GUI mode
   
  // QApplication must be created in the proper "GUI" thread
  // It may be called from TQtApplicationThread::Run
   if (!qApp) {
      // QApplication::setColorSpec( QApplication::NormalColor );
       QApplication::setColorSpec( QApplication::ManyColor );
       QString display = gSystem->Getenv("DISPLAY");
       // check the QT_BATCH option
       if (display.contains("QT_BATCH")) GUIenabled = false;
#ifndef R__WIN32       
       QCoreApplication::setAttribute(Qt::AA_ImmediateWidgetCreation);
#endif       
       // Check whether we want to debug the Qt X11
       QString fatalWarnings = gSystem->Getenv("QT_FATAL_WARNINGS");
       if (fatalWarnings.contains("1")) {
          int argC = 2;
          static const char *argV[] = {"root.exe", "-sync" };
          qDebug() << "TQtApplication::CreateQApplication: "
                   << "ATTENTION !!! "
                   << "The env variable \"QT_FATAL_WARNIGNS\" was defined. The special debug option has  been turned on." 
                   << " argc = " << argc << " argv = " << argv[0] << argv[1];
          qDebug() << " You may want to restart ROOT with " << argC << " paramaters :"
                   << " like this: \"" << argV[0] << " " << argV[1];
//          new QApplication(argC,argV,GUIenabled);
          new QApplication(argc,argv,GUIenabled);
       } else {       
          new QApplication(argc,argv,GUIenabled);
       }
       // The string must be one of the QStyleFactory::keys(),
       // typically one of
       //      "windows", "motif",     "cde",    "motifplus", "platinum", "sgi"
       //  and "compact", "windowsxp", "aqua" or "macintosh"
      QString fromConfig = "native";
      if (gEnv)
         fromConfig = gEnv->GetValue("Gui.Style","native");
      if (fromConfig != "native" ) QApplication::setStyle(fromConfig);
#ifdef Q_WS_MACX
      // create a timer to force the event loop with no X-server
      TTimer *idle = new TTimer(240); idle->TurnOn();
#endif
   }
   // Check the compatibility
   Int_t validQtVersion = QVersion(ROOT_VALID_QT_VERSION);
   Int_t thisQtVersion  = QVersion(qVersion());
   if (thisQtVersion < validQtVersion) {
       QString s = QApplication::tr("Executable '%1' was compiled with Qt %2 and requires Qt %3 at least, found Qt %4.")
            .arg(qAppName())
            .arg(QT_VERSION_STR)
            .arg(QString::fromLatin1(ROOT_VALID_QT_VERSION))
            .arg(QString::fromLatin1(qVersion()) ); 
      QMessageBox::critical( 0, QApplication::tr("Incompatible Qt Library Error" ), s, QMessageBox::Abort,0 );
      qFatal("%s",s.toAscii().data());
   } else if (thisQtVersion < QtVersion()) {
       QString s = QApplication::tr("Executable '%1' was compiled with Qt %2, found Qt %3.")
            .arg(qAppName())
            .arg(QT_VERSION_STR)
            .arg(QString::fromLatin1(qVersion()) ); 
      QMessageBox::warning( 0, QApplication::tr("Upgrade Qt Library Warning" ), s, QMessageBox::Abort,0 );
      qWarning("%s",s.toAscii().data());
   }
  
   // Add Qt plugin path if  present (it is the case for Windows binary ROOT distribution)
   char *qtPluginPath = gSystem->ConcatFileName(gSystem->Getenv("ROOTSYS"),"/Qt/plugins");
   if (!gSystem->AccessPathName(qtPluginPath))
       qApp->addLibraryPath(qtPluginPath);
   delete [] qtPluginPath;
}
//______________________________________________________________________________
void TQtApplication::CreateGUIThread(int &argc, char **argv)
{
  // Create GUI thread to Qt event loop
   if (gROOT->IsBatch()) {
     CreateQApplication(argc,argv,kFALSE);
   } else {
     CreateQApplication(argc,argv, TRUE);
   }
}
//______________________________________________________________________________
TQtApplication *TQtApplication::GetQtApplication(){return fgQtApplication;}
//______________________________________________________________________________
bool TQtApplication::Terminate()
{
  // Terminate GUI thread
  if (fgQtApplication) {
    TQtApplication *app = fgQtApplication;
    fgQtApplication = 0;
    delete  app;
  }
  return TRUE;
}
//______________________________________________________________________________
Int_t TQtApplication::QtVersion(){
     // The Qt version the package was compiled with
   return  QVersion(QT_VERSION_STR);
}
//______________________________________________________________________________
bool TQtApplication::IsThisGuiThread()
{
   // Check whether the current thread belongs the GUI
  return true;
}
 TQtApplication.cxx:1
 TQtApplication.cxx:2
 TQtApplication.cxx:3
 TQtApplication.cxx:4
 TQtApplication.cxx:5
 TQtApplication.cxx:6
 TQtApplication.cxx:7
 TQtApplication.cxx:8
 TQtApplication.cxx:9
 TQtApplication.cxx:10
 TQtApplication.cxx:11
 TQtApplication.cxx:12
 TQtApplication.cxx:13
 TQtApplication.cxx:14
 TQtApplication.cxx:15
 TQtApplication.cxx:16
 TQtApplication.cxx:17
 TQtApplication.cxx:18
 TQtApplication.cxx:19
 TQtApplication.cxx:20
 TQtApplication.cxx:21
 TQtApplication.cxx:22
 TQtApplication.cxx:23
 TQtApplication.cxx:24
 TQtApplication.cxx:25
 TQtApplication.cxx:26
 TQtApplication.cxx:27
 TQtApplication.cxx:28
 TQtApplication.cxx:29
 TQtApplication.cxx:30
 TQtApplication.cxx:31
 TQtApplication.cxx:32
 TQtApplication.cxx:33
 TQtApplication.cxx:34
 TQtApplication.cxx:35
 TQtApplication.cxx:36
 TQtApplication.cxx:37
 TQtApplication.cxx:38
 TQtApplication.cxx:39
 TQtApplication.cxx:40
 TQtApplication.cxx:41
 TQtApplication.cxx:42
 TQtApplication.cxx:43
 TQtApplication.cxx:44
 TQtApplication.cxx:45
 TQtApplication.cxx:46
 TQtApplication.cxx:47
 TQtApplication.cxx:48
 TQtApplication.cxx:49
 TQtApplication.cxx:50
 TQtApplication.cxx:51
 TQtApplication.cxx:52
 TQtApplication.cxx:53
 TQtApplication.cxx:54
 TQtApplication.cxx:55
 TQtApplication.cxx:56
 TQtApplication.cxx:57
 TQtApplication.cxx:58
 TQtApplication.cxx:59
 TQtApplication.cxx:60
 TQtApplication.cxx:61
 TQtApplication.cxx:62
 TQtApplication.cxx:63
 TQtApplication.cxx:64
 TQtApplication.cxx:65
 TQtApplication.cxx:66
 TQtApplication.cxx:67
 TQtApplication.cxx:68
 TQtApplication.cxx:69
 TQtApplication.cxx:70
 TQtApplication.cxx:71
 TQtApplication.cxx:72
 TQtApplication.cxx:73
 TQtApplication.cxx:74
 TQtApplication.cxx:75
 TQtApplication.cxx:76
 TQtApplication.cxx:77
 TQtApplication.cxx:78
 TQtApplication.cxx:79
 TQtApplication.cxx:80
 TQtApplication.cxx:81
 TQtApplication.cxx:82
 TQtApplication.cxx:83
 TQtApplication.cxx:84
 TQtApplication.cxx:85
 TQtApplication.cxx:86
 TQtApplication.cxx:87
 TQtApplication.cxx:88
 TQtApplication.cxx:89
 TQtApplication.cxx:90
 TQtApplication.cxx:91
 TQtApplication.cxx:92
 TQtApplication.cxx:93
 TQtApplication.cxx:94
 TQtApplication.cxx:95
 TQtApplication.cxx:96
 TQtApplication.cxx:97
 TQtApplication.cxx:98
 TQtApplication.cxx:99
 TQtApplication.cxx:100
 TQtApplication.cxx:101
 TQtApplication.cxx:102
 TQtApplication.cxx:103
 TQtApplication.cxx:104
 TQtApplication.cxx:105
 TQtApplication.cxx:106
 TQtApplication.cxx:107
 TQtApplication.cxx:108
 TQtApplication.cxx:109
 TQtApplication.cxx:110
 TQtApplication.cxx:111
 TQtApplication.cxx:112
 TQtApplication.cxx:113
 TQtApplication.cxx:114
 TQtApplication.cxx:115
 TQtApplication.cxx:116
 TQtApplication.cxx:117
 TQtApplication.cxx:118
 TQtApplication.cxx:119
 TQtApplication.cxx:120
 TQtApplication.cxx:121
 TQtApplication.cxx:122
 TQtApplication.cxx:123
 TQtApplication.cxx:124
 TQtApplication.cxx:125
 TQtApplication.cxx:126
 TQtApplication.cxx:127
 TQtApplication.cxx:128
 TQtApplication.cxx:129
 TQtApplication.cxx:130
 TQtApplication.cxx:131
 TQtApplication.cxx:132
 TQtApplication.cxx:133
 TQtApplication.cxx:134
 TQtApplication.cxx:135
 TQtApplication.cxx:136
 TQtApplication.cxx:137
 TQtApplication.cxx:138
 TQtApplication.cxx:139
 TQtApplication.cxx:140
 TQtApplication.cxx:141
 TQtApplication.cxx:142
 TQtApplication.cxx:143
 TQtApplication.cxx:144
 TQtApplication.cxx:145
 TQtApplication.cxx:146
 TQtApplication.cxx:147
 TQtApplication.cxx:148
 TQtApplication.cxx:149
 TQtApplication.cxx:150
 TQtApplication.cxx:151
 TQtApplication.cxx:152
 TQtApplication.cxx:153
 TQtApplication.cxx:154
 TQtApplication.cxx:155
 TQtApplication.cxx:156
 TQtApplication.cxx:157
 TQtApplication.cxx:158
 TQtApplication.cxx:159
 TQtApplication.cxx:160
 TQtApplication.cxx:161
 TQtApplication.cxx:162
 TQtApplication.cxx:163
 TQtApplication.cxx:164
 TQtApplication.cxx:165