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() {
00062                         init();     //# (You may not need to create one, though:
00063                 }
00064                 ~QtApp() {  }            //#  everything's static).
00065 
00066 
00067                 // Return the program's [unique] QApplication object, creating it
00068                 // if it doesn't yet exist.
00069                 // Note: use QtApp::destroy() to delete the QApplication.
00070                 static QApplication* app( ) {
00071 
00072                         static Int argc = 1;
00073                         static Char *argv[1];
00074                         static Char name[] = "casa.qtapp";
00075                         argv[0] = name;
00076 
00077                         QCoreApplication* qcapp = QApplication::instance();
00078                         if(QApplication::startingUp() || qcapp==0) {
00079                                 qcapp = new QApplication(argc, argv);
00080                         }
00081 
00082                         QApplication* qapp = dynamic_cast<QApplication*>(qcapp);
00083 
00084                         if(qapp==0) {      //# This probably should not happen....
00085                                 //# Someone created a QCoreApplication which was not a full-fledged
00086                                 //# (i.e. gui-capable) QApplication, before calling this.  We want a
00087                                 //# full-monty QApplication.  The following may be marginally better
00088                                 //# than throwing/crashing if the caller is truly done with the old
00089                                 //# QCoreApp (s/he shouldn't have called this routine otherwise).
00090                                 delete qcapp;
00091                                 qapp = new QApplication(argc, argv);
00092                         }
00093 
00094                         return qapp;
00095                 }
00096 
00097 
00098                 // Another name for app() that may be clearer during initialization....
00099                 static QApplication* init( ) {
00100                         return app( );
00101                 }
00102 
00103 
00104                 // Enter the QApp's event loop.
00105                 static Int exec() {
00106                         return app()->exec();
00107                 }
00108 
00109 
00110                 // Exit the QApp's event loop.
00111                 static void exit(Int returnCode=0) {
00112                         app()->exit(returnCode);
00113                 }
00114 
00115 
00116                 // Call when completely finished with Qt, if you're a stickler for cleanup.
00117                 static void destroy() {
00118                         if(!QApplication::startingUp()) delete QApplication::instance();
00119                 }
00120 
00121 
00122                 // If True, a full-fledged QApplication has been created (though it
00123                 // may not necessarily be executing its event loop).
00124                 static Bool exists() {
00125                         return !QApplication::startingUp() &&
00126                                dynamic_cast<QApplication*>(QApplication::instance())!=0;
00127                 }
00128 
00129 
00130                 // Is the QApp executing its event loop?
00131                 // (In many cases, caller probably ought to know this already...).
00132                 static Bool isInLoop() {
00133                         return exists() && app()->thread()->isRunning();
00134                 }
00135                 //# (Gleaned from QCoreApplication::exec() in qtapplication.cpp)
00136 
00137 
00138 
00139         };
00140 
00141 
00142 
00143 } //# NAMESPACE CASA - END
00144 
00145 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 31 Aug 2016 for casa by  doxygen 1.6.1