From ca9e98959d58c1aa402c9a65009576144b075549 Mon Sep 17 00:00:00 2001 From: Lost Robot <34612565+LostRobotMusic@users.noreply.github.com> Date: Sat, 7 Oct 2023 21:01:30 -0700 Subject: [PATCH] Fix LcdFloatSpinBox mouse behavior and negative values (#6923) * Fix LcdFloatSpinBox mouse behavior and negative values --- include/LcdFloatSpinBox.h | 6 ++++++ include/LcdWidget.h | 5 +++-- src/gui/widgets/LcdFloatSpinBox.cpp | 22 +++++++++++++--------- src/gui/widgets/LcdWidget.cpp | 16 ++++++++++++++++ 4 files changed, 38 insertions(+), 11 deletions(-) diff --git a/include/LcdFloatSpinBox.h b/include/LcdFloatSpinBox.h index 74a870114..a87588b62 100644 --- a/include/LcdFloatSpinBox.h +++ b/include/LcdFloatSpinBox.h @@ -49,6 +49,12 @@ public: } void setLabel(const QString &label) { m_label = label; } + + void setSeamless(bool left, bool right) + { + m_wholeDisplay.setSeamless(left, true); + m_fractionDisplay.setSeamless(true, right); + } public slots: virtual void update(); diff --git a/include/LcdWidget.h b/include/LcdWidget.h index f19c2c583..cef121b3f 100644 --- a/include/LcdWidget.h +++ b/include/LcdWidget.h @@ -49,8 +49,9 @@ public: ~LcdWidget() override; - void setValue( int value ); - void setLabel( const QString& label ); + void setValue(int value); + void setValue(float value); + void setLabel(const QString& label); void addTextForValue( int value, const QString& text ) { diff --git a/src/gui/widgets/LcdFloatSpinBox.cpp b/src/gui/widgets/LcdFloatSpinBox.cpp index 96f2b27e1..c7e20467a 100644 --- a/src/gui/widgets/LcdFloatSpinBox.cpp +++ b/src/gui/widgets/LcdFloatSpinBox.cpp @@ -109,11 +109,16 @@ void LcdFloatSpinBox::layoutSetup(const QString &style) void LcdFloatSpinBox::update() { - const int whole = static_cast(model()->value()); - const float fraction = model()->value() - whole; - const int intFraction = fraction * std::pow(10.f, m_fractionDisplay.numDigits()); - m_wholeDisplay.setValue(whole); - m_fractionDisplay.setValue(intFraction); + const int digitValue = std::pow(10.f, m_fractionDisplay.numDigits()); + float value = model()->value(); + int fraction = std::abs(std::round((value - static_cast(value)) * digitValue)); + if (fraction == digitValue) + { + value += std::copysign(1, value); + fraction = 0; + } + m_wholeDisplay.setValue(value); + m_fractionDisplay.setValue(fraction); QWidget::update(); } @@ -129,6 +134,9 @@ void LcdFloatSpinBox::contextMenuEvent(QContextMenuEvent* event) void LcdFloatSpinBox::mousePressEvent(QMouseEvent* event) { + // switch between integer and fractional step based on cursor position + m_intStep = event->x() < m_wholeDisplay.width(); + if (event->button() == Qt::LeftButton && !(event->modifiers() & Qt::ControlModifier) && event->y() < m_wholeDisplay.cellHeight() + 2) @@ -152,10 +160,6 @@ void LcdFloatSpinBox::mousePressEvent(QMouseEvent* event) void LcdFloatSpinBox::mouseMoveEvent(QMouseEvent* event) { - // switch between integer and fractional step based on cursor position - if (event->x() < m_wholeDisplay.width()) { m_intStep = true; } - else { m_intStep = false; } - if (m_mouseMoving) { int dy = event->globalY() - m_origMousePos.y(); diff --git a/src/gui/widgets/LcdWidget.cpp b/src/gui/widgets/LcdWidget.cpp index 0f5e13466..b8afcd46d 100644 --- a/src/gui/widgets/LcdWidget.cpp +++ b/src/gui/widgets/LcdWidget.cpp @@ -94,6 +94,22 @@ void LcdWidget::setValue(int value) update(); } +void LcdWidget::setValue(float value) +{ + if (value < 0 && value > -1) + { + QString s = QString::number(static_cast(value)); + s.prepend('-'); + + m_display = s; + update(); + } + else + { + setValue(static_cast(value)); + } +} +