1 /*=========================================================================
3 Module: $RCSfile: bbkwSlider.cxx,v $
5 Date: $Date: 2008/11/29 21:41:58 $
6 Version: $Revision: 1.1 $
7 =========================================================================*/
9 /* ---------------------------------------------------------------------
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
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.
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
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 * ------------------------------------------------------------------------ */
33 * \brief Short description in one line
42 #include "bbkwSlider.h"
43 #include "bbkwPackage.h"
44 #include "bbtkUtilities.h"
46 #include "vtkKWScale.h"
52 //-------------------------------------------------------------------------
53 SliderWidget::SliderWidget(Slider* box, kwWindow *parent,
55 bool changeresolution,
63 kwPanel( parent, -1, kwDefaultPosition, kwDefaultSize, kwTAB_TRAVERSAL),
67 reactiveOnTrack(reactiveontrack)
69 kwPanel * panel = this;
73 label_vertical = NULL;
74 mResolutionOfSlider = NULL;
83 kworientation = kwSL_HORIZONTAL;
86 kwlabels = kwSL_LABELS;
93 kworientation = kwSL_VERTICAL;
96 //---------------------------------------------------------------------
97 // 1) Creation of the components of the widget
98 // Any top level sub-widget must have the panel returned by panel
100 mkwSlider = new kwSlider( panel,
104 kworientation | kwlabels );
106 // mkwSlider->SetInitialSize(kwSize(sizeX,sizeY));
108 // mkwSlider->SetTickFreq(100,0);
109 mkwSlider->SetRange(min,max);
110 mkwSlider->SetValue(value);
114 // Connecting events to callbacks
115 Connect( mkwSlider->GetId(),
116 kwEVT_SCROLL_THUMBRELEASE,
118 (kwObjectEventFunction)
119 (void (kwPanel::*)(kwScrollEvent&))
120 &SliderWidget::OnSliderRelease );
122 Connect( mkwSlider->GetId(),
123 kwEVT_SCROLL_THUMBTRACK,
125 (kwObjectEventFunction)
126 (void (kwPanel::*)(kwScrollEvent&))
127 &SliderWidget::OnSliderTrack );
129 Connect( mkwSlider->GetId(),
130 kwEVT_SCROLL_CHANGED,
132 (kwObjectEventFunction)
133 (void (kwPanel::*)(kwScrollEvent&))
134 &SliderWidget::OnSliderTrack );
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,
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 );
159 //---------------------------------------------------------------------
161 //---------------------------------------------------------------------
162 // 2) Insertion of the components in the window
164 // We use a FlexGridSizer
165 kwFlexGridSizer *sizer;
171 if (mResolutionOfSlider!=NULL)
173 sizer = new kwFlexGridSizer(2);
176 sizer -> Add( new kwStaticText(panel,-1, title ) );
177 sizer -> Add( new kwStaticText(panel,-1, _T("") ) );
182 sizer = new kwFlexGridSizer(1);
185 sizer -> Add( new kwStaticText(panel,-1, title ) );
188 sizer -> Add( mkwSlider,1,kwGROW );
189 sizer -> AddGrowableCol(0);
190 if (mResolutionOfSlider!=NULL)
192 sizer -> Add( mResolutionOfSlider );
198 sizer = new kwFlexGridSizer(1);
199 if (labels==true) // with lable
201 label_vertical = new kwStaticText(panel,-1,_T(""));
202 label_min = new kwStaticText(panel,-1,_T(""));
203 label_max = new kwStaticText(panel,-1,_T(""));
206 sizer -> Add( new kwStaticText(panel,-1, title ) );
207 sizer -> AddGrowableRow(3);
211 sizer -> AddGrowableRow(2);
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)
219 sizer -> Add( mResolutionOfSlider );
226 sizer -> Add( new kwStaticText(panel,-1, title ) );
227 sizer -> AddGrowableRow(1);
231 sizer -> AddGrowableRow(0);
233 sizer -> Add( mkwSlider,1,kwGROW );
234 if (mResolutionOfSlider!=NULL)
236 sizer -> Add( mResolutionOfSlider );
240 // Initialize the labels
242 // Insert the sizer in the main panel and refresh the layout
243 panel->SetSizer(sizer);
245 //-------------------------------------------------------------------------
248 //-------------------------------------------------------------------------
249 SliderWidget::~SliderWidget()
252 //-------------------------------------------------------------------------
255 //-------------------------------------------------------------------------
256 void SliderWidget::OnResolutionOfSlider(kwScrollEvent& event)
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;
271 mkwSlider->SetRange(minTmp,maxTmp);
274 //-------------------------------------------------------------------------
278 //-------------------------------------------------------------------------
279 void SliderWidget::OnSliderTrack(kwScrollEvent& event)
283 // When user releases the slider
284 // we update the output of the box
286 mBox->bbSetOutputOut( mkwSlider->GetValue() );
287 mBox->bbSetInputIn( mkwSlider->GetValue() );
288 // and signal that the output has changed
289 mBox->bbSignalOutputModification("Out");
293 //-------------------------------------------------------------------------
296 //-------------------------------------------------------------------------
297 void SliderWidget::OnSliderRelease(kwScrollEvent& event)
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");
306 //-------------------------------------------------------------------------
309 //-------------------------------------------------------------------------
310 void SliderWidget::RefreshLabels()
313 if (label_vertical!=NULL)
315 strValue.Printf( _T("%d"), mkwSlider->GetValue() );
316 label_vertical->SetLabel(strValue);
320 strValue.Printf( _T("%d"), mkwSlider->GetMin() );
321 label_min->SetLabel(strValue);
325 strValue.Printf( _T("%d"), mkwSlider->GetMax() );
326 label_max->SetLabel(strValue);
329 //-------------------------------------------------------------------------
332 //-------------------------------------------------------------------------
333 void SliderWidget::SetRange(int min, int max)
337 mkwSlider->SetRange(min,max);
340 //-------------------------------------------------------------------------
345 //--------------------------------------------------------------------------
346 //-------------------------------------------------------------------------
347 // KwBlackBox implementation
348 //--------------------------------------------------------------------------
349 //--------------------------------------------------------------------------
351 //--------------------------------------------------------------------------
352 BBTK_BLACK_BOX_IMPLEMENTATION(Slider,bbtk::KWBlackBox);
353 BBTK_ADD_BLACK_BOX_TO_PACKAGE(kw,Slider);
355 //--------------------------------------------------------------------------
356 void Slider::bbUserConstructor()
362 bbSetOutputWidget(0);
363 bbSetInputOrientation("HORIZONTAL");
364 bbSetInputChangeResolution(false);
365 bbSetInputLabel(true);
366 bbSetInputReactiveOnTrack(0);
369 //--------------------------------------------------------------------------
370 void Slider::Process()
373 //--------------------------------------------------------------------------
374 void Slider::CreateWidget(vtkKWWidget* parent)
376 vtkKWScale* s = vtkKWScale::New();
377 bbSetOutputWidget(s);
378 s->SetParent(parent);
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)
387 s->SetOrientationToHorizontal();
389 else if (bbtk::Utilities::loosematch(bbGetInputOrientation(),"1|V|VERTICAL")==true)
391 s->SetOrientationToVertical();
395 bbtkWarning("Unrecognized value for input 'Orientation' of "<<bbGetFullName());
398 s->GetApplication()->Script("pack %s -side left -anchor c -expand y",
401 bbSetOutputOut( bbGetInputIn() );
406 #endif // USE_KWWIDGETS