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