00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <StreamCenter.hpp>
00021
00022 #include <QRealSpinBox.hpp>
00023
00024 #include <cmath>
00025
00026 QRealSpinBox::QRealSpinBox(QWidget* parent)
00027 : QSpinBox(parent),
00028 __validator(new QDoubleValidator( -2e10, 2e10, 2, this))
00029 {
00030 this->setRange(int(-2e10), int(2e10));
00031 fferr(1) << __FILE__ << ':' << __LINE__ << ":warning: Validator not inactivated\n";
00032
00033 this->setValue( 0.0 );
00034 }
00035
00036 QRealSpinBox::QRealSpinBox(const real_t& minValue,
00037 const real_t& maxValue,
00038 const int& decimals,
00039 const real_t& step,
00040 QWidget* parent)
00041 : QSpinBox(parent ),
00042 __validator(new QDoubleValidator( minValue, maxValue, 2, this)),
00043 __decimals( decimals )
00044 {
00045 this->setRange(int( minValue * std::pow( 10., decimals )), int( maxValue * std::pow( 10., decimals ) ));
00046 this->setSingleStep(int(rint(step * std::pow(10., decimals))));
00047 fferr(1) << __FILE__ << ':' << __LINE__ << ":warning: Validator not inactivated\n";
00048
00049 this->setValue( 0.5*(minValue+maxValue) );
00050 }
00051
00052 QRealSpinBox::~QRealSpinBox()
00053 {
00054 ;
00055 }
00056
00057 QString QRealSpinBox::mapValueToText ( int givenValue )
00058 {
00059 QString s;
00060 s.setNum(givenValue/std::pow( 10., __decimals ), 'f', __decimals );
00061 return s;
00062 }
00063
00064 int QRealSpinBox::mapTextToValue ( bool * ok )
00065 {
00066 return int( cleanText().toFloat( ok ) * std::pow( 10., __decimals ) );
00067 }
00068
00069 real_t QRealSpinBox::value() const
00070 {
00071 return QSpinBox::value()/std::pow( 10., __decimals );
00072 }
00073
00074 int QRealSpinBox::percentage() const
00075 {
00076 if (maximum() != minimum()) {
00077 return 100 * (QSpinBox::value() - minimum())
00078 / (maximum() - minimum());
00079 } else {
00080 return 100;
00081 }
00082 }
00083 void QRealSpinBox::setPercentage(int i)
00084 {
00085
00086 this->QSpinBox::setValue(int((QSpinBox::minimum()*(99.-i)
00087 + QSpinBox::maximum()*i)/99.));
00088 }
00089
00090
00091 real_t QRealSpinBox::minValue() const
00092 {
00093 return minimum()/std::pow( 10., __decimals );
00094 }
00095
00096 real_t QRealSpinBox::maxValue() const
00097 {
00098 return maximum()/std::pow( 10., __decimals );
00099 }
00100
00101 void QRealSpinBox::setValue( real_t givenValue )
00102 {
00103 this->QSpinBox::setValue(int(givenValue * std::pow( 10., __decimals )));
00104 }
00105
00106 void QRealSpinBox::setRange( real_t minValue, real_t maxValue )
00107 {
00108 QSpinBox::setRange(int(minValue * std::pow(10., __decimals )),
00109 int(maxValue * std::pow(10., __decimals )));
00110 __validator->setRange(minValue, maxValue, 2);
00111 }
00112
00113 void QRealSpinBox::valueChange()
00114 {
00115 emit valueChanged(value());
00116 }
00117
00118 QSize QRealSpinBox::sizeHint() const
00119 {
00120 QFontMetrics fm = fontMetrics();
00121 int h = fm.height();
00122 if ( h < 12 )
00123 h = 12;
00124 int w = 35;
00125 int wx = fm.width( " " );
00126 QString s;
00127 s.setNum( minValue(), 'f', __decimals );
00128 s.prepend( prefix() );
00129 s.append( suffix() );
00130 w = std::max( w, fm.width( s ) + wx );
00131 s.setNum( maxValue(), 'f', __decimals );
00132 s.prepend( prefix() );
00133 s.append( suffix() );
00134 w = std::max( w, fm.width( s ) + wx );
00135 s = specialValueText();
00136 w = std::max( w, fm.width( s ) + wx );
00137
00138 QSize r ( h
00139 + 6
00140 + w,
00141
00142
00143 + 4
00144 + h
00145 );
00146 return r;
00147 }