casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
QtApp.h
Go to the documentation of this file.
1 //# QtApp.h: Management of the QApp object needed by any Qt application.
2 //# Copyright (C) 2005
3 //# Associated Universities, Inc. Washington DC, USA.
4 //#
5 //# This library is free software; you can redistribute it and/or modify it
6 //# under the terms of the GNU Library General Public License as published by
7 //# the Free Software Foundation; either version 2 of the License, or (at your
8 //# option) any later version.
9 //#
10 //# This library is distributed in the hope that it will be useful, but WITHOUT
11 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 //# License for more details.
14 //#
15 //# You should have received a copy of the GNU Library General Public License
16 //# along with this library; if not, write to the Free Software Foundation,
17 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 //#
19 //# Correspondence concerning AIPS++ should be addressed as follows:
20 //# Internet email: aips2-request@nrao.edu.
21 //# Postal address: AIPS++ Project Office
22 //# National Radio Astronomy Observatory
23 //# 520 Edgemont Road
24 //# Charlottesville, VA 22903-2475 USA
25 //#
26 //# $Id$
27 
28 #ifndef QTAPP_H
29 #define QTAPP_H
30 
31 #include <casa/aips.h>
32 
33 #include <graphics/X11/X_enter.h>
34 # include <QApplication>
35 # include <QThread>
36 #include <graphics/X11/X_exit.h>
37 
38 namespace casa { //# NAMESPACE CASA - BEGIN
39 
40 
41 // <summary>
42 // Management of the QApp object needed by any Qt application.
43 // </summary>
44 
45 // <synopsis>
46 // This adds just a little to QApplication's [static] services. (Actually,
47 // I wish all this _were_ there instead -- it could be...). Casa
48 // applications which use Qt can just call QtApp::app() to retrieve
49 // 'the' (unique) QApplication object for the program; that routine
50 // will create it if it doesn't yet exist. It is recommended that all
51 // access by casa to the QApplication object go through this routine, to
52 // assure only one is created.
53 //#
54 //# This class has utility for _any_ Qt casa app, not just qtviewer.
55 //# Ultimately, it probably belongs outside in a different directory.
56 // </synopsis>
57  class QtApp {
58 
59  public:
60 
61  QtApp() {
62  init(); //# (You may not need to create one, though:
63  }
64  ~QtApp() { } //# everything's static).
65 
66 
67  // Return the program's [unique] QApplication object, creating it
68  // if it doesn't yet exist.
69  // Note: use QtApp::destroy() to delete the QApplication.
70  static QApplication* app( ) {
71 
72  static casacore::Int argc = 1;
73  static casacore::Char *argv[1];
74  static casacore::Char name[] = "casa.qtapp";
75  argv[0] = name;
76 
77  QCoreApplication* qcapp = QApplication::instance();
78  if(QApplication::startingUp() || qcapp==0) {
79  qcapp = new QApplication(argc, argv);
80  }
81 
82  QApplication* qapp = dynamic_cast<QApplication*>(qcapp);
83 
84  if(qapp==0) { //# This probably should not happen....
85  //# Someone created a QCoreApplication which was not a full-fledged
86  //# (i.e. gui-capable) QApplication, before calling this. We want a
87  //# full-monty QApplication. The following may be marginally better
88  //# than throwing/crashing if the caller is truly done with the old
89  //# QCoreApp (s/he shouldn't have called this routine otherwise).
90  delete qcapp;
91  qapp = new QApplication(argc, argv);
92  }
93 
94  return qapp;
95  }
96 
97 
98  // Another name for app() that may be clearer during initialization....
99  static QApplication* init( ) {
100  return app( );
101  }
102 
103 
104  // Enter the QApp's event loop.
105  static casacore::Int exec() {
106  return app()->exec();
107  }
108 
109 
110  // Exit the QApp's event loop.
111  static void exit(casacore::Int returnCode=0) {
112  app()->exit(returnCode);
113  }
114 
115 
116  // Call when completely finished with Qt, if you're a stickler for cleanup.
117  static void destroy() {
118  if(!QApplication::startingUp()) delete QApplication::instance();
119  }
120 
121 
122  // If true, a full-fledged QApplication has been created (though it
123  // may not necessarily be executing its event loop).
125  return !QApplication::startingUp() &&
126  dynamic_cast<QApplication*>(QApplication::instance())!=0;
127  }
128 
129 
130  // Is the QApp executing its event loop?
131  // (In many cases, caller probably ought to know this already...).
133  return exists() && app()->thread()->isRunning();
134  }
135  //# (Gleaned from QCoreApplication::exec() in qtapplication.cpp)
136 
137 
138 
139  };
140 
141 
142 
143 } //# NAMESPACE CASA - END
144 
145 #endif
static QApplication * init()
Another name for app() that may be clearer during initialization....
Definition: QtApp.h:99
int Int
Definition: aipstype.h:50
static QApplication * app()
Return the program&#39;s [unique] QApplication object, creating it if it doesn&#39;t yet exist.
Definition: QtApp.h:70
QtApp()
Definition: QtApp.h:61
static casacore::Int exec()
Enter the QApp&#39;s event loop.
Definition: QtApp.h:105
static void exit(casacore::Int returnCode=0)
Exit the QApp&#39;s event loop.
Definition: QtApp.h:111
char Char
Definition: aipstype.h:46
ABSTRACT CLASSES Abstract class for colors Any implementation of color should be able to provide a hexadecimal form of the if a human readable name(i.e."black").In many places throughout the plotter
static casacore::Bool isInLoop()
Is the QApp executing its event loop? (In many cases, caller probably ought to know this already...
Definition: QtApp.h:132
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
~QtApp()
Definition: QtApp.h:64
static casacore::Bool exists()
If true, a full-fledged QApplication has been created (though it may not necessarily be executing its...
Definition: QtApp.h:124
Management of the QApp object needed by any Qt application.
Definition: QtApp.h:57
static void destroy()
Call when completely finished with Qt, if you&#39;re a stickler for cleanup.
Definition: QtApp.h:117