WFloatLineEdit.cpp
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 #include <math.h>
00029 #include "WFloatLineEdit.h"
00030 #include <iostream>
00031 using std::cerr; using std::endl;
00032
00033
00034 WFloatLineEdit::WFloatLineEdit(QWidget *parent,const char *name) : QLineEdit(parent,name)
00035 {
00036 m_iPercent=-1;
00037 m_fMinValue=0.0;
00038 m_fMaxValue=1.0;
00039 m_fValue=0.0;
00040 m_bFirst=true;
00041 m_DblValid=new QDoubleValidator(m_fMinValue,m_fMaxValue,6,this);
00042 setValidator(m_DblValid);
00043
00044 connect(this,SIGNAL(returnPressed()),
00045 this,SLOT (slotReturnPressed()));
00046
00047 m_bInternal=false;
00048 validateAndSet(QString("%1").arg(m_fValue),0,0,0);
00049 }
00050
00051 void WFloatLineEdit::slotReturnPressed()
00052 {
00053
00054 m_bInternal=true;
00055
00056 slotCalcFloatValue(text().toFloat());
00057
00058 }
00059
00060 void WFloatLineEdit::slotCalcFloatValue(float f)
00061 {
00062 int p;
00063
00064
00065 if (f < m_fMinValue)
00066 f=m_fMinValue;
00067 else if (f > m_fMaxValue)
00068 f=m_fMaxValue;
00069 if (f != m_fValue || m_bFirst)
00070 {
00071 m_bFirst=false;
00072 m_fValue=f;
00073
00074 emit (floatValueChanged(f));
00075 p=(int)(100.0*(f - m_fMinValue)/(m_fMaxValue - m_fMinValue));
00076 if (p != m_iPercent)
00077 {
00078
00079 emit (valuePercentChanged(p));
00080 m_iPercent=p;
00081 }
00082 update();
00083 }
00084 validateAndSet(QString("%1").arg(m_fValue),0,0,0);
00085 }
00086
00087 void WFloatLineEdit::slotCalcFloatValue(const QString& s)
00088 {
00089 slotCalcFloatValue(s.toFloat());
00090 }
00091
00092 void WFloatLineEdit::setFloatValue(float f)
00093 {
00094 m_bInternal=true;
00095 slotCalcFloatValue(f);
00096 }
00097
00098 void WFloatLineEdit::setIntValue(int f)
00099 {
00100 setFloatValue(static_cast<float>(f));
00101 }
00102
00103 void WFloatLineEdit::setValuePercent(int p)
00104 {
00105 if (!m_bInternal)
00106 setFloatValue(m_fMinValue + (m_fMaxValue -
00107 m_fMinValue)*((double)p)/99.0);
00108 else
00109 m_bInternal=false;
00110 }
00111
00112 int WFloatLineEdit::valuePercent()
00113 {
00114 return ((int)(99.0*(m_fValue - m_fMinValue)/(m_fMaxValue - m_fMinValue)));
00115 }
00116
00117 void WFloatLineEdit::keyPressEvent(QKeyEvent *e)
00118 {
00119 if (e->key() == Qt::Key_Escape)
00120 validateAndSet(QString("%1").arg(m_fValue),0,0,0);
00121 else
00122 QLineEdit::keyPressEvent(e);
00123 }
00124
00125