[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    KDE/kdeplasma-addons/applets/unitconverter
From:       Gerhard Gappmeier <gappy1502 () gmx ! net>
Date:       2010-09-02 17:48:11
Message-ID: 20100902174811.0EE1FAC884 () svn ! kde ! org
[Download RAW message or body]

SVN commit 1171095 by gergap:

Add reverse calculation support.

Now you can convert also from the right to the left.
The lineedit that is changed determines calculation direction.
The applet stores the last used direction and reuses this if one of the units is \
changed.



 M  +39 -2     unitconverter.cpp  
 M  +3 -0      unitconverter.h  


--- trunk/KDE/kdeplasma-addons/applets/unitconverter/unitconverter.cpp \
#1171094:1171095 @@ -39,7 +39,7 @@
 
 UnitConverter::UnitConverter(QObject *parent, const QVariantList &args)
 : Plasma::PopupApplet(parent, args)
-, m_widget(0)
+, m_widget(0), m_bCalculateReverse(false)
 {
     KGlobal::locale()->insertCatalog("libconversion");
     setAspectRatioMode(Plasma::IgnoreAspectRatio);
@@ -95,15 +95,29 @@
 void UnitConverter::sltUnitChanged(int index)
 {
     Q_UNUSED(index);
+    if ( m_bCalculateReverse ) {
+        calculateReverse();
+    }
+    else {
     calculate();
 }
+}
 
 void UnitConverter::sltValueChanged(const QString &sNewValue)
 {
     Q_UNUSED(sNewValue);
+    m_bCalculateReverse = false; // store calculation direction
     calculate();
 }
 
+void UnitConverter::sltValueChangedReverse(const QString &sNewValue)
+{
+    Q_UNUSED(sNewValue);
+    m_bCalculateReverse = true; // store calculation direction
+    calculateReverse();
+}
+
+/// Calculates from left to right
 void UnitConverter::calculate()
 {
     UnitPtr in = m_pCmbUnit1->nativeWidget()->itemData(
@@ -124,6 +138,27 @@
     }
 }
 
+/// Calculates from right to left
+void UnitConverter::calculateReverse()
+{
+    UnitPtr in = m_pCmbUnit2->nativeWidget()->itemData(
+            m_pCmbUnit2->nativeWidget()->currentIndex()).value<UnitPtr>();
+    UnitPtr out = m_pCmbUnit1->nativeWidget()->itemData(
+            m_pCmbUnit1->nativeWidget()->currentIndex()).value<UnitPtr>();
+    if (!in.isNull() && !out.isNull()) {
+        Value dblValueIn(m_pTxtValue2->text().toDouble(), in);
+        Value dblValueOut = dblValueIn.convertTo(out->id());
+        QRegExp decimalCheck("^\\d+\\.0$");
+        QRegExp onlyDecimal("^\\d+$");
+        if(decimalCheck.exactMatch(m_pTxtValue2->text()) && \
onlyDecimal.exactMatch(QString::number(dblValueOut.number()))) { +           QString \
addZero = QString::number(dblValueOut.number()) + ".0"; +           \
m_pTxtValue1->setText(addZero); +        } else {
+           m_pTxtValue1->setText(QString::number(dblValueOut.number()));
+        }
+    }
+}
+
 QGraphicsWidget *UnitConverter::graphicsWidget()
 {
     if (!m_widget) {
@@ -164,8 +199,10 @@
         }
         m_pCmbCategory->nativeWidget()->model()->sort(0);
 
-        connect(m_pTxtValue1->nativeWidget(), SIGNAL(textChanged(const QString&)),
+        connect(m_pTxtValue1->nativeWidget(), SIGNAL(textEdited(const QString&)),
                 this, SLOT(sltValueChanged(const QString&)));
+        connect(m_pTxtValue2->nativeWidget(), SIGNAL(textEdited(const QString&)),
+                this, SLOT(sltValueChangedReverse(const QString&)));
         connect(m_pCmbCategory->nativeWidget(), SIGNAL(currentIndexChanged(int)),
                 this, SLOT(sltCategoryChanged(int)));
         connect(m_pCmbUnit1->nativeWidget(), SIGNAL(currentIndexChanged(int)),
--- trunk/KDE/kdeplasma-addons/applets/unitconverter/unitconverter.h #1171094:1171095
@@ -47,11 +47,13 @@
 
     private slots:
         void sltValueChanged(const QString &sNewValue);
+        void sltValueChangedReverse(const QString &sNewValue);
         void sltUnitChanged(int index);
         void sltCategoryChanged(int index);
 
     private:
         void calculate();
+        void calculateReverse();
 
     private:
         QGraphicsWidget  *m_widget;
@@ -63,6 +65,7 @@
         Plasma::Label    *m_pInfo;
         int               m_iCategory;
         KUnitConversion::Converter m_converter;
+        bool              m_bCalculateReverse;
 };
 
 // This is the command that links your applet to the .desktop file


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic