]> 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/05/18 10:45:41 $
6   Version:   $Revision: 1.2 $
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   const 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         char* aargv = const_cast<char*>(argv);
99         mgQtApp = new QApplication(argc,&aargv);
100
101         //      std::cout << ">>>> mgQtApp.exec()"<<std::endl;
102         
103         //      mgQtApp->exec();
104
105         //      std::cout << "<<<< mgQtApp.exec()"<<std::endl;
106         //      wxApp::SetInstance(mgQtApp);
107
108         //int argc = 0;
109         //wxEntry(argc,0);
110
111         //      wxInitialize();
112       }
113     
114   }
115   //=========================================================================
116
117   //=========================================================================
118   void Qt::DestroyQtAppIfNeeded()
119   {
120     /*
121     if (mgQtApp!= 0) 
122       {
123         bbtkDebugMessage("wx",1,"  --> Destructing bbtk QApplication"<<std::endl);
124         //delete mgQtApp;
125         //      mgQtApp = 0;
126         // Uninit wx
127         //      wxUninitialize();
128       }
129     */
130   }
131   //=========================================================================
132
133   //=========================================================================  
134   void Qt::SetTopWindowParent(QWidget* w)
135   {
136     /*
137     if (mgTopWindowParent != 0)
138       {
139         bbtkGlobalError("Qt::SetTopWindowParent : top window parent != 0");
140       }
141     mgTopWindowParent = w;
142     */
143   }
144   //=========================================================================
145
146   //=========================================================================
147   void Qt::CreateTopWindowIfNeeded()
148   {
149     /*
150     if (mgTopWindow!=0) return;
151     bbtkDebugMessage("wx",1,"  --> Creating bbtk top window"<<std::endl);
152
153     CreateQtAppIfNeeded();
154
155     wxWindow* top = 
156       new wxFrame(mgTopWindowParent,
157                   -1,
158                   _T("TOP BBTK FRAME (YOU SHOULD NOT SEE ME !!)"));
159     top->Hide();
160
161     Qt::SetTopWindow(top);
162     */
163   }
164   //=========================================================================
165
166   //=========================================================================
167   void Qt::DestroyTopWindowIfNeeded()
168   {
169     /*
170     if ( (mgNbWindowsAlive==0) && 
171          (mgAutoDestroyTopWindow) )
172       {
173         bbtkDebugMessage("wx",1,"  --> Destructing bbtk top window"<<std::endl);
174         mgTopWindow->Close();
175         mgTopWindow = 0;
176         
177         DestroyQtAppIfNeeded();
178       }
179     */
180   } 
181   //=========================================================================
182
183   //=========================================================================
184   void Qt::LoopUntilAllWindowsClose()
185   {
186     /*
187     int i = 0;
188     while (mgTopWindow != 0)
189       {
190         if (i % 100 == 0) 
191           {
192             bbtkDebugMessage("wx",2,"Qt::Loop "<<i << std::endl);
193           }
194         i++;
195         wxMilliSleep(10);
196
197       }
198     */
199   }
200   //=========================================================================
201
202   //=========================================================================
203   QWidget* Qt::GetTopWindow() 
204   { 
205     /*
206     Qt::CreateTopWindowIfNeeded();
207     return mgTopWindow; 
208     */
209     return 0;
210   }
211   //=========================================================================
212   
213   //=========================================================================
214   bool Qt::TopWindowExists()
215   {
216     //return (mgTopWindow!=0);
217     return true;
218   }
219   //=========================================================================
220
221   // BBTKWXSIG
222   //=========================================================================
223   void Qt::AddSignalObserver(Slot_function_type f)
224   {
225     mgQtSignal.connect(f);
226   }
227   //=========================================================================
228   // \BBTKWXSIG
229
230   //=========================================================================
231   void Qt::SetAutoDestroyTopWindow(bool b)
232   {
233     // mgAutoDestroyTopWindow = b;
234   }
235   //=========================================================================
236   
237   //=========================================================================
238   void Qt::SetTopWindow(QWidget* w) 
239   {
240     /*
241     if ( mgTopWindow ) 
242       {
243         bbtkGlobalError("wx::SetTopWindow : top window already set !");
244       } 
245     mgTopWindow = w;
246     */
247   }
248   //=========================================================================
249
250
251   //=========================================================================
252   void Qt::IncNbWindowsAlive() 
253   { 
254     mgNbWindowsAlive++; 
255     bbtkDebugMessage("wx",2,"* Number of windows alive = "<<mgNbWindowsAlive
256                      <<std::endl);
257     // BBTKWXSIG
258     mgQtSignal();
259     // \BBTKWXSIG
260   }
261   //=========================================================================
262
263   //=========================================================================
264   void Qt::DecNbWindowsAlive()
265   { 
266     mgNbWindowsAlive--; 
267     bbtkDebugMessage("wx",2,"* Number of windows alive = "<<mgNbWindowsAlive
268                      <<std::endl);
269
270     DestroyTopWindowIfNeeded();
271      // BBTKWXSIG
272     mgQtSignal();
273     // \BBTKWXSIG
274   }
275   //=========================================================================
276   
277   //=========================================================================
278   void Qt::IncNbWindowsShown() 
279   { 
280     mgNbWindowsShown++; 
281     bbtkDebugMessage("wx",2,"* Number of windows shown = "<<mgNbWindowsShown
282                      <<std::endl);
283     // BBTKWXSIG
284     mgQtSignal();
285     // \BBTKWXSIG
286   }
287   //=========================================================================
288
289   //=========================================================================
290   void Qt::DecNbWindowsShown()
291   { 
292     mgNbWindowsShown--; 
293     bbtkDebugMessage("wx",2,"* Number of windows shown = "<<mgNbWindowsShown
294                      <<std::endl);
295
296     DestroyTopWindowIfNeeded();
297
298     // BBTKWXSIG
299     mgQtSignal();
300     // \BBTKWXSIG
301   }
302   //=========================================================================
303
304
305   //=========================================================================
306   int  Qt::GetNbWindowsAlive() 
307   { 
308     return mgNbWindowsAlive; 
309   }
310   //=========================================================================
311   
312   //=========================================================================
313   bool Qt::IsSomeWindowAlive() 
314   { 
315     return (mgNbWindowsAlive>0);
316   }
317   //=========================================================================
318   
319   
320   //=========================================================================
321   int  Qt::GetNbWindowsShown() 
322   { 
323     return mgNbWindowsShown; 
324   }
325   //=========================================================================
326   
327   //=========================================================================
328   bool Qt::IsSomeWindowShown() 
329   { 
330     return (mgNbWindowsShown>0);
331   }
332   //=========================================================================
333   
334
335   //=========================================================================
336   Qt::BusyCursor::BusyCursor()
337   {
338     /*
339     mCursor = 0;
340     if (wxApp::GetInstance()!=0) 
341       {         
342         bbtkDebugMessage("wx",2,
343                          "Qt::BusyCursor::BusyCursor() : creating new cursor"
344                          <<std::endl);
345         mCursor = new wxBusyCursor; 
346     }
347     */
348   }
349   Qt::BusyCursor::~BusyCursor()
350   {
351     /*
352     if (mCursor)
353       { 
354         delete mCursor;
355         bbtkDebugMessage("wx",2,
356                          "Qt::BusyCursor::~BusyCursor() : deleting cursor"<<std::endl);
357       }
358     */
359   }
360   //=========================================================================
361
362   /*
363   //=========================================================================
364   void Qt::ResetCursor() 
365   {
366     if (!TopWindowExists()) return;
367     bbtkDebugMessage("wx",9,"Qt::ResetCursor()"<<std::endl);
368     while (wxIsBusy()) ::wxEndBusyCursor();
369   }
370   //=========================================================================
371   //=========================================================================
372   void Qt::BeginBusyCursor() 
373   {
374     if (!TopWindowExists()) return;
375     bbtkDebugMessage("wx",9,"Qt::BeginBusyCursor()"<<std::endl);
376     ::wxBeginBusyCursor();
377   }
378   //=========================================================================
379   //=========================================================================
380   void Qt::EndBusyCursor()
381   {
382     if (!TopWindowExists()) return;
383     bbtkDebugMessage("wx",9,"Qt::EndBusyCursor()"<<std::endl);
384     ::wxEndBusyCursor();
385   }
386   //=========================================================================
387   */
388
389 } // namespace bbtk
390
391 #else
392 //=======================================================================
393 // WITHOUT WX
394 //=========================================================================
395 namespace bbtk
396 {
397   Qt::BusyCursor::BusyCursor()
398   {
399   }
400   Qt::BusyCursor::~BusyCursor()
401   {
402   }
403   //=========================================================================
404
405 } // namespace bbtk
406 #endif
407