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