1 /*=========================================================================
3 Module: $RCSfile: bbtkKWBlackBox.cxx,v $
5 Date: $Date: 2008/12/02 08:38:08 $
6 Version: $Revision: 1.2 $
7 =========================================================================*/
9 /* ---------------------------------------------------------------------
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
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.
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
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 * ------------------------------------------------------------------------ */
41 #include "bbtkKWBlackBox.h"
42 #include "vtkKWBlackBoxDialog.h"
51 //=========================================================================
52 // KWBlackBoxWidgetEventHandler
53 //=========================================================================
55 //=========================================================================
56 KWBlackBoxWidgetEventHandler::
57 KWBlackBoxWidgetEventHandler( KWBlackBox::Pointer box,
63 bbtkDebugMessage("kw",9,"KWBlackBoxWidgetEventHandler::KWBlackBoxWidgetEventHandler("<<mBox.lock()->bbGetFullName()<<")"<<std::endl);
65 mBox.lock()->bbSetWidgetEventHandler(this);
67 Connect ( mWindow->GetId(),
69 (kwObjectEventFunction)
70 (void (kwEvtHandler::*)(kwWindowDestroyEvent& c))
71 &KWBlackBoxWidgetEventHandler::OnWindowDestroy );
73 mWindow->PushEventHandler(this);
76 //=========================================================================
78 //=========================================================================
79 KWBlackBoxWidgetEventHandler::~KWBlackBoxWidgetEventHandler()
81 if (mBox.expired()) return;
82 bbtkDebugMessage("kw",9,
83 "KWBlackBoxWidgetEventHandler::~KWBlackBoxWidgetEventHandler() ["
84 <<mBox.lock()->bbGetFullName()<<"]"<<std::endl);
85 mBox.lock()->bbSetWidgetEventHandler(0);
87 //=========================================================================
89 //=========================================================================
90 void KWBlackBoxWidgetEventHandler::OnWindowDestroy(kwWindowDestroyEvent&)
92 if (mBox.expired()) return;
93 bbtkDebugMessage("kw",9,"KWBlackBoxWidgetEventHandler::OnWindowDestroy() ["
94 <<mBox.lock()->bbGetFullName()<<"]"<<std::endl);
95 mBox.lock()->bbSetOutputWidget(0);
96 mBox.lock()->bbSetModifiedStatus();
98 //=========================================================================
106 //=========================================================================
108 //=========================================================================
110 //=========================================================================
111 //=========================================================================
112 //=========================================================================
113 //=========================================================================
114 BBTK_BLACK_BOX_IMPLEMENTATION(KWBlackBox,AtomicBlackBox);
115 //=========================================================================
117 //=========================================================================
118 void KWBlackBox::bbUserConstructor()
120 bbtkDebugMessage("Kernel",9,"KWBlackBox::bbUserConstructor()"<<std::endl);
123 //=========================================================================
125 //=========================================================================
126 void KWBlackBox::bbUserCopyConstructor(bbtk::BlackBox::Pointer)
128 bbtkDebugMessage("Kernel",9,"KWBlackBox::bbUserCopyConstructor()"
132 //=========================================================================
135 //=========================================================================
136 void KWBlackBox::bbUserDestructor()
138 bbtkDebugMessage("kw",9,"==> KWBlackBox::bbUserDestructor() ["<<bbGetFullName()<<"]"<<std::endl);
140 bbGetWindow()->bbClose();
143 bbtkDebugMessage("kw",9,"<== KWBlackBox::bbUserDestructor() ["<<bbGetFullName()<<"]"<<std::endl);
145 //=========================================================================
149 //=========================================================================
151 * \brief Initialize the attributes of the class
154 void KWBlackBox::bbInitAttributes()
158 // bbSetInputWinParent(0);
159 bbSetInputWinTitle(bbGetName());
160 bbSetInputWinWidth(800);
161 bbSetInputWinHeight(800);
162 bbSetInputWinDialog(false);
163 bbSetOutputWidget(0);
165 // bbSetWidgetEventHandler(0);
166 bbSetUpdateTransferedToParent(false);
168 //=========================================================================
170 //=========================================================================
171 /// Main processing method of the box.
172 void KWBlackBox::bbExecute(bool force)
174 bbtkDebugMessageInc("process",2,
175 "=> KWBlackBox::bbExecute("<<(int)force<<") ["
176 <<bbGetFullName()<<"]"<<std::endl);
178 // If the output 'Widget' is connected then
179 // we must execute the parent box
180 BlackBox::OutputConnectorMapType::const_iterator i
181 = bbGetOutputConnectorMap().find("Widget");
183 if ( i->second->GetConnectionVector().size() != 0 )
185 bbtkDebugMessage("process",3,
186 "-> Output 'Widget' connected : transfering execution to parent"
189 i->second->GetConnectionVector().front() //.lock()
190 ->GetBlackBoxTo()->bbExecute(force);
193 // else call 'standard' BlackBox execution method
196 BlackBox::bbExecute(force);
200 bbtkDebugMessageDec("process",2,
201 "<= KWBlackBox::bbExecute() ["
202 <<bbGetFullName()<<"]"<<std::endl);
204 //=========================================================================
207 //=========================================================================
208 /// Main processing method of the box.
209 IOStatus KWBlackBox::bbBackwardUpdate( Connection::Pointer caller )
211 bbtkDebugMessage("process",3,
212 "=> KWBlackBox::bbBackwardUpdate("
213 <<(caller?caller->GetFullName():"0")<<") ["
214 <<bbGetFullName()<<"]"<<std::endl);
216 if ( ! (( bbGetStatus() == MODIFIED ) ||
217 ( bbBoxProcessModeIsAlways() )) )
219 bbtkDebugMessage("process",3,"Up-to-date : nothing to do"<<std::endl);
220 bbtkDebugMessage("process",3,
221 "<= KWBlackBox::bbBackwardUpdate("
222 <<(caller?caller->GetFullName():"0")<<") ["
223 <<bbGetFullName()<<"]"<<std::endl);
224 return bbGetStatus();
227 // If the caller's box to is not the box to connected to the
230 BlackBox::OutputConnectorMapType::const_iterator i
231 = bbGetOutputConnectorMap().find("Widget") ;
232 if ( i->second->GetConnectionVector().size() != 0 )
235 BlackBox::Pointer to =
236 i->second->GetConnectionVector()[0]->GetBlackBoxTo();
240 bbtkDebugMessage("process",3,
241 "-> Output 'Widget' connected to '"
242 <<to->bbGetFullName()<<"' - caller->to = '"
243 <<caller->GetBlackBoxTo()->bbGetFullName()
249 bbtkDebugMessage("process",3,
250 "-> Output 'Widget' connected to '"
251 <<to->bbGetFullName()<<"'"
256 (caller->GetBlackBoxTo() != to)&&
257 (!bbGetUpdateTransferedToParent())&&
258 (!to->bbGetExecuting())
262 bbtkDebugMessage("process",3,
263 " ... Transfering update order to parent"
266 bbSetUpdateTransferedToParent(true);
267 i->second->GetConnectionVector().front() //.lock()
268 ->GetBlackBoxTo()->bbExecute(false);
272 bbSetUpdateTransferedToParent(false);
273 bbtkDebugMessage("process",3,
274 " ... No need to transfer to parent"
280 // If the caller is not the connection to the output widget
281 // and the output 'Widget' is connected then
282 // we must execute the parent box
284 // (this is the role of the flag UpdateTransferedToParent)
286 ((caller!=0)&&(caller->GetBlackBoxFromOutput()!="Widget"))
291 // call 'standard' BlackBox execution method
292 if (!bbGetUpdateTransferedToParent())
294 AtomicBlackBox::bbBackwardUpdate(caller);
297 bbtkDebugMessageDec("process",3,
298 "<= KWBlackBox::bbBackwardUpdate() ["
299 <<bbGetFullName()<<"]"<<std::endl);
301 return bbGetStatus();
306 //=========================================================================
307 void KWBlackBox::bbProcess()
310 if (bbGetOutputWidget()==0) this->bbUserCreateWidget();
311 this->bbUserProcess();
313 // this->bbUserOnShow();
315 this->bbUserProcess();
317 // If output widget not connected create the window
318 if ( (*bbGetOutputConnectorMap().find("Widget")).second
319 ->GetConnectionVector().size() == 0 )
322 // If the window already exists : no need creating it
323 if (bbGetWindow()!=0)
325 bbtkDebugMessage("kw",2,
326 "-> Window already exists"
328 show = bbGetWindow();
330 // Else create window
333 bbtkDebugMessage("kw",2,
334 "-> Creating the window"
336 KW::GetApplication();
338 // Input WinDialog set to true : creating a Dialog
340 if (bbGetInputWinDialog())
342 bbtkDebugMessage("kw",2,
343 " Input WinDialog set to true : creating a Dialog"
346 vtkKWBlackBoxDialog* win = vtkKWBlackBoxDialog::New();
347 show = (Window*) win;
348 win->bbSetBlackBox( GetThisPointer<KWBlackBox>());
349 //bbGetOutputWidget()->GetWidgetName());
350 // win->SetName( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN" );
351 // win->SetWidth( bbGetInputWinWidth() );
352 // win->SetHeight( bbGetInputWinHeight() );
353 KW::GetApplication()->AddWindow((vtkKWWindowBase*)win);
355 // std::cout << "TclName='"<<win->GetTclName()<<"'"<<std::endl;
357 bbUserCreateWidget(win);
358 KW::GetApplication()->Script("pack %s -side top -expand yes",
359 bbGetOutputWidget()->GetWidgetName());
361 bbGetOutputWidget()->SetParent(win);
362 bbGetOutputWidget()->Create();
363 KW::GetApplication()->Script("pack %s -side left -anchor c -expand y",
365 //"pack %s -side top -anchor nw -expand y -fill none -padx 2 -pady 2",
366 bbGetOutputWidget()->GetWidgetName());
371 // Input WinDialog set to false : creating a Frame
374 bbtkDebugMessage("process",2,
375 " Input WinDialog set to false : creating a Frame"
377 vtkKWBlackBoxFrame* win = vtkKWBlackBoxFrame::New();
378 show = (Window*) win;
379 win->bbSetBlackBox( GetThisPointer<KWBlackBox>());
380 // win->SetName( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN" );
381 // win->SetWidth( bbGetInputWinWidth() );
382 // win->SetHeight( bbGetInputWinHeight() );
383 KW::GetApplication()->AddWindow((vtkKWWindowBase*)win);
386 bbGetOutputWidget()->SetParent(win);
387 bbGetOutputWidget()->Create();
388 KW::GetApplication()->Script("pack %s -side top -anchor nw -expand y -fill none -padx 2 -pady 2",
389 bbGetOutputWidget()->GetWidgetName()); }
398 // std::cout << "KW::GetApplication()->Start();"<<std::endl;
399 // KW::GetApplication()->Start();
400 // std::cout << "AFTER KW::GetApplication()->Start();"<<std::endl;
406 //=========================================================================
410 // LG 24/11/08 : New widget pipeline
411 void KWBlackBox::bbCreateWidgetAndEventHandler(vtkKWWidget* parent)
413 if (bbGetOutputWidget()==0)
415 this->bbUserCreateWidget(parent);
418 // If Event Handler for the widget does not exist or is obsolete : create it
420 if (bbGetOutputWidget()!=0)
422 if (bbGetWidgetEventHandler()==0)
424 bbtkDebugMessage("kw",3,
425 "-> No widget event handler : creating one"
427 new KWBlackBoxWidgetEventHandler(GetThisPointer<KWBlackBox>(),
428 bbGetOutputWidget());
430 else if ( ! bbGetWidgetEventHandler()->IsHandlerOf
431 ( bbGetOutputWidget() ) )
433 bbtkDebugMessage("kw",3,
434 "-> Obsolete widget event handler : re-creating one"
436 delete bbGetWidgetEventHandler();
437 new KWBlackBoxWidgetEventHandler(GetThisPointer<KWBlackBox>(),
438 bbGetOutputWidget());
440 // Sets the name of the vtkKWWidget to the input WinTitle
441 bbGetOutputWidget()->SetName(bbtk::std2kw(bbGetInputWinTitle()));
448 vtkKWWidget* KWBlackBox::bbCreateWidgetOfInput(const std::string& in, vtkKWWidget* parent)
451 // If input is connected
452 BlackBoxInputConnector* c = bbGetInputConnectorMap().find(in)->second ;
453 if ( c->IsConnected() )
455 // Get black box from
456 BlackBox::Pointer from =
457 c->GetConnection()->GetBlackBoxFrom();
458 // Cast it into a KWBlackBox
459 KWBlackBox::Pointer wfrom = boost::dynamic_pointer_cast<KWBlackBox>(from);
460 // Call bbCreateWidget
461 wfrom->bbUserCreateWidget(parent);
462 // Get the widget created
463 w = wfrom->bbGetOutputWidget();
470 //==================================================================
471 /// Specific methods for window creation during pipeline execution
472 /// Shows the window associated to the box
473 /// (called after bbProcess during bbExecute)
474 void KWBlackBox::bbShowWindow()
476 bbtkDebugMessageInc("kw",1,"=> KWBlackBox::bbShowWindow() ["
477 <<bbGetFullName()<<"]"<<std::endl);
479 // If Event Handler for the widget does not exist or is obsolete : create it
480 if (bbGetOutputWidget()!=0)
482 if (bbGetWidgetEventHandler()==0)
484 bbtkDebugMessage("kw",3,
485 "-> No widget event handler : creating one"
487 new KWBlackBoxWidgetEventHandler(GetThisPointer<KWBlackBox>(),
488 bbGetOutputWidget());
490 else if ( ! bbGetWidgetEventHandler()->IsHandlerOf
491 ( bbGetOutputWidget() ) )
493 bbtkDebugMessage("kw",3,
494 "-> Obsolete widget event handler : re-creating one"
496 delete bbGetWidgetEventHandler();
497 new KWBlackBoxWidgetEventHandler(GetThisPointer<KWBlackBox>(),
498 bbGetOutputWidget());
500 // Sets the name of the vtkKWWidget to the input WinTitle
501 bbGetOutputWidget()->SetName(bbtk::std2kw(bbGetInputWinTitle()));
504 // If the output 'Widget' is connected then it's gonna
505 // be captured by its parent window : nothing to do
506 if ( (*bbGetOutputConnectorMap().find("Widget")).second
507 ->GetConnectionVector().size() != 0 )
510 bbtkDebugMessage("kw",2,
511 "-> Output 'Widget' connected : nothing to do"
518 // If the window already exists : no need creating it
519 if (bbGetWindow()!=0)
521 bbtkDebugMessage("kw",2,
522 "-> Window already exists"
524 show = bbGetWindow();
526 // Else if the widget exists : create window
527 else if (bbGetOutputWidget()!=0)
529 bbtkDebugMessage("kw",2,
530 "-> Widget exists : creating the window"
534 // Input WinDialog set to true : creating a Dialog
535 if (bbGetInputWinDialog())
537 bbtkDebugMessage("kw",2,
538 " Input WinDialog set to true : creating a Dialog"
540 show = (Window*) new KWBlackBoxDialog( GetThisPointer<KWBlackBox>(),
542 // LG 24/11/08 : New widget pipeline
544 std2kw( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN"),
545 kwSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
547 // Input WinDialog set to false : creating a Frame
550 bbtkDebugMessage("process",2,
551 " Input WinDialog set to false : creating a Frame"
553 show = (Window*) new KWBlackBoxFrame( GetThisPointer<KWBlackBox>(),
555 // LG 24/11/08 : New widget pipeline
557 std2kw( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN"),
558 kwSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
562 // No window nor widget : error
565 bbtkError("KWBlackBox::bbShowWindow() ["
567 <<"] : No widget. Did you set the box output 'Widget' in the processing method of the box ?");
572 if (true) //!show->IsShown())
578 bbtkDebugMessage("kw",2,"-> Already shown : nothing to do"<<std::endl);
582 bbtkDebugMessage("kw",2,"<= KWBlackBox::bbShowWindow() ["
583 <<bbGetFullName()<<"]"<<std::endl);
586 //==================================================================
591 //==================================================================
592 void KWBlackBox::bbHideWindow()
594 bbtkDebugMessageInc("kw",1,"=> KWBlackBox::bbHideWindow() ["
595 <<bbGetFullName()<<"]"<<std::endl);
597 if (bbGetWindow()!=0) bbGetWindow()->bbHide();
599 bbtkDebugMessageDec("kw",2,"<= KWBlackBox::bbHideWindow() ["
600 <<bbGetFullName()<<"]"<<std::endl);
602 //==================================================================
605 //==================================================================
606 void KWBlackBox::bbCloseWindow()
608 bbtkDebugMessageInc("kw",1,"=> KWBlackBox::bbCloseWindow() ["
609 <<bbGetFullName()<<"]"<<std::endl);
611 if (bbGetWindow()!=0) bbGetWindow()->bbClose();
613 bbtkDebugMessageDec("kw",2,"<= KWBlackBox::bbCloseWindow() ["
614 <<bbGetFullName()<<"]"<<std::endl);
616 //==================================================================
618 //==================================================================
619 KWBlackBox::Window* KWBlackBox::bbGetContainingWindow()
621 if (bbGetWindow()!=0) return bbGetWindow();
622 BlackBox::OutputConnectorMapType::const_iterator i
623 = bbGetOutputConnectorMap().find("Widget");
624 if ( i->second->GetConnectionVector().size() != 0 )
626 return boost::static_pointer_cast<KWBlackBox>
627 (i->second->GetConnectionVector().front() //.lock()
628 ->GetBlackBoxTo())->bbGetContainingWindow();
632 //==================================================================
635 //==================================================================
636 // LG 24/11/08 : New widget pipeline
637 // vtkKWWidget* KWBlackBox::bbGetKWParent() { return KW::GetTopWindow(); }
638 //==================================================================
641 //==================================================================
642 bool KWBlackBox::bbIsShown()
644 if (bbGetContainingWindow()!=0)
645 return bbGetContainingWindow()->bbIsShown();
648 //==================================================================