Spin boxes now trim trailing zeroes

This commit is contained in:
parax0 2015-08-22 09:43:42 -04:00
parent f0cb6169ae
commit 04b4f36da9
3 changed files with 63 additions and 3 deletions

View File

@ -142,7 +142,7 @@
</property> </property>
<property name="minimumSize"> <property name="minimumSize">
<size> <size>
<width>70</width> <width>78</width>
<height>0</height> <height>0</height>
</size> </size>
</property> </property>
@ -195,7 +195,7 @@
</property> </property>
<property name="minimumSize"> <property name="minimumSize">
<size> <size>
<width>60</width> <width>78</width>
<height>0</height> <height>0</height>
</size> </size>
</property> </property>
@ -248,7 +248,7 @@
</property> </property>
<property name="minimumSize"> <property name="minimumSize">
<size> <size>
<width>60</width> <width>78</width>
<height>0</height> <height>0</height>
</size> </size>
</property> </property>

View File

@ -8,6 +8,8 @@ WDraggableSpinBox::WDraggableSpinBox(QWidget *parent) : QDoubleSpinBox(parent)
{ {
mBeingDragged = false; mBeingDragged = false;
mDefaultValue = 0; mDefaultValue = 0;
mMinDecimals = 1;
mTrimTrailingZeroes = true;
setMinimum(-1000000.0); setMinimum(-1000000.0);
setMaximum(1000000.0); setMaximum(1000000.0);
lineEdit()->installEventFilter(this); lineEdit()->installEventFilter(this);
@ -93,7 +95,60 @@ bool WDraggableSpinBox::eventFilter(QObject *, QEvent *pEvent)
return false; return false;
} }
QString WDraggableSpinBox::textFromValue(double val) const
{
QString str = QString::number(val, 'f', decimals());
int decIndex = str.indexOf('.');
int numDecs;
if (decIndex == -1)
numDecs = 0;
else
numDecs = str.size() - decIndex - 1;
if (numDecs < mMinDecimals)
{
int size = str.size() + mMinDecimals + 1;
str.reserve(size);
str += '.';
for (int iDec = 0; iDec < mMinDecimals; iDec++)
str += '0';
}
else if ((numDecs > mMinDecimals) && mTrimTrailingZeroes)
{
while (numDecs > mMinDecimals)
{
if (str.endsWith('0')) {
str.chop(1);
numDecs--;
}
else if (str.endsWith('.')) {
str.chop(1);
break;
}
else break;
}
}
return str;
}
void WDraggableSpinBox::SetDefaultValue(double value) void WDraggableSpinBox::SetDefaultValue(double value)
{ {
mDefaultValue = value; mDefaultValue = value;
} }
void WDraggableSpinBox::SetMinDecimals(int dec)
{
mMinDecimals = dec;
}
void WDraggableSpinBox::TrimTrailingZeroes(bool trim)
{
mTrimTrailingZeroes = trim;
}

View File

@ -10,6 +10,8 @@ class WDraggableSpinBox : public QDoubleSpinBox
bool mBeenDragged; bool mBeenDragged;
double mDefaultValue; double mDefaultValue;
int mLastY; int mLastY;
int mMinDecimals;
bool mTrimTrailingZeroes;
public: public:
explicit WDraggableSpinBox(QWidget *parent = 0); explicit WDraggableSpinBox(QWidget *parent = 0);
@ -19,7 +21,10 @@ public:
void mouseMoveEvent(QMouseEvent *pEvent); void mouseMoveEvent(QMouseEvent *pEvent);
void wheelEvent(QWheelEvent *pEvent); void wheelEvent(QWheelEvent *pEvent);
bool eventFilter(QObject *pObj, QEvent *pEvent); bool eventFilter(QObject *pObj, QEvent *pEvent);
QString textFromValue(double val) const;
void SetDefaultValue(double value); void SetDefaultValue(double value);
void SetMinDecimals(int dec);
void TrimTrailingZeroes(bool trim);
}; };
#endif // WDRAGGABLESPINBOX_H #endif // WDRAGGABLESPINBOX_H