]> Creatis software - bbtk.git/blob - kernel/src/bbtkKWBlackBox.cxx
99a0da480292be5bc98768ff481a1d428c7975d1
[bbtk.git] / kernel / src / bbtkKWBlackBox.cxx
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbtkKWBlackBox.cxx,v $
4   Language:  C++
5   Date:      $Date: 2008/12/03 09:38:02 $
6   Version:   $Revision: 1.5 $
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     */
194     if (false)
195       {
196       }
197     // else call 'standard' BlackBox execution method
198     else 
199       {
200         AtomicBlackBox::bbExecute(force);
201       }
202     //
203
204     bbtkDebugMessageDec("process",2,
205                         "<= KWBlackBox::bbExecute() ["
206                         <<bbGetFullName()<<"]"<<std::endl);
207   }
208   //=========================================================================
209
210
211   //=========================================================================
212   /// Main processing method of the box.
213   IOStatus KWBlackBox::bbBackwardUpdate( Connection::Pointer caller )
214   {
215     /*
216     bbtkDebugMessage("process",3,
217                      "=> KWBlackBox::bbBackwardUpdate("
218                      <<(caller?caller->GetFullName():"0")<<") ["
219                      <<bbGetFullName()<<"]"<<std::endl);
220
221     if ( ! (( bbGetStatus() == MODIFIED ) ||
222             ( bbBoxProcessModeIsAlways() )) )
223       {
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();
230       }
231     */
232     // If the caller's box to is not the box to connected to the 
233     // output 'Widget'
234     /*
235     BlackBox::OutputConnectorMapType::const_iterator i 
236       = bbGetOutputConnectorMap().find("Widget") ;
237     if ( i->second->GetConnectionVector().size() != 0 )
238       
239       {
240         BlackBox::Pointer to = 
241           i->second->GetConnectionVector()[0]->GetBlackBoxTo();
242                 
243         if (caller) 
244           {
245             bbtkDebugMessage("process",3,
246                              "-> Output 'Widget' connected to '"
247                              <<to->bbGetFullName()<<"' - caller->to = '"
248                              <<caller->GetBlackBoxTo()->bbGetFullName()
249                              <<"'"
250                              <<std::endl);
251           }
252         else
253           {
254             bbtkDebugMessage("process",3,
255                              "-> Output 'Widget' connected to '"
256                              <<to->bbGetFullName()<<"'"
257                              <<std::endl);
258           }
259         if ((caller==0) ||
260             ( (caller!=0) && 
261               (caller->GetBlackBoxTo() != to)&&
262               (!bbGetUpdateTransferedToParent())&&
263               (!to->bbGetExecuting()) 
264               )
265             )
266           {
267             bbtkDebugMessage("process",3,
268                              "   ... Transfering update order to parent"
269                              <<std::endl);
270             
271             bbSetUpdateTransferedToParent(true);
272             i->second->GetConnectionVector().front() //.lock()
273               ->GetBlackBoxTo()->bbExecute(false);
274           }
275         else
276           {
277             bbSetUpdateTransferedToParent(false);
278             bbtkDebugMessage("process",3,
279                              "   ... No need to transfer to parent"
280                              <<std::endl);
281           }
282       
283           }
284     */
285     /*    
286
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 
290     // but only one time 
291     // (this is the role of the flag UpdateTransferedToParent)
292     if ( (caller==0) ||
293          ((caller!=0)&&(caller->GetBlackBoxFromOutput()!="Widget"))
294          )
295       {
296       }
297     */
298     // call 'standard' BlackBox execution method
299     //if (!bbGetUpdateTransferedToParent()) 
300       { 
301           return AtomicBlackBox::bbBackwardUpdate(caller);
302       }
303       /*
304     bbtkDebugMessageDec("process",3,
305                         "<= KWBlackBox::bbBackwardUpdate() ["
306                         <<bbGetFullName()<<"]"<<std::endl);
307       */
308    return bbGetStatus();
309      
310
311   }
312
313   //=========================================================================
314   void KWBlackBox::bbProcess()
315   { 
316 /*
317           if (bbGetOutputWidget()==0) this->bbUserCreateWidget();
318     this->bbUserProcess(); 
319     bbShowWindow();
320     //    this->bbUserOnShow();
321 */
322     this->bbUserProcess(); 
323
324     // If output widget not connected create the window 
325     if ( (*bbGetOutputConnectorMap().find("Widget")).second
326          ->GetConnectionVector().size() == 0 ) 
327       {
328         Window* show = 0;
329         // If the window already exists : no need creating it
330         if (bbGetWindow()!=0)
331           {
332             bbtkDebugMessage("kw",2,
333                              "-> Window already exists"
334                              <<std::endl);
335             show = bbGetWindow();
336           }
337         // Else create window 
338         else 
339           {
340             bbtkDebugMessage("kw",2,
341                              "-> Creating the window"
342                              <<std::endl);
343             KW::GetApplication();
344             
345             // Input WinDialog set to true : creating a Dialog
346             /*
347             if (bbGetInputWinDialog()) 
348               {
349                 bbtkDebugMessage("kw",2,
350                                  "   Input WinDialog set to true : creating a Dialog"
351                                  <<std::endl);
352             */
353                 vtkKWBlackBoxDialog* win = vtkKWBlackBoxDialog::New();
354                 show = (Window*) win;
355                 win->bbSetBlackBox( GetThisPointer<KWBlackBox>());
356                 KW::GetApplication()->AddWindow((vtkKWWindowBase*)win);
357                 win->Create();
358                 win->SetResizable(0,0);
359
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());
365                 
366
367
368
369
370                 /*
371                 bbGetOutputWidget()->SetParent(win);
372                 bbGetOutputWidget()->Create();
373                 KW::GetApplication()->Script("pack %s -side left -anchor c -expand y",
374                         
375                                      //"pack %s -side top -anchor nw -expand y -fill none -padx 2 -pady 2", 
376                                              bbGetOutputWidget()->GetWidgetName());
377                 */                           
378                 /*      
379                   }
380         
381             // Input WinDialog set to false : creating a Frame
382             else 
383               {
384                 bbtkDebugMessage("process",2,
385                                  "   Input WinDialog set to false : creating a Frame"
386                                  <<std::endl);
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);
394                 win->Create();
395                 
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());           }
400         */
401         
402                 //      win->Invoke();
403           }
404         
405         // Show the window
406
407         show->bbShow(); 
408         //      std::cout << "KW::GetApplication()->Start();"<<std::endl;
409         //      KW::GetApplication()->Start();
410         //      std::cout << "AFTER KW::GetApplication()->Start();"<<std::endl;
411         
412       }           
413     //   
414     
415   }
416   //=========================================================================
417   
418   /*
419   //=========================================================================
420   void KWBlackBox::bbPlaceWidget()
421   {
422     vtkKWFrame* parent = (vtkKWFrame*)(bbGetOutputWidget()->GetParent());
423     bbGetOutputWidget->GetApplication()->Script("place %s -x 0 -y 0 -width %s -height %s",
424                                                 bbGetOutputWidget()->GetWidgetName(),
425                                                 parent->GetWidth(),
426                                                 parent->GetHeight());
427   }
428   //=========================================================================
429   */
430
431   /*
432   // LG 24/11/08 : New widget pipeline
433   void KWBlackBox::bbCreateWidgetAndEventHandler(vtkKWWidget* parent)
434   {
435     if (bbGetOutputWidget()==0)
436       {
437         this->bbUserCreateWidget(parent);
438       } 
439     
440     // If Event Handler for the widget does not exist or is obsolete : create it 
441     
442       if (bbGetOutputWidget()!=0)
443       {
444         if (bbGetWidgetEventHandler()==0)
445           {
446             bbtkDebugMessage("kw",3,
447                              "-> No widget event handler : creating one"
448                              <<std::endl);
449             new KWBlackBoxWidgetEventHandler(GetThisPointer<KWBlackBox>(),
450                                              bbGetOutputWidget());
451           }
452         else if ( ! bbGetWidgetEventHandler()->IsHandlerOf 
453                   ( bbGetOutputWidget() ) )
454           {
455             bbtkDebugMessage("kw",3,
456                              "-> Obsolete widget event handler : re-creating one"
457                              <<std::endl);
458             delete bbGetWidgetEventHandler();
459             new KWBlackBoxWidgetEventHandler(GetThisPointer<KWBlackBox>(),
460                                              bbGetOutputWidget());
461           }
462         // Sets the name of the vtkKWWidget to the input WinTitle
463         bbGetOutputWidget()->SetName(bbtk::std2kw(bbGetInputWinTitle()));
464       }
465    
466   }
467     
468   */
469   
470   vtkKWWidget*  KWBlackBox::bbCreateWidgetOfInput(const std::string& in, vtkKWFrame* parent)
471   {
472     vtkKWWidget* w = 0;
473     // If input is connected 
474     BlackBoxInputConnector* c = bbGetInputConnectorMap().find(in)->second ;
475     if ( c->IsConnected() )                     
476       {
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();
486       }
487     return w;
488   }
489   
490   
491 /*
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()
497   {
498     bbtkDebugMessageInc("kw",1,"=> KWBlackBox::bbShowWindow() ["
499                         <<bbGetFullName()<<"]"<<std::endl);
500  
501     // If Event Handler for the widget does not exist or is obsolete : create it 
502     if (bbGetOutputWidget()!=0)
503       {
504         if (bbGetWidgetEventHandler()==0)
505           {
506             bbtkDebugMessage("kw",3,
507                              "-> No widget event handler : creating one"
508                              <<std::endl);
509             new KWBlackBoxWidgetEventHandler(GetThisPointer<KWBlackBox>(),
510                                              bbGetOutputWidget());
511           }
512         else if ( ! bbGetWidgetEventHandler()->IsHandlerOf 
513                   ( bbGetOutputWidget() ) )
514           {
515             bbtkDebugMessage("kw",3,
516                              "-> Obsolete widget event handler : re-creating one"
517                              <<std::endl);
518             delete bbGetWidgetEventHandler();
519             new KWBlackBoxWidgetEventHandler(GetThisPointer<KWBlackBox>(),
520                                              bbGetOutputWidget());
521           }
522         // Sets the name of the vtkKWWidget to the input WinTitle
523         bbGetOutputWidget()->SetName(bbtk::std2kw(bbGetInputWinTitle()));
524       }
525
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 ) 
530       {
531         
532         bbtkDebugMessage("kw",2,
533                          "-> Output 'Widget' connected : nothing to do"
534                          <<std::endl);
535         return;
536       }
537
538
539     Window* show = 0;
540     // If the window already exists : no need creating it
541     if (bbGetWindow()!=0)
542       {
543         bbtkDebugMessage("kw",2,
544                          "-> Window already exists"
545                          <<std::endl);
546         show = bbGetWindow();
547       }
548     // Else if the widget exists : create window 
549     else if (bbGetOutputWidget()!=0) 
550       {
551         bbtkDebugMessage("kw",2,
552                          "-> Widget exists : creating the window"
553                          <<std::endl);
554
555
556         // Input WinDialog set to true : creating a Dialog
557         if (bbGetInputWinDialog()) 
558           {
559             bbtkDebugMessage("kw",2,
560                              "   Input WinDialog set to true : creating a Dialog"
561                              <<std::endl);
562             show = (Window*) new KWBlackBoxDialog( GetThisPointer<KWBlackBox>(),
563                                                   // bbGetKWParent(), 
564                                                                                           // LG 24/11/08 : New widget pipeline
565                                                                                           KW::GetTopWindow(),
566                                                    std2kw( bbGetInputWinTitle() + " - bbtk (c) CREATIS LRMN"),
567                                                    kwSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
568           }
569         // Input WinDialog set to false : creating a Frame
570         else 
571           {
572             bbtkDebugMessage("process",2,
573                              "   Input WinDialog set to false : creating a Frame"
574                              <<std::endl);
575             show = (Window*) new KWBlackBoxFrame( GetThisPointer<KWBlackBox>(),
576                                                   // bbGetKWParent(), 
577                                                                                          // LG 24/11/08 : New widget pipeline
578                                                                                          KW::GetTopWindow(),
579                                                   std2kw( bbGetInputWinTitle()  + " - bbtk (c) CREATIS LRMN"),
580                                                   kwSize( bbGetInputWinWidth() , bbGetInputWinHeight() ) );
581           }
582
583       }
584     // No window nor widget : error
585     else
586       {
587         bbtkError("KWBlackBox::bbShowWindow() ["
588                   <<bbGetFullName()
589                   <<"] : No widget. Did you set the box output 'Widget' in the processing method of the box ?");
590       }
591  
592     
593     // Show the window
594     if (true) //!show->IsShown())
595       {
596         show->bbShow(); 
597       }
598     else 
599       {
600         bbtkDebugMessage("kw",2,"-> Already shown : nothing to do"<<std::endl);
601       }
602   
603
604     bbtkDebugMessage("kw",2,"<= KWBlackBox::bbShowWindow() ["
605                         <<bbGetFullName()<<"]"<<std::endl);
606
607   }
608   //==================================================================
609 */
610
611
612
613   //==================================================================
614    void KWBlackBox::bbHideWindow()
615   {
616     bbtkDebugMessageInc("kw",1,"=> KWBlackBox::bbHideWindow() ["
617                         <<bbGetFullName()<<"]"<<std::endl);
618
619     if (bbGetWindow()!=0) bbGetWindow()->bbHide();
620
621     bbtkDebugMessageDec("kw",2,"<= KWBlackBox::bbHideWindow() ["
622                         <<bbGetFullName()<<"]"<<std::endl);
623   }
624   //==================================================================
625
626
627   //==================================================================
628    void KWBlackBox::bbCloseWindow()
629   {
630     bbtkDebugMessageInc("kw",1,"=> KWBlackBox::bbCloseWindow() ["
631                         <<bbGetFullName()<<"]"<<std::endl);
632
633     if (bbGetWindow()!=0) bbGetWindow()->bbClose();
634
635     bbtkDebugMessageDec("kw",2,"<= KWBlackBox::bbCloseWindow() ["
636                         <<bbGetFullName()<<"]"<<std::endl);
637   }
638   //==================================================================
639
640   //==================================================================
641   KWBlackBox::Window* KWBlackBox::bbGetContainingWindow()
642   {
643     if (bbGetWindow()!=0) return bbGetWindow();
644     BlackBox::OutputConnectorMapType::const_iterator i 
645       = bbGetOutputConnectorMap().find("Widget");
646     if ( i->second->GetConnectionVector().size() != 0 ) 
647       {
648         return boost::static_pointer_cast<KWBlackBox>
649           (i->second->GetConnectionVector().front() //.lock()
650            ->GetBlackBoxTo())->bbGetContainingWindow();
651       }
652     return 0;
653   }
654   //==================================================================
655
656
657   //==================================================================
658         // LG 24/11/08 : New widget pipeline
659         //  vtkKWWidget* KWBlackBox::bbGetKWParent() { return KW::GetTopWindow(); }
660   //==================================================================
661   
662   
663   //==================================================================
664   bool KWBlackBox::bbIsShown()
665   {
666     if (bbGetContainingWindow()!=0)
667       return bbGetContainingWindow()->bbIsShown();
668     return false;
669   }
670   //==================================================================
671
672
673 }//namespace bbtk
674
675
676 #endif
677