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

View File

@ -8,6 +8,8 @@ WDraggableSpinBox::WDraggableSpinBox(QWidget *parent) : QDoubleSpinBox(parent)
{
mBeingDragged = false;
mDefaultValue = 0;
mMinDecimals = 1;
mTrimTrailingZeroes = true;
setMinimum(-1000000.0);
setMaximum(1000000.0);
lineEdit()->installEventFilter(this);
@ -93,7 +95,60 @@ bool WDraggableSpinBox::eventFilter(QObject *, QEvent *pEvent)
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)
{
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;
double mDefaultValue;
int mLastY;
int mMinDecimals;
bool mTrimTrailingZeroes;
public:
explicit WDraggableSpinBox(QWidget *parent = 0);
@ -19,7 +21,10 @@ public:
void mouseMoveEvent(QMouseEvent *pEvent);
void wheelEvent(QWheelEvent *pEvent);
bool eventFilter(QObject *pObj, QEvent *pEvent);
QString textFromValue(double val) const;
void SetDefaultValue(double value);
void SetMinDecimals(int dec);
void TrimTrailingZeroes(bool trim);
};
#endif // WDRAGGABLESPINBOX_H