Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 #include "cmtkQtSliderEntry.h"
00034 
00035 #include <stdlib.h>
00036 #include <math.h>
00037 
00038 #include <qfont.h>
00039 
00040 #include <QGridLayout>
00041 #include <QLabel>
00042 
00043 #include <Base/cmtkMathUtil.h>
00044 
00045 namespace
00046 cmtk
00047 {
00048 
00051 
00052 QtSliderEntry::QtSliderEntry( QWidget* parent )
00053   : QWidget( parent )
00054 {
00055   QFont font = this->font();
00056   font.setPointSize( 2 * font.pointSize() / 4 );
00057   
00058   Layout = new QGridLayout( this );
00059   Layout->setColumnStretch( 0, 1 );
00060   Layout->setColumnStretch( 1, 1 );
00061   Layout->setColumnStretch( 2, 0 );
00062   Layout->setColumnStretch( 3, 0 );
00063 
00064   Slider = new QSlider( Qt::Horizontal, this );
00065   QObject::connect( Slider, SIGNAL( valueChanged( int ) ), this, SLOT( slotSliderValueChanged( int ) ) );
00066 
00067   Layout->addWidget( Slider, 1, 0, 1, 2 );
00068 
00069   Edit = new QLineEdit( this );
00070   Edit->setFixedWidth( 100 );
00071   Validator = new QDoubleValidator( Edit );
00072   Edit->setValidator( Validator );
00073   QObject::connect( Edit, SIGNAL( returnPressed() ), this, SLOT( slotEditReturnPressed() ) );
00074   Layout->addWidget( Edit, 1, 3 );
00075 
00076   TitleLabel = new QLabel( this );
00077   TitleLabel->hide();
00078   MinLabel = new QLabel( this );
00079   MinLabel->setFont( font );
00080   MinLabel->hide();
00081   MaxLabel = new QLabel( this );
00082   MaxLabel->setFont( font );
00083   MaxLabel->setAlignment( Qt::AlignRight );
00084   MaxLabel->hide();
00085 
00086   Precision = 0;
00087   PrecisionFactor = 1;
00088 }
00089 
00090 double 
00091 QtSliderEntry::GetValue() const
00092 {
00093   return Edit->text().toDouble();
00094 }
00095 
00096 double
00097 QtSliderEntry::GetMinValue() const
00098 {
00099   return 1.0 * Slider->minimum() / PrecisionFactor;
00100 }
00101 
00102 double
00103 QtSliderEntry::GetMaxValue() const
00104 {
00105   return 1.0 * Slider->minimum() / PrecisionFactor;
00106 }
00107 
00108 void
00109 QtSliderEntry::slotSetRange( double rangeFrom, double rangeTo )
00110 {
00111   const double rangeWidth = rangeTo - rangeFrom;
00112   
00113   if ( rangeWidth > 0 ) 
00114     {
00115     const int autoPrecision = static_cast<int>( (log( 0.001 ) + log( rangeWidth )) / log( 0.1 ) );
00116     this->slotSetPrecision( std::max<int>( this->Precision, autoPrecision ) );
00117     }
00118   
00119   Slider->setRange( static_cast<int>( rangeFrom * PrecisionFactor ), static_cast<int>( rangeTo * PrecisionFactor ) );
00120   
00121   Validator->setRange( rangeFrom - 10 * rangeWidth, rangeTo + 10 * rangeWidth, Precision );
00122   
00123   MinLabel->setNum( rangeFrom );
00124   MaxLabel->setNum( rangeTo );
00125 }
00126 
00127 void
00128 QtSliderEntry::slotSetPrecision( int precision )
00129 {
00130   Precision = precision;
00131   PrecisionFactor = static_cast<uint>( pow( 10.0, precision ) );
00132   Validator->setDecimals( Precision );
00133 }
00134 
00135 void
00136 QtSliderEntry::slotSetTitle( const QString& title )
00137 {
00138   TitleLabel->setText( title );
00139 
00140   Layout->addWidget( TitleLabel, 0, 0, 1, 3 );
00141   TitleLabel->show();
00142 }
00143 
00144 void
00145 QtSliderEntry::slotSetMinMaxLabels( const QString& minLabel, const QString& maxLabel )
00146 {
00147   if ( minLabel != QString::null ) 
00148     {
00149     MinLabel->setText( minLabel );
00150     } 
00151   else
00152     {
00153     MinLabel->setNum( Validator->bottom() );
00154     }
00155   Layout->addWidget( MinLabel, 2, 0 );
00156   MinLabel->show();
00157   
00158   if ( maxLabel != QString::null ) 
00159     {
00160     MaxLabel->setText( maxLabel );
00161     } 
00162   else
00163     {
00164     MaxLabel->setNum( Validator->top() );
00165     }
00166   Layout->addWidget( MaxLabel, 2, 1 );
00167   MaxLabel->show();
00168 }
00169 
00170 void 
00171 QtSliderEntry::slotEditReturnPressed()
00172 {
00173   double value = Edit->text().toDouble();
00174   int valueSlider = static_cast<int>( value * PrecisionFactor );
00175 
00176   if ( valueSlider < Slider->minimum() )
00177     this->slotSetRange( value, Slider->maximum() / PrecisionFactor );
00178   if ( valueSlider > Slider->maximum() )
00179     this->slotSetRange( Slider->minimum() / PrecisionFactor, value );
00180 
00181   Slider->setValue( valueSlider );
00182   emit valueChanged( value );
00183 }
00184 
00185 void 
00186 QtSliderEntry::slotSliderValueChanged( int value )
00187 {
00188   double realValue = 1.0 * value / PrecisionFactor;
00189   QString valueString;
00190   Edit->setText( valueString.setNum( realValue, 'f', Precision ) );
00191   emit valueChanged( realValue );
00192 }
00193 
00194 void 
00195 QtSliderEntry::slotCenter()
00196 {
00197   Slider->setValue( (Slider->minimum() + Slider->maximum()) / 2 );
00198   
00199 }
00200 
00201 void 
00202 QtSliderEntry::slotSetValue( const double value )
00203 {
00204   QString valueString;
00205   Edit->setText( valueString.setNum( value, 'f', Precision ) );
00206   int valueSlider = static_cast<int>( value * PrecisionFactor );
00207 
00208   if ( valueSlider < Slider->minimum() )
00209     this->slotSetRange( value, Slider->maximum() / PrecisionFactor );
00210   if ( valueSlider > Slider->maximum() )
00211     this->slotSetRange( Slider->minimum() / PrecisionFactor, value );
00212 
00213   Slider->setValue( valueSlider );
00214   emit valueChanged( value );
00215 }
00216 
00217 }