]> Creatis software - bbtk.git/blob - kernel/src/bbtkKWBlackBox.cxx
f0237f9a8b3ccbe16c7e094c9c341ff4475a06d1
[bbtk.git] / kernel / src / bbtkKWBlackBox.cxx
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbtkKWBlackBox.cxx,v $
4   Language:  C++
5   Date:      $Date: 2008/12/08 12:54:26 $
6   Version:   $Revision: 1.6 $
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 /**
31  *  \file 
32  *  \brief 
33  */
34
35 #ifdef USE_KWWIDGETS
36
37
38
39
40  
41 #include "bbtkKWBlackBox.h"
42 #include "bbtkBlackBoxOutputConnector.h"
43 #include "vtkKWBlackBoxDialog.h"
44
45
46 namespace bbtk
47 {
48
49   //=========================================================================
50   // KWBlackBox
51   //=========================================================================
52
53   //=========================================================================
54   //=========================================================================
55   //=========================================================================
56   //=========================================================================
57   BBTK_BLACK_BOX_IMPLEMENTATION(KWBlackBox,AtomicBlackBox);
58   //=========================================================================
59   
60   //=========================================================================
61   void KWBlackBox::bbUserConstructor()
62   {
63     bbtkDebugMessage("Kernel",9,"KWBlackBox::bbUserConstructor()"<<std::endl);
64     bbInitAttributes();
65   }
66   //=========================================================================
67
68   //=========================================================================
69   void KWBlackBox::bbUserCopyConstructor(bbtk::BlackBox::Pointer)
70   {
71     bbtkDebugMessage("Kernel",9,"KWBlackBox::bbUserCopyConstructor()"
72                      <<std::endl);
73     bbInitAttributes();
74   }
75   //=========================================================================
76
77
78   //=========================================================================
79   void KWBlackBox::bbUserDestructor()
80   {
81     bbtkDebugMessage("kw",9,"==> KWBlackBox::bbUserDestructor() ["<<bbGetFullName()<<"]"<<std::endl);
82     if (bbGetWindow()) {
83       bbGetWindow()->bbClose();
84       bbSetWindow(0);
85     }
86     bbtkDebugMessage("kw",9,"<== KWBlackBox::bbUserDestructor() ["<<bbGetFullName()<<"]"<<std::endl);
87   }
88   //=========================================================================
89   
90
91
92   //=========================================================================
93   /**
94    * \brief Initialize the attributes of the class
95    *
96    */
97   void KWBlackBox::bbInitAttributes()
98   {
99     bbmWindow = 0;
100     //    bbmWidget = 0;
101     //    bbSetInputWinParent(0);
102     bbSetInputWinTitle(bbGetName());
103     bbSetInputWinWidth(800);
104     bbSetInputWinHeight(800);
105     bbSetInputWinDialog(false);
106     bbSetOutputWidget(0);
107
108     //    bbSetWidgetEventHandler(0);
109     //  bbSetUpdateTransferedToParent(false);
110   }
111   //=========================================================================
112
113   //=========================================================================
114   /// Main processing method of the box.
115   void KWBlackBox::bbExecute(bool force)
116   {
117     bbtkDebugMessageInc("process",2,
118                         "=> KWBlackBox::bbExecute("<<(int)force<<") ["
119                         <<bbGetFullName()<<"]"<<std::endl);
120   
121     // If the output 'Widget' is connected then 
122     // we must execute the parent box
123     BlackBox::OutputConnectorMapType::const_iterator i 
124       = bbGetOutputConnectorMap().find("Widget");
125     
126     if ( i->second->GetConnectionVector().size() != 0 ) 
127       {
128         bbtkWarning("Execution called on '"<<bbGetFullName()
129                     <<"' although its Output 'Widget' is connected: "
130                     <<"if the widget is not created yet then it will not be! "
131                     <<"Execute the top level Layout widget to create and "
132                     <<"display the widget.");
133       }
134
135     AtomicBlackBox::bbExecute(force);
136
137     bbtkDebugMessageDec("process",2,
138                         "<= KWBlackBox::bbExecute() ["
139                         <<bbGetFullName()<<"]"<<std::endl);
140   }
141   //=========================================================================
142
143
144   //=========================================================================
145   void KWBlackBox::bbProcess()
146   { 
147 /*
148           if (bbGetOutputWidget()==0) this->bbUserCreateWidget();
149     this->bbUserProcess(); 
150     bbShowWindow();
151     //    this->bbUserOnShow();
152 */
153     this->bbUserProcess(); 
154
155     // If output widget not connected create the window 
156     if ( (*bbGetOutputConnectorMap().find("Widget")).second
157          ->GetConnectionVector().size() == 0 ) 
158       {
159         Window* show = 0;
160         // If the window already exists : no need creating it
161         if (bbGetWindow()!=0)
162           {
163             bbtkDebugMessage("kw",2,
164                              "-> Window already exists"
165                              <<std::endl);
166             show = bbGetWindow();
167           }
168         // Else create window 
169         else 
170           {
171             bbtkDebugMessage("kw",2,
172                              "-> Creating the window"
173                              <<std::endl);
174             KW::GetApplication();
175             
176             // Input WinDialog set to true : creating a Dialog
177             /*
178             if (bbGetInputWinDialog()) 
179               {
180                 bbtkDebugMessage("kw",2,
181                                  "   Input WinDialog set to true : creating a Dialog"
182                                  <<std::endl);
183             */
184                 vtkKWBlackBoxDialog* win = vtkKWBlackBoxDialog::New();
185                 show = (Window*) win;
186                 win->bbSetBlackBox( GetThisPointer<KWBlackBox>());
187                 KW::GetApplication()->AddWindow((vtkKWWindowBase*)win);
188                 win->Create();
189                 win->SetResizable(0,0);
190
191                 bbUserCreateWidget(win->GetFrame());            
192                 KW::GetApplication()->Script("place %s -x 0 -y 0 -width %d -height %d",
193                                              bbGetOutputWidget()->GetWidgetName(),
194                                              bbGetInputWinWidth(),
195                                              bbGetInputWinHeight());
196                 
197
198
199
200
201                 /*
202                 bbGetOutputWidget()->SetParent(win);
203                 bbGetOutputWidget()->Create();
204                 KW::GetApplication()->Script("pack %s -side left -anchor c -expand y",
205                         
206                                      //"pack %s -side top -anchor nw -expand y -fill none -padx 2 -pady 2", 
207                                              bbGetOutputWidget()->GetWidgetName());
208                 */                           
209                 /*      
210                   }
211         
212             // Input WinDialog set to false : creating a Frame
213             else 
214               {
215                 bbtkDebugMessage("process",2,
216                                  "   Input WinDialog set to false : creating a Frame"
217                                  <<std::endl);
218                 vtkKWBlackBoxFrame* win = vtkKWBlackBoxFrame::New();
219                 show = (Window*) win;
220                 win->bbSetBlackBox( GetThisPointer<KWBlackBox>());
221                 //              win->SetName( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN" );
222                 //              win->SetWidth( bbGetInputWinWidth() );
223                 //              win->SetHeight( bbGetInputWinHeight() );
224                 KW::GetApplication()->AddWindow((vtkKWWindowBase*)win);
225                 win->Create();
226                 
227                 bbGetOutputWidget()->SetParent(win);
228                 bbGetOutputWidget()->Create();
229                 KW::GetApplication()->Script("pack %s -side top -anchor nw -expand y -fill none -padx 2 -pady 2", 
230                                              bbGetOutputWidget()->GetWidgetName());           }
231         */
232         
233                 //      win->Invoke();
234           }
235         
236         // Show the window
237
238         show->bbShow(); 
239         //      std::cout << "KW::GetApplication()->Start();"<<std::endl;
240         //      KW::GetApplication()->Start();
241         //      std::cout << "AFTER KW::GetApplication()->Start();"<<std::endl;
242         
243       }           
244     //   
245     
246   }
247   //=========================================================================
248   
249
250   //==================================================================
251   vtkKWWidget*  KWBlackBox::bbCreateWidgetOfInput(const std::string& in, vtkKWFrame* parent)
252   {
253     vtkKWWidget* w = 0;
254     // If input is connected 
255     BlackBoxInputConnector* c = bbGetInputConnectorMap().find(in)->second ;
256     if ( c->IsConnected() )                     
257       {
258         // Get black box from 
259         BlackBox::Pointer from = 
260           c->GetConnection()->GetBlackBoxFrom();
261         // Cast it into a KWBlackBox
262         KWBlackBox::Pointer wfrom = boost::dynamic_pointer_cast<KWBlackBox>(from);
263         // Call bbCreateWidget
264         wfrom->bbUserCreateWidget(parent);
265         // Get the widget created
266         w = wfrom->bbGetOutputWidget();
267       }
268     return w;
269   }
270   //==================================================================
271
272
273   //==================================================================
274    void KWBlackBox::bbHideWindow()
275   {
276     bbtkDebugMessageInc("kw",1,"=> KWBlackBox::bbHideWindow() ["
277                         <<bbGetFullName()<<"]"<<std::endl);
278
279     if (bbGetWindow()!=0) bbGetWindow()->bbHide();
280
281     bbtkDebugMessageDec("kw",2,"<= KWBlackBox::bbHideWindow() ["
282                         <<bbGetFullName()<<"]"<<std::endl);
283   }
284   //==================================================================
285
286
287   //==================================================================
288    void KWBlackBox::bbCloseWindow()
289   {
290     bbtkDebugMessageInc("kw",1,"=> KWBlackBox::bbCloseWindow() ["
291                         <<bbGetFullName()<<"]"<<std::endl);
292
293     if (bbGetWindow()!=0) bbGetWindow()->bbClose();
294
295     bbtkDebugMessageDec("kw",2,"<= KWBlackBox::bbCloseWindow() ["
296                         <<bbGetFullName()<<"]"<<std::endl);
297   }
298   //==================================================================
299
300   //==================================================================
301   KWBlackBox::Window* KWBlackBox::bbGetContainingWindow()
302   {
303     if (bbGetWindow()!=0) return bbGetWindow();
304     BlackBox::OutputConnectorMapType::const_iterator i 
305       = bbGetOutputConnectorMap().find("Widget");
306     if ( i->second->GetConnectionVector().size() != 0 ) 
307       {
308         return boost::static_pointer_cast<KWBlackBox>
309           (i->second->GetConnectionVector().front() //.lock()
310            ->GetBlackBoxTo())->bbGetContainingWindow();
311       }
312     return 0;
313   }
314   //==================================================================
315
316
317   //==================================================================
318   bool KWBlackBox::bbIsShown()
319   {
320     if (bbGetContainingWindow()!=0)
321       return bbGetContainingWindow()->bbIsShown();
322     return false;
323   }
324   //==================================================================
325
326
327 }//namespace bbtk
328
329
330 #endif
331