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 <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2024-05-20 11:20:48 +03:00
parent 4696a44ada
commit b9161e7987
2 changed files with 44 additions and 40 deletions

View file

@ -31,6 +31,8 @@
#include <Plasma/Frame>
#include <kunitconversion.h>
#include <limits.h>
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<KTemperature::KTempUnit>(fromindex));
m_pTxtValue2->setText(QString::number(temp.convertTo(static_cast<KTemperature::KTempUnit>(toindex))));
KTemperature temp(m_pSpnValue1->value(), static_cast<KTemperature::KTempUnit>(fromindex));
m_pSpnValue2->setValue(qRound(temp.convertTo(static_cast<KTemperature::KTempUnit>(toindex))));
break;
}
case 1: {
KVelocity velo(m_pTxtValue1->text().toDouble(), static_cast<KVelocity::KVeloUnit>(fromindex));
m_pTxtValue2->setText(QString::number(velo.convertTo(static_cast<KVelocity::KVeloUnit>(toindex))));
KVelocity velo(m_pSpnValue1->value(), static_cast<KVelocity::KVeloUnit>(fromindex));
m_pSpnValue2->setValue(qRound(velo.convertTo(static_cast<KVelocity::KVeloUnit>(toindex))));
break;
}
case 2: {
KPressure pres(m_pTxtValue1->text().toDouble(), static_cast<KPressure::KPresUnit>(fromindex));
m_pTxtValue2->setText(QString::number(pres.convertTo(static_cast<KPressure::KPresUnit>(toindex))));
KPressure pres(m_pSpnValue1->value(), static_cast<KPressure::KPresUnit>(fromindex));
m_pSpnValue2->setValue(qRound(pres.convertTo(static_cast<KPressure::KPresUnit>(toindex))));
break;
}
case 3: {
KLength leng(m_pTxtValue1->text().toDouble(), static_cast<KLength::KLengUnit>(fromindex));
m_pTxtValue2->setText(QString::number(leng.convertTo(static_cast<KLength::KLengUnit>(toindex))));
KLength leng(m_pSpnValue1->value(), static_cast<KLength::KLengUnit>(fromindex));
m_pSpnValue2->setValue(qRound(leng.convertTo(static_cast<KLength::KLengUnit>(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<KTemperature::KTempUnit>(fromindex));
m_pTxtValue1->setText(QString::number(temp.convertTo(static_cast<KTemperature::KTempUnit>(toindex))));
KTemperature temp(m_pSpnValue2->value(), static_cast<KTemperature::KTempUnit>(fromindex));
m_pSpnValue1->setValue(qRound(temp.convertTo(static_cast<KTemperature::KTempUnit>(toindex))));
break;
}
case 1: {
KVelocity velo(m_pTxtValue2->text().toDouble(), static_cast<KVelocity::KVeloUnit>(fromindex));
m_pTxtValue1->setText(QString::number(velo.convertTo(static_cast<KVelocity::KVeloUnit>(toindex))));
KVelocity velo(m_pSpnValue2->value(), static_cast<KVelocity::KVeloUnit>(fromindex));
m_pSpnValue1->setValue(qRound(velo.convertTo(static_cast<KVelocity::KVeloUnit>(toindex))));
break;
}
case 2: {
KPressure pres(m_pTxtValue2->text().toDouble(), static_cast<KPressure::KPresUnit>(fromindex));
m_pTxtValue1->setText(QString::number(pres.convertTo(static_cast<KPressure::KPresUnit>(toindex))));
KPressure pres(m_pSpnValue2->value(), static_cast<KPressure::KPresUnit>(fromindex));
m_pSpnValue1->setValue(qRound(pres.convertTo(static_cast<KPressure::KPresUnit>(toindex))));
break;
}
case 3: {
KLength leng(m_pTxtValue2->text().toDouble(), static_cast<KLength::KLengUnit>(fromindex));
m_pTxtValue1->setText(QString::number(leng.convertTo(static_cast<KLength::KLengUnit>(toindex))));
KLength leng(m_pSpnValue2->value(), static_cast<KLength::KLengUnit>(fromindex));
m_pSpnValue1->setValue(qRound(leng.convertTo(static_cast<KLength::KLengUnit>(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();
}

View file

@ -23,7 +23,7 @@
#include <Plasma/PopupApplet>
#include <Plasma/ComboBox>
#include <Plasma/LineEdit>
#include <Plasma/SpinBox>
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,12 +63,12 @@ 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;
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;
};