]> 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/18 11:54:44 $
6   Version:   $Revision: 1.8 $
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                 /*
158                 KW::GetApplication()->Script
159                   ("pack %s -expand yes -fill both",
160                    bbGetOutputWidget()->GetWidgetName());
161                 */
162                 
163                 KW::GetApplication()->Script("place %s -x 0 -y 0 -width %d -height %d",
164                                              bbGetOutputWidget()->GetWidgetName(),
165                                              bbGetInputWinWidth(),
166                                              bbGetInputWinHeight());
167                 
168                 
169
170
171
172                 /*
173                 bbGetOutputWidget()->SetParent(win);
174                 bbGetOutputWidget()->Create();
175                 KW::GetApplication()->Script("pack %s -side left -anchor c -expand y",
176                         
177                                      //"pack %s -side top -anchor nw -expand y -fill none -padx 2 -pady 2", 
178                                              bbGetOutputWidget()->GetWidgetName());
179                 */                           
180                 /*      
181                   }
182         
183             // Input WinDialog set to false : creating a Frame
184             else 
185               {
186                 bbtkDebugMessage("process",2,
187                                  "   Input WinDialog set to false : creating a Frame"
188                                  <<std::endl);
189                 vtkKWBlackBoxFrame* win = vtkKWBlackBoxFrame::New();
190                 show = (Window*) win;
191                 win->bbSetBlackBox( GetThisPointer<KWBlackBox>());
192                 //              win->SetName( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN" );
193                 //              win->SetWidth( bbGetInputWinWidth() );
194                 //              win->SetHeight( bbGetInputWinHeight() );
195                 KW::GetApplication()->AddWindow((vtkKWWindowBase*)win);
196                 win->Create();
197                 
198                 bbGetOutputWidget()->SetParent(win);
199                 bbGetOutputWidget()->Create();
200                 KW::GetApplication()->Script("pack %s -side top -anchor nw -expand y -fill none -padx 2 -pady 2", 
201                                              bbGetOutputWidget()->GetWidgetName());           }
202         */
203         
204                 //      win->Invoke();
205           }
206         
207         // Show the window
208
209         show->bbShow(); 
210         //      std::cout << "KW::GetApplication()->Start();"<<std::endl;
211         //      KW::GetApplication()->Start();
212         //      std::cout << "AFTER KW::GetApplication()->Start();"<<std::endl;
213         
214       }           
215     //   
216     
217   }
218   //=========================================================================
219   
220
221   //==================================================================
222   vtkKWWidget*  KWBlackBox::bbCreateWidgetOfInput(const std::string& in, vtkKWFrame* parent)
223   {
224     vtkKWWidget* w = 0;
225     // If input is connected 
226     BlackBoxInputConnector* c = bbGetInputConnectorMap().find(in)->second ;
227     if ( c->IsConnected() )                     
228       {
229         // Get black box from 
230         BlackBox::Pointer from = 
231           c->GetConnection()->GetBlackBoxFrom();
232         // Cast it into a KWBlackBox
233         KWBlackBox::Pointer wfrom = boost::dynamic_pointer_cast<KWBlackBox>(from);
234         // Call bbCreateWidget
235         wfrom->bbUserCreateWidget(parent);
236         // Get the widget created
237         w = wfrom->bbGetOutputWidget();
238       }
239     return w;
240   }
241   //==================================================================
242
243
244   //==================================================================
245    void KWBlackBox::bbHideWindow()
246   {
247     bbtkDebugMessageInc("kw",1,"=> KWBlackBox::bbHideWindow() ["
248                         <<bbGetFullName()<<"]"<<std::endl);
249
250     if (bbGetWindow()!=0) bbGetWindow()->bbHide();
251
252     bbtkDebugMessageDec("kw",2,"<= KWBlackBox::bbHideWindow() ["
253                         <<bbGetFullName()<<"]"<<std::endl);
254   }
255   //==================================================================
256
257
258   //==================================================================
259    void KWBlackBox::bbCloseWindow()
260   {
261     bbtkDebugMessageInc("kw",1,"=> KWBlackBox::bbCloseWindow() ["
262                         <<bbGetFullName()<<"]"<<std::endl);
263
264     if (bbGetWindow()!=0) bbGetWindow()->bbClose();
265
266     bbtkDebugMessageDec("kw",2,"<= KWBlackBox::bbCloseWindow() ["
267                         <<bbGetFullName()<<"]"<<std::endl);
268   }
269   //==================================================================
270
271   //==================================================================
272   KWBlackBox::Window* KWBlackBox::bbGetContainingWindow()
273   {
274     if (bbGetWindow()!=0) return bbGetWindow();
275     BlackBox::OutputConnectorMapType::const_iterator i 
276       = bbGetOutputConnectorMap().find("Widget");
277     if ( i->second->GetConnectionVector().size() != 0 ) 
278       {
279         return boost::static_pointer_cast<KWBlackBox>
280           (i->second->GetConnectionVector().front() //.lock()
281            ->GetBlackBoxTo())->bbGetContainingWindow();
282       }
283     return 0;
284   }
285   //==================================================================
286
287
288   //==================================================================
289   bool KWBlackBox::bbIsShown()
290   {
291     if (bbGetContainingWindow()!=0)
292       return bbGetContainingWindow()->bbIsShown();
293     return false;
294   }
295   //==================================================================
296
297
298 }//namespace bbtk
299
300
301 #endif
302