From 04b4f36da969896e3d331f5e13b41be32be55a05 Mon Sep 17 00:00:00 2001 From: parax0 Date: Sat, 22 Aug 2015 09:43:42 -0400 Subject: [PATCH] Spin boxes now trim trailing zeroes --- UI/CWorldEditor.ui | 6 ++--- UI/WDraggableSpinBox.cpp | 55 ++++++++++++++++++++++++++++++++++++++++ UI/WDraggableSpinBox.h | 5 ++++ 3 files changed, 63 insertions(+), 3 deletions(-) diff --git a/UI/CWorldEditor.ui b/UI/CWorldEditor.ui index 40935ee2..613d16d2 100644 --- a/UI/CWorldEditor.ui +++ b/UI/CWorldEditor.ui @@ -142,7 +142,7 @@ - 70 + 78 0 @@ -195,7 +195,7 @@ - 60 + 78 0 @@ -248,7 +248,7 @@ - 60 + 78 0 diff --git a/UI/WDraggableSpinBox.cpp b/UI/WDraggableSpinBox.cpp index 453242a4..697f2029 100644 --- a/UI/WDraggableSpinBox.cpp +++ b/UI/WDraggableSpinBox.cpp @@ -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; +} diff --git a/UI/WDraggableSpinBox.h b/UI/WDraggableSpinBox.h index e51a8e50..ee9b017d 100644 --- a/UI/WDraggableSpinBox.h +++ b/UI/WDraggableSpinBox.h @@ -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