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

List:       kde-commits
Subject:    branches/KDE/4.3/kdeplasma-addons/applets/weatherstation
From:       Petri Damstén <petri.damsten () kdemail ! net>
Date:       2009-08-06 18:02:18
Message-ID: 1249581738.359653.15349.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 1008028 by pdamsten:

Backport support for translatable labels as requested by i18n people. Adds support \
for modifying svg texts to lcd widget.

 M  +63 -66    lcd.cpp  
 M  +3 -2      lcd.h  
 M             lcd.svgz  
 M             lcd_panel.svgz  
 M  +22 -10    weatherstation.cpp  


--- branches/KDE/4.3/kdeplasma-addons/applets/weatherstation/lcd.cpp #1008027:1008028
@@ -39,12 +39,13 @@
         // lcd numbers did not look good with that.
         KSvgRenderer svg;
         bool dirty;
+        bool xmlDirty;
         QPixmap img;
         QStringList items;
         QMap<QString, QStringList> groups;
-        QHash<QString, QColor> colors;
-        QHash<QString, QString> labels;
+        QHash<QString, QDomText> texts;
         QStringList clickable;
+        QDomDocument doc;
 
         static const QString A;
         static const QString B;
@@ -149,39 +150,43 @@
             return r;
         }
 
-        void updateImage()
+        void checkIfDirty()
         {
-            if (l->size().toSize() != img.size()) {
-                img = QPixmap(l->size().toSize());
+            if (xmlDirty) {
+                //kDebug() << "xml dirty";
+                svg.load(doc.toByteArray(0));
+                xmlDirty = false;
             }
-            img.fill(Qt::transparent);
+            if (dirty || (l->size().toSize() != img.size() && l->size().toSize() != \
QSize(0, 0))) { +                //kDebug() << "Making bitmap" << l->size();
+                if (l->size().toSize() != img.size()) {
+                    img = QPixmap(l->size().toSize());
+                }
+                img.fill(Qt::transparent);
 
-            QPainter p(&img);
+                QPainter p(&img);
 
-            xScale = l->size().width() / svg.defaultSize().width();
-            yScale = l->size().height() / svg.defaultSize().height();
-            p.setRenderHint(QPainter::TextAntialiasing, true);
-            p.setRenderHint(QPainter::Antialiasing, true);
-            p.setRenderHint(QPainter::SmoothPixmapTransform, true);
+                xScale = l->size().width() / svg.defaultSize().width();
+                yScale = l->size().height() / svg.defaultSize().height();
+                p.setRenderHint(QPainter::TextAntialiasing, true);
+                p.setRenderHint(QPainter::Antialiasing, true);
+                p.setRenderHint(QPainter::SmoothPixmapTransform, true);
 
-            p.save();
-            p.scale(l->size().width() / svg.defaultSize().width(),
-                    l->size().height() / svg.defaultSize().height());
+                p.save();
+                p.scale(l->size().width() / svg.defaultSize().width(),
+                        l->size().height() / svg.defaultSize().height());
 
-            foreach (const QString& item, items) {
-                paint(&p, item);
+                foreach (const QString& item, items) {
+                    paint(&p, item);
+                }
+                p.restore();
+                dirty = false;
             }
-            p.restore();
-            foreach (const QString& label, labels.keys()) {
-                text(&p, label);
-            }
-            dirty = false;
         }
 
         void parseXml()
         {
             QIODevice *device = KFilterDev::deviceForFile(content, \
                "application/x-gzip");
-            QDomDocument doc;
 
             doc.setContent(device);
             QList<QDomNodeList> lists;
@@ -196,13 +201,22 @@
                     QString id = element.attribute("id");
                     if ((pos = id.lastIndexOf(':')) > -1) {
                         groups[id.left(pos)] << id.mid(pos + 1);
-                    } else if (element.tagName() == "rect") {
-                        if (rx.indexIn(element.attribute("style")) > -1) {
-                            colors[id] = QColor(rx.cap(1));
-                        }
                     }
                 }
             }
+            QDomNodeList list = doc.elementsByTagName("text");
+            for (int i = 0; i < list.count(); ++i) {
+                QDomElement element = list.item(i).toElement();
+                QDomNodeList l = element.elementsByTagName("tspan");
+                QDomElement e = l.item(0).toElement();
+                for (QDomNode n = e.firstChild(); !n.isNull(); n = n.nextSibling()) \
{ +                    QDomText t = n.toText();
+                    if (!t.isNull()) {
+                        texts[element.attribute("id")] = t;
+                    }
+                }
+            }
+
             //kDebug() << groups;
             delete device;
         }
@@ -222,35 +236,6 @@
             }
             return result;
         }
-
-        void text(QPainter *p, const QString elementID)
-        {
-            QString text = labels[elementID];
-
-            if (svg.elementExists(elementID)) {
-                QRectF elementRect = scaledRect(elementID);
-                Qt::Alignment align = Qt::AlignCenter;
-
-                p->setPen(QPen(colors[elementID]));
-                p->setFont(fitText(p, text, elementRect));
-                if (elementRect.width() > elementRect.height()) {
-                    p->drawText(elementRect, align, text);
-                } else {
-                    p->save();
-                    QPointF rotateCenter(
-                            elementRect.left() + elementRect.width() / 2,
-                            elementRect.top() + elementRect.height() / 2);
-                    p->translate(rotateCenter);
-                    p->rotate(-90);
-                    p->translate(elementRect.height() / -2,
-                                elementRect.width() / -2);
-                    QRectF r(0, 0, elementRect.height(), elementRect.width());
-                    p->drawText(r, align, text);
-                    p->restore();
-                }
-                //p->drawRect(elementRect);
-            }
-        }
 };
 
 QMap<QChar, QStringList> LCD::Private::sevenSegmentDigits;
@@ -287,10 +272,9 @@
     } else {
         d->content = Plasma::Theme::defaultTheme()->imagePath(svg);
     }
-    d->svg.load(d->content);
     d->parseXml();
-    setPreferredSize(d->svg.defaultSize());
     d->dirty = true;
+    d->xmlDirty = true;
     update();
 }
 
@@ -304,9 +288,7 @@
     Q_UNUSED(option)
     Q_UNUSED(widget)
 
-    if (d->dirty || size().toSize() != d->img.size()) {
-        d->updateImage();
-    }
+    d->checkIfDirty();
     p->drawPixmap(0, 0, d->img);
 }
 
@@ -386,19 +368,20 @@
 
 void LCD::setLabel(const QString &name, const QString &text)
 {
-    d->labels[name] = text;
+    if (d->texts[name].data() != text) {
+        d->texts[name].setData(text);
+        d->xmlDirty = true;
+    }
 }
 
 QString LCD::label(const QString &name) const
 {
-    return d->labels[name];
+    return d->texts[name].data();
 }
 
 QPixmap LCD::toPixmap()
 {
-    if (d->dirty || size().toSize() != d->img.size()) {
-        d->updateImage();
-    }
+    d->checkIfDirty();
     return d->img;
 }
 
@@ -431,4 +414,18 @@
     }
 }
 
+QSizeF LCD::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
+{
+    QSizeF s = QGraphicsWidget::sizeHint(which, constraint);
+    d->checkIfDirty();
+    if (which == Qt::PreferredSize) {
+        s = d->svg.defaultSize();
+    } else if (which == Qt::MinimumSize) {
+        s = d->svg.defaultSize() / 2;
+    } else {
+        s = QGraphicsWidget::sizeHint(which, constraint);
+    }
+    return s;
+}
+
 #include "lcd.moc"
--- branches/KDE/4.3/kdeplasma-addons/applets/weatherstation/lcd.h #1008027:1008028
@@ -139,14 +139,15 @@
      * Reimplemented from QGraphicsWidget
      */
     void paint(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget \
                *widget);
-    
+
 signals:
     void clicked(const QString &name);
 
 protected:
     virtual void hoverMoveEvent(QGraphicsSceneHoverEvent* event);
     virtual void mousePressEvent(QGraphicsSceneMouseEvent* event);
-    
+    virtual QSizeF sizeHint(Qt::SizeHint which, const QSizeF& constraint = QSizeF()) \
const; +
 private:
     class Private;
     Private * const d;
--- branches/KDE/4.3/kdeplasma-addons/applets/weatherstation/weatherstation.cpp \
#1008027:1008028 @@ -45,12 +45,22 @@
 {
     m_lcd = new LCD(this);
     m_lcd->setSvg("weatherstation/lcd");
-    // So we don't show in panel
-    m_lcd->setMinimumSize(m_lcd->preferredSize() / 2);
+    // i18n: This and other all-caps messages are pieces of text shown on
+    // an LCD-like image mimicking a electronic weather station display.
+    // If weather station displays in your country are always in English,
+    // you may want to consider leaving these strings in English too,
+    // to achieve a more realistic feeling.
+    m_lcd->setLabel("pressure-label", i18n("PRESSURE"));
+    m_lcd->setLabel("weather-label", i18n("CURRENT WEATHER"));
+    m_lcd->setLabel("temperature-label", i18n("OUTDOOR TEMP"));
+    m_lcd->setLabel("humidity-label", i18n("HUMIDITY"));
+    m_lcd->setLabel("wind-label", i18n("WIND"));
+    m_lcd->setLabel("provider-label", QString());
     connect(m_lcd, SIGNAL(clicked(const QString&)), this, SLOT(clicked(const \
QString&)));  
     m_lcdPanel = new LCD(this);
     m_lcdPanel->setSvg("weatherstation/lcd_panel");
+    m_lcdPanel->setLabel("temperature-label", i18n("OUTDOOR TEMP"));
     m_lcdPanel->hide();
 
     //m_lcd->setItemOn("under_construction");
@@ -144,9 +154,9 @@
         setWind(value(data["Wind Speed"].toString(),
                 WeatherUtils::getUnitString(data["Wind Speed Unit"].toInt(), true)),
                 data["Wind Direction"].toString());
-        m_lcd->setLabel("label0", data["Credit"].toString());
+        m_lcd->setLabel("provider-label", data["Credit"].toString());
         m_url = data["Credit Url"].toString();
-        m_lcd->setItemClickable("label0", !m_url.isEmpty());
+        m_lcd->setItemClickable("provider-click", !m_url.isEmpty());
 
         if (m_showToolTip) {
             Plasma::ToolTipContent ttc(data["Place"].toString(),
@@ -224,9 +234,10 @@
     current = fromCondition(condition);
     m_lcd->setGroup("weather", current);
 
-    QString s = fitValue(Conversion::Converter::self()->convert(pressure, \
pressureUnit()), 5); +    Conversion::Value value = \
Conversion::Converter::self()->convert(pressure, pressureUnit()); +    QString s = \
fitValue(value, 5);  m_lcd->setNumber("pressure", s);
-    m_lcd->setGroup("pressure_unit", QStringList() << pressureUnit());
+    m_lcd->setLabel("pressure-unit-label", value.unit()->symbol());
 
     qreal t;
     if (tendencyString.toLower() == "rising") {
@@ -249,8 +260,8 @@
 void WeatherStation::setTemperature(const Conversion::Value& temperature)
 {
     Conversion::Value v = Conversion::Converter::self()->convert(temperature, \
                temperatureUnit());
-    m_lcd->setGroup("temp_unit", QStringList() << temperatureUnit());
-    m_lcdPanel->setGroup("temp_unit", QStringList() << temperatureUnit());
+    m_lcd->setLabel("temperature-unit-label", v.unit()->symbol());
+    m_lcdPanel->setLabel("temperature-unit-label", v.unit()->symbol());
     m_lcd->setNumber("temperature", fitValue(v , 4));
     m_lcdPanel->setNumber("temperature", fitValue(v , 3));
     setLCDIcon();
@@ -269,7 +280,8 @@
 void WeatherStation::setWind(const Conversion::Value& speed, const QString& dir)
 {
     //kDebug() << speed.number() << speed.unit()->symbol() << dir;
-    QString s = fitValue(Conversion::Converter::self()->convert(speed, speedUnit()), \
3); +    Conversion::Value value = Conversion::Converter::self()->convert(speed, \
speedUnit()); +    QString s = fitValue(value, 3);
 
     if (dir == "N/A") {
         m_lcd->setGroup("wind", QStringList());
@@ -277,7 +289,7 @@
         m_lcd->setGroup("wind", QStringList() << dir);
     }
     m_lcd->setNumber("wind_speed", s);
-    m_lcd->setGroup("wind_unit", QStringList() << speedUnit());
+    m_lcd->setLabel("wind-unit-label", value.unit()->symbol());
 }
 
 void WeatherStation::clicked(const QString &name)


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

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