1 /*=========================================================================
3 Module: $RCSfile: bbtkKWBlackBox.cxx,v $
5 Date: $Date: 2008/11/29 21:41:33 $
6 Version: $Revision: 1.1 $
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 "vtkObjectFactory.h"
49 //=========================================================================
50 // vtkKWBlackBoxWindow
51 //=========================================================================
55 //=========================================================================
56 vtkKWBlackBoxWindow::vtkKWBlackBoxWindow()
59 bbtkDebugMessage("kw",9,"vtkKWBlackBoxWindow::vtkKWBlackBoxWindow()"
61 KW::IncNbWindowsAlive();
63 //=========================================================================
67 //=========================================================================
68 vtkKWBlackBoxWindow::~vtkKWBlackBoxWindow()
70 bbtkDebugMessage("kw",9,"vtkKWBlackBoxWindow::~vtkKWBlackBoxWindow() "
73 KW::DecNbWindowsAlive();
76 mBox.lock()->bbSetWindow(0);
79 //=========================================================================
82 //=========================================================================
83 void vtkKWBlackBoxWindow::bbShow()
85 if (bbIsShown()) return;
86 bbtkDebugMessage("kw",9,"vtkKWBlackBoxWindow::bbShow()"<<std::endl);
87 KW::IncNbWindowsShown();
90 //=========================================================================
92 //=========================================================================
93 void vtkKWBlackBoxWindow::bbHide()
95 if (!bbIsShown()) return;
96 bbtkDebugMessage("kw",9,"vtkKWBlackBoxWindow::bbHide()"<<std::endl);
97 KW::DecNbWindowsShown();
100 //=========================================================================
102 //=========================================================================
103 void vtkKWBlackBoxWindow::bbClose()
106 //=========================================================================
109 //=========================================================================
110 // vtkKWBlackBoxDialog
111 //=========================================================================
113 //-------------------------------------------------------------------------
114 vtkStandardNewMacro( vtkKWBlackBoxDialog );
115 vtkCxxRevisionMacro(vtkKWBlackBoxDialog, "$Revision: 1.1 $");
116 //-------------------------------------------------------------------------
118 //=========================================================================
119 vtkKWBlackBoxDialog::vtkKWBlackBoxDialog()
121 bbtkDebugMessage("kw",9,"vtkKWBlackBoxDialog::vtkKWBlackBoxDialog()"
124 //=========================================================================
126 //=========================================================================
127 void vtkKWBlackBoxDialog::bbShow()
129 if (bbIsShown()) return;
130 bbtkDebugMessage("kw",5,"vtkKWBlackBoxDialog::bbShow() ["
131 <<bbGetBlackBox()->bbGetFullName()<<"]"<<std::endl);
132 vtkKWBlackBoxWindow::bbShow();
133 //SetReturnCode( kwDialog::ShowModal() );
137 //=========================================================================
139 //=========================================================================
140 void vtkKWBlackBoxDialog::bbHide()
142 bbtkDebugMessage("kw",9,"vtkKWBlackBoxDialog::bbHide()"<<std::endl);
143 vtkKWBlackBoxWindow::bbHide();
147 //=========================================================================
149 //=========================================================================
150 void vtkKWBlackBoxDialog::bbClose()
152 bbtkDebugMessage("kw",9,"vtkKWBlackBoxDialog::bbClose()"<<std::endl);
153 this->Done = vtkKWDialog::StatusCanceled;
156 //=========================================================================
158 //=========================================================================
159 void vtkKWBlackBoxDialog::Cancel()
161 bbtkDebugMessage("kw",9,"vtkKWBlackBoxDialog::Cancel()"<<std::endl);
164 //=========================================================================
165 //=========================================================================
166 vtkKWBlackBoxDialog::~vtkKWBlackBoxDialog()
169 //=========================================================================
175 //=========================================================================
176 // vtkKWBlackBoxFrame
177 //=========================================================================
179 //-------------------------------------------------------------------------
180 vtkStandardNewMacro( vtkKWBlackBoxFrame );
181 vtkCxxRevisionMacro(vtkKWBlackBoxFrame, "$Revision: 1.1 $");
182 //-------------------------------------------------------------------------
183 //=========================================================================
184 vtkKWBlackBoxFrame::vtkKWBlackBoxFrame()
186 bbtkDebugMessage("kw",9,"vtkKWBlackBoxFrame::vtkKWBlackBoxFrame()"<<std::endl);
188 //=========================================================================
190 //=========================================================================
191 vtkKWBlackBoxFrame::~vtkKWBlackBoxFrame()
194 //=========================================================================
196 //=========================================================================
197 void vtkKWBlackBoxFrame::bbShow()
199 if (bbIsShown()) return;
200 bbtkDebugMessage("kw",5,"vtkKWBlackBoxFrame::bbShow("
201 <<bbGetBlackBox()->bbGetFullName()<<")"<<std::endl);
202 vtkKWBlackBoxWindow::bbShow();
203 // vtkKWFrame::Show();
205 // This Update is ** MANDATORY **
206 // to synchronize kwvtkRenderWindowInteractor objects
207 // (force kw objects creation **NOW**)
217 if (bbGetBlackBox()) bbGetBlackBox()->bbUserOnShow();
219 //=========================================================================
221 //=========================================================================
222 void vtkKWBlackBoxFrame::bbHide()
224 bbtkDebugMessage("kw",9,"vtkKWBlackBoxFrame::bbHide()"<<std::endl);
225 vtkKWBlackBoxWindow::bbHide();
226 // vtkKWFrame::Hide();
228 if (bbGetBlackBox()) bbGetBlackBox()->bbUserOnHide();
230 //=========================================================================
232 //=========================================================================
233 void vtkKWBlackBoxFrame::bbClose()
235 bbtkDebugMessage("kw",9,"vtkKWBlackBoxFrame::bbClose()"<<std::endl);
236 // vtkKWFrame::Close();
239 //=========================================================================
243 //=========================================================================
244 // KWBlackBoxWidgetEventHandler
245 //=========================================================================
247 //=========================================================================
248 KWBlackBoxWidgetEventHandler::
249 KWBlackBoxWidgetEventHandler( KWBlackBox::Pointer box,
250 vtkKWWidget *widget )
255 bbtkDebugMessage("kw",9,"KWBlackBoxWidgetEventHandler::KWBlackBoxWidgetEventHandler("<<mBox.lock()->bbGetFullName()<<")"<<std::endl);
257 mBox.lock()->bbSetWidgetEventHandler(this);
259 Connect ( mWindow->GetId(),
261 (kwObjectEventFunction)
262 (void (kwEvtHandler::*)(kwWindowDestroyEvent& c))
263 &KWBlackBoxWidgetEventHandler::OnWindowDestroy );
265 mWindow->PushEventHandler(this);
268 //=========================================================================
270 //=========================================================================
271 KWBlackBoxWidgetEventHandler::~KWBlackBoxWidgetEventHandler()
273 if (mBox.expired()) return;
274 bbtkDebugMessage("kw",9,
275 "KWBlackBoxWidgetEventHandler::~KWBlackBoxWidgetEventHandler() ["
276 <<mBox.lock()->bbGetFullName()<<"]"<<std::endl);
277 mBox.lock()->bbSetWidgetEventHandler(0);
279 //=========================================================================
281 //=========================================================================
282 void KWBlackBoxWidgetEventHandler::OnWindowDestroy(kwWindowDestroyEvent&)
284 if (mBox.expired()) return;
285 bbtkDebugMessage("kw",9,"KWBlackBoxWidgetEventHandler::OnWindowDestroy() ["
286 <<mBox.lock()->bbGetFullName()<<"]"<<std::endl);
287 mBox.lock()->bbSetOutputWidget(0);
288 mBox.lock()->bbSetModifiedStatus();
290 //=========================================================================
298 //=========================================================================
300 //=========================================================================
302 //=========================================================================
303 //=========================================================================
304 //=========================================================================
305 //=========================================================================
306 BBTK_BLACK_BOX_IMPLEMENTATION(KWBlackBox,AtomicBlackBox);
307 //=========================================================================
309 //=========================================================================
310 void KWBlackBox::bbUserConstructor()
312 bbtkDebugMessage("Kernel",9,"KWBlackBox::bbUserConstructor()"<<std::endl);
315 //=========================================================================
317 //=========================================================================
318 void KWBlackBox::bbUserCopyConstructor(bbtk::BlackBox::Pointer)
320 bbtkDebugMessage("Kernel",9,"KWBlackBox::bbUserCopyConstructor()"
324 //=========================================================================
327 //=========================================================================
328 void KWBlackBox::bbUserDestructor()
330 bbtkDebugMessage("kw",9,"==> KWBlackBox::bbUserDestructor() ["<<bbGetFullName()<<"]"<<std::endl);
332 bbGetWindow()->bbClose();
335 bbtkDebugMessage("kw",9,"<== KWBlackBox::bbUserDestructor() ["<<bbGetFullName()<<"]"<<std::endl);
337 //=========================================================================
341 //=========================================================================
343 * \brief Initialize the attributes of the class
346 void KWBlackBox::bbInitAttributes()
350 // bbSetInputWinParent(0);
351 bbSetInputWinTitle(bbGetName());
352 bbSetInputWinWidth(800);
353 bbSetInputWinHeight(800);
354 bbSetInputWinDialog(false);
355 bbSetOutputWidget(0);
357 // bbSetWidgetEventHandler(0);
358 bbSetUpdateTransferedToParent(false);
360 //=========================================================================
362 //=========================================================================
363 /// Main processing method of the box.
364 void KWBlackBox::bbExecute(bool force)
366 bbtkDebugMessageInc("process",2,
367 "=> KWBlackBox::bbExecute("<<(int)force<<") ["
368 <<bbGetFullName()<<"]"<<std::endl);
370 // If the output 'Widget' is connected then
371 // we must execute the parent box
372 BlackBox::OutputConnectorMapType::const_iterator i
373 = bbGetOutputConnectorMap().find("Widget");
375 if ( i->second->GetConnectionVector().size() != 0 )
377 bbtkDebugMessage("process",3,
378 "-> Output 'Widget' connected : transfering execution to parent"
381 i->second->GetConnectionVector().front() //.lock()
382 ->GetBlackBoxTo()->bbExecute(force);
385 // else call 'standard' BlackBox execution method
388 BlackBox::bbExecute(force);
392 bbtkDebugMessageDec("process",2,
393 "<= KWBlackBox::bbExecute() ["
394 <<bbGetFullName()<<"]"<<std::endl);
396 //=========================================================================
399 //=========================================================================
400 /// Main processing method of the box.
401 IOStatus KWBlackBox::bbBackwardUpdate( Connection::Pointer caller )
403 bbtkDebugMessage("process",3,
404 "=> KWBlackBox::bbBackwardUpdate("
405 <<(caller?caller->GetFullName():"0")<<") ["
406 <<bbGetFullName()<<"]"<<std::endl);
408 if ( ! (( bbGetStatus() == MODIFIED ) ||
409 ( bbBoxProcessModeIsAlways() )) )
411 bbtkDebugMessage("process",3,"Up-to-date : nothing to do"<<std::endl);
412 bbtkDebugMessage("process",3,
413 "<= KWBlackBox::bbBackwardUpdate("
414 <<(caller?caller->GetFullName():"0")<<") ["
415 <<bbGetFullName()<<"]"<<std::endl);
416 return bbGetStatus();
419 // If the caller's box to is not the box to connected to the
422 BlackBox::OutputConnectorMapType::const_iterator i
423 = bbGetOutputConnectorMap().find("Widget") ;
424 if ( i->second->GetConnectionVector().size() != 0 )
427 BlackBox::Pointer to =
428 i->second->GetConnectionVector()[0]->GetBlackBoxTo();
432 bbtkDebugMessage("process",3,
433 "-> Output 'Widget' connected to '"
434 <<to->bbGetFullName()<<"' - caller->to = '"
435 <<caller->GetBlackBoxTo()->bbGetFullName()
441 bbtkDebugMessage("process",3,
442 "-> Output 'Widget' connected to '"
443 <<to->bbGetFullName()<<"'"
448 (caller->GetBlackBoxTo() != to)&&
449 (!bbGetUpdateTransferedToParent())&&
450 (!to->bbGetExecuting())
454 bbtkDebugMessage("process",3,
455 " ... Transfering update order to parent"
458 bbSetUpdateTransferedToParent(true);
459 i->second->GetConnectionVector().front() //.lock()
460 ->GetBlackBoxTo()->bbExecute(false);
464 bbSetUpdateTransferedToParent(false);
465 bbtkDebugMessage("process",3,
466 " ... No need to transfer to parent"
472 // If the caller is not the connection to the output widget
473 // and the output 'Widget' is connected then
474 // we must execute the parent box
476 // (this is the role of the flag UpdateTransferedToParent)
478 ((caller!=0)&&(caller->GetBlackBoxFromOutput()!="Widget"))
483 // call 'standard' BlackBox execution method
484 if (!bbGetUpdateTransferedToParent())
486 AtomicBlackBox::bbBackwardUpdate(caller);
489 bbtkDebugMessageDec("process",3,
490 "<= KWBlackBox::bbBackwardUpdate() ["
491 <<bbGetFullName()<<"]"<<std::endl);
493 return bbGetStatus();
498 //=========================================================================
499 void KWBlackBox::bbProcess()
502 if (bbGetOutputWidget()==0) this->bbUserCreateWidget();
503 this->bbUserProcess();
505 // this->bbUserOnShow();
507 this->bbUserProcess();
509 // If output widget not connected create the window
510 if ( (*bbGetOutputConnectorMap().find("Widget")).second
511 ->GetConnectionVector().size() == 0 )
514 // If the window already exists : no need creating it
515 if (bbGetWindow()!=0)
517 bbtkDebugMessage("kw",2,
518 "-> Window already exists"
520 show = bbGetWindow();
522 // Else create window
525 bbtkDebugMessage("kw",2,
526 "-> Creating the window"
528 KW::GetApplication();
530 // Input WinDialog set to true : creating a Dialog
532 if (bbGetInputWinDialog())
534 bbtkDebugMessage("kw",2,
535 " Input WinDialog set to true : creating a Dialog"
538 vtkKWBlackBoxDialog* win = vtkKWBlackBoxDialog::New();
539 show = (Window*) win;
540 win->bbSetBlackBox( GetThisPointer<KWBlackBox>());
541 //bbGetOutputWidget()->GetWidgetName());
542 // win->SetName( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN" );
543 // win->SetWidth( bbGetInputWinWidth() );
544 // win->SetHeight( bbGetInputWinHeight() );
545 KW::GetApplication()->AddWindow((vtkKWWindowBase*)win);
547 std::cout << "TclName='"<<win->GetTclName()<<"'"<<std::endl;
549 bbUserCreateWidget(win);
550 KW::GetApplication()->Script("pack %s -side left -anchor c -expand y",
551 bbGetOutputWidget()->GetWidgetName());
553 bbGetOutputWidget()->SetParent(win);
554 bbGetOutputWidget()->Create();
555 KW::GetApplication()->Script("pack %s -side left -anchor c -expand y",
557 //"pack %s -side top -anchor nw -expand y -fill none -padx 2 -pady 2",
558 bbGetOutputWidget()->GetWidgetName());
563 // Input WinDialog set to false : creating a Frame
566 bbtkDebugMessage("process",2,
567 " Input WinDialog set to false : creating a Frame"
569 vtkKWBlackBoxFrame* win = vtkKWBlackBoxFrame::New();
570 show = (Window*) win;
571 win->bbSetBlackBox( GetThisPointer<KWBlackBox>());
572 // win->SetName( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN" );
573 // win->SetWidth( bbGetInputWinWidth() );
574 // win->SetHeight( bbGetInputWinHeight() );
575 KW::GetApplication()->AddWindow((vtkKWWindowBase*)win);
578 bbGetOutputWidget()->SetParent(win);
579 bbGetOutputWidget()->Create();
580 KW::GetApplication()->Script("pack %s -side top -anchor nw -expand y -fill none -padx 2 -pady 2",
581 bbGetOutputWidget()->GetWidgetName()); }
590 KW::GetApplication()->Start();
596 //=========================================================================
600 // LG 24/11/08 : New widget pipeline
601 void KWBlackBox::bbCreateWidgetAndEventHandler(vtkKWWidget* parent)
603 if (bbGetOutputWidget()==0)
605 this->bbUserCreateWidget(parent);
608 // If Event Handler for the widget does not exist or is obsolete : create it
610 if (bbGetOutputWidget()!=0)
612 if (bbGetWidgetEventHandler()==0)
614 bbtkDebugMessage("kw",3,
615 "-> No widget event handler : creating one"
617 new KWBlackBoxWidgetEventHandler(GetThisPointer<KWBlackBox>(),
618 bbGetOutputWidget());
620 else if ( ! bbGetWidgetEventHandler()->IsHandlerOf
621 ( bbGetOutputWidget() ) )
623 bbtkDebugMessage("kw",3,
624 "-> Obsolete widget event handler : re-creating one"
626 delete bbGetWidgetEventHandler();
627 new KWBlackBoxWidgetEventHandler(GetThisPointer<KWBlackBox>(),
628 bbGetOutputWidget());
630 // Sets the name of the vtkKWWidget to the input WinTitle
631 bbGetOutputWidget()->SetName(bbtk::std2kw(bbGetInputWinTitle()));
638 vtkKWWidget* KWBlackBox::bbCreateWidgetOfInput(const std::string& in, vtkKWWidget* parent)
641 // If input is connected
642 BlackBoxInputConnector* c = bbGetInputConnectorMap().find(in)->second ;
643 if ( c->IsConnected() )
645 // Get black box from
646 BlackBox::Pointer from =
647 c->GetConnection()->GetBlackBoxFrom();
648 // Cast it into a KWBlackBox
649 KWBlackBox::Pointer wfrom = boost::dynamic_pointer_cast<KWBlackBox>(from);
650 // Call bbCreateWidget
651 wfrom->bbUserCreateWidget(parent);
652 // Get the widget created
653 w = wfrom->bbGetOutputWidget();
660 //==================================================================
661 /// Specific methods for window creation during pipeline execution
662 /// Shows the window associated to the box
663 /// (called after bbProcess during bbExecute)
664 void KWBlackBox::bbShowWindow()
666 bbtkDebugMessageInc("kw",1,"=> KWBlackBox::bbShowWindow() ["
667 <<bbGetFullName()<<"]"<<std::endl);
669 // If Event Handler for the widget does not exist or is obsolete : create it
670 if (bbGetOutputWidget()!=0)
672 if (bbGetWidgetEventHandler()==0)
674 bbtkDebugMessage("kw",3,
675 "-> No widget event handler : creating one"
677 new KWBlackBoxWidgetEventHandler(GetThisPointer<KWBlackBox>(),
678 bbGetOutputWidget());
680 else if ( ! bbGetWidgetEventHandler()->IsHandlerOf
681 ( bbGetOutputWidget() ) )
683 bbtkDebugMessage("kw",3,
684 "-> Obsolete widget event handler : re-creating one"
686 delete bbGetWidgetEventHandler();
687 new KWBlackBoxWidgetEventHandler(GetThisPointer<KWBlackBox>(),
688 bbGetOutputWidget());
690 // Sets the name of the vtkKWWidget to the input WinTitle
691 bbGetOutputWidget()->SetName(bbtk::std2kw(bbGetInputWinTitle()));
694 // If the output 'Widget' is connected then it's gonna
695 // be captured by its parent window : nothing to do
696 if ( (*bbGetOutputConnectorMap().find("Widget")).second
697 ->GetConnectionVector().size() != 0 )
700 bbtkDebugMessage("kw",2,
701 "-> Output 'Widget' connected : nothing to do"
708 // If the window already exists : no need creating it
709 if (bbGetWindow()!=0)
711 bbtkDebugMessage("kw",2,
712 "-> Window already exists"
714 show = bbGetWindow();
716 // Else if the widget exists : create window
717 else if (bbGetOutputWidget()!=0)
719 bbtkDebugMessage("kw",2,
720 "-> Widget exists : creating the window"
724 // Input WinDialog set to true : creating a Dialog
725 if (bbGetInputWinDialog())
727 bbtkDebugMessage("kw",2,
728 " Input WinDialog set to true : creating a Dialog"
730 show = (Window*) new KWBlackBoxDialog( GetThisPointer<KWBlackBox>(),
732 // LG 24/11/08 : New widget pipeline
734 std2kw( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN"),
735 kwSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
737 // Input WinDialog set to false : creating a Frame
740 bbtkDebugMessage("process",2,
741 " Input WinDialog set to false : creating a Frame"
743 show = (Window*) new KWBlackBoxFrame( GetThisPointer<KWBlackBox>(),
745 // LG 24/11/08 : New widget pipeline
747 std2kw( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN"),
748 kwSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
752 // No window nor widget : error
755 bbtkError("KWBlackBox::bbShowWindow() ["
757 <<"] : No widget. Did you set the box output 'Widget' in the processing method of the box ?");
762 if (true) //!show->IsShown())
768 bbtkDebugMessage("kw",2,"-> Already shown : nothing to do"<<std::endl);
772 bbtkDebugMessage("kw",2,"<= KWBlackBox::bbShowWindow() ["
773 <<bbGetFullName()<<"]"<<std::endl);
776 //==================================================================
781 //==================================================================
782 void KWBlackBox::bbHideWindow()
784 bbtkDebugMessageInc("kw",1,"=> KWBlackBox::bbHideWindow() ["
785 <<bbGetFullName()<<"]"<<std::endl);
787 if (bbGetWindow()!=0) bbGetWindow()->bbHide();
789 bbtkDebugMessageDec("kw",2,"<= KWBlackBox::bbHideWindow() ["
790 <<bbGetFullName()<<"]"<<std::endl);
792 //==================================================================
795 //==================================================================
796 void KWBlackBox::bbCloseWindow()
798 bbtkDebugMessageInc("kw",1,"=> KWBlackBox::bbCloseWindow() ["
799 <<bbGetFullName()<<"]"<<std::endl);
801 if (bbGetWindow()!=0) bbGetWindow()->bbClose();
803 bbtkDebugMessageDec("kw",2,"<= KWBlackBox::bbCloseWindow() ["
804 <<bbGetFullName()<<"]"<<std::endl);
806 //==================================================================
808 //==================================================================
809 KWBlackBox::Window* KWBlackBox::bbGetContainingWindow()
811 if (bbGetWindow()!=0) return bbGetWindow();
812 BlackBox::OutputConnectorMapType::const_iterator i
813 = bbGetOutputConnectorMap().find("Widget");
814 if ( i->second->GetConnectionVector().size() != 0 )
816 return boost::static_pointer_cast<KWBlackBox>
817 (i->second->GetConnectionVector().front() //.lock()
818 ->GetBlackBoxTo())->bbGetContainingWindow();
822 //==================================================================
825 //==================================================================
826 // LG 24/11/08 : New widget pipeline
827 // vtkKWWidget* KWBlackBox::bbGetKWParent() { return KW::GetTopWindow(); }
828 //==================================================================
831 //==================================================================
832 bool KWBlackBox::bbIsShown()
834 if (bbGetContainingWindow()!=0)
835 return bbGetContainingWindow()->bbIsShown();
838 //==================================================================