-/*=========================================================================
- Program: bbtk
- Module: $RCSfile: bbqtQSlider.cxx~,v $
- Language: C++
- Date: $Date: 2009/04/08 07:56:33 $
- Version: $Revision: 1.1 $
-=========================================================================*/
-
-/* ---------------------------------------------------------------------
-
-* Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
-* Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
-*
-* This software is governed by the CeCILL-B license under French law and
-* abiding by the rules of distribution of free software. You can use,
-* modify and/ or redistribute the software under the terms of the CeCILL-B
-* license as circulated by CEA, CNRS and INRIA at the following URL
-* http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
-* or in the file LICENSE.txt.
-*
-* As a counterpart to the access to the source code and rights to copy,
-* modify and redistribute granted by the license, users are provided only
-* with a limited warranty and the software's author, the holder of the
-* economic rights, and the successive licensors have only limited
-* liability.
-*
-* The fact that you are presently reading this means that you have had
-* knowledge of the CeCILL-B license and that you accept its terms.
-* ------------------------------------------------------------------------ */
-
-/**
- * \file
- * \brief Short description in one line
- *
- * Long
- * description
- *
- */
-
-#ifdef USE_QT
-
-#include "bbqtQSlider.h"
-#include "bbqtPackage.h"
-#include "bbtkUtilities.h"
-
-namespace bbqt
-{
-
- /*
- //--------------------------------------------------------------------------
- // The widget created by the box
- class SliderWidget : public qtPanel
- {
- public:
- /// Ctor with the two first params the parent window and the creator box
- /// which must be passed to the QtBlackBoxWidget constructor.
- /// The other params initialize the widget
- SliderWidget(Slider* box, qtWindow *parent,
- int orientation,
- bool changeresolution,
- bool label,
- qtString title,
- int vmin,
- int vmax,
- int value,
- bool track);
- /// Dtor
- ~SliderWidget();
- /// Events callbacks
- /// Called when the slider is moved
- void OnSliderTrack(qtScrollEvent& event);
- /// Called when the slider is released
- void OnSliderRelease(qtScrollEvent& event);
- /// Called when the little slider which controls the resolution
- /// of the main slider is moved (if activated)
- void OnResolutionOfSlider(qtScrollEvent& event);
-
- // Accessors
- void SetValue(int v) { mqtSlider->SetValue(v); }
- int GetValue() { return mqtSlider->GetValue(); }
- void SetRange(int min, int max);
- // Update the texts which display the min/max/current values of the slider
- void RefreshLabels();
-
- private:
- Slider* mBox;
- qtSlider *mqtSlider;
- qtSlider *mResolutionOfSlider;
- int min;
- int max;
- bool reactiveOnTrack;
- qtStaticText *label_vertical;
- qtStaticText *label_min;
- qtStaticText *label_max;
- };
- //------------------------------------------------------------------------
- //------------------------------------------------------------------------
- //------------------------------------------------------------------------
-
-
-
- //-------------------------------------------------------------------------
- SliderWidget::SliderWidget(Slider* box, qtWindow *parent,
- int orientation,
- bool changeresolution,
- bool labels,
- qtString title,
- int vmin,
- int vmax,
- int value,
- bool reactiveontrack)
- :
- qtPanel( parent, -1, qtDefaultPosition, qtDefaultSize, qtTAB_TRAVERSAL),
- mBox(box),
- min(vmin),
- max(vmax),
- reactiveOnTrack(reactiveontrack)
- {
- qtPanel * panel = this;
-
- label_min = NULL;
- label_max = NULL;
- label_vertical = NULL;
- mResolutionOfSlider = NULL;
-
- int sizeX,sizeY;
- long qtlabels=0;
- long qtorientation=0;
- if (orientation==0)
- {
- sizeX = 2;
- sizeY = 40;
- qtorientation = qtSL_HORIZONTAL;
- if (labels==true)
- {
- qtlabels = qtSL_LABELS;
- }
- }
- else
- {
- sizeX = 20;
- sizeY = 2;
- qtorientation = qtSL_VERTICAL;
- }
-
- //---------------------------------------------------------------------
- // 1) Creation of the components of the widget
- // Any top level sub-widget must have the panel returned by panel
- // for parent
- mqtSlider = new qtSlider( panel,
- -1, 0 , 0, 500 ,
- qtDefaultPosition,
- qtSize(sizeX,sizeY),
- qtorientation | qtlabels );
-
- // mqtSlider->SetInitialSize(qtSize(sizeX,sizeY));
-
- // mqtSlider->SetTickFreq(100,0);
- mqtSlider->SetRange(min,max);
- mqtSlider->SetValue(value);
-
- // RefreshLabels();
-
- // Connecting events to callbacks
- Connect( mqtSlider->GetId(),
- qtEVT_SCROLL_THUMBRELEASE,
- (qtObjectEventFunction)
- (void (qtPanel::*)(qtScrollEvent&))
- &SliderWidget::OnSliderRelease );
-
- Connect( mqtSlider->GetId(),
- qtEVT_SCROLL_THUMBTRACK,
- (qtObjectEventFunction)
- (void (qtPanel::*)(qtScrollEvent&))
- &SliderWidget::OnSliderTrack );
-
- Connect( mqtSlider->GetId(),
- qtEVT_SCROLL_CHANGED,
- (qtObjectEventFunction)
- (void (qtPanel::*)(qtScrollEvent&))
- &SliderWidget::OnSliderTrack );
-
- // If asked : creation of the other little slider which allows to change
- // the resolution of the main slider
- if (changeresolution==true){
- // has panel for parent too
- mResolutionOfSlider = new qtSlider(panel,
- -1,5,1,10,
- qtDefaultPosition,
- qtSize(25,45),
- qtSL_VERTICAL |
- qtSL_AUTOTICKS |
- qtSL_LEFT );
-
-
- mResolutionOfSlider->SetRange(1,8);
- mResolutionOfSlider->SetValue(5);
- // Is qtEVT_COMMAND_SLIDER_UPDATED event
- // is connected to the method OnResolutionOfSlider
- Connect(mResolutionOfSlider->GetId(),
- qtEVT_COMMAND_SLIDER_UPDATED,
- (qtObjectEventFunction)
- (void (qtPanel::*)(qtScrollEvent&))
- &SliderWidget::OnResolutionOfSlider );
- }
- //---------------------------------------------------------------------
-
- //---------------------------------------------------------------------
- // 2) Insertion of the components in the window
-
- // We use a FlexGridSizer
- qtFlexGridSizer *sizer;
-
-
- if (orientation==0)
- {
- // HORIZONTAL
- if (mResolutionOfSlider!=NULL)
- {
- sizer = new qtFlexGridSizer(2);
- if (title!=_T(""))
- {
- sizer -> Add( new qtStaticText(panel,-1, title ) );
- sizer -> Add( new qtStaticText(panel,-1, _T("") ) );
- }
- }
- else
- {
- sizer = new qtFlexGridSizer(1);
- if (title!=_T(""))
- {
- sizer -> Add( new qtStaticText(panel,-1, title ) );
- }
- }
- sizer -> Add( mqtSlider,1,qtGROW );
- sizer -> AddGrowableCol(0);
- if (mResolutionOfSlider!=NULL)
- {
- sizer -> Add( mResolutionOfSlider );
- }
- }
- else
- {
- // VERTICAL
- sizer = new qtFlexGridSizer(1);
- if (labels==true) // with lable
- {
- label_vertical = new qtStaticText(panel,-1,_T(""));
- label_min = new qtStaticText(panel,-1,_T(""));
- label_max = new qtStaticText(panel,-1,_T(""));
- if (title!=_T(""))
- {
- sizer -> Add( new qtStaticText(panel,-1, title ) );
- sizer -> AddGrowableRow(3);
- }
- else
- {
- sizer -> AddGrowableRow(2);
- }
- sizer -> Add( label_vertical );
- sizer -> Add( label_min );
- sizer -> Add( mqtSlider,1,qtGROW );
- sizer -> Add( label_max );
- if (mResolutionOfSlider!=NULL)
- {
- sizer -> Add( mResolutionOfSlider );
- }
- }
- else
- {
- if (title!=_T(""))
- {
- sizer -> Add( new qtStaticText(panel,-1, title ) );
- sizer -> AddGrowableRow(1);
- }
- else
- {
- sizer -> AddGrowableRow(0);
- }
- sizer -> Add( mqtSlider,1,qtGROW );
- if (mResolutionOfSlider!=NULL)
- {
- sizer -> Add( mResolutionOfSlider );
- }
- }
- }
- // Initialize the labels
- RefreshLabels();
- // Insert the sizer in the main panel and refresh the layout
- panel->SetSizer(sizer);
- }
- //-------------------------------------------------------------------------
-
-
- //-------------------------------------------------------------------------
- SliderWidget::~SliderWidget()
- {
- }
- //-------------------------------------------------------------------------
-
-
- //-------------------------------------------------------------------------
- void SliderWidget::OnResolutionOfSlider(qtScrollEvent& event)
- {
- int value = mqtSlider->GetValue();
- int delta = (int) (pow( 4 , mResolutionOfSlider->GetValue() ));
- int minTmp = value - delta/2;
- int maxTmp = value + delta/2;
- if (minTmp<min)
- {
- minTmp = min;
- // maxTmp = delta;
- }
- if (maxTmp>max)
- {
- maxTmp = max;
- }
- mqtSlider->SetRange(minTmp,maxTmp);
- RefreshLabels();
- }
- //-------------------------------------------------------------------------
-
-
-
- //-------------------------------------------------------------------------
- void SliderWidget::OnSliderTrack(qtScrollEvent& event)
- {
- if(reactiveOnTrack)
- {
- // When user releases the slider
- // we update the output of the box
-
- mBox->bbSetOutputOut( mqtSlider->GetValue() );
- mBox->bbSetInputIn( mqtSlider->GetValue() );
- // and signal that the output has changed
- mBox->bbSignalOutputModification(std::string("Out"));
- }
- RefreshLabels();
- }
- //-------------------------------------------------------------------------
-
-
- //-------------------------------------------------------------------------
- void SliderWidget::OnSliderRelease(qtScrollEvent& event)
- {
- // When user releases the slider
- // we update the output of the box
- mBox->bbSetOutputOut( mqtSlider->GetValue() );
- mBox->bbSetInputIn( mqtSlider->GetValue() );
- // and signal that the output has changed
- mBox->bbSignalOutputModification(std::string("Out"));
- }
- //-------------------------------------------------------------------------
-
-
- //-------------------------------------------------------------------------
- void SliderWidget::RefreshLabels()
- {
- qtString strValue;
- if (label_vertical!=NULL)
- {
- strValue.Printf( _T("%d"), mqtSlider->GetValue() );
- label_vertical->SetLabel(strValue);
- }
- if (label_min!=NULL)
- {
- strValue.Printf( _T("%d"), mqtSlider->GetMin() );
- label_min->SetLabel(strValue);
- }
- if (label_max!=NULL)
- {
- strValue.Printf( _T("%d"), mqtSlider->GetMax() );
- label_max->SetLabel(strValue);
- }
- }
- //-------------------------------------------------------------------------
-
-
- //-------------------------------------------------------------------------
- void SliderWidget::SetRange(int min, int max)
- {
- this->min = min;
- this->max = max;
- mqtSlider->SetRange(min,max);
- RefreshLabels();
- }
- //-------------------------------------------------------------------------
-
- */
-
-
- //--------------------------------------------------------------------------
- //-------------------------------------------------------------------------
- // QtBlackBox implementation
- //--------------------------------------------------------------------------
- //--------------------------------------------------------------------------
-
- //--------------------------------------------------------------------------
- BBTK_BLACK_BOX_IMPLEMENTATION(Slider,bbtk::QtBlackBox);
- BBTK_ADD_BLACK_BOX_TO_PACKAGE(qt,Slider);
-
- //--------------------------------------------------------------------------
- void Slider::bbUserConstructor()
- {
- bbSetInputIn(0);
- bbSetInputMin(0);
- bbSetInputMax(500);
- bbSetOutputOut(0);
- bbSetInputOrientation("HORIZONTAL");
- bbSetInputChangeResolution(false);
- bbSetInputLabel(true);
- bbSetInputReactiveOnTrack(false);
- }
- //--------------------------------------------------------------------------
-
- //--------------------------------------------------------------------------
- void Slider::Process()
- {
- bbtkDebugMessage("process",3,
- "Slider "<<bbGetName()<<" input="
- <<bbGetInputIn()<<std::endl);
-
- bbSetOutputOut( bbGetInputIn() );
- if (bbGetOutputWidget()!=0)
- {
- ((QSlider*)bbGetOutputWidget())->setValue(bbGetInputIn());
- }
- }
- //--------------------------------------------------------------------------
-
- //--------------------------------------------------------------------------
- void Slider::CreateWidget(QWidget* parent)
- {
-
- Qt::Orientation orientation;
- if (bbtk::Utilities::loosematch(bbGetInputOrientation(),"0|H|HORIZONTAL")
- ==true)
- { orientation = Qt::Horizontal; }
- if (bbtk::Utilities::loosematch(bbGetInputOrientation(),"1|V|VERTICAL")
- ==true)
- { orientation = Qt::Vertical ; }
-
-
- // std::cout << "bbGetQtParent = "<<bbGetQtParent()<<std::endl;
- QSlider *w = new QSlider(parent);
- w->setOrientation(orientation);
- w->setMinimum( bbGetInputMin() );
- w->setMaximum( bbGetInputMax() );
- w->setSliderPosition ( bbGetInputIn() );
-
- if (bbGetInputReactiveOnTrack())
- w->setTracking(true);
- else
- w->setTracking(false);
-
- connect(w,SIGNAL(valueChanged(int)),this,SLOT(OnValueChanged(int)));
-
- w->setSizePolicy( QSizePolicy(QSizePolicy::Expanding,
- QSizePolicy::Expanding) );
-
- bbSetOutputWidget( w );
- }
- //--------------------------------------------------------------------------
-
- //--------------------------------------------------------------------------
- void Slider::OnValueChanged(int v)
- {
- std::cout << "OnValueChanged "<<v<<std::endl;
- bbSetOutputOut( v );
- bbSetInputIn( v );
- // and signal that the output has changed
- bbSignalOutputModification(std::string("Out"));
-
- }
- //--------------------------------------------------------------------------
-
-
-} //namespace bbqt
-
-#endif // USE_QT
-
-