]> Creatis software - creaWT.git/blob - wt/bbtk_wt_PKG/src/bbwtSlider.cxx
dfc7747d5a820fbe0153eab131f35ace4352ada9
[creaWT.git] / wt / bbtk_wt_PKG / src / bbwtSlider.cxx
1 //===== 
2 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
3 //===== 
4 #include "bbwtSlider.h"
5 #include "bbwtPackage.h"
6 #include "bbtkUtilities.h"
7 namespace bbwt
8 {
9
10         class SliderWidget : public Wt::WContainerWidget
11         {
12                 public:
13                         SliderWidget(Slider* box, Wt::WContainerWidget *parent,
14                                                                          int orientation,
15                                                                          bool changeresolution,
16                                                                          Wt::WString title,
17                                                                          int vmin,
18                                                                          int vmax,
19                                                                          int value,
20                                                                          bool reactiveontrack);
21
22                         ~SliderWidget();
23
24                 /// Events callbacks
25     /// Called when the slider is moved
26         void OnSliderTrack();
27         void OnSliderMove();
28     /// Called when the little slider which controls the resolution
29     /// of the main slider is moved (if activated)
30         void OnResolutionOfSlider();
31
32     // Accessors
33         void SetValue(int v) { mwtSlider->setValue(v); }
34                   int  GetValue() { return mwtSlider->value(); }
35                   int GetMin() {return min;}
36                   int GetMax() {return max;}    
37         void SetRange(int min, int max);
38                         void SetReactiveOnTrack(bool ok);
39
40
41                 private:
42         Slider*      mBox;
43                   Wt::WSlider     *mwtSlider;
44                   Wt::WSlider     *mResolutionOfSlider;
45                   int          min;
46                   int          max;
47                         bool         reactiveOnTrack;
48
49
50         };
51
52   //------------------------------------------------------------------------
53   //------------------------------------------------------------------------
54   //------------------------------------------------------------------------
55
56   
57     
58   //-------------------------------------------------------------------------
59   SliderWidget::SliderWidget(Slider* box, Wt::WContainerWidget *parent,
60                                                                                                      int orientation,
61                                                                                                      bool changeresolution,
62                                                                                                                  Wt::WString title,
63                                                                                                                  int vmin,
64                                                                                                                  int vmax,
65                                                                                                                  int value,
66                              bool reactiveontrack)
67     : 
68         Wt::WContainerWidget(parent),
69         mBox(box),
70                   min(vmin),
71                   max(vmax),
72                         reactiveOnTrack(reactiveontrack)
73
74   {
75                 std::cout<<"Creando SLIDER  --  0  "<<std::endl;
76     Wt::WContainerWidget * panel = this;
77                 Wt::WBorderLayout* layout = new Wt::WBorderLayout();            
78     
79
80
81     mResolutionOfSlider = NULL;
82
83     int sizeX,sizeY;
84     Wt::Orientation wtorientation;
85                 std::cout<<"Creando SLIDER  --  1  "<<std::endl;
86     if (orientation==0) 
87     {
88                                 sizeX                   = 2; 
89                                 sizeY                   = 40;
90                                 wtorientation           = Wt::Horizontal;
91         
92                 } 
93     else 
94     {
95                         sizeX           = 20;
96                         sizeY           = 2;
97                         wtorientation   = Wt::Vertical;
98     }
99     
100     //---------------------------------------------------------------------
101     // 1) Creation of the components of the widget
102     // Any top level sub-widget must have the panel returned by panel
103     // for parent
104     
105                 std::cout<<"Creando SLIDER  --  2  "<<std::endl;
106                 mwtSlider = new Wt::WSlider(wtorientation);
107                 mwtSlider->resize(sizeX,sizeY);
108                 mwtSlider->setMinimum(min);
109                 mwtSlider->setMaximum(max);             
110                 mwtSlider->setValue(value);
111                 mwtSlider->setTickPosition(Wt::WSlider::TicksBothSides);
112                 //mwtSlider->setValueText(title);
113                 mwtSlider->valueChanged().connect(this,&SliderWidget::OnSliderTrack);
114                 mwtSlider->sliderMoved().connect(this,&SliderWidget::OnSliderMove);
115
116                
117     // If asked : creation of the other little slider which allows to change 
118     // the resolution of the main slider
119                                 std::cout<<"Creando SLIDER  --  3  "<<std::endl;
120     if (changeresolution==true)
121                 {
122       // has panel for parent too
123       mResolutionOfSlider = new Wt::WSlider(Wt::Vertical);
124                 mResolutionOfSlider->setMinimum(1);
125                         mResolutionOfSlider->setMaximum(8);
126                         mResolutionOfSlider->setValue(5);               
127       mResolutionOfSlider->valueChanged().connect(this,&SliderWidget::OnResolutionOfSlider);    
128                         mResolutionOfSlider->resize(5,10);              
129                         if (orientation==0) 
130                   {
131                                 // HORIZONTAL
132                                 layout->addWidget(mResolutionOfSlider,Wt::WBorderLayout::West);
133                         
134                   } 
135                   else 
136                   {
137                                 // VERTICAL 
138                                 layout->addWidget(mResolutionOfSlider,Wt::WBorderLayout::South);
139                                 
140                         }
141
142     }
143
144     //---------------------------------------------------------------------
145
146     //---------------------------------------------------------------------
147     // 2) Insertion of the components in the window
148                 std::cout<<"Creando SLIDER  --  4  "<<std::endl;
149                 layout->addWidget(new Wt::WText(title),Wt::WBorderLayout::North);
150                 layout->addWidget(mwtSlider,Wt::WBorderLayout::Center);
151                                 std::cout<<"Creando SLIDER  --  5  "<<std::endl;
152     // Insert the sizer in the main panel and refresh the layout
153     panel->setLayout(layout);
154                 std::cout<<"Creando SLIDER  --  6  "<<std::endl;
155   }
156   //-------------------------------------------------------------------------
157   
158         
159         
160   //-------------------------------------------------------------------------
161   SliderWidget::~SliderWidget()
162   {
163   }
164   //-------------------------------------------------------------------------
165
166
167   //-------------------------------------------------------------------------
168   void SliderWidget::OnResolutionOfSlider()
169   {
170     int value  = mwtSlider->value();
171     int delta  = (int) (pow((double) 4 ,(double) mResolutionOfSlider->value() ));
172     int minTmp = value - delta/2;
173     int maxTmp = value + delta/2;
174     if (minTmp<min)
175     {
176                         minTmp = min;
177         //              maxTmp = delta;
178     }
179     if (maxTmp>max)
180     {
181                         maxTmp = max;
182     }
183                 mwtSlider->setMinimum(minTmp);
184                 mwtSlider->setMaximum(maxTmp);  
185    
186   }
187   //-------------------------------------------------------------------------
188
189
190
191   //-------------------------------------------------------------------------
192   void SliderWidget::OnSliderTrack()
193   {
194
195     // When user releases the slider 
196     // we update the output of the box
197      if(reactiveOnTrack)
198     {
199                   mBox->bbSetOutputOut( mwtSlider->value() );
200                   mBox->bbSetInputIn( mwtSlider->value() );
201                 }
202     // and signal that the output has changed
203     mBox->bbSignalOutputModification(std::string("Out"));    
204
205   }
206   
207   
208   //-------------------------------------------------------------------------
209   void SliderWidget::OnSliderMove()
210   {
211
212     // When user releases the slider 
213     // we update the output of the box
214      if(!reactiveOnTrack)
215     {
216                   mBox->bbSetOutputOut( mwtSlider->value() );
217                   mBox->bbSetInputIn( mwtSlider->value() );
218         }
219     // and signal that the output has changed
220     mBox->bbSignalOutputModification(std::string("Out"));    
221
222   }
223
224   //-------------------------------------------------------------------------
225   
226
227   //-------------------------------------------------------------------------
228   void SliderWidget::SetRange(int min, int max)
229   {
230     this->min = min;
231     this->max = max;
232                 mwtSlider->setMinimum(min);
233                 mwtSlider->setMaximum(max);
234   }
235         
236         
237   //-------------------------------------------------------------------------
238         void SliderWidget::SetReactiveOnTrack(bool ok)
239         {
240                 reactiveOnTrack = ok;
241         }
242  
243
244
245
246         BBTK_ADD_BLACK_BOX_TO_PACKAGE(wt,Slider);
247         BBTK_BLACK_BOX_IMPLEMENTATION(Slider,bbtk::WtBlackBox);
248         //===== 
249         // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your                            modifications will be lost)
250         //===== 
251         void Slider::Process()
252         {
253
254                 bbtkDebugMessage("process",3,
255                                    "Slider "<<bbGetName()<<" input="
256                                    <<bbGetInputIn()<<std::endl);
257                 
258
259                         // desperate try // JPR
260                 if ( bbGetInputMin() != ((SliderWidget*)bbGetOutputWidget())->GetMin() ||  bbGetInputMax() != ((SliderWidget*)bbGetOutputWidget())->GetMax() )       
261                 {    
262                         ((SliderWidget*)bbGetOutputWidget())->SetRange(bbGetInputMin(),bbGetInputMax()) ;
263                 }
264
265                 ((SliderWidget*)bbGetOutputWidget())->SetReactiveOnTrack( bbGetInputReactiveOnTrack() ) ;
266                         
267                 bbSetOutputOut( bbGetInputIn() );
268                 if (bbGetOutputWidget()!=0)
269                 {
270                         ((SliderWidget*)bbGetOutputWidget())->SetValue(bbGetInputIn());
271                  }
272                 
273         }
274 //===== 
275 // Before editing this file, make sure it'sWt::WSlider a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
276 //===== 
277         void Slider::CreateWidget(Wt::WContainerWidget* parent)
278         {
279
280         int orientation=0;
281         if (bbtk::Utilities::loosematch(bbGetInputOrientation(),"1|V|VERTICAL")==true)    { orientation=1; }
282     
283     
284     //    std::cout << "bbGetWxParent = "<<bbGetWxParent()<<std::endl;
285     SliderWidget *w =  new SliderWidget(this, 
286                                         parent, //bbGetWxParent(),
287                                         orientation , 
288                                         bbGetInputChangeResolution(), 
289                                         bbtk::std2wt( bbGetInputTitle() ),
290                                         bbGetInputMin(), 
291                                         bbGetInputMax(),
292                                         bbGetInputIn(),
293                                         bbGetInputReactiveOnTrack()
294                                         ); 
295     //    std::cout << "w = "<<w<<std::endl;
296     //  w->SetName( bbtk::std2wx( bbGetInputWinTitle() ) );
297           
298     bbSetOutputWidget( w );
299   }
300   
301   
302
303 //===== 
304 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
305 //===== 
306 void Slider::bbUserSetDefaultValues()
307 {
308                 bbSetInputIn(0);
309                 bbSetInputMin(0);
310                 bbSetInputMax(500);
311                 bbSetOutputOut(0);
312                 bbSetInputOrientation("HORIZONTAL");
313                 bbSetInputChangeResolution(false);
314                 bbSetInputReactiveOnTrack(false);
315
316
317 }
318 //===== 
319 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
320 //===== 
321 void Slider::bbUserInitializeProcessing()
322 {
323
324 }
325 //===== 
326 // Before editing this file, make sure it's a file of your own (i.e.: it wasn't generated from xml description; if so : your modifications will be lost)
327 //===== 
328 void Slider::bbUserFinalizeProcessing()
329 {
330
331 }
332 }
333 // EO namespace bbwt
334
335