]> Creatis software - bbtk.git/blob - kernel/src/bbtkWidgetBlackBox.txx
Feature #1774
[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 = 
201           c->GetConnection()->GetBlackBoxFrom();
202         // Cast it into a WidgetBlackBox
203         typename WidgetBlackBox<T>::Pointer wfrom 
204           = boost::dynamic_pointer_cast<WidgetBlackBox<T> >(from);
205         // If the black box from construction is not done yet : do it
206          // If not constructed do it 
207         wfrom->bbInitializeProcessing();
208         // Call bbUserCreateWidget
209         wfrom->bbUserCreateWidget(parent);
210         // Get the widget created
211         w = wfrom->bbGetOutputWidget();
212         // Insert the box into the NestedWidgetBoxes list
213         bbmNestedWidgetBoxes.push_back(from);
214       }
215     return w;
216   }
217   //==================================================================
218
219   //=========================================================================
220   template <class T>  
221   bool WidgetBlackBox<T>::bbIsOutputWidgetConnected()
222   {
223     return ((*bbGetOutputConnectorMap().find("Widget")).second->GetConnectionVector().size() != 0 );
224   }
225   //==================================================================
226
227   //==================================================================
228   template <class T>  
229   void WidgetBlackBox<T>::bbSetShown(bool b)
230   {
231     if (b==bbmShown) return;
232
233     bbtkBlackBoxDebugMessage("widget",5,"WidgetBlackBox::bbSetShown("
234                              <<b<<")"
235                              <<std::endl);
236     
237     bbmShown = b;
238     if (b)
239       {
240         this->bbUserOnShow();
241      }
242     // Have to propagate to nested widgets
243     std::vector<BlackBox::WeakPointer>::const_iterator i;
244     for (i =bbmNestedWidgetBoxes.begin();
245          i!=bbmNestedWidgetBoxes.end();
246          ++i)
247       {
248         if (!i->expired()) i->lock()->bbSetShown(b);
249       }
250          
251   }
252   //==================================================================
253
254   //==================================================================
255    template <class T>  
256    bool WidgetBlackBox<T>::bbIsShown()
257   {
258     return bbmShown;
259   }
260   //==================================================================
261  
262   /*
263  //==================================================================
264   template <class T>  
265    void WidgetBlackBox<T>::bbShowWindow()
266   {
267     bbtkBlackBoxDebugMessage("widget",1,"=> WidgetBlackBox::bbShowWindow() ["
268                         <<bbGetFullName()<<"]"<<std::endl);
269
270     if (bbGetWindow()!=0) bbGetWindow()->bbShow();
271
272     bbtkBlackBoxDebugMessage("widget",1,"<= WidgetBlackBox::bbShowWindow() ["
273                         <<bbGetFullName()<<"]"<<std::endl);
274   }
275   //==================================================================
276
277   //==================================================================
278   template <class T>  
279    void WidgetBlackBox<T>::bbHideWindow()
280   {
281     bbtkBlackBoxDebugMessage("widget",1,"=> WidgetBlackBox::bbHideWindow() ["
282                         <<bbGetFullName()<<"]"<<std::endl);
283
284     if (bbGetWindow()!=0) bbGetWindow()->bbHide();
285
286     bbtkBlackBoxDebugMessage("widget",1,"<= WidgetBlackBox::bbHideWindow() ["
287                         <<bbGetFullName()<<"]"<<std::endl);
288   }
289   //==================================================================
290
291
292   //==================================================================
293   template <class T>  
294   void WidgetBlackBox<T>::bbCloseWindow()
295   {
296     bbtkBlackBoxDebugMessage("widget",1,"=> WidgetBlackBox::bbCloseWindow() ["
297                         <<bbGetFullName()<<"]"<<std::endl);
298
299     if (bbGetWindow()!=0) bbGetWindow()->bbDestroy();
300
301     bbtkBlackBoxDebugMessage("widget",1,"<= WidgetBlackBox::bbCloseWindow() ["
302                         <<bbGetFullName()<<"]"<<std::endl);
303   }
304   //==================================================================
305
306   //==================================================================
307   template <class T>  
308   typename WidgetBlackBox<T>::Window* 
309   WidgetBlackBox<T>::bbGetContainingWindow()
310   {
311     if (bbGetWindow()!=0) return bbGetWindow();
312     BlackBox::OutputConnectorMapType::const_iterator i 
313       = bbGetOutputConnectorMap().find("Widget");
314     if ( i->second->GetConnectionVector().size() != 0 ) 
315       {
316         return boost::static_pointer_cast<WidgetBlackBox>
317           (i->second->GetConnectionVector().front() //.lock()
318            ->GetBlackBoxTo())->bbGetContainingWindow();
319       }
320     return 0;
321   }
322   //==================================================================
323
324
325   //==================================================================
326   template <class T>  
327   bool WidgetBlackBox<T>::bbIsShown()
328   {
329     if (bbGetContainingWindow()!=0)
330       return bbGetContainingWindow()->bbIsShown();
331     return false;
332   }
333   //==================================================================
334   */
335
336 }//namespace bbtk
337
338
339
340