]> Creatis software - bbtk.git/blob - kernel/src/bbtkKWBlackBox.cxx
3df2005599c64c1f11bb3940eebb209baccee6d4
[bbtk.git] / kernel / src / bbtkKWBlackBox.cxx
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbtkKWBlackBox.cxx,v $
4   Language:  C++
5   Date:      $Date: 2008/12/02 13:37:54 $
6   Version:   $Revision: 1.4 $
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 "vtkKWBlackBoxDialog.h"
43
44
45 namespace bbtk
46 {
47
48
49
50   /*
51   //=========================================================================
52   // KWBlackBoxWidgetEventHandler
53   //=========================================================================
54
55   //=========================================================================
56   KWBlackBoxWidgetEventHandler::
57   KWBlackBoxWidgetEventHandler( KWBlackBox::Pointer box, 
58                                 vtkKWWidget *widget )
59     :
60     mBox(box),
61     mWindow(widget)
62   { 
63     bbtkDebugMessage("kw",9,"KWBlackBoxWidgetEventHandler::KWBlackBoxWidgetEventHandler("<<mBox.lock()->bbGetFullName()<<")"<<std::endl);
64
65     mBox.lock()->bbSetWidgetEventHandler(this);
66
67     Connect (  mWindow->GetId(),
68                kwEVT_DESTROY,
69                (kwObjectEventFunction) 
70                (void (kwEvtHandler::*)(kwWindowDestroyEvent& c))
71                 &KWBlackBoxWidgetEventHandler::OnWindowDestroy );
72     
73     mWindow->PushEventHandler(this);
74     
75   }
76   //=========================================================================
77
78   //=========================================================================
79   KWBlackBoxWidgetEventHandler::~KWBlackBoxWidgetEventHandler()
80   {
81     if (mBox.expired()) return;
82     bbtkDebugMessage("kw",9,
83                      "KWBlackBoxWidgetEventHandler::~KWBlackBoxWidgetEventHandler() ["
84                      <<mBox.lock()->bbGetFullName()<<"]"<<std::endl);
85      mBox.lock()->bbSetWidgetEventHandler(0);   
86   }
87   //=========================================================================
88
89   //=========================================================================
90   void KWBlackBoxWidgetEventHandler::OnWindowDestroy(kwWindowDestroyEvent&)
91   {
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();
97   }
98   //=========================================================================
99
100
101   */
102
103
104
105
106   //=========================================================================
107   // KWBlackBox
108   //=========================================================================
109
110   //=========================================================================
111   //=========================================================================
112   //=========================================================================
113   //=========================================================================
114   BBTK_BLACK_BOX_IMPLEMENTATION(KWBlackBox,AtomicBlackBox);
115   //=========================================================================
116   
117   //=========================================================================
118   void KWBlackBox::bbUserConstructor()
119   {
120     bbtkDebugMessage("Kernel",9,"KWBlackBox::bbUserConstructor()"<<std::endl);
121     bbInitAttributes();
122   }
123   //=========================================================================
124
125   //=========================================================================
126   void KWBlackBox::bbUserCopyConstructor(bbtk::BlackBox::Pointer)
127   {
128     bbtkDebugMessage("Kernel",9,"KWBlackBox::bbUserCopyConstructor()"
129                      <<std::endl);
130     bbInitAttributes();
131   }
132   //=========================================================================
133
134
135   //=========================================================================
136   void KWBlackBox::bbUserDestructor()
137   {
138     bbtkDebugMessage("kw",9,"==> KWBlackBox::bbUserDestructor() ["<<bbGetFullName()<<"]"<<std::endl);
139     if (bbGetWindow()) {
140       bbGetWindow()->bbClose();
141       bbSetWindow(0);
142     }
143     bbtkDebugMessage("kw",9,"<== KWBlackBox::bbUserDestructor() ["<<bbGetFullName()<<"]"<<std::endl);
144   }
145   //=========================================================================
146   
147
148
149   //=========================================================================
150   /**
151    * \brief Initialize the attributes of the class
152    *
153    */
154   void KWBlackBox::bbInitAttributes()
155   {
156     bbmWindow = 0;
157     //    bbmWidget = 0;
158     //    bbSetInputWinParent(0);
159     bbSetInputWinTitle(bbGetName());
160     bbSetInputWinWidth(800);
161     bbSetInputWinHeight(800);
162     bbSetInputWinDialog(false);
163     bbSetOutputWidget(0);
164
165     //    bbSetWidgetEventHandler(0);
166     bbSetUpdateTransferedToParent(false);
167   }
168   //=========================================================================
169
170   //=========================================================================
171   /// Main processing method of the box.
172   void KWBlackBox::bbExecute(bool force)
173   {
174     bbtkDebugMessageInc("process",2,
175                         "=> KWBlackBox::bbExecute("<<(int)force<<") ["
176                         <<bbGetFullName()<<"]"<<std::endl);
177
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");
182
183     if ( i->second->GetConnectionVector().size() != 0 ) 
184       {
185         bbtkDebugMessage("process",3,
186                          "-> Output 'Widget' connected : transfering execution to parent"
187                          <<std::endl);
188         
189         i->second->GetConnectionVector().front() //.lock()
190           ->GetBlackBoxTo()->bbExecute(force);
191
192       }
193     // else call 'standard' BlackBox execution method
194     else 
195       {
196         BlackBox::bbExecute(force);
197       }
198     //
199
200     bbtkDebugMessageDec("process",2,
201                         "<= KWBlackBox::bbExecute() ["
202                         <<bbGetFullName()<<"]"<<std::endl);
203   }
204   //=========================================================================
205
206
207   //=========================================================================
208   /// Main processing method of the box.
209   IOStatus KWBlackBox::bbBackwardUpdate( Connection::Pointer caller )
210   {
211     bbtkDebugMessage("process",3,
212                      "=> KWBlackBox::bbBackwardUpdate("
213                      <<(caller?caller->GetFullName():"0")<<") ["
214                      <<bbGetFullName()<<"]"<<std::endl);
215
216     if ( ! (( bbGetStatus() == MODIFIED ) ||
217             ( bbBoxProcessModeIsAlways() )) )
218       {
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();
225       }
226  
227     // If the caller's box to is not the box to connected to the 
228     // output 'Widget'
229
230     BlackBox::OutputConnectorMapType::const_iterator i 
231       = bbGetOutputConnectorMap().find("Widget") ;
232     if ( i->second->GetConnectionVector().size() != 0 )
233       
234       {
235         BlackBox::Pointer to = 
236           i->second->GetConnectionVector()[0]->GetBlackBoxTo();
237                 
238         if (caller) 
239           {
240             bbtkDebugMessage("process",3,
241                              "-> Output 'Widget' connected to '"
242                              <<to->bbGetFullName()<<"' - caller->to = '"
243                              <<caller->GetBlackBoxTo()->bbGetFullName()
244                              <<"'"
245                              <<std::endl);
246           }
247         else
248           {
249             bbtkDebugMessage("process",3,
250                              "-> Output 'Widget' connected to '"
251                              <<to->bbGetFullName()<<"'"
252                              <<std::endl);
253           }
254         if ((caller==0) ||
255             ( (caller!=0) && 
256               (caller->GetBlackBoxTo() != to)&&
257               (!bbGetUpdateTransferedToParent())&&
258               (!to->bbGetExecuting()) 
259               )
260             )
261           {
262             bbtkDebugMessage("process",3,
263                              "   ... Transfering update order to parent"
264                              <<std::endl);
265             
266             bbSetUpdateTransferedToParent(true);
267             i->second->GetConnectionVector().front() //.lock()
268               ->GetBlackBoxTo()->bbExecute(false);
269           }
270         else
271           {
272             bbSetUpdateTransferedToParent(false);
273             bbtkDebugMessage("process",3,
274                              "   ... No need to transfer to parent"
275                              <<std::endl);
276           }
277       }
278     /*    
279
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 
283     // but only one time 
284     // (this is the role of the flag UpdateTransferedToParent)
285     if ( (caller==0) ||
286          ((caller!=0)&&(caller->GetBlackBoxFromOutput()!="Widget"))
287          )
288       {
289       }
290     */
291     // call 'standard' BlackBox execution method
292     if (!bbGetUpdateTransferedToParent()) 
293       { 
294           AtomicBlackBox::bbBackwardUpdate(caller);
295       }
296     
297     bbtkDebugMessageDec("process",3,
298                         "<= KWBlackBox::bbBackwardUpdate() ["
299                         <<bbGetFullName()<<"]"<<std::endl);
300     
301    return bbGetStatus();
302      
303
304   }
305
306   //=========================================================================
307   void KWBlackBox::bbProcess()
308   { 
309 /*
310           if (bbGetOutputWidget()==0) this->bbUserCreateWidget();
311     this->bbUserProcess(); 
312     bbShowWindow();
313     //    this->bbUserOnShow();
314 */
315     this->bbUserProcess(); 
316
317     // If output widget not connected create the window 
318     if ( (*bbGetOutputConnectorMap().find("Widget")).second
319          ->GetConnectionVector().size() == 0 ) 
320       {
321         Window* show = 0;
322         // If the window already exists : no need creating it
323         if (bbGetWindow()!=0)
324           {
325             bbtkDebugMessage("kw",2,
326                              "-> Window already exists"
327                              <<std::endl);
328             show = bbGetWindow();
329           }
330         // Else create window 
331         else 
332           {
333             bbtkDebugMessage("kw",2,
334                              "-> Creating the window"
335                              <<std::endl);
336             KW::GetApplication();
337             
338             // Input WinDialog set to true : creating a Dialog
339             /*
340             if (bbGetInputWinDialog()) 
341               {
342                 bbtkDebugMessage("kw",2,
343                                  "   Input WinDialog set to true : creating a Dialog"
344                                  <<std::endl);
345             */
346                 vtkKWBlackBoxDialog* win = vtkKWBlackBoxDialog::New();
347                 show = (Window*) win;
348                 win->bbSetBlackBox( GetThisPointer<KWBlackBox>());
349                 KW::GetApplication()->AddWindow((vtkKWWindowBase*)win);
350                 win->Create();
351                 win->SetResizable(0,0);
352
353                 bbUserCreateWidget(win->GetFrame());            
354                 KW::GetApplication()->Script("place %s -x 0 -y 0 -width %d -height %d",
355                                              bbGetOutputWidget()->GetWidgetName(),
356                                              bbGetInputWinWidth(),
357                                              bbGetInputWinHeight());
358                 
359
360
361
362
363                 /*
364                 bbGetOutputWidget()->SetParent(win);
365                 bbGetOutputWidget()->Create();
366                 KW::GetApplication()->Script("pack %s -side left -anchor c -expand y",
367                         
368                                      //"pack %s -side top -anchor nw -expand y -fill none -padx 2 -pady 2", 
369                                              bbGetOutputWidget()->GetWidgetName());
370                 */                           
371                 /*      
372                   }
373         
374             // Input WinDialog set to false : creating a Frame
375             else 
376               {
377                 bbtkDebugMessage("process",2,
378                                  "   Input WinDialog set to false : creating a Frame"
379                                  <<std::endl);
380                 vtkKWBlackBoxFrame* win = vtkKWBlackBoxFrame::New();
381                 show = (Window*) win;
382                 win->bbSetBlackBox( GetThisPointer<KWBlackBox>());
383                 //              win->SetName( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN" );
384                 //              win->SetWidth( bbGetInputWinWidth() );
385                 //              win->SetHeight( bbGetInputWinHeight() );
386                 KW::GetApplication()->AddWindow((vtkKWWindowBase*)win);
387                 win->Create();
388                 
389                 bbGetOutputWidget()->SetParent(win);
390                 bbGetOutputWidget()->Create();
391                 KW::GetApplication()->Script("pack %s -side top -anchor nw -expand y -fill none -padx 2 -pady 2", 
392                                              bbGetOutputWidget()->GetWidgetName());           }
393         */
394         
395                 //      win->Invoke();
396           }
397         
398         // Show the window
399
400         show->bbShow(); 
401         //      std::cout << "KW::GetApplication()->Start();"<<std::endl;
402         //      KW::GetApplication()->Start();
403         //      std::cout << "AFTER KW::GetApplication()->Start();"<<std::endl;
404         
405       }           
406     //   
407     
408   }
409   //=========================================================================
410   
411   /*
412   //=========================================================================
413   void KWBlackBox::bbPlaceWidget()
414   {
415     vtkKWFrame* parent = (vtkKWFrame*)(bbGetOutputWidget()->GetParent());
416     bbGetOutputWidget->GetApplication()->Script("place %s -x 0 -y 0 -width %s -height %s",
417                                                 bbGetOutputWidget()->GetWidgetName(),
418                                                 parent->GetWidth(),
419                                                 parent->GetHeight());
420   }
421   //=========================================================================
422   */
423
424   /*
425   // LG 24/11/08 : New widget pipeline
426   void KWBlackBox::bbCreateWidgetAndEventHandler(vtkKWWidget* parent)
427   {
428     if (bbGetOutputWidget()==0)
429       {
430         this->bbUserCreateWidget(parent);
431       } 
432     
433     // If Event Handler for the widget does not exist or is obsolete : create it 
434     
435       if (bbGetOutputWidget()!=0)
436       {
437         if (bbGetWidgetEventHandler()==0)
438           {
439             bbtkDebugMessage("kw",3,
440                              "-> No widget event handler : creating one"
441                              <<std::endl);
442             new KWBlackBoxWidgetEventHandler(GetThisPointer<KWBlackBox>(),
443                                              bbGetOutputWidget());
444           }
445         else if ( ! bbGetWidgetEventHandler()->IsHandlerOf 
446                   ( bbGetOutputWidget() ) )
447           {
448             bbtkDebugMessage("kw",3,
449                              "-> Obsolete widget event handler : re-creating one"
450                              <<std::endl);
451             delete bbGetWidgetEventHandler();
452             new KWBlackBoxWidgetEventHandler(GetThisPointer<KWBlackBox>(),
453                                              bbGetOutputWidget());
454           }
455         // Sets the name of the vtkKWWidget to the input WinTitle
456         bbGetOutputWidget()->SetName(bbtk::std2kw(bbGetInputWinTitle()));
457       }
458    
459   }
460     
461   */
462   
463   vtkKWWidget*  KWBlackBox::bbCreateWidgetOfInput(const std::string& in, vtkKWFrame* parent)
464   {
465     vtkKWWidget* w = 0;
466     // If input is connected 
467     BlackBoxInputConnector* c = bbGetInputConnectorMap().find(in)->second ;
468     if ( c->IsConnected() )                     
469       {
470         // Get black box from 
471         BlackBox::Pointer from = 
472           c->GetConnection()->GetBlackBoxFrom();
473         // Cast it into a KWBlackBox
474         KWBlackBox::Pointer wfrom = boost::dynamic_pointer_cast<KWBlackBox>(from);
475         // Call bbCreateWidget
476         wfrom->bbUserCreateWidget(parent);
477         // Get the widget created
478         w = wfrom->bbGetOutputWidget();
479       }
480     return w;
481   }
482   
483   
484 /*
485   //==================================================================
486   /// Specific methods for window creation during pipeline execution
487   /// Shows the window associated to the box 
488   /// (called after bbProcess during bbExecute)
489   void KWBlackBox::bbShowWindow()
490   {
491     bbtkDebugMessageInc("kw",1,"=> KWBlackBox::bbShowWindow() ["
492                         <<bbGetFullName()<<"]"<<std::endl);
493  
494     // If Event Handler for the widget does not exist or is obsolete : create it 
495     if (bbGetOutputWidget()!=0)
496       {
497         if (bbGetWidgetEventHandler()==0)
498           {
499             bbtkDebugMessage("kw",3,
500                              "-> No widget event handler : creating one"
501                              <<std::endl);
502             new KWBlackBoxWidgetEventHandler(GetThisPointer<KWBlackBox>(),
503                                              bbGetOutputWidget());
504           }
505         else if ( ! bbGetWidgetEventHandler()->IsHandlerOf 
506                   ( bbGetOutputWidget() ) )
507           {
508             bbtkDebugMessage("kw",3,
509                              "-> Obsolete widget event handler : re-creating one"
510                              <<std::endl);
511             delete bbGetWidgetEventHandler();
512             new KWBlackBoxWidgetEventHandler(GetThisPointer<KWBlackBox>(),
513                                              bbGetOutputWidget());
514           }
515         // Sets the name of the vtkKWWidget to the input WinTitle
516         bbGetOutputWidget()->SetName(bbtk::std2kw(bbGetInputWinTitle()));
517       }
518
519     // If the output 'Widget' is connected then it's gonna 
520     // be captured by its parent window : nothing to do 
521     if ( (*bbGetOutputConnectorMap().find("Widget")).second
522          ->GetConnectionVector().size() != 0 ) 
523       {
524         
525         bbtkDebugMessage("kw",2,
526                          "-> Output 'Widget' connected : nothing to do"
527                          <<std::endl);
528         return;
529       }
530
531
532     Window* show = 0;
533     // If the window already exists : no need creating it
534     if (bbGetWindow()!=0)
535       {
536         bbtkDebugMessage("kw",2,
537                          "-> Window already exists"
538                          <<std::endl);
539         show = bbGetWindow();
540       }
541     // Else if the widget exists : create window 
542     else if (bbGetOutputWidget()!=0) 
543       {
544         bbtkDebugMessage("kw",2,
545                          "-> Widget exists : creating the window"
546                          <<std::endl);
547
548
549         // Input WinDialog set to true : creating a Dialog
550         if (bbGetInputWinDialog()) 
551           {
552             bbtkDebugMessage("kw",2,
553                              "   Input WinDialog set to true : creating a Dialog"
554                              <<std::endl);
555             show = (Window*) new KWBlackBoxDialog( GetThisPointer<KWBlackBox>(),
556                                                   // bbGetKWParent(), 
557                                                                                           // LG 24/11/08 : New widget pipeline
558                                                                                           KW::GetTopWindow(),
559                                                    std2kw( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN"),
560                                                    kwSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
561           }
562         // Input WinDialog set to false : creating a Frame
563         else 
564           {
565             bbtkDebugMessage("process",2,
566                              "   Input WinDialog set to false : creating a Frame"
567                              <<std::endl);
568             show = (Window*) new KWBlackBoxFrame( GetThisPointer<KWBlackBox>(),
569                                                   // bbGetKWParent(), 
570                                                                                          // LG 24/11/08 : New widget pipeline
571                                                                                          KW::GetTopWindow(),
572                                                   std2kw( bbGetInputWinTitle()  + " - bbtk (c) CREATIS LRMN"),
573                                                   kwSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
574           }
575
576       }
577     // No window nor widget : error
578     else
579       {
580         bbtkError("KWBlackBox::bbShowWindow() ["
581                   <<bbGetFullName()
582                   <<"] : No widget. Did you set the box output 'Widget' in the processing method of the box ?");
583       }
584  
585     
586     // Show the window
587     if (true) //!show->IsShown())
588       {
589         show->bbShow(); 
590       }
591     else 
592       {
593         bbtkDebugMessage("kw",2,"-> Already shown : nothing to do"<<std::endl);
594       }
595   
596
597     bbtkDebugMessage("kw",2,"<= KWBlackBox::bbShowWindow() ["
598                         <<bbGetFullName()<<"]"<<std::endl);
599
600   }
601   //==================================================================
602 */
603
604
605
606   //==================================================================
607    void KWBlackBox::bbHideWindow()
608   {
609     bbtkDebugMessageInc("kw",1,"=> KWBlackBox::bbHideWindow() ["
610                         <<bbGetFullName()<<"]"<<std::endl);
611
612     if (bbGetWindow()!=0) bbGetWindow()->bbHide();
613
614     bbtkDebugMessageDec("kw",2,"<= KWBlackBox::bbHideWindow() ["
615                         <<bbGetFullName()<<"]"<<std::endl);
616   }
617   //==================================================================
618
619
620   //==================================================================
621    void KWBlackBox::bbCloseWindow()
622   {
623     bbtkDebugMessageInc("kw",1,"=> KWBlackBox::bbCloseWindow() ["
624                         <<bbGetFullName()<<"]"<<std::endl);
625
626     if (bbGetWindow()!=0) bbGetWindow()->bbClose();
627
628     bbtkDebugMessageDec("kw",2,"<= KWBlackBox::bbCloseWindow() ["
629                         <<bbGetFullName()<<"]"<<std::endl);
630   }
631   //==================================================================
632
633   //==================================================================
634   KWBlackBox::Window* KWBlackBox::bbGetContainingWindow()
635   {
636     if (bbGetWindow()!=0) return bbGetWindow();
637     BlackBox::OutputConnectorMapType::const_iterator i 
638       = bbGetOutputConnectorMap().find("Widget");
639     if ( i->second->GetConnectionVector().size() != 0 ) 
640       {
641         return boost::static_pointer_cast<KWBlackBox>
642           (i->second->GetConnectionVector().front() //.lock()
643            ->GetBlackBoxTo())->bbGetContainingWindow();
644       }
645     return 0;
646   }
647   //==================================================================
648
649
650   //==================================================================
651         // LG 24/11/08 : New widget pipeline
652         //  vtkKWWidget* KWBlackBox::bbGetKWParent() { return KW::GetTopWindow(); }
653   //==================================================================
654   
655   
656   //==================================================================
657   bool KWBlackBox::bbIsShown()
658   {
659     if (bbGetContainingWindow()!=0)
660       return bbGetContainingWindow()->bbIsShown();
661     return false;
662   }
663   //==================================================================
664
665
666 }//namespace bbtk
667
668
669 #endif
670