]> Creatis software - bbtk.git/blob - packages/kw/src/bbkwSlider.cxx
41a8008b9a447bd14cad6acb497672d19cf02425
[bbtk.git] / packages / kw / src / bbkwSlider.cxx
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbkwSlider.cxx,v $
4   Language:  C++
5   Date:      $Date: 2008/11/29 21:41:58 $
6   Version:   $Revision: 1.1 $
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 /**
32  * \file 
33  * \brief Short description in one line
34  *
35  * Long 
36  * description
37  *  
38  */
39
40 #ifdef USE_KWWIDGETS
41
42 #include "bbkwSlider.h"
43 #include "bbkwPackage.h"
44 #include "bbtkUtilities.h"
45
46 #include "vtkKWScale.h"
47
48 namespace bbkw
49 {
50   
51   /*
52   //-------------------------------------------------------------------------
53   SliderWidget::SliderWidget(Slider* box, kwWindow *parent,
54                              int orientation,
55                              bool changeresolution,
56                              bool labels,
57                              kwString title,
58                              int vmin,
59                              int vmax,
60                              int value,
61                              int reactiveontrack)
62     :  
63     kwPanel( parent, -1, kwDefaultPosition, kwDefaultSize, kwTAB_TRAVERSAL),
64     mBox(box),
65     min(vmin),
66     max(vmax),
67     reactiveOnTrack(reactiveontrack)
68   {
69     kwPanel * panel = this;
70     
71     label_min           = NULL;
72     label_max           = NULL;
73     label_vertical      = NULL;
74     mResolutionOfSlider = NULL;
75
76     int sizeX,sizeY;
77     long kwlabels=0;
78     long kworientation=0;
79     if (orientation==0) 
80       {
81         sizeX                   = 2;
82         sizeY                   = 40;
83         kworientation   = kwSL_HORIZONTAL;
84         if (labels==true)
85           {
86             kwlabels = kwSL_LABELS;
87           }
88       } 
89     else 
90       {
91         sizeX                   = 20;
92         sizeY                   = 2;
93         kworientation   = kwSL_VERTICAL;
94       }
95     
96     //---------------------------------------------------------------------
97     // 1) Creation of the components of the widget
98     // Any top level sub-widget must have the panel returned by panel
99     // for parent
100     mkwSlider = new kwSlider( panel, 
101                               -1, 0 , 0, 500 , 
102                               kwDefaultPosition, 
103                               kwSize(sizeX,sizeY), 
104                               kworientation  | kwlabels );
105
106     //    mkwSlider->SetInitialSize(kwSize(sizeX,sizeY));
107
108     //  mkwSlider->SetTickFreq(100,0);
109     mkwSlider->SetRange(min,max);
110     mkwSlider->SetValue(value);
111
112     //    RefreshLabels();
113
114     // Connecting events to callbacks
115     Connect( mkwSlider->GetId(), 
116              kwEVT_SCROLL_THUMBRELEASE, 
117
118              (kwObjectEventFunction) 
119              (void (kwPanel::*)(kwScrollEvent&))
120              &SliderWidget::OnSliderRelease ); 
121
122     Connect( mkwSlider->GetId(),  
123              kwEVT_SCROLL_THUMBTRACK, 
124
125              (kwObjectEventFunction) 
126              (void (kwPanel::*)(kwScrollEvent&))
127               &SliderWidget::OnSliderTrack ); 
128     
129     Connect( mkwSlider->GetId(),  
130              kwEVT_SCROLL_CHANGED, 
131
132              (kwObjectEventFunction) 
133              (void (kwPanel::*)(kwScrollEvent&))
134               &SliderWidget::OnSliderTrack );
135                
136     // If asked : creation of the other little slider which allows to change 
137     // the resolution of the main slider
138     if (changeresolution==true){
139       // has panel for parent too
140       mResolutionOfSlider = new kwSlider(panel,
141                                          -1,5,1,10,
142                                          kwDefaultPosition,
143                                          kwSize(25,45),
144                                          kwSL_VERTICAL | 
145                                          kwSL_AUTOTICKS |
146                                          kwSL_LEFT  );
147    
148
149       mResolutionOfSlider->SetRange(1,8);
150       mResolutionOfSlider->SetValue(5);
151       // Is kwEVT_COMMAND_SLIDER_UPDATED event 
152       // is connected to the method OnResolutionOfSlider
153       Connect(mResolutionOfSlider->GetId(),
154               kwEVT_COMMAND_SLIDER_UPDATED,
155               (kwObjectEventFunction) 
156               (void (kwPanel::*)(kwScrollEvent&))
157               &SliderWidget::OnResolutionOfSlider ); 
158     }
159     //---------------------------------------------------------------------
160
161     //---------------------------------------------------------------------
162     // 2) Insertion of the components in the window
163     
164     // We use a FlexGridSizer
165     kwFlexGridSizer *sizer;
166
167
168     if (orientation==0) 
169       {
170         // HORIZONTAL
171         if (mResolutionOfSlider!=NULL) 
172           {
173             sizer       = new kwFlexGridSizer(2);
174             if (title!=_T(""))
175               {
176                 sizer   -> Add( new kwStaticText(panel,-1, title ) ); 
177                 sizer   -> Add( new kwStaticText(panel,-1, _T("") ) ); 
178               }
179           } 
180         else 
181           {
182             sizer       = new kwFlexGridSizer(1);
183             if (title!=_T(""))
184               {
185                 sizer   -> Add( new kwStaticText(panel,-1, title ) ); 
186               }
187           }
188         sizer   -> Add( mkwSlider,1,kwGROW ); 
189         sizer   -> AddGrowableCol(0);
190         if (mResolutionOfSlider!=NULL) 
191           {
192             sizer       -> Add( mResolutionOfSlider ); 
193           }
194       } 
195     else 
196       {
197         // VERTICAL 
198         sizer   = new kwFlexGridSizer(1);
199         if (labels==true)  // with lable
200           {
201             label_vertical = new kwStaticText(panel,-1,_T(""));
202             label_min = new kwStaticText(panel,-1,_T(""));
203             label_max = new kwStaticText(panel,-1,_T(""));
204             if (title!=_T(""))
205               {
206                 sizer   -> Add( new kwStaticText(panel,-1, title ) );
207                 sizer   -> AddGrowableRow(3);
208               } 
209             else 
210               {
211                 sizer   -> AddGrowableRow(2);
212               }
213             sizer       -> Add( label_vertical );
214             sizer       -> Add( label_min );
215             sizer       -> Add( mkwSlider,1,kwGROW );
216             sizer       -> Add( label_max );
217             if (mResolutionOfSlider!=NULL) 
218               {
219                 sizer   -> Add( mResolutionOfSlider ); 
220               }
221           } 
222         else 
223           {
224             if (title!=_T(""))
225               {
226                 sizer   -> Add( new kwStaticText(panel,-1, title ) );
227                 sizer   -> AddGrowableRow(1);
228               } 
229             else 
230               {
231                 sizer   -> AddGrowableRow(0);
232               }
233             sizer       -> Add( mkwSlider,1,kwGROW );
234             if (mResolutionOfSlider!=NULL) 
235               {
236                 sizer   -> Add( mResolutionOfSlider ); 
237               }
238           }
239       }
240     // Initialize the labels 
241     RefreshLabels();  
242     // Insert the sizer in the main panel and refresh the layout
243     panel->SetSizer(sizer);
244   }
245   //-------------------------------------------------------------------------
246   
247
248   //-------------------------------------------------------------------------
249   SliderWidget::~SliderWidget()
250   {
251   }
252   //-------------------------------------------------------------------------
253
254
255   //-------------------------------------------------------------------------
256   void SliderWidget::OnResolutionOfSlider(kwScrollEvent& event)
257   {
258     int value = mkwSlider->GetValue();
259     int delta = (int) (pow( 4 , mResolutionOfSlider->GetValue() ));
260     int minTmp   = value - delta/2;
261     int maxTmp   = value + delta/2;
262     if (minTmp<min)
263       {
264         minTmp = min;
265         //              maxTmp = delta;
266       }
267     if (maxTmp>max)
268       {
269         maxTmp = max;
270       }
271     mkwSlider->SetRange(minTmp,maxTmp);
272     RefreshLabels();
273   }
274   //-------------------------------------------------------------------------
275
276
277
278   //-------------------------------------------------------------------------
279   void SliderWidget::OnSliderTrack(kwScrollEvent& event)
280   {
281     if(reactiveOnTrack)
282     {
283     // When user releases the slider 
284     // we update the output of the box
285     
286     mBox->bbSetOutputOut( mkwSlider->GetValue() );
287     mBox->bbSetInputIn( mkwSlider->GetValue() );
288     // and signal that the output has changed
289     mBox->bbSignalOutputModification("Out");    
290     }
291     RefreshLabels();
292   }
293   //-------------------------------------------------------------------------
294
295
296   //-------------------------------------------------------------------------
297   void SliderWidget::OnSliderRelease(kwScrollEvent& event)
298   {
299     // When user releases the slider 
300     // we update the output of the box
301     mBox->bbSetOutputOut( mkwSlider->GetValue() );
302     mBox->bbSetInputIn( mkwSlider->GetValue() );
303     // and signal that the output has changed
304     mBox->bbSignalOutputModification("Out");
305   }
306   //-------------------------------------------------------------------------
307   
308
309   //-------------------------------------------------------------------------
310   void SliderWidget::RefreshLabels()
311   {
312     kwString strValue;
313     if (label_vertical!=NULL)
314       {
315         strValue.Printf( _T("%d"), mkwSlider->GetValue() );
316         label_vertical->SetLabel(strValue);
317       }
318     if (label_min!=NULL)
319       {
320         strValue.Printf( _T("%d"), mkwSlider->GetMin() );
321         label_min->SetLabel(strValue);
322       }
323     if (label_max!=NULL)
324       {
325         strValue.Printf( _T("%d"), mkwSlider->GetMax() );
326         label_max->SetLabel(strValue);
327       }  
328   }
329   //-------------------------------------------------------------------------
330   
331
332   //-------------------------------------------------------------------------
333   void SliderWidget::SetRange(int min, int max)
334   {
335     this->min = min;
336     this->max = max;
337     mkwSlider->SetRange(min,max);
338     RefreshLabels();
339   }
340   //-------------------------------------------------------------------------
341
342  */
343
344
345   //--------------------------------------------------------------------------
346   //-------------------------------------------------------------------------
347   // KwBlackBox implementation
348   //--------------------------------------------------------------------------
349   //--------------------------------------------------------------------------
350
351   //--------------------------------------------------------------------------
352   BBTK_BLACK_BOX_IMPLEMENTATION(Slider,bbtk::KWBlackBox);
353   BBTK_ADD_BLACK_BOX_TO_PACKAGE(kw,Slider);
354   
355   //--------------------------------------------------------------------------
356   void Slider::bbUserConstructor() 
357   { 
358     bbSetInputIn(0);
359     bbSetInputMin(0);
360     bbSetInputMax(500);
361     bbSetOutputOut(0);
362     bbSetOutputWidget(0);
363     bbSetInputOrientation("HORIZONTAL");
364     bbSetInputChangeResolution(false);
365     bbSetInputLabel(true);
366     bbSetInputReactiveOnTrack(0);    
367   }
368   
369  //--------------------------------------------------------------------------
370   void Slider::Process() 
371   {
372   }
373   //--------------------------------------------------------------------------
374   void Slider::CreateWidget(vtkKWWidget* parent) 
375   {
376     vtkKWScale* s = vtkKWScale::New();
377     bbSetOutputWidget(s);
378     s->SetParent(parent);
379     s->Create();
380
381     s->SetRange(bbGetInputMin(),bbGetInputMax());
382     // s->SetResolution(1.0);
383     // s->SetLength(150);
384     s->SetLabelText("A simple scale:");
385     if (bbtk::Utilities::loosematch(bbGetInputOrientation(),"0|H|HORIZONTAL")==true) 
386       {
387         s->SetOrientationToHorizontal();
388       }
389     else if (bbtk::Utilities::loosematch(bbGetInputOrientation(),"1|V|VERTICAL")==true)  
390       {
391         s->SetOrientationToVertical();
392       }
393     else 
394       {
395         bbtkWarning("Unrecognized value for input 'Orientation' of "<<bbGetFullName());
396       }
397    
398     s->GetApplication()->Script("pack %s -side left -anchor c -expand y",
399                                 s->GetWidgetName());
400
401     bbSetOutputOut( bbGetInputIn() );
402   }
403
404 } //namespace bbkw
405
406 #endif // USE_KWWIDGETS
407
408