mirror of
https://bitbucket.org/smil3y/kde-extraapps.git
synced 2025-02-23 18:32:53 +00:00
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:
parent
4696a44ada
commit
b9161e7987
2 changed files with 44 additions and 40 deletions
|
@ -31,6 +31,8 @@
|
||||||
#include <Plasma/Frame>
|
#include <Plasma/Frame>
|
||||||
#include <kunitconversion.h>
|
#include <kunitconversion.h>
|
||||||
|
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
ComboBox::ComboBox(QGraphicsWidget* parent)
|
ComboBox::ComboBox(QGraphicsWidget* parent)
|
||||||
: Plasma::ComboBox(parent)
|
: Plasma::ComboBox(parent)
|
||||||
{
|
{
|
||||||
|
@ -58,7 +60,7 @@ UnitConverter::~UnitConverter()
|
||||||
cg.writeEntry("category", m_pCmbCategory->nativeWidget()->currentIndex());
|
cg.writeEntry("category", m_pCmbCategory->nativeWidget()->currentIndex());
|
||||||
cg.writeEntry("unit1", m_pCmbUnit1->nativeWidget()->currentIndex());
|
cg.writeEntry("unit1", m_pCmbUnit1->nativeWidget()->currentIndex());
|
||||||
cg.writeEntry("unit2", m_pCmbUnit2->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)
|
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
|
m_bCalculateReverse = false; // store calculation direction
|
||||||
calculate();
|
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
|
m_bCalculateReverse = true; // store calculation direction
|
||||||
calculateReverse();
|
calculateReverse();
|
||||||
}
|
}
|
||||||
|
@ -136,23 +138,23 @@ void UnitConverter::calculate()
|
||||||
const int toindex = m_pCmbUnit2->nativeWidget()->currentIndex();
|
const int toindex = m_pCmbUnit2->nativeWidget()->currentIndex();
|
||||||
switch (m_pCmbCategory->nativeWidget()->currentIndex()) {
|
switch (m_pCmbCategory->nativeWidget()->currentIndex()) {
|
||||||
case 0: {
|
case 0: {
|
||||||
KTemperature temp(m_pTxtValue1->text().toDouble(), static_cast<KTemperature::KTempUnit>(fromindex));
|
KTemperature temp(m_pSpnValue1->value(), static_cast<KTemperature::KTempUnit>(fromindex));
|
||||||
m_pTxtValue2->setText(QString::number(temp.convertTo(static_cast<KTemperature::KTempUnit>(toindex))));
|
m_pSpnValue2->setValue(qRound(temp.convertTo(static_cast<KTemperature::KTempUnit>(toindex))));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 1: {
|
case 1: {
|
||||||
KVelocity velo(m_pTxtValue1->text().toDouble(), static_cast<KVelocity::KVeloUnit>(fromindex));
|
KVelocity velo(m_pSpnValue1->value(), static_cast<KVelocity::KVeloUnit>(fromindex));
|
||||||
m_pTxtValue2->setText(QString::number(velo.convertTo(static_cast<KVelocity::KVeloUnit>(toindex))));
|
m_pSpnValue2->setValue(qRound(velo.convertTo(static_cast<KVelocity::KVeloUnit>(toindex))));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 2: {
|
case 2: {
|
||||||
KPressure pres(m_pTxtValue1->text().toDouble(), static_cast<KPressure::KPresUnit>(fromindex));
|
KPressure pres(m_pSpnValue1->value(), static_cast<KPressure::KPresUnit>(fromindex));
|
||||||
m_pTxtValue2->setText(QString::number(pres.convertTo(static_cast<KPressure::KPresUnit>(toindex))));
|
m_pSpnValue2->setValue(qRound(pres.convertTo(static_cast<KPressure::KPresUnit>(toindex))));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 3: {
|
case 3: {
|
||||||
KLength leng(m_pTxtValue1->text().toDouble(), static_cast<KLength::KLengUnit>(fromindex));
|
KLength leng(m_pSpnValue1->value(), static_cast<KLength::KLengUnit>(fromindex));
|
||||||
m_pTxtValue2->setText(QString::number(leng.convertTo(static_cast<KLength::KLengUnit>(toindex))));
|
m_pSpnValue2->setValue(qRound(leng.convertTo(static_cast<KLength::KLengUnit>(toindex))));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -165,23 +167,23 @@ void UnitConverter::calculateReverse()
|
||||||
const int toindex = m_pCmbUnit1->nativeWidget()->currentIndex();
|
const int toindex = m_pCmbUnit1->nativeWidget()->currentIndex();
|
||||||
switch (m_pCmbCategory->nativeWidget()->currentIndex()) {
|
switch (m_pCmbCategory->nativeWidget()->currentIndex()) {
|
||||||
case 0: {
|
case 0: {
|
||||||
KTemperature temp(m_pTxtValue2->text().toDouble(), static_cast<KTemperature::KTempUnit>(fromindex));
|
KTemperature temp(m_pSpnValue2->value(), static_cast<KTemperature::KTempUnit>(fromindex));
|
||||||
m_pTxtValue1->setText(QString::number(temp.convertTo(static_cast<KTemperature::KTempUnit>(toindex))));
|
m_pSpnValue1->setValue(qRound(temp.convertTo(static_cast<KTemperature::KTempUnit>(toindex))));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 1: {
|
case 1: {
|
||||||
KVelocity velo(m_pTxtValue2->text().toDouble(), static_cast<KVelocity::KVeloUnit>(fromindex));
|
KVelocity velo(m_pSpnValue2->value(), static_cast<KVelocity::KVeloUnit>(fromindex));
|
||||||
m_pTxtValue1->setText(QString::number(velo.convertTo(static_cast<KVelocity::KVeloUnit>(toindex))));
|
m_pSpnValue1->setValue(qRound(velo.convertTo(static_cast<KVelocity::KVeloUnit>(toindex))));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 2: {
|
case 2: {
|
||||||
KPressure pres(m_pTxtValue2->text().toDouble(), static_cast<KPressure::KPresUnit>(fromindex));
|
KPressure pres(m_pSpnValue2->value(), static_cast<KPressure::KPresUnit>(fromindex));
|
||||||
m_pTxtValue1->setText(QString::number(pres.convertTo(static_cast<KPressure::KPresUnit>(toindex))));
|
m_pSpnValue1->setValue(qRound(pres.convertTo(static_cast<KPressure::KPresUnit>(toindex))));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case 3: {
|
case 3: {
|
||||||
KLength leng(m_pTxtValue2->text().toDouble(), static_cast<KLength::KLengUnit>(fromindex));
|
KLength leng(m_pSpnValue2->value(), static_cast<KLength::KLengUnit>(fromindex));
|
||||||
m_pTxtValue1->setText(QString::number(leng.convertTo(static_cast<KLength::KLengUnit>(toindex))));
|
m_pSpnValue1->setValue(qRound(leng.convertTo(static_cast<KLength::KLengUnit>(toindex))));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -208,8 +210,10 @@ QGraphicsWidget *UnitConverter::graphicsWidget()
|
||||||
connect(m_pCmbUnit2, SIGNAL(mousePressed()), this, SLOT(raise()));
|
connect(m_pCmbUnit2, SIGNAL(mousePressed()), this, SLOT(raise()));
|
||||||
m_pCmbUnit1->setZValue(1);
|
m_pCmbUnit1->setZValue(1);
|
||||||
m_pCmbUnit2->setZValue(1);
|
m_pCmbUnit2->setZValue(1);
|
||||||
m_pTxtValue1 = new Plasma::LineEdit(this);
|
m_pSpnValue1 = new Plasma::SpinBox(this);
|
||||||
m_pTxtValue2 = new Plasma::LineEdit(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);
|
QGraphicsGridLayout *pGridLayout = new QGraphicsGridLayout(m_widget);
|
||||||
pGridLayout->addItem(pHeader, 0, 0, 1, 2);
|
pGridLayout->addItem(pHeader, 0, 0, 1, 2);
|
||||||
|
@ -217,8 +221,8 @@ QGraphicsWidget *UnitConverter::graphicsWidget()
|
||||||
pGridLayout->addItem(m_pCmbCategory, 1, 1);
|
pGridLayout->addItem(m_pCmbCategory, 1, 1);
|
||||||
pGridLayout->addItem(m_pCmbUnit1, 2, 0);
|
pGridLayout->addItem(m_pCmbUnit1, 2, 0);
|
||||||
pGridLayout->addItem(m_pCmbUnit2, 2, 1);
|
pGridLayout->addItem(m_pCmbUnit2, 2, 1);
|
||||||
pGridLayout->addItem(m_pTxtValue1, 3, 0);
|
pGridLayout->addItem(m_pSpnValue1, 3, 0);
|
||||||
pGridLayout->addItem(m_pTxtValue2, 3, 1);
|
pGridLayout->addItem(m_pSpnValue2, 3, 1);
|
||||||
pGridLayout->setRowStretchFactor(4, 1);
|
pGridLayout->setRowStretchFactor(4, 1);
|
||||||
|
|
||||||
m_pCmbCategory->nativeWidget()->addItem(KTemperature::description(), QVariant::fromValue(0));
|
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(KPressure::description(), QVariant::fromValue(2));
|
||||||
m_pCmbCategory->nativeWidget()->addItem(KLength::description(), QVariant::fromValue(3));
|
m_pCmbCategory->nativeWidget()->addItem(KLength::description(), QVariant::fromValue(3));
|
||||||
|
|
||||||
connect(m_pTxtValue1->nativeWidget(), SIGNAL(textEdited(QString)),
|
connect(m_pSpnValue1, SIGNAL(valueChanged(int)),
|
||||||
this, SLOT(sltValueChanged(QString)));
|
this, SLOT(sltValueChanged(int)));
|
||||||
connect(m_pTxtValue2->nativeWidget(), SIGNAL(textEdited(QString)),
|
connect(m_pSpnValue2, SIGNAL(valueChanged(int)),
|
||||||
this, SLOT(sltValueChangedReverse(QString)));
|
this, SLOT(sltValueChangedReverse(int)));
|
||||||
connect(m_pCmbCategory->nativeWidget(), SIGNAL(currentIndexChanged(int)),
|
connect(m_pCmbCategory->nativeWidget(), SIGNAL(currentIndexChanged(int)),
|
||||||
this, SLOT(sltCategoryChanged(int)));
|
this, SLOT(sltCategoryChanged(int)));
|
||||||
connect(m_pCmbUnit1->nativeWidget(), SIGNAL(currentIndexChanged(int)),
|
connect(m_pCmbUnit1->nativeWidget(), SIGNAL(currentIndexChanged(int)),
|
||||||
|
@ -257,7 +261,7 @@ void UnitConverter::configChanged()
|
||||||
if (unit2 >= 0) {
|
if (unit2 >= 0) {
|
||||||
m_pCmbUnit2->nativeWidget()->setCurrentIndex(unit2);
|
m_pCmbUnit2->nativeWidget()->setCurrentIndex(unit2);
|
||||||
}
|
}
|
||||||
m_pTxtValue1->setText(cg.readEntry("value", "1"));
|
m_pSpnValue1->setValue(cg.readEntry("value", 1));
|
||||||
calculate();
|
calculate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,7 @@
|
||||||
|
|
||||||
#include <Plasma/PopupApplet>
|
#include <Plasma/PopupApplet>
|
||||||
#include <Plasma/ComboBox>
|
#include <Plasma/ComboBox>
|
||||||
#include <Plasma/LineEdit>
|
#include <Plasma/SpinBox>
|
||||||
|
|
||||||
class ComboBox : public Plasma::ComboBox
|
class ComboBox : public Plasma::ComboBox
|
||||||
{
|
{
|
||||||
|
@ -53,8 +53,8 @@ class UnitConverter : public Plasma::PopupApplet
|
||||||
void configChanged();
|
void configChanged();
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void sltValueChanged(const QString &sNewValue);
|
void sltValueChanged(const int iNewValue);
|
||||||
void sltValueChangedReverse(const QString &sNewValue);
|
void sltValueChangedReverse(const int iNewValue);
|
||||||
void sltUnitChanged(int index);
|
void sltUnitChanged(int index);
|
||||||
void sltCategoryChanged(int index);
|
void sltCategoryChanged(int index);
|
||||||
|
|
||||||
|
@ -67,8 +67,8 @@ class UnitConverter : public Plasma::PopupApplet
|
||||||
Plasma::ComboBox* m_pCmbCategory;
|
Plasma::ComboBox* m_pCmbCategory;
|
||||||
Plasma::ComboBox* m_pCmbUnit1;
|
Plasma::ComboBox* m_pCmbUnit1;
|
||||||
Plasma::ComboBox* m_pCmbUnit2;
|
Plasma::ComboBox* m_pCmbUnit2;
|
||||||
Plasma::LineEdit *m_pTxtValue1;
|
Plasma::SpinBox* m_pSpnValue1;
|
||||||
Plasma::LineEdit *m_pTxtValue2;
|
Plasma::SpinBox* m_pSpnValue2;
|
||||||
bool m_bCalculateReverse;
|
bool m_bCalculateReverse;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue