]> Creatis software - bbtk.git/blob - kernel/src/bbtkQt.cxx
*** empty log message ***
[bbtk.git] / kernel / src / bbtkQt.cxx
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbtkQt.cxx,v $
4   Language:  C++
5   Date:      $Date: 2009/04/08 07:56:11 $
6   Version:   $Revision: 1.1 $
7 =========================================================================*/
8
9 /* ---------------------------------------------------------------------
10
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
13 *
14 *  This software is governed by the CeCILL-B license under French law and 
15 *  abiding by the rules of distribution of free software. You can  use, 
16 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
17 *  license as circulated by CEA, CNRS and INRIA at the following URL 
18 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
19 *  or in the file LICENSE.txt.
20 *
21 *  As a counterpart to the access to the source code and  rights to copy,
22 *  modify and redistribute granted by the license, users are provided only
23 *  with a limited warranty  and the software's author,  the holder of the
24 *  economic rights,  and the successive licensors  have only  limited
25 *  liability. 
26 *
27 *  The fact that you are presently reading this means that you have had
28 *  knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */                                                                         
30 #include "bbtkQt.h"
31 #include "bbtkMessageManager.h"
32
33 #ifdef USE_QT
34
35 #include <QApplication>
36
37
38 namespace bbtk
39 {
40   //=========================================================================
41   //  class QtApp;
42   //=========================================================================
43
44   //=========================================================================
45   static QApplication* mgQtApp = 0;
46   //  static wxWindow* mgTopWindow = 0;
47   //  static wxWindow* mgTopWindowParent = 0;
48   //  static bool mgAutoDestroyTopWindow = true;
49   static int mgNbWindowsAlive  = 0;
50   static int mgNbWindowsShown  = 0;
51   //=========================================================================
52
53   //=========================================================================
54   // BBTKWXSIG
55   static Qt::Signal_type mgQtSignal;
56   // \BBTKWXSIG
57   //=========================================================================
58
59  
60   //=========================================================================
61   // The wxApp class which is used when no user wxApp was created
62   /*
63   class QtApp : public wxApp
64   {
65   public:
66     bool OnInit( );
67     int  OnExit() { return true; }
68   };
69   //=========================================================================
70   IMPLEMENT_APP_NO_MAIN(QtApp);
71   //=========================================================================
72   bool QtApp::OnInit( )
73   {      
74     wxApp::OnInit();
75 #ifdef __WXGTK__
76     //See http://www.wxwindows.org/faqgtk.htm#locale
77     setlocale(LC_NUMERIC, "C");
78 #endif
79     return true;
80   }
81   */
82   //=========================================================================
83
84   int argc = 1;
85   char* argv = "QApp";
86
87   //=========================================================================
88   void Qt::CreateQtAppIfNeeded()
89   {
90     
91     if (QApplication::instance()==0)
92       {
93         if (mgQtApp != 0) 
94           {
95             bbtkGlobalError("Qt::CreateQtAppIfNeeded() : INTERNAL ERROR ! (QCoreApplication::instance()==0) && (mgQtApp != 0)");
96           }
97         bbtkDebugMessage("qt",1,"  --> Creating bbtk QApplication"<<std::endl);
98         mgQtApp = new QApplication(argc,&argv);
99
100         //      std::cout << ">>>> mgQtApp.exec()"<<std::endl;
101         
102         //      mgQtApp->exec();
103
104         //      std::cout << "<<<< mgQtApp.exec()"<<std::endl;
105         //      wxApp::SetInstance(mgQtApp);
106
107         //int argc = 0;
108         //wxEntry(argc,0);
109
110         //      wxInitialize();
111       }
112     
113   }
114   //=========================================================================
115
116   //=========================================================================
117   void Qt::DestroyQtAppIfNeeded()
118   {
119     /*
120     if (mgQtApp!= 0) 
121       {
122         bbtkDebugMessage("wx",1,"  --> Destructing bbtk QApplication"<<std::endl);
123         //delete mgQtApp;
124         //      mgQtApp = 0;
125         // Uninit wx
126         //      wxUninitialize();
127       }
128     */
129   }
130   //=========================================================================
131
132   //=========================================================================  
133   void Qt::SetTopWindowParent(QWidget* w)
134   {
135     /*
136     if (mgTopWindowParent != 0)
137       {
138         bbtkGlobalError("Qt::SetTopWindowParent : top window parent != 0");
139       }
140     mgTopWindowParent = w;
141     */
142   }
143   //=========================================================================
144
145   //=========================================================================
146   void Qt::CreateTopWindowIfNeeded()
147   {
148     /*
149     if (mgTopWindow!=0) return;
150     bbtkDebugMessage("wx",1,"  --> Creating bbtk top window"<<std::endl);
151
152     CreateQtAppIfNeeded();
153
154     wxWindow* top = 
155       new wxFrame(mgTopWindowParent,
156                   -1,
157                   _T("TOP BBTK FRAME (YOU SHOULD NOT SEE ME !!)"));
158     top->Hide();
159
160     Qt::SetTopWindow(top);
161     */
162   }
163   //=========================================================================
164
165   //=========================================================================
166   void Qt::DestroyTopWindowIfNeeded()
167   {
168     /*
169     if ( (mgNbWindowsAlive==0) && 
170          (mgAutoDestroyTopWindow) )
171       {
172         bbtkDebugMessage("wx",1,"  --> Destructing bbtk top window"<<std::endl);
173         mgTopWindow->Close();
174         mgTopWindow = 0;
175         
176         DestroyQtAppIfNeeded();
177       }
178     */
179   } 
180   //=========================================================================
181
182   //=========================================================================
183   void Qt::LoopUntilAllWindowsClose()
184   {
185     /*
186     int i = 0;
187     while (mgTopWindow != 0)
188       {
189         if (i % 100 == 0) 
190           {
191             bbtkDebugMessage("wx",2,"Qt::Loop "<<i << std::endl);
192           }
193         i++;
194         wxMilliSleep(10);
195
196       }
197     */
198   }
199   //=========================================================================
200
201   //=========================================================================
202   QWidget* Qt::GetTopWindow() 
203   { 
204     /*
205     Qt::CreateTopWindowIfNeeded();
206     return mgTopWindow; 
207     */
208     return 0;
209   }
210   //=========================================================================
211   
212   //=========================================================================
213   bool Qt::TopWindowExists()
214   {
215     //return (mgTopWindow!=0);
216     return true;
217   }
218   //=========================================================================
219
220   // BBTKWXSIG
221   //=========================================================================
222   void Qt::AddSignalObserver(Slot_function_type f)
223   {
224     mgQtSignal.connect(f);
225   }
226   //=========================================================================
227   // \BBTKWXSIG
228
229   //=========================================================================
230   void Qt::SetAutoDestroyTopWindow(bool b)
231   {
232     // mgAutoDestroyTopWindow = b;
233   }
234   //=========================================================================
235   
236   //=========================================================================
237   void Qt::SetTopWindow(QWidget* w) 
238   {
239     /*
240     if ( mgTopWindow ) 
241       {
242         bbtkGlobalError("wx::SetTopWindow : top window already set !");
243       } 
244     mgTopWindow = w;
245     */
246   }
247   //=========================================================================
248
249
250   //=========================================================================
251   void Qt::IncNbWindowsAlive() 
252   { 
253     mgNbWindowsAlive++; 
254     bbtkDebugMessage("wx",2,"* Number of windows alive = "<<mgNbWindowsAlive
255                      <<std::endl);
256     // BBTKWXSIG
257     mgQtSignal();
258     // \BBTKWXSIG
259   }
260   //=========================================================================
261
262   //=========================================================================
263   void Qt::DecNbWindowsAlive()
264   { 
265     mgNbWindowsAlive--; 
266     bbtkDebugMessage("wx",2,"* Number of windows alive = "<<mgNbWindowsAlive
267                      <<std::endl);
268
269     DestroyTopWindowIfNeeded();
270      // BBTKWXSIG
271     mgQtSignal();
272     // \BBTKWXSIG
273   }
274   //=========================================================================
275   
276   //=========================================================================
277   void Qt::IncNbWindowsShown() 
278   { 
279     mgNbWindowsShown++; 
280     bbtkDebugMessage("wx",2,"* Number of windows shown = "<<mgNbWindowsShown
281                      <<std::endl);
282     // BBTKWXSIG
283     mgQtSignal();
284     // \BBTKWXSIG
285   }
286   //=========================================================================
287
288   //=========================================================================
289   void Qt::DecNbWindowsShown()
290   { 
291     mgNbWindowsShown--; 
292     bbtkDebugMessage("wx",2,"* Number of windows shown = "<<mgNbWindowsShown
293                      <<std::endl);
294
295     DestroyTopWindowIfNeeded();
296
297     // BBTKWXSIG
298     mgQtSignal();
299     // \BBTKWXSIG
300   }
301   //=========================================================================
302
303
304   //=========================================================================
305   int  Qt::GetNbWindowsAlive() 
306   { 
307     return mgNbWindowsAlive; 
308   }
309   //=========================================================================
310   
311   //=========================================================================
312   bool Qt::IsSomeWindowAlive() 
313   { 
314     return (mgNbWindowsAlive>0);
315   }
316   //=========================================================================
317   
318   
319   //=========================================================================
320   int  Qt::GetNbWindowsShown() 
321   { 
322     return mgNbWindowsShown; 
323   }
324   //=========================================================================
325   
326   //=========================================================================
327   bool Qt::IsSomeWindowShown() 
328   { 
329     return (mgNbWindowsShown>0);
330   }
331   //=========================================================================
332   
333
334   //=========================================================================
335   Qt::BusyCursor::BusyCursor()
336   {
337     /*
338     mCursor = 0;
339     if (wxApp::GetInstance()!=0) 
340       {         
341         bbtkDebugMessage("wx",2,
342                          "Qt::BusyCursor::BusyCursor() : creating new cursor"
343                          <<std::endl);
344         mCursor = new wxBusyCursor; 
345     }
346     */
347   }
348   Qt::BusyCursor::~BusyCursor()
349   {
350     /*
351     if (mCursor)
352       { 
353         delete mCursor;
354         bbtkDebugMessage("wx",2,
355                          "Qt::BusyCursor::~BusyCursor() : deleting cursor"<<std::endl);
356       }
357     */
358   }
359   //=========================================================================
360
361   /*
362   //=========================================================================
363   void Qt::ResetCursor() 
364   {
365     if (!TopWindowExists()) return;
366     bbtkDebugMessage("wx",9,"Qt::ResetCursor()"<<std::endl);
367     while (wxIsBusy()) ::wxEndBusyCursor();
368   }
369   //=========================================================================
370   //=========================================================================
371   void Qt::BeginBusyCursor() 
372   {
373     if (!TopWindowExists()) return;
374     bbtkDebugMessage("wx",9,"Qt::BeginBusyCursor()"<<std::endl);
375     ::wxBeginBusyCursor();
376   }
377   //=========================================================================
378   //=========================================================================
379   void Qt::EndBusyCursor()
380   {
381     if (!TopWindowExists()) return;
382     bbtkDebugMessage("wx",9,"Qt::EndBusyCursor()"<<std::endl);
383     ::wxEndBusyCursor();
384   }
385   //=========================================================================
386   */
387
388 } // namespace bbtk
389
390 #else
391 //=======================================================================
392 // WITHOUT WX
393 //=========================================================================
394 namespace bbtk
395 {
396   Qt::BusyCursor::BusyCursor()
397   {
398   }
399   Qt::BusyCursor::~BusyCursor()
400   {
401   }
402   //=========================================================================
403
404 } // namespace bbtk
405 #endif
406