casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
QtApp.h
Go to the documentation of this file.
00001 //# QtApp.h: Management of the QApp object needed by any Qt application.
00002 //# Copyright (C) 2005
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id$
00027 
00028 #ifndef QTAPP_H
00029 #define QTAPP_H
00030 
00031 #include <casa/aips.h>
00032 
00033 #include <graphics/X11/X_enter.h>
00034 #  include <QApplication>
00035 #  include <QThread>
00036 #include <graphics/X11/X_exit.h>
00037 
00038 namespace casa { //# NAMESPACE CASA - BEGIN
00039 
00040 
00041 // <summary>
00042 // Management of the QApp object needed by any Qt application.
00043 // </summary>
00044 
00045 // <synopsis>
00046 // This adds just a little to QApplication's [static] services.  (Actually,
00047 // I wish all this _were_ there instead -- it could be...).  Casa
00048 // applications which use Qt can just call QtApp::app() to retrieve
00049 // 'the' (unique) QApplication object for the program; that routine
00050 // will create it if it doesn't yet exist.  It is recommended that all
00051 // access by casa to the QApplication object go through this routine, to
00052 // assure only one is created.
00053 //#
00054 //# This class has utility for _any_ Qt casa app, not just qtviewer.
00055 //# Ultimately, it probably belongs outside in a different directory.
00056 // </synopsis>
00057 class QtApp {
00058 
00059  public:
00060   
00061   QtApp() { init();  }   //# (You may not need to create one, though:
00062   ~QtApp() {  }          //#  everything's static).
00063   
00064   
00065   // Return the program's [unique] QApplication object, creating it
00066   // if it doesn't yet exist.
00067   // Note: use QtApp::destroy() to delete the QApplication.
00068   static QApplication* app( ) {
00069 
00070     static Int argc = 1;
00071     static Char *argv[1];
00072     static Char name[] = "casa.qtapp";
00073     argv[0] = name;
00074 
00075     QCoreApplication* qcapp = QApplication::instance();
00076     if(QApplication::startingUp() || qcapp==0) {
00077       qcapp = new QApplication(argc, argv);  }
00078     
00079     QApplication* qapp = dynamic_cast<QApplication*>(qcapp);
00080     
00081     if(qapp==0) {      //# This probably should not happen....
00082       //# Someone created a QCoreApplication which was not a full-fledged
00083       //# (i.e. gui-capable) QApplication, before calling this.  We want a
00084       //# full-monty QApplication.  The following may be marginally better
00085       //# than throwing/crashing if the caller is truly done with the old
00086       //# QCoreApp (s/he shouldn't have called this routine otherwise).
00087       delete qcapp; 
00088       qapp = new QApplication(argc, argv);  }
00089       
00090     return qapp;  }
00091   
00092   
00093   // Another name for app() that may be clearer during initialization....
00094   static QApplication* init( ) {
00095     return app( );  }
00096 
00097   
00098   // Enter the QApp's event loop.
00099   static Int exec() { return app()->exec();  }
00100 
00101   
00102   // Exit the QApp's event loop.
00103   static void exit(Int returnCode=0) { app()->exit(returnCode);  }
00104 
00105   
00106   // Call when completely finished with Qt, if you're a stickler for cleanup.
00107   static void destroy() { 
00108     if(!QApplication::startingUp()) delete QApplication::instance();  }
00109     
00110   
00111   // If True, a full-fledged QApplication has been created (though it
00112   // may not necessarily be executing its event loop).
00113   static Bool exists() { 
00114     return !QApplication::startingUp() && 
00115             dynamic_cast<QApplication*>(QApplication::instance())!=0;  }
00116 
00117   
00118   // Is the QApp executing its event loop? 
00119   // (In many cases, caller probably ought to know this already...).
00120   static Bool isInLoop() {
00121     return exists() && app()->thread()->isRunning();  }
00122         //# (Gleaned from QCoreApplication::exec() in qtapplication.cpp)
00123 
00124   
00125     
00126 };
00127 
00128 
00129 
00130 } //# NAMESPACE CASA - END
00131 
00132 #endif