00001
00002 #include <math.h>
00003 #include "WFloatLineEdit.h"
00004 #include <iostream>
00005 using std::cerr; using std::endl;
00006
00007
00008 WFloatLineEdit::WFloatLineEdit(QWidget *parent,const char *name) : QLineEdit(parent,name)
00009 {
00010 m_iPercent=-1;
00011 m_fMinValue=0.0;
00012 m_fMaxValue=1.0;
00013 m_fValue=0.0;
00014 m_bFirst=true;
00015 m_DblValid=new QDoubleValidator(m_fMinValue,m_fMaxValue,6,this);
00016 setValidator(m_DblValid);
00017 connect(this,SIGNAL(returnPressed()),
00018 this,SLOT(slotReturnPressed()));
00019 m_bInternal=false;
00020 validateAndSet(QString("%1").arg(m_fValue),0,0,0);
00021 }
00022
00023 void WFloatLineEdit::slotReturnPressed()
00024 {
00025
00026 m_bInternal=true;
00027 slotCalcFloatValue(text().toFloat());
00028 }
00029
00030 void WFloatLineEdit::slotCalcFloatValue(float f)
00031 {
00032 int p;
00033
00034
00035 if (f < m_fMinValue)
00036 f=m_fMinValue;
00037 else if (f > m_fMaxValue)
00038 f=m_fMaxValue;
00039 if (f != m_fValue || m_bFirst)
00040 {
00041 m_bFirst=false;
00042 m_fValue=f;
00043
00044 emit (floatValueChanged(f));
00045 p=(int)(100.0*(f - m_fMinValue)/(m_fMaxValue - m_fMinValue));
00046 if (p != m_iPercent)
00047 {
00048
00049 emit (valuePercentChanged(p));
00050 m_iPercent=p;
00051 }
00052 update();
00053 }
00054 validateAndSet(QString("%1").arg(m_fValue),0,0,0);
00055 }
00056
00057 void WFloatLineEdit::slotCalcFloatValue(const QString& s)
00058 {
00059 slotCalcFloatValue(s.toFloat());
00060 }
00061
00062 void WFloatLineEdit::setFloatValue(float f)
00063 {
00064 m_bInternal=true;
00065 slotCalcFloatValue(f);
00066 }
00067
00068 void WFloatLineEdit::setIntValue(int f)
00069 {
00070 setFloatValue(static_cast<float>(f));
00071 }
00072
00073 void WFloatLineEdit::setValuePercent(int p)
00074 {
00075 if (!m_bInternal)
00076 setFloatValue(m_fMinValue + (m_fMaxValue -
00077 m_fMinValue)*((double)p)/99.0);
00078 else
00079 m_bInternal=false;
00080 }
00081
00082 int WFloatLineEdit::valuePercent()
00083 {
00084 return ((int)(99.0*(m_fValue - m_fMinValue)/(m_fMaxValue - m_fMinValue)));
00085 }
00086
00087 void WFloatLineEdit::keyPressEvent(QKeyEvent *e)
00088 {
00089 if (e->key() == Qt::Key_Escape)
00090 validateAndSet(QString("%1").arg(m_fValue),0,0,0);
00091 else
00092 QLineEdit::keyPressEvent(e);
00093 }
00094
00095