From 89773cc69c625174c4b07aa9938e4382822bdc05 Mon Sep 17 00:00:00 2001 From: Ivailo Monev Date: Mon, 6 May 2024 01:20:43 +0300 Subject: [PATCH] plasma: implement number increment/decrement on mouse wheel event for calculator applet Signed-off-by: Ivailo Monev --- plasma/applets/calculator/calculator.cpp | 28 ++++++++++++++++++------ plasma/applets/calculator/calculator.h | 4 +++- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/plasma/applets/calculator/calculator.cpp b/plasma/applets/calculator/calculator.cpp index 499c3237..757fad40 100644 --- a/plasma/applets/calculator/calculator.cpp +++ b/plasma/applets/calculator/calculator.cpp @@ -28,11 +28,12 @@ static const QString s_decimal = QString::fromLatin1("."); static const QLatin1String s_zero = QLatin1String("0"); +// hard-limit +static const int s_limit = 6; -static QString kAddNumber(const QString &string, const ushort number) +static QString kAddNumber(const QString &string, const short number) { - if (string.size() > 6) { - // hard-limit + if (string.size() > s_limit) { return string; } if (string == s_zero) { @@ -63,6 +64,8 @@ public: CalculatorAppletWidget(QGraphicsWidget *parent); + void addToNumber(const short number); + public Q_SLOTS: void slotClear(); void slotDiv(); @@ -294,6 +297,11 @@ CalculatorAppletWidget::CalculatorAppletWidget(QGraphicsWidget *parent) ); } +void CalculatorAppletWidget::addToNumber(const short number) +{ + m_label->setText(QString::number(m_label->text().toFloat() + number)); +} + void CalculatorAppletWidget::slotClear() { m_label->setText(QString::fromLatin1("0")); @@ -466,10 +474,6 @@ QGraphicsWidget* CalculatorApplet::graphicsWidget() return m_calculatorwidget; } -// TODO: two ideas come into mind: -// 1. increment/decrement the number on mouse wheel event -// 2. instead of calling slots directly trigger buttons press and then release as indicator why the -// number changed (even tho it will happen very quickly) void CalculatorApplet::keyPressEvent(QKeyEvent *event) { bool eventhandled = false; @@ -577,5 +581,15 @@ void CalculatorApplet::keyPressEvent(QKeyEvent *event) } } +void CalculatorApplet::wheelEvent(QGraphicsSceneWheelEvent *event) +{ + event->ignore(); + if (event->delta() > 0) { + m_calculatorwidget->addToNumber(1); + } else { + m_calculatorwidget->addToNumber(-1); + } +} + #include "moc_calculator.cpp" #include "calculator.moc" diff --git a/plasma/applets/calculator/calculator.h b/plasma/applets/calculator/calculator.h index b17b62b4..78774888 100644 --- a/plasma/applets/calculator/calculator.h +++ b/plasma/applets/calculator/calculator.h @@ -19,6 +19,7 @@ #ifndef CALCULATOR_H #define CALCULATOR_H +#include #include class CalculatorAppletWidget; @@ -34,8 +35,9 @@ public: QGraphicsWidget* graphicsWidget() final; protected: - // QGraphicsWidget reimplementation + // QGraphicsWidget reimplementations void keyPressEvent(QKeyEvent *event) final; + void wheelEvent(QGraphicsSceneWheelEvent *event) final; private: friend CalculatorAppletWidget;