]> Creatis software - bbtk.git/blob - samples/SampleOutputObserver/bbtkSampleOutputObserver.cxx
Feature #1774
[bbtk.git] / samples / SampleOutputObserver / bbtkSampleOutputObserver.cxx
1 /*
2  # ---------------------------------------------------------------------
3  #
4  # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5  #                        pour la SantÈ)
6  # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7  # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8  # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9  #
10  #  This software is governed by the CeCILL-B license under French law and
11  #  abiding by the rules of distribution of free software. You can  use,
12  #  modify and/ or redistribute the software under the terms of the CeCILL-B
13  #  license as circulated by CEA, CNRS and INRIA at the following URL
14  #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15  #  or in the file LICENSE.txt.
16  #
17  #  As a counterpart to the access to the source code and  rights to copy,
18  #  modify and redistribute granted by the license, users are provided only
19  #  with a limited warranty  and the software's author,  the holder of the
20  #  economic rights,  and the successive licensors  have only  limited
21  #  liability.
22  #
23  #  The fact that you are presently reading this means that you have had
24  #  knowledge of the CeCILL-B license and that you accept its terms.
25  # ------------------------------------------------------------------------ */
26
27
28 #include <bbwxSlider.h>
29 #include <bbwxLayoutLine.h>
30
31 //=========================================================================
32 // Illustrates the Output Observer mechanism
33 //=========================================================================
34
35 //=========================================================================
36 // FIRST EXAMPLE USING A FUNCTION AS CALLBACK
37 void CallbackFunction ( bbtk::BlackBox::Pointer p,  // The black box which changed 
38                         const std::string& o, // The output which changed
39                         bbtk::IOStatus s) // The new status of the output
40 {
41   std::cout << "== Callback function called with p="<<p->bbGetName()
42             <<" o="<<o<<" s="<<s/*bbtk::GetIOStatusString(s)*/<<std::endl;
43
44   // Cast the black box pointer into Slider pointer
45   bbwx::Slider::Pointer slider = boost::dynamic_pointer_cast<bbwx::Slider>(p);
46   if (slider)
47     {
48       std::cout << "* Current slider value = "<<slider->bbGetOutputOut()
49                 <<std::endl;
50     }
51 }
52 //=========================================================================
53
54 //=========================================================================
55 void SliderCallback()
56 {
57   std::cout << "============== Callback function called when slider is released" 
58             << std::endl;
59    try
60     {
61       bbwx::Slider::Pointer slider = bbwx::Slider::New("slider");
62       slider->bbSetInputWinDialog(true);  // mandatory
63       // Add the observer 
64       slider->bbAddOutputObserver( "Out", &CallbackFunction );
65       slider->bbExecute();
66     }
67   catch (bbtk::Exception e)
68     {
69       e.Print();
70     } 
71 }
72 //=========================================================================
73
74
75 //=========================================================================
76 // SECOND EXAMPLE : MEMBER METHOD CALLED WHEN SLIDER IS RELEASED
77 class ClassWithTwoSliders
78 {
79 public:
80   // Ctor
81   ClassWithTwoSliders();
82   // Callback
83   void OnSlider( bbtk::BlackBox::Pointer p, 
84                  const std::string& o, 
85                  bbtk::IOStatus s );
86 private:
87   bbwx::Slider::Pointer mSlider1;
88   bbwx::Slider::Pointer mSlider2;
89 bbwx::LayoutLine::Pointer mLayout;
90 };
91 //=========================================================================
92
93 //=========================================================================
94 ClassWithTwoSliders::ClassWithTwoSliders()
95 {
96   std::cout << "============== Callback member called when first slider is released or second slider moves" 
97             << std::endl;
98   try
99     {
100       mSlider1 = bbwx::Slider::New("slider1");
101       // Add the observer method
102       // As it is not a FREEHAND function we must use the macro 
103       // BBTK_MAKE_OUTPUT_OBSERVER which takes the object and the method to call
104       mSlider1->bbAddOutputObserver("Out", 
105                                     BBTK_MAKE_OUTPUT_OBSERVER
106                                     (this,
107                                      &ClassWithTwoSliders::OnSlider ));
108       
109       mSlider2 = bbwx::Slider::New("slider2");
110       mSlider2->bbSetInputReactiveOnTrack(true);
111       mSlider2->bbAddOutputObserver("Out", 
112                                     BBTK_MAKE_OUTPUT_OBSERVER
113                                     (this,
114                                      &ClassWithTwoSliders::OnSlider ));
115       
116       mLayout = bbwx::LayoutLine::New("layout");
117       mLayout->bbSetInputWinDialog(true);  // mandatory
118       
119       bbtk::Connection::Pointer c1 = bbtk::Connection::New( mSlider1, 
120                                                             "Widget", 
121                                                             mLayout, 
122                                                             "Widget1");
123       bbtk::Connection::Pointer c2 = bbtk::Connection::New( mSlider2, 
124                                                             "Widget", 
125                                                             mLayout, 
126                                                             "Widget2");
127       
128       mLayout->bbExecute();
129     }
130   catch (bbtk::Exception e)
131     {
132       e.Print();
133     }   
134 }
135 //=========================================================================
136
137 //=========================================================================
138 void ClassWithTwoSliders::OnSlider( bbtk::BlackBox::Pointer p, 
139                                     const std::string& o, 
140                                     bbtk::IOStatus s )
141 {
142   std::cout << "== 'OnSlider' called with p="<<p->bbGetName()
143             <<" o="<<o<<" s="<<s/*bbtk::GetIOStatusString(s)*/<<std::endl;
144   
145   if ((p==mSlider1) && (o=="Out"))
146     {
147       std::cout << "* First slider released. New value = "
148                 <<mSlider1->bbGetOutputOut()<<std::endl;
149     }                                                  
150   else if ((p==mSlider2) && (o=="Out"))
151     {
152       std::cout << "* Second slider moved. New value = "
153                 <<mSlider2->bbGetOutputOut()<<std::endl;
154     }
155 }
156 //=========================================================================
157
158 //=========================================================================
159 int main(int argv, char* argc[])
160 {
161  // To track all ...
162         bbtk::MessageManager::SetMessageLevel("all",9);
163   SliderCallback();
164   ClassWithTwoSliders C;
165 }
166 //=========================================================================