1 /*=========================================================================
3 Module: $RCSfile: bbqtQSlider.cxx~,v $
5 Date: $Date: 2009/04/08 07:56:33 $
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 "bbqtQSlider.h"
43 #include "bbqtPackage.h"
44 #include "bbtkUtilities.h"
50 //--------------------------------------------------------------------------
51 // The widget created by the box
52 class SliderWidget : public qtPanel
55 /// Ctor with the two first params the parent window and the creator box
56 /// which must be passed to the QtBlackBoxWidget constructor.
57 /// The other params initialize the widget
58 SliderWidget(Slider* box, qtWindow *parent,
60 bool changeresolution,
70 /// Called when the slider is moved
71 void OnSliderTrack(qtScrollEvent& event);
72 /// Called when the slider is released
73 void OnSliderRelease(qtScrollEvent& event);
74 /// Called when the little slider which controls the resolution
75 /// of the main slider is moved (if activated)
76 void OnResolutionOfSlider(qtScrollEvent& event);
79 void SetValue(int v) { mqtSlider->SetValue(v); }
80 int GetValue() { return mqtSlider->GetValue(); }
81 void SetRange(int min, int max);
82 // Update the texts which display the min/max/current values of the slider
88 qtSlider *mResolutionOfSlider;
92 qtStaticText *label_vertical;
93 qtStaticText *label_min;
94 qtStaticText *label_max;
96 //------------------------------------------------------------------------
97 //------------------------------------------------------------------------
98 //------------------------------------------------------------------------
102 //-------------------------------------------------------------------------
103 SliderWidget::SliderWidget(Slider* box, qtWindow *parent,
105 bool changeresolution,
111 bool reactiveontrack)
113 qtPanel( parent, -1, qtDefaultPosition, qtDefaultSize, qtTAB_TRAVERSAL),
117 reactiveOnTrack(reactiveontrack)
119 qtPanel * panel = this;
123 label_vertical = NULL;
124 mResolutionOfSlider = NULL;
128 long qtorientation=0;
133 qtorientation = qtSL_HORIZONTAL;
136 qtlabels = qtSL_LABELS;
143 qtorientation = qtSL_VERTICAL;
146 //---------------------------------------------------------------------
147 // 1) Creation of the components of the widget
148 // Any top level sub-widget must have the panel returned by panel
150 mqtSlider = new qtSlider( panel,
154 qtorientation | qtlabels );
156 // mqtSlider->SetInitialSize(qtSize(sizeX,sizeY));
158 // mqtSlider->SetTickFreq(100,0);
159 mqtSlider->SetRange(min,max);
160 mqtSlider->SetValue(value);
164 // Connecting events to callbacks
165 Connect( mqtSlider->GetId(),
166 qtEVT_SCROLL_THUMBRELEASE,
167 (qtObjectEventFunction)
168 (void (qtPanel::*)(qtScrollEvent&))
169 &SliderWidget::OnSliderRelease );
171 Connect( mqtSlider->GetId(),
172 qtEVT_SCROLL_THUMBTRACK,
173 (qtObjectEventFunction)
174 (void (qtPanel::*)(qtScrollEvent&))
175 &SliderWidget::OnSliderTrack );
177 Connect( mqtSlider->GetId(),
178 qtEVT_SCROLL_CHANGED,
179 (qtObjectEventFunction)
180 (void (qtPanel::*)(qtScrollEvent&))
181 &SliderWidget::OnSliderTrack );
183 // If asked : creation of the other little slider which allows to change
184 // the resolution of the main slider
185 if (changeresolution==true){
186 // has panel for parent too
187 mResolutionOfSlider = new qtSlider(panel,
196 mResolutionOfSlider->SetRange(1,8);
197 mResolutionOfSlider->SetValue(5);
198 // Is qtEVT_COMMAND_SLIDER_UPDATED event
199 // is connected to the method OnResolutionOfSlider
200 Connect(mResolutionOfSlider->GetId(),
201 qtEVT_COMMAND_SLIDER_UPDATED,
202 (qtObjectEventFunction)
203 (void (qtPanel::*)(qtScrollEvent&))
204 &SliderWidget::OnResolutionOfSlider );
206 //---------------------------------------------------------------------
208 //---------------------------------------------------------------------
209 // 2) Insertion of the components in the window
211 // We use a FlexGridSizer
212 qtFlexGridSizer *sizer;
218 if (mResolutionOfSlider!=NULL)
220 sizer = new qtFlexGridSizer(2);
223 sizer -> Add( new qtStaticText(panel,-1, title ) );
224 sizer -> Add( new qtStaticText(panel,-1, _T("") ) );
229 sizer = new qtFlexGridSizer(1);
232 sizer -> Add( new qtStaticText(panel,-1, title ) );
235 sizer -> Add( mqtSlider,1,qtGROW );
236 sizer -> AddGrowableCol(0);
237 if (mResolutionOfSlider!=NULL)
239 sizer -> Add( mResolutionOfSlider );
245 sizer = new qtFlexGridSizer(1);
246 if (labels==true) // with lable
248 label_vertical = new qtStaticText(panel,-1,_T(""));
249 label_min = new qtStaticText(panel,-1,_T(""));
250 label_max = new qtStaticText(panel,-1,_T(""));
253 sizer -> Add( new qtStaticText(panel,-1, title ) );
254 sizer -> AddGrowableRow(3);
258 sizer -> AddGrowableRow(2);
260 sizer -> Add( label_vertical );
261 sizer -> Add( label_min );
262 sizer -> Add( mqtSlider,1,qtGROW );
263 sizer -> Add( label_max );
264 if (mResolutionOfSlider!=NULL)
266 sizer -> Add( mResolutionOfSlider );
273 sizer -> Add( new qtStaticText(panel,-1, title ) );
274 sizer -> AddGrowableRow(1);
278 sizer -> AddGrowableRow(0);
280 sizer -> Add( mqtSlider,1,qtGROW );
281 if (mResolutionOfSlider!=NULL)
283 sizer -> Add( mResolutionOfSlider );
287 // Initialize the labels
289 // Insert the sizer in the main panel and refresh the layout
290 panel->SetSizer(sizer);
292 //-------------------------------------------------------------------------
295 //-------------------------------------------------------------------------
296 SliderWidget::~SliderWidget()
299 //-------------------------------------------------------------------------
302 //-------------------------------------------------------------------------
303 void SliderWidget::OnResolutionOfSlider(qtScrollEvent& event)
305 int value = mqtSlider->GetValue();
306 int delta = (int) (pow( 4 , mResolutionOfSlider->GetValue() ));
307 int minTmp = value - delta/2;
308 int maxTmp = value + delta/2;
318 mqtSlider->SetRange(minTmp,maxTmp);
321 //-------------------------------------------------------------------------
325 //-------------------------------------------------------------------------
326 void SliderWidget::OnSliderTrack(qtScrollEvent& event)
330 // When user releases the slider
331 // we update the output of the box
333 mBox->bbSetOutputOut( mqtSlider->GetValue() );
334 mBox->bbSetInputIn( mqtSlider->GetValue() );
335 // and signal that the output has changed
336 mBox->bbSignalOutputModification(std::string("Out"));
340 //-------------------------------------------------------------------------
343 //-------------------------------------------------------------------------
344 void SliderWidget::OnSliderRelease(qtScrollEvent& event)
346 // When user releases the slider
347 // we update the output of the box
348 mBox->bbSetOutputOut( mqtSlider->GetValue() );
349 mBox->bbSetInputIn( mqtSlider->GetValue() );
350 // and signal that the output has changed
351 mBox->bbSignalOutputModification(std::string("Out"));
353 //-------------------------------------------------------------------------
356 //-------------------------------------------------------------------------
357 void SliderWidget::RefreshLabels()
360 if (label_vertical!=NULL)
362 strValue.Printf( _T("%d"), mqtSlider->GetValue() );
363 label_vertical->SetLabel(strValue);
367 strValue.Printf( _T("%d"), mqtSlider->GetMin() );
368 label_min->SetLabel(strValue);
372 strValue.Printf( _T("%d"), mqtSlider->GetMax() );
373 label_max->SetLabel(strValue);
376 //-------------------------------------------------------------------------
379 //-------------------------------------------------------------------------
380 void SliderWidget::SetRange(int min, int max)
384 mqtSlider->SetRange(min,max);
387 //-------------------------------------------------------------------------
392 //--------------------------------------------------------------------------
393 //-------------------------------------------------------------------------
394 // QtBlackBox implementation
395 //--------------------------------------------------------------------------
396 //--------------------------------------------------------------------------
398 //--------------------------------------------------------------------------
399 BBTK_BLACK_BOX_IMPLEMENTATION(Slider,bbtk::QtBlackBox);
400 BBTK_ADD_BLACK_BOX_TO_PACKAGE(qt,Slider);
402 //--------------------------------------------------------------------------
403 void Slider::bbUserConstructor()
409 bbSetInputOrientation("HORIZONTAL");
410 bbSetInputChangeResolution(false);
411 bbSetInputLabel(true);
412 bbSetInputReactiveOnTrack(false);
414 //--------------------------------------------------------------------------
416 //--------------------------------------------------------------------------
417 void Slider::Process()
419 bbtkDebugMessage("process",3,
420 "Slider "<<bbGetName()<<" input="
421 <<bbGetInputIn()<<std::endl);
423 bbSetOutputOut( bbGetInputIn() );
424 if (bbGetOutputWidget()!=0)
426 ((QSlider*)bbGetOutputWidget())->setValue(bbGetInputIn());
429 //--------------------------------------------------------------------------
431 //--------------------------------------------------------------------------
432 void Slider::CreateWidget(QWidget* parent)
435 Qt::Orientation orientation;
436 if (bbtk::Utilities::loosematch(bbGetInputOrientation(),"0|H|HORIZONTAL")
438 { orientation = Qt::Horizontal; }
439 if (bbtk::Utilities::loosematch(bbGetInputOrientation(),"1|V|VERTICAL")
441 { orientation = Qt::Vertical ; }
444 // std::cout << "bbGetQtParent = "<<bbGetQtParent()<<std::endl;
445 QSlider *w = new QSlider(parent);
446 w->setOrientation(orientation);
447 w->setMinimum( bbGetInputMin() );
448 w->setMaximum( bbGetInputMax() );
449 w->setSliderPosition ( bbGetInputIn() );
451 if (bbGetInputReactiveOnTrack())
452 w->setTracking(true);
454 w->setTracking(false);
456 connect(w,SIGNAL(valueChanged(int)),this,SLOT(OnValueChanged(int)));
458 w->setSizePolicy( QSizePolicy(QSizePolicy::Expanding,
459 QSizePolicy::Expanding) );
461 bbSetOutputWidget( w );
463 //--------------------------------------------------------------------------
465 //--------------------------------------------------------------------------
466 void Slider::OnValueChanged(int v)
468 std::cout << "OnValueChanged "<<v<<std::endl;
471 // and signal that the output has changed
472 bbSignalOutputModification(std::string("Out"));
475 //--------------------------------------------------------------------------