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