From b9161e7987ed3d2f94b6811a33230bf006a7f05f Mon Sep 17 00:00:00 2001 From: Ivailo Monev Date: Mon, 20 May 2024 11:20:48 +0300 Subject: [PATCH] kdeplasma-addons: use Plasma::SpinBox for the values in unitcoverter applet that means no double values but otherwise the input could contain letters and then conversion will fail Signed-off-by: Ivailo Monev --- .../applets/unitconverter/unitconverter.cpp | 64 ++++++++++--------- .../applets/unitconverter/unitconverter.h | 20 +++--- 2 files changed, 44 insertions(+), 40 deletions(-) diff --git a/kdeplasma-addons/applets/unitconverter/unitconverter.cpp b/kdeplasma-addons/applets/unitconverter/unitconverter.cpp index e5d0d5ed..5eb4f147 100644 --- a/kdeplasma-addons/applets/unitconverter/unitconverter.cpp +++ b/kdeplasma-addons/applets/unitconverter/unitconverter.cpp @@ -31,6 +31,8 @@ #include #include +#include + ComboBox::ComboBox(QGraphicsWidget* parent) : Plasma::ComboBox(parent) { @@ -58,7 +60,7 @@ UnitConverter::~UnitConverter() cg.writeEntry("category", m_pCmbCategory->nativeWidget()->currentIndex()); cg.writeEntry("unit1", m_pCmbUnit1->nativeWidget()->currentIndex()); cg.writeEntry("unit2", m_pCmbUnit2->nativeWidget()->currentIndex()); - cg.writeEntry("value", m_pTxtValue1->text()); + cg.writeEntry("value", m_pSpnValue1->value()); } void UnitConverter::sltCategoryChanged(int index) @@ -115,16 +117,16 @@ void UnitConverter::sltUnitChanged(int index) } } -void UnitConverter::sltValueChanged(const QString &sNewValue) +void UnitConverter::sltValueChanged(const int iNewValue) { - Q_UNUSED(sNewValue); + Q_UNUSED(iNewValue); m_bCalculateReverse = false; // store calculation direction calculate(); } -void UnitConverter::sltValueChangedReverse(const QString &sNewValue) +void UnitConverter::sltValueChangedReverse(const int iNewValue) { - Q_UNUSED(sNewValue); + Q_UNUSED(iNewValue); m_bCalculateReverse = true; // store calculation direction calculateReverse(); } @@ -136,23 +138,23 @@ void UnitConverter::calculate() const int toindex = m_pCmbUnit2->nativeWidget()->currentIndex(); switch (m_pCmbCategory->nativeWidget()->currentIndex()) { case 0: { - KTemperature temp(m_pTxtValue1->text().toDouble(), static_cast(fromindex)); - m_pTxtValue2->setText(QString::number(temp.convertTo(static_cast(toindex)))); + KTemperature temp(m_pSpnValue1->value(), static_cast(fromindex)); + m_pSpnValue2->setValue(qRound(temp.convertTo(static_cast(toindex)))); break; } case 1: { - KVelocity velo(m_pTxtValue1->text().toDouble(), static_cast(fromindex)); - m_pTxtValue2->setText(QString::number(velo.convertTo(static_cast(toindex)))); + KVelocity velo(m_pSpnValue1->value(), static_cast(fromindex)); + m_pSpnValue2->setValue(qRound(velo.convertTo(static_cast(toindex)))); break; } case 2: { - KPressure pres(m_pTxtValue1->text().toDouble(), static_cast(fromindex)); - m_pTxtValue2->setText(QString::number(pres.convertTo(static_cast(toindex)))); + KPressure pres(m_pSpnValue1->value(), static_cast(fromindex)); + m_pSpnValue2->setValue(qRound(pres.convertTo(static_cast(toindex)))); break; } case 3: { - KLength leng(m_pTxtValue1->text().toDouble(), static_cast(fromindex)); - m_pTxtValue2->setText(QString::number(leng.convertTo(static_cast(toindex)))); + KLength leng(m_pSpnValue1->value(), static_cast(fromindex)); + m_pSpnValue2->setValue(qRound(leng.convertTo(static_cast(toindex)))); break; } } @@ -165,23 +167,23 @@ void UnitConverter::calculateReverse() const int toindex = m_pCmbUnit1->nativeWidget()->currentIndex(); switch (m_pCmbCategory->nativeWidget()->currentIndex()) { case 0: { - KTemperature temp(m_pTxtValue2->text().toDouble(), static_cast(fromindex)); - m_pTxtValue1->setText(QString::number(temp.convertTo(static_cast(toindex)))); + KTemperature temp(m_pSpnValue2->value(), static_cast(fromindex)); + m_pSpnValue1->setValue(qRound(temp.convertTo(static_cast(toindex)))); break; } case 1: { - KVelocity velo(m_pTxtValue2->text().toDouble(), static_cast(fromindex)); - m_pTxtValue1->setText(QString::number(velo.convertTo(static_cast(toindex)))); + KVelocity velo(m_pSpnValue2->value(), static_cast(fromindex)); + m_pSpnValue1->setValue(qRound(velo.convertTo(static_cast(toindex)))); break; } case 2: { - KPressure pres(m_pTxtValue2->text().toDouble(), static_cast(fromindex)); - m_pTxtValue1->setText(QString::number(pres.convertTo(static_cast(toindex)))); + KPressure pres(m_pSpnValue2->value(), static_cast(fromindex)); + m_pSpnValue1->setValue(qRound(pres.convertTo(static_cast(toindex)))); break; } case 3: { - KLength leng(m_pTxtValue2->text().toDouble(), static_cast(fromindex)); - m_pTxtValue1->setText(QString::number(leng.convertTo(static_cast(toindex)))); + KLength leng(m_pSpnValue2->value(), static_cast(fromindex)); + m_pSpnValue1->setValue(qRound(leng.convertTo(static_cast(toindex)))); break; } } @@ -208,8 +210,10 @@ QGraphicsWidget *UnitConverter::graphicsWidget() connect(m_pCmbUnit2, SIGNAL(mousePressed()), this, SLOT(raise())); m_pCmbUnit1->setZValue(1); m_pCmbUnit2->setZValue(1); - m_pTxtValue1 = new Plasma::LineEdit(this); - m_pTxtValue2 = new Plasma::LineEdit(this); + m_pSpnValue1 = new Plasma::SpinBox(this); + m_pSpnValue1->setRange(-SHRT_MAX, SHRT_MAX); + m_pSpnValue2 = new Plasma::SpinBox(this); + m_pSpnValue2->setRange(-SHRT_MAX, SHRT_MAX); QGraphicsGridLayout *pGridLayout = new QGraphicsGridLayout(m_widget); pGridLayout->addItem(pHeader, 0, 0, 1, 2); @@ -217,8 +221,8 @@ QGraphicsWidget *UnitConverter::graphicsWidget() pGridLayout->addItem(m_pCmbCategory, 1, 1); pGridLayout->addItem(m_pCmbUnit1, 2, 0); pGridLayout->addItem(m_pCmbUnit2, 2, 1); - pGridLayout->addItem(m_pTxtValue1, 3, 0); - pGridLayout->addItem(m_pTxtValue2, 3, 1); + pGridLayout->addItem(m_pSpnValue1, 3, 0); + pGridLayout->addItem(m_pSpnValue2, 3, 1); pGridLayout->setRowStretchFactor(4, 1); m_pCmbCategory->nativeWidget()->addItem(KTemperature::description(), QVariant::fromValue(0)); @@ -226,10 +230,10 @@ QGraphicsWidget *UnitConverter::graphicsWidget() m_pCmbCategory->nativeWidget()->addItem(KPressure::description(), QVariant::fromValue(2)); m_pCmbCategory->nativeWidget()->addItem(KLength::description(), QVariant::fromValue(3)); - connect(m_pTxtValue1->nativeWidget(), SIGNAL(textEdited(QString)), - this, SLOT(sltValueChanged(QString))); - connect(m_pTxtValue2->nativeWidget(), SIGNAL(textEdited(QString)), - this, SLOT(sltValueChangedReverse(QString))); + connect(m_pSpnValue1, SIGNAL(valueChanged(int)), + this, SLOT(sltValueChanged(int))); + connect(m_pSpnValue2, SIGNAL(valueChanged(int)), + this, SLOT(sltValueChangedReverse(int))); connect(m_pCmbCategory->nativeWidget(), SIGNAL(currentIndexChanged(int)), this, SLOT(sltCategoryChanged(int))); connect(m_pCmbUnit1->nativeWidget(), SIGNAL(currentIndexChanged(int)), @@ -257,7 +261,7 @@ void UnitConverter::configChanged() if (unit2 >= 0) { m_pCmbUnit2->nativeWidget()->setCurrentIndex(unit2); } - m_pTxtValue1->setText(cg.readEntry("value", "1")); + m_pSpnValue1->setValue(cg.readEntry("value", 1)); calculate(); } diff --git a/kdeplasma-addons/applets/unitconverter/unitconverter.h b/kdeplasma-addons/applets/unitconverter/unitconverter.h index 72e3e330..e329c886 100644 --- a/kdeplasma-addons/applets/unitconverter/unitconverter.h +++ b/kdeplasma-addons/applets/unitconverter/unitconverter.h @@ -23,7 +23,7 @@ #include #include -#include +#include class ComboBox : public Plasma::ComboBox { @@ -53,8 +53,8 @@ class UnitConverter : public Plasma::PopupApplet void configChanged(); private slots: - void sltValueChanged(const QString &sNewValue); - void sltValueChangedReverse(const QString &sNewValue); + void sltValueChanged(const int iNewValue); + void sltValueChangedReverse(const int iNewValue); void sltUnitChanged(int index); void sltCategoryChanged(int index); @@ -63,13 +63,13 @@ class UnitConverter : public Plasma::PopupApplet void calculateReverse(); private: - QGraphicsWidget *m_widget; - Plasma::ComboBox *m_pCmbCategory; - Plasma::ComboBox *m_pCmbUnit1; - Plasma::ComboBox *m_pCmbUnit2; - Plasma::LineEdit *m_pTxtValue1; - Plasma::LineEdit *m_pTxtValue2; - bool m_bCalculateReverse; + QGraphicsWidget* m_widget; + Plasma::ComboBox* m_pCmbCategory; + Plasma::ComboBox* m_pCmbUnit1; + Plasma::ComboBox* m_pCmbUnit2; + Plasma::SpinBox* m_pSpnValue1; + Plasma::SpinBox* m_pSpnValue2; + bool m_bCalculateReverse; }; // This is the command that links your applet to the .desktop file