1 /*=========================================================================
3 Module: $RCSfile: bbtkKWBlackBox.cxx,v $
5 Date: $Date: 2008/12/03 09:38:02 $
6 Version: $Revision: 1.5 $
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);
197 // else call 'standard' BlackBox execution method
200 AtomicBlackBox::bbExecute(force);
204 bbtkDebugMessageDec("process",2,
205 "<= KWBlackBox::bbExecute() ["
206 <<bbGetFullName()<<"]"<<std::endl);
208 //=========================================================================
211 //=========================================================================
212 /// Main processing method of the box.
213 IOStatus KWBlackBox::bbBackwardUpdate( Connection::Pointer caller )
216 bbtkDebugMessage("process",3,
217 "=> KWBlackBox::bbBackwardUpdate("
218 <<(caller?caller->GetFullName():"0")<<") ["
219 <<bbGetFullName()<<"]"<<std::endl);
221 if ( ! (( bbGetStatus() == MODIFIED ) ||
222 ( bbBoxProcessModeIsAlways() )) )
224 bbtkDebugMessage("process",3,"Up-to-date : nothing to do"<<std::endl);
225 bbtkDebugMessage("process",3,
226 "<= KWBlackBox::bbBackwardUpdate("
227 <<(caller?caller->GetFullName():"0")<<") ["
228 <<bbGetFullName()<<"]"<<std::endl);
229 return bbGetStatus();
232 // If the caller's box to is not the box to connected to the
235 BlackBox::OutputConnectorMapType::const_iterator i
236 = bbGetOutputConnectorMap().find("Widget") ;
237 if ( i->second->GetConnectionVector().size() != 0 )
240 BlackBox::Pointer to =
241 i->second->GetConnectionVector()[0]->GetBlackBoxTo();
245 bbtkDebugMessage("process",3,
246 "-> Output 'Widget' connected to '"
247 <<to->bbGetFullName()<<"' - caller->to = '"
248 <<caller->GetBlackBoxTo()->bbGetFullName()
254 bbtkDebugMessage("process",3,
255 "-> Output 'Widget' connected to '"
256 <<to->bbGetFullName()<<"'"
261 (caller->GetBlackBoxTo() != to)&&
262 (!bbGetUpdateTransferedToParent())&&
263 (!to->bbGetExecuting())
267 bbtkDebugMessage("process",3,
268 " ... Transfering update order to parent"
271 bbSetUpdateTransferedToParent(true);
272 i->second->GetConnectionVector().front() //.lock()
273 ->GetBlackBoxTo()->bbExecute(false);
277 bbSetUpdateTransferedToParent(false);
278 bbtkDebugMessage("process",3,
279 " ... No need to transfer to parent"
287 // If the caller is not the connection to the output widget
288 // and the output 'Widget' is connected then
289 // we must execute the parent box
291 // (this is the role of the flag UpdateTransferedToParent)
293 ((caller!=0)&&(caller->GetBlackBoxFromOutput()!="Widget"))
298 // call 'standard' BlackBox execution method
299 //if (!bbGetUpdateTransferedToParent())
301 return AtomicBlackBox::bbBackwardUpdate(caller);
304 bbtkDebugMessageDec("process",3,
305 "<= KWBlackBox::bbBackwardUpdate() ["
306 <<bbGetFullName()<<"]"<<std::endl);
308 return bbGetStatus();
313 //=========================================================================
314 void KWBlackBox::bbProcess()
317 if (bbGetOutputWidget()==0) this->bbUserCreateWidget();
318 this->bbUserProcess();
320 // this->bbUserOnShow();
322 this->bbUserProcess();
324 // If output widget not connected create the window
325 if ( (*bbGetOutputConnectorMap().find("Widget")).second
326 ->GetConnectionVector().size() == 0 )
329 // If the window already exists : no need creating it
330 if (bbGetWindow()!=0)
332 bbtkDebugMessage("kw",2,
333 "-> Window already exists"
335 show = bbGetWindow();
337 // Else create window
340 bbtkDebugMessage("kw",2,
341 "-> Creating the window"
343 KW::GetApplication();
345 // Input WinDialog set to true : creating a Dialog
347 if (bbGetInputWinDialog())
349 bbtkDebugMessage("kw",2,
350 " Input WinDialog set to true : creating a Dialog"
353 vtkKWBlackBoxDialog* win = vtkKWBlackBoxDialog::New();
354 show = (Window*) win;
355 win->bbSetBlackBox( GetThisPointer<KWBlackBox>());
356 KW::GetApplication()->AddWindow((vtkKWWindowBase*)win);
358 win->SetResizable(0,0);
360 bbUserCreateWidget(win->GetFrame());
361 KW::GetApplication()->Script("place %s -x 0 -y 0 -width %d -height %d",
362 bbGetOutputWidget()->GetWidgetName(),
363 bbGetInputWinWidth(),
364 bbGetInputWinHeight());
371 bbGetOutputWidget()->SetParent(win);
372 bbGetOutputWidget()->Create();
373 KW::GetApplication()->Script("pack %s -side left -anchor c -expand y",
375 //"pack %s -side top -anchor nw -expand y -fill none -padx 2 -pady 2",
376 bbGetOutputWidget()->GetWidgetName());
381 // Input WinDialog set to false : creating a Frame
384 bbtkDebugMessage("process",2,
385 " Input WinDialog set to false : creating a Frame"
387 vtkKWBlackBoxFrame* win = vtkKWBlackBoxFrame::New();
388 show = (Window*) win;
389 win->bbSetBlackBox( GetThisPointer<KWBlackBox>());
390 // win->SetName( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN" );
391 // win->SetWidth( bbGetInputWinWidth() );
392 // win->SetHeight( bbGetInputWinHeight() );
393 KW::GetApplication()->AddWindow((vtkKWWindowBase*)win);
396 bbGetOutputWidget()->SetParent(win);
397 bbGetOutputWidget()->Create();
398 KW::GetApplication()->Script("pack %s -side top -anchor nw -expand y -fill none -padx 2 -pady 2",
399 bbGetOutputWidget()->GetWidgetName()); }
408 // std::cout << "KW::GetApplication()->Start();"<<std::endl;
409 // KW::GetApplication()->Start();
410 // std::cout << "AFTER KW::GetApplication()->Start();"<<std::endl;
416 //=========================================================================
419 //=========================================================================
420 void KWBlackBox::bbPlaceWidget()
422 vtkKWFrame* parent = (vtkKWFrame*)(bbGetOutputWidget()->GetParent());
423 bbGetOutputWidget->GetApplication()->Script("place %s -x 0 -y 0 -width %s -height %s",
424 bbGetOutputWidget()->GetWidgetName(),
426 parent->GetHeight());
428 //=========================================================================
432 // LG 24/11/08 : New widget pipeline
433 void KWBlackBox::bbCreateWidgetAndEventHandler(vtkKWWidget* parent)
435 if (bbGetOutputWidget()==0)
437 this->bbUserCreateWidget(parent);
440 // If Event Handler for the widget does not exist or is obsolete : create it
442 if (bbGetOutputWidget()!=0)
444 if (bbGetWidgetEventHandler()==0)
446 bbtkDebugMessage("kw",3,
447 "-> No widget event handler : creating one"
449 new KWBlackBoxWidgetEventHandler(GetThisPointer<KWBlackBox>(),
450 bbGetOutputWidget());
452 else if ( ! bbGetWidgetEventHandler()->IsHandlerOf
453 ( bbGetOutputWidget() ) )
455 bbtkDebugMessage("kw",3,
456 "-> Obsolete widget event handler : re-creating one"
458 delete bbGetWidgetEventHandler();
459 new KWBlackBoxWidgetEventHandler(GetThisPointer<KWBlackBox>(),
460 bbGetOutputWidget());
462 // Sets the name of the vtkKWWidget to the input WinTitle
463 bbGetOutputWidget()->SetName(bbtk::std2kw(bbGetInputWinTitle()));
470 vtkKWWidget* KWBlackBox::bbCreateWidgetOfInput(const std::string& in, vtkKWFrame* parent)
473 // If input is connected
474 BlackBoxInputConnector* c = bbGetInputConnectorMap().find(in)->second ;
475 if ( c->IsConnected() )
477 // Get black box from
478 BlackBox::Pointer from =
479 c->GetConnection()->GetBlackBoxFrom();
480 // Cast it into a KWBlackBox
481 KWBlackBox::Pointer wfrom = boost::dynamic_pointer_cast<KWBlackBox>(from);
482 // Call bbCreateWidget
483 wfrom->bbUserCreateWidget(parent);
484 // Get the widget created
485 w = wfrom->bbGetOutputWidget();
492 //==================================================================
493 /// Specific methods for window creation during pipeline execution
494 /// Shows the window associated to the box
495 /// (called after bbProcess during bbExecute)
496 void KWBlackBox::bbShowWindow()
498 bbtkDebugMessageInc("kw",1,"=> KWBlackBox::bbShowWindow() ["
499 <<bbGetFullName()<<"]"<<std::endl);
501 // If Event Handler for the widget does not exist or is obsolete : create it
502 if (bbGetOutputWidget()!=0)
504 if (bbGetWidgetEventHandler()==0)
506 bbtkDebugMessage("kw",3,
507 "-> No widget event handler : creating one"
509 new KWBlackBoxWidgetEventHandler(GetThisPointer<KWBlackBox>(),
510 bbGetOutputWidget());
512 else if ( ! bbGetWidgetEventHandler()->IsHandlerOf
513 ( bbGetOutputWidget() ) )
515 bbtkDebugMessage("kw",3,
516 "-> Obsolete widget event handler : re-creating one"
518 delete bbGetWidgetEventHandler();
519 new KWBlackBoxWidgetEventHandler(GetThisPointer<KWBlackBox>(),
520 bbGetOutputWidget());
522 // Sets the name of the vtkKWWidget to the input WinTitle
523 bbGetOutputWidget()->SetName(bbtk::std2kw(bbGetInputWinTitle()));
526 // If the output 'Widget' is connected then it's gonna
527 // be captured by its parent window : nothing to do
528 if ( (*bbGetOutputConnectorMap().find("Widget")).second
529 ->GetConnectionVector().size() != 0 )
532 bbtkDebugMessage("kw",2,
533 "-> Output 'Widget' connected : nothing to do"
540 // If the window already exists : no need creating it
541 if (bbGetWindow()!=0)
543 bbtkDebugMessage("kw",2,
544 "-> Window already exists"
546 show = bbGetWindow();
548 // Else if the widget exists : create window
549 else if (bbGetOutputWidget()!=0)
551 bbtkDebugMessage("kw",2,
552 "-> Widget exists : creating the window"
556 // Input WinDialog set to true : creating a Dialog
557 if (bbGetInputWinDialog())
559 bbtkDebugMessage("kw",2,
560 " Input WinDialog set to true : creating a Dialog"
562 show = (Window*) new KWBlackBoxDialog( GetThisPointer<KWBlackBox>(),
564 // LG 24/11/08 : New widget pipeline
566 std2kw( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN"),
567 kwSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
569 // Input WinDialog set to false : creating a Frame
572 bbtkDebugMessage("process",2,
573 " Input WinDialog set to false : creating a Frame"
575 show = (Window*) new KWBlackBoxFrame( GetThisPointer<KWBlackBox>(),
577 // LG 24/11/08 : New widget pipeline
579 std2kw( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN"),
580 kwSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
584 // No window nor widget : error
587 bbtkError("KWBlackBox::bbShowWindow() ["
589 <<"] : No widget. Did you set the box output 'Widget' in the processing method of the box ?");
594 if (true) //!show->IsShown())
600 bbtkDebugMessage("kw",2,"-> Already shown : nothing to do"<<std::endl);
604 bbtkDebugMessage("kw",2,"<= KWBlackBox::bbShowWindow() ["
605 <<bbGetFullName()<<"]"<<std::endl);
608 //==================================================================
613 //==================================================================
614 void KWBlackBox::bbHideWindow()
616 bbtkDebugMessageInc("kw",1,"=> KWBlackBox::bbHideWindow() ["
617 <<bbGetFullName()<<"]"<<std::endl);
619 if (bbGetWindow()!=0) bbGetWindow()->bbHide();
621 bbtkDebugMessageDec("kw",2,"<= KWBlackBox::bbHideWindow() ["
622 <<bbGetFullName()<<"]"<<std::endl);
624 //==================================================================
627 //==================================================================
628 void KWBlackBox::bbCloseWindow()
630 bbtkDebugMessageInc("kw",1,"=> KWBlackBox::bbCloseWindow() ["
631 <<bbGetFullName()<<"]"<<std::endl);
633 if (bbGetWindow()!=0) bbGetWindow()->bbClose();
635 bbtkDebugMessageDec("kw",2,"<= KWBlackBox::bbCloseWindow() ["
636 <<bbGetFullName()<<"]"<<std::endl);
638 //==================================================================
640 //==================================================================
641 KWBlackBox::Window* KWBlackBox::bbGetContainingWindow()
643 if (bbGetWindow()!=0) return bbGetWindow();
644 BlackBox::OutputConnectorMapType::const_iterator i
645 = bbGetOutputConnectorMap().find("Widget");
646 if ( i->second->GetConnectionVector().size() != 0 )
648 return boost::static_pointer_cast<KWBlackBox>
649 (i->second->GetConnectionVector().front() //.lock()
650 ->GetBlackBoxTo())->bbGetContainingWindow();
654 //==================================================================
657 //==================================================================
658 // LG 24/11/08 : New widget pipeline
659 // vtkKWWidget* KWBlackBox::bbGetKWParent() { return KW::GetTopWindow(); }
660 //==================================================================
663 //==================================================================
664 bool KWBlackBox::bbIsShown()
666 if (bbGetContainingWindow()!=0)
667 return bbGetContainingWindow()->bbIsShown();
670 //==================================================================