]> Creatis software - bbtk.git/blob - kernel/src/bbtkKWBlackBox.cxx
*** empty log message ***
[bbtk.git] / kernel / src / bbtkKWBlackBox.cxx
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbtkKWBlackBox.cxx,v $
4   Language:  C++
5   Date:      $Date: 2008/12/11 09:50:35 $
6   Version:   $Revision: 1.7 $
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   //=========================================================================
115   void KWBlackBox::bbProcess()
116   { 
117
118     this->bbUserProcess(); 
119
120     // If output widget not connected create the window 
121     if ( (*bbGetOutputConnectorMap().find("Widget")).second
122          ->GetConnectionVector().size() == 0 ) 
123       {
124         Window* show = 0;
125         // If the window already exists : no need creating it
126         if (bbGetWindow()!=0)
127           {
128             bbtkDebugMessage("kw",2,
129                              "-> Window already exists"
130                              <<std::endl);
131             show = bbGetWindow();
132           }
133         // Else create window 
134         else 
135           {
136             bbtkDebugMessage("kw",2,
137                              "-> Creating the window"
138                              <<std::endl);
139             KW::GetApplication();
140             
141             // Input WinDialog set to true : creating a Dialog
142             /*
143             if (bbGetInputWinDialog()) 
144               {
145                 bbtkDebugMessage("kw",2,
146                                  "   Input WinDialog set to true : creating a Dialog"
147                                  <<std::endl);
148             */
149                 vtkKWBlackBoxDialog* win = vtkKWBlackBoxDialog::New();
150                 show = (Window*) win;
151                 win->bbSetBlackBox( GetThisPointer<KWBlackBox>());
152                 KW::GetApplication()->AddWindow((vtkKWWindowBase*)win);
153                 win->Create();
154                 win->SetResizable(0,0);
155
156                 bbUserCreateWidget(win->GetFrame());            
157                 KW::GetApplication()->Script("place %s -x 0 -y 0 -width %d -height %d",
158                                              bbGetOutputWidget()->GetWidgetName(),
159                                              bbGetInputWinWidth(),
160                                              bbGetInputWinHeight());
161                 
162
163
164
165
166                 /*
167                 bbGetOutputWidget()->SetParent(win);
168                 bbGetOutputWidget()->Create();
169                 KW::GetApplication()->Script("pack %s -side left -anchor c -expand y",
170                         
171                                      //"pack %s -side top -anchor nw -expand y -fill none -padx 2 -pady 2", 
172                                              bbGetOutputWidget()->GetWidgetName());
173                 */                           
174                 /*      
175                   }
176         
177             // Input WinDialog set to false : creating a Frame
178             else 
179               {
180                 bbtkDebugMessage("process",2,
181                                  "   Input WinDialog set to false : creating a Frame"
182                                  <<std::endl);
183                 vtkKWBlackBoxFrame* win = vtkKWBlackBoxFrame::New();
184                 show = (Window*) win;
185                 win->bbSetBlackBox( GetThisPointer<KWBlackBox>());
186                 //              win->SetName( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN" );
187                 //              win->SetWidth( bbGetInputWinWidth() );
188                 //              win->SetHeight( bbGetInputWinHeight() );
189                 KW::GetApplication()->AddWindow((vtkKWWindowBase*)win);
190                 win->Create();
191                 
192                 bbGetOutputWidget()->SetParent(win);
193                 bbGetOutputWidget()->Create();
194                 KW::GetApplication()->Script("pack %s -side top -anchor nw -expand y -fill none -padx 2 -pady 2", 
195                                              bbGetOutputWidget()->GetWidgetName());           }
196         */
197         
198                 //      win->Invoke();
199           }
200         
201         // Show the window
202
203         show->bbShow(); 
204         //      std::cout << "KW::GetApplication()->Start();"<<std::endl;
205         //      KW::GetApplication()->Start();
206         //      std::cout << "AFTER KW::GetApplication()->Start();"<<std::endl;
207         
208       }           
209     //   
210     
211   }
212   //=========================================================================
213   
214
215   //==================================================================
216   vtkKWWidget*  KWBlackBox::bbCreateWidgetOfInput(const std::string& in, vtkKWFrame* parent)
217   {
218     vtkKWWidget* w = 0;
219     // If input is connected 
220     BlackBoxInputConnector* c = bbGetInputConnectorMap().find(in)->second ;
221     if ( c->IsConnected() )                     
222       {
223         // Get black box from 
224         BlackBox::Pointer from = 
225           c->GetConnection()->GetBlackBoxFrom();
226         // Cast it into a KWBlackBox
227         KWBlackBox::Pointer wfrom = boost::dynamic_pointer_cast<KWBlackBox>(from);
228         // Call bbCreateWidget
229         wfrom->bbUserCreateWidget(parent);
230         // Get the widget created
231         w = wfrom->bbGetOutputWidget();
232       }
233     return w;
234   }
235   //==================================================================
236
237
238   //==================================================================
239    void KWBlackBox::bbHideWindow()
240   {
241     bbtkDebugMessageInc("kw",1,"=> KWBlackBox::bbHideWindow() ["
242                         <<bbGetFullName()<<"]"<<std::endl);
243
244     if (bbGetWindow()!=0) bbGetWindow()->bbHide();
245
246     bbtkDebugMessageDec("kw",2,"<= KWBlackBox::bbHideWindow() ["
247                         <<bbGetFullName()<<"]"<<std::endl);
248   }
249   //==================================================================
250
251
252   //==================================================================
253    void KWBlackBox::bbCloseWindow()
254   {
255     bbtkDebugMessageInc("kw",1,"=> KWBlackBox::bbCloseWindow() ["
256                         <<bbGetFullName()<<"]"<<std::endl);
257
258     if (bbGetWindow()!=0) bbGetWindow()->bbClose();
259
260     bbtkDebugMessageDec("kw",2,"<= KWBlackBox::bbCloseWindow() ["
261                         <<bbGetFullName()<<"]"<<std::endl);
262   }
263   //==================================================================
264
265   //==================================================================
266   KWBlackBox::Window* KWBlackBox::bbGetContainingWindow()
267   {
268     if (bbGetWindow()!=0) return bbGetWindow();
269     BlackBox::OutputConnectorMapType::const_iterator i 
270       = bbGetOutputConnectorMap().find("Widget");
271     if ( i->second->GetConnectionVector().size() != 0 ) 
272       {
273         return boost::static_pointer_cast<KWBlackBox>
274           (i->second->GetConnectionVector().front() //.lock()
275            ->GetBlackBoxTo())->bbGetContainingWindow();
276       }
277     return 0;
278   }
279   //==================================================================
280
281
282   //==================================================================
283   bool KWBlackBox::bbIsShown()
284   {
285     if (bbGetContainingWindow()!=0)
286       return bbGetContainingWindow()->bbIsShown();
287     return false;
288   }
289   //==================================================================
290
291
292 }//namespace bbtk
293
294
295 #endif
296