]> Creatis software - bbtk.git/blob - kernel/src/bbtkWidgetBlackBox.txx
#3299 BBTK Bug New Normal wt4
[bbtk.git] / kernel / src / bbtkWidgetBlackBox.txx
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: bbtkWidgetBlackBox.txx,v $
31   Language:  C++
32   Date:      $Date: 2012/11/16 08:49:01 $
33   Version:   $Revision: 1.7 $
34 =========================================================================*/
35
36
37
38 /**
39  *  \file 
40  *  \brief 
41  */
42
43
44  
45 //#include "bbtkWidgetBlackBoxWindow.h"
46 #include "bbtkBlackBoxOutputConnector.h"
47
48
49 namespace bbtk
50 {
51
52   //=========================================================================
53   // WidgetBlackBox
54   //=========================================================================
55
56   //=========================================================================
57   //=========================================================================
58   //=========================================================================
59   //=========================================================================
60   BBTK_BLACK_BOX_TEMPLATE_IMPLEMENTATION(WidgetBlackBox,AtomicBlackBox);
61   //=========================================================================
62   
63 //=========================================================================
64   template <class T>  
65   void WidgetBlackBox<T>::bbUserSetDefaultValues()
66   {
67     bbtkBlackBoxDebugMessage("widget",9,"WidgetBlackBox::bbUserSetDefaultValues()"<<std::endl);
68     //    bbmWindow = 0;
69     bbSetInputWinTitle(bbGetName());
70     bbSetInputWinWidth(800);
71     bbSetInputWinHeight(800);
72     bbSetInputWinDialog(false);
73     bbSetOutputWidget(0);
74   }
75   //=========================================================================
76
77  
78   //=========================================================================
79   template <class T>  
80   void WidgetBlackBox<T>::bbUserInitializeProcessing()
81   {
82     bbmShown = false;
83   }
84   //=========================================================================
85
86   //=========================================================================
87   template <class T>  
88   void WidgetBlackBox<T>::bbUserFinalizeProcessing()
89   {
90     /*
91     std::cout << "****** WidgetBlackBox<T>::bbUserFinalizeProcessing()"<<std::endl;
92     bbtkBlackBoxDebugMessage("widget",9,"==> WidgetBlackBox::bbUserFinalizeProcessing() ["<<bbGetFullName()<<"]"<<std::endl);
93     if (bbGetWindow()) 
94       {
95         bool shown = bbGetWindow()->bbIsShown();
96         bbGetWindow()->bbDestroy();
97         bbSetWindow(0);
98         
99         if (GetUseCount()==0)
100           {    
101             bbtkBlackBoxDebugMessage("widget",9,"**** HERE use count == 0"<<std::endl);
102             if (shown) DecNbWindowsShown();
103             DecNbWindowsAlive();
104           }
105
106       }
107     bbtkBlackBoxDebugMessage("widget",9,"<== WidgetBlackBox::bbUserFinalizeProcessing() ["<<bbGetFullName()<<"]"<<std::endl);
108     */
109   }
110   //=========================================================================
111   
112
113
114  
115
116   //=========================================================================
117   template <class T>  
118   void WidgetBlackBox<T>::bbProcess()
119   { 
120     // TODO : update the window size and title 
121     this->bbUserProcess(); 
122   }
123   //=========================================================================
124
125   //=========================================================================
126   template <class T>  
127   void WidgetBlackBox<T>::bbCreateWindow()
128   { 
129
130
131     // If output 'Widget' not connected then
132     // it is a top level Widget have to create and show the Window
133     if ( ! bbIsOutputWidgetConnected() )
134       {
135         // create the Window if does not exist
136         if (!bbWindowIsCreated())
137           {
138             bbtkBlackBoxDebugMessage("widget",2,
139                              "-> Creating the window"
140                              <<std::endl);
141             // Reinitializing
142             bbSetShown(false);
143             bbmNestedWidgetBoxes.clear();
144             
145             //      this->InitWindowManagerIfNeeded();
146             // If is a Dialog requested
147             if ( bbGetInputWinDialog() )
148               {
149                 bbtkBlackBoxDebugMessage("widget",2,
150                                  "   Input WinDialog set to true : creating a Dialog"
151                                  <<std::endl);
152                 this->bbCreateDialogWindow();
153               }
154             // Input WinDialog set to false : creating a Frame
155             else 
156               {
157                 bbtkBlackBoxDebugMessage("widget",2,
158                                  "   Input WinDialog set to false : creating a Frame"
159                                  <<std::endl);
160                 this->bbCreateFrameWindow();
161               }
162           }
163         /*
164         // Show the window
165         if ( !bbGetWindow() )
166           {
167             bbtkInternalError("Need to show the Window of widget "<<bbGetName()
168                               <<" however was not created by apropriate cb");
169           }
170         */
171
172         /*
173         bbtkBlackBoxDebugMessage("widget",2,
174                          "-> Showing the window"
175                          <<std::endl);
176         //      bbGetWindow()->bbShow(); 
177         this->bbShowWindow();
178         */
179       }           
180     //   
181   }
182   //=========================================================================
183   
184
185   //==================================================================
186   template <class T>  
187   typename WidgetBlackBox<T>::WidgetPointer  
188   WidgetBlackBox<T>::bbCreateWidgetOfInput
189   (const std::string& in, typename WidgetBlackBox<T>::WidgetPointer parent)
190   {
191     bbtkBlackBoxDebugMessage("widget",2,"bbCreateWidgetOfInput("
192                              <<in<<","<<parent<<")"
193                              <<std::endl);
194     Widget* w = 0;
195     // If input is connected 
196     BlackBoxInputConnector* c = bbGetInputConnectorMap().find(in)->second ;
197     if ( c->IsConnected() )                     
198     {
199                 // Get black box from 
200                 BlackBox::Pointer from = c->GetConnection()->GetBlackBoxFrom();
201                 // Cast it into a WidgetBlackBox
202                 typename WidgetBlackBox<T>::Pointer wfrom  = boost::dynamic_pointer_cast<WidgetBlackBox<T> >(from);
203                 // If the black box from construction is not done yet : do it
204                  // If not constructed do it 
205                 wfrom->bbInitializeProcessing();
206                 // Call bbUserCreateWidget
207                 wfrom->bbUserCreateWidget(parent);
208                 // Get the widget created
209                 w = wfrom->bbGetOutputWidget();
210                 // Insert the box into the NestedWidgetBoxes list
211                 bbmNestedWidgetBoxes.push_back(from);
212     } // if IsConnected
213     return w;
214   }
215   //==================================================================
216
217   //=========================================================================
218   template <class T>  
219   bool WidgetBlackBox<T>::bbIsOutputWidgetConnected()
220   {
221     return ((*bbGetOutputConnectorMap().find("Widget")).second->GetConnectionVector().size() != 0 );
222   }
223   //==================================================================
224
225   //==================================================================
226   template <class T>  
227   void WidgetBlackBox<T>::bbSetShown(bool b)
228   {
229     if (b==bbmShown) return;
230
231     bbtkBlackBoxDebugMessage("widget",5,"WidgetBlackBox::bbSetShown("
232                              <<b<<")"
233                              <<std::endl);
234     
235     bbmShown = b;
236     if (b)
237       {
238         this->bbUserOnShow();
239      }
240     // Have to propagate to nested widgets
241     std::vector<BlackBox::WeakPointer>::const_iterator i;
242     for (i =bbmNestedWidgetBoxes.begin();
243          i!=bbmNestedWidgetBoxes.end();
244          ++i)
245       {
246         if (!i->expired()) i->lock()->bbSetShown(b);
247       }
248          
249   }
250   //==================================================================
251
252   //==================================================================
253    template <class T>  
254    bool WidgetBlackBox<T>::bbIsShown()
255   {
256     return bbmShown;
257   }
258   //==================================================================
259  
260   /*
261  //==================================================================
262   template <class T>  
263    void WidgetBlackBox<T>::bbShowWindow()
264   {
265     bbtkBlackBoxDebugMessage("widget",1,"=> WidgetBlackBox::bbShowWindow() ["
266                         <<bbGetFullName()<<"]"<<std::endl);
267
268     if (bbGetWindow()!=0) bbGetWindow()->bbShow();
269
270     bbtkBlackBoxDebugMessage("widget",1,"<= WidgetBlackBox::bbShowWindow() ["
271                         <<bbGetFullName()<<"]"<<std::endl);
272   }
273   //==================================================================
274
275   //==================================================================
276   template <class T>  
277    void WidgetBlackBox<T>::bbHideWindow()
278   {
279     bbtkBlackBoxDebugMessage("widget",1,"=> WidgetBlackBox::bbHideWindow() ["
280                         <<bbGetFullName()<<"]"<<std::endl);
281
282     if (bbGetWindow()!=0) bbGetWindow()->bbHide();
283
284     bbtkBlackBoxDebugMessage("widget",1,"<= WidgetBlackBox::bbHideWindow() ["
285                         <<bbGetFullName()<<"]"<<std::endl);
286   }
287   //==================================================================
288
289
290   //==================================================================
291   template <class T>  
292   void WidgetBlackBox<T>::bbCloseWindow()
293   {
294     bbtkBlackBoxDebugMessage("widget",1,"=> WidgetBlackBox::bbCloseWindow() ["
295                         <<bbGetFullName()<<"]"<<std::endl);
296                         
297 printf("EED WidgetBlackBoxWindow<W>::bbDestroy\n");                     
298
299
300     if (bbGetWindow()!=0) bbGetWindow()->bbDestroy();
301
302     bbtkBlackBoxDebugMessage("widget",1,"<= WidgetBlackBox::bbCloseWindow() ["
303                         <<bbGetFullName()<<"]"<<std::endl);
304   }
305   //==================================================================
306
307   //==================================================================
308   template <class T>  
309   typename WidgetBlackBox<T>::Window* 
310   WidgetBlackBox<T>::bbGetContainingWindow()
311   {
312     if (bbGetWindow()!=0) return bbGetWindow();
313     BlackBox::OutputConnectorMapType::const_iterator i 
314       = bbGetOutputConnectorMap().find("Widget");
315     if ( i->second->GetConnectionVector().size() != 0 ) 
316       {
317         return boost::static_pointer_cast<WidgetBlackBox>
318           (i->second->GetConnectionVector().front() //.lock()
319            ->GetBlackBoxTo())->bbGetContainingWindow();
320       }
321     return 0;
322   }
323   //==================================================================
324
325
326   //==================================================================
327   template <class T>  
328   bool WidgetBlackBox<T>::bbIsShown()
329   {
330     if (bbGetContainingWindow()!=0)
331       return bbGetContainingWindow()->bbIsShown();
332     return false;
333   }
334   //==================================================================
335   */
336
337 }//namespace bbtk
338
339
340
341