⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 yawp.cpp

📁 KDE4.1 weather plasmoid
💻 CPP
字号:
//// C++ Implementation: yawp//// Description:////// Author: Ruan <ruans@kr8.co.za>, (C) 2008//// Copyright: Do what you want, whenever you want.////#include "yawp.h"#include "yawpconfig.h"#include "yawpwserv.h"#include <QPainter>#include <QFontMetrics>#include <QSizeF>#include <KConfigGroup>#include <QTimer>#define YAWP_MINUTE 60000#define YAWP_ORIG_SIZEX (273.0f)#define YAWP_ORIG_SIZEY (255.0f)//-----------------------------------------------------------------------------YaWP::YaWP(QObject *parent, const QVariantList &args) : Plasma::Applet(parent, args), m_svg(this) {    //NULLS    m_cdiag = NULL;    m_wservice = NULL;        m_svg.setImagePath("widgets/yawp_theme02");    m_svg.setContainsMultipleImages(false);        setHasConfigurationInterface(true);    setAspectRatioMode(Plasma::KeepAspectRatio);        resize(YAWP_ORIG_SIZEX, YAWP_ORIG_SIZEY);        m_font.setFamily("Sans Serif");    m_units     = YaWPDay::METRIC;    m_city      = "Nanjing";    m_country   = "China";    m_theme     = "default";    m_service   = "google";    m_interval  = 30;    m_message   = "Connecting...";        m_days.append(YaWPDay());    m_days.append(YaWPDay());    m_days.append(YaWPDay());    m_days.append(YaWPDay());    m_wservice = new YaWPGoogleWeatherService();    connect(m_wservice, SIGNAL(completed(QObject *, int)), this, SLOT(updateInfo(QObject *, int)));        m_timer = new QTimer(this);    m_timer->setInterval(5000);    connect(m_timer, SIGNAL(timeout()), this, SLOT(timeout()));}//-----------------------------------------------------------------------------YaWP::~YaWP() {    if (hasFailedToLaunch()) {        // Perform some cleanup    } else {        saveConfig();    }}//-----------------------------------------------------------------------------void YaWP::init() {    m_timer->stop();    loadConfig();    m_timer->start();}//-----------------------------------------------------------------------------void YaWP::constraintsEvent(Plasma::Constraints constraints) {    if (constraints & Plasma::FormFactorConstraint) {        if(m_theme == "default")            setBackgroundHints(DefaultBackground);        else            setBackgroundHints(NoBackground);            }        if (constraints & Plasma::SizeConstraint) {        //reserve    }}//-----------------------------------------------------------------------------void YaWP::paintInterface(QPainter *p, const QStyleOptionGraphicsItem *option, const QRect &contentsRect) {    Q_UNUSED(option);        //Day's title    #define YAWP_ORIG_DAYY      (104.0f * scale)    #define YAWP_ORIG_DAYH      (60.0f  * scale)                //Top large current degrees    #define YAWP_ORIG_DEGX      (180.0f * scale)    #define YAWP_ORIG_DEGY      (4.0f   * scale)    #define YAWP_ORIG_DEGH      (46.0f  * scale)    //Today' temp    #define YAWP_ORIG_CURHIGHY  (5.0f * scale)     #define YAWP_ORIG_CURHIGHH  (25.0f  * scale)    #define YAWP_ORIG_CURLOWY   (27.0f * scale)     #define YAWP_ORIG_CURLOWH   (25.0f  * scale)    //Today' wind and humidity    #define YAWP_ORIG_CURHUMY   (60.0f * scale)     #define YAWP_ORIG_CURHUMW   (100.0f * scale)    #define YAWP_ORIG_CURHUMH   (25.0f  * scale)    //Day current    #define YAWP_ORIG_CURX      (10.0f  * scale)    #define YAWP_ORIG_CURY      (6.0f   * scale)    #define YAWP_ORIG_CURW      (90.0f  * scale)    #define YAWP_ORIG_CURH      (130.0f * scale)    //Padding    #define YAWP_ORIG_PADD      (10.0f  * scale)    //City label    #define YAWP_ORIG_CITYY     (75.0f * scale)     //Emblem sizes    #define YAWP_ORIG_EMBL      (90.0f  * scale)    #define YAWP_ORIG_EMBS      (50.0f  * scale)    //Day's emblems y    #define YAWP_ORIG_EMBY      (155.0f * scale)     //Day' temp    #define YAWP_ORIG_HIGHY     (202.0f  * scale)     #define YAWP_ORIG_HIGHH     (25.0f  * scale)    #define YAWP_ORIG_LOWY      (222.0f  * scale)    #define YAWP_ORIG_LOWH      (25.0f  * scale)    #define DRAWTEXT(rect, align, text)({\    p->setPen(QColor(0,0,0,100));\    p->drawText(rect.translated(shadow, shadow), align, text);\    p->setPen(Qt::white);\    p->drawText(rect, align, text);\    })    #define DRAWTEXTGREY(rect, align, text)({\    p->setPen(QColor(0,0,0,100));\    p->drawText(rect.translated(1.0f * scale, 1.0f * scale), align, text);\    p->setPen(Qt::gray);\    p->drawText(rect, align, text);\    })    float width, height, shadow, scale, colWidth;    QRectF wrect, rect;    const YaWPDay *day = NULL;    p->setRenderHint(QPainter::SmoothPixmapTransform);    wrect = contentsRect;    width = wrect.width();    height = wrect.height();    scale = width / YAWP_ORIG_SIZEX;    shadow = 2.0f * scale;    colWidth = width / 3.0f;    p->setRenderHint(QPainter::SmoothPixmapTransform);    p->setRenderHint(QPainter::Antialiasing);    //Draw back    m_svg.paint(p, wrect, QString("back-%1").arg(m_theme));    p->translate ( wrect.topLeft()  );    QFont font = m_font;    //NOTE Should not be here, but people are complaining fonts don't fit? I though that perhaps majestically things changed?    font.setFamily("Sans Serif");    font.setBold(true);    font.setPointSize(16.0f * scale);    p->setFont(font);    //Draw head    day = &(m_days.at(0));    //Draw current degrees    font.setPointSize(40.0f * scale);    font.setBold(false);    p->setFont(font);    rect.setRect(YAWP_ORIG_DEGX, YAWP_ORIG_DEGY, width - YAWP_ORIG_DEGX - YAWP_ORIG_PADD / 1.5f, YAWP_ORIG_DEGH);    DRAWTEXT(rect, Qt::AlignRight | Qt::AlignVCenter, QString("%1").arg(YaWPDay::convertDegrees(day->units(), m_units, day->current())) + QChar(0xBA));    //Draw today high low degrees    font.setBold(true);    font.setPointSize(16.0f * scale);    p->setFont(font);    rect.setRect(YAWP_ORIG_PADD, YAWP_ORIG_CURHIGHY, colWidth / 1.5f, YAWP_ORIG_CURHIGHH);    DRAWTEXT(rect, Qt::AlignHCenter | Qt::AlignVCenter, QString("H: %1").arg(YaWPDay::convertDegrees(day->units(), m_units, day->high())) + QChar(0xB0));    font.setPointSize(14.0f * scale);    p->setFont(font);    rect.setRect( YAWP_ORIG_PADD, YAWP_ORIG_CURLOWY, colWidth / 1.5f, YAWP_ORIG_CURLOWH);    DRAWTEXTGREY(rect, Qt::AlignHCenter | Qt::AlignVCenter, QString("L: %1").arg(YaWPDay::convertDegrees(day->units(), m_units, day->low())) + QChar(0xB0));    //Draw today humidity and wind degrees    font.setBold(false);    font.setPointSize(11.0f * scale);    p->setFont(font);    rect.setRect(YAWP_ORIG_PADD / 2.0f, YAWP_ORIG_CURHUMY, YAWP_ORIG_CURHUMW, YAWP_ORIG_CURHUMH);    DRAWTEXT(rect, Qt::AlignHCenter | Qt::AlignVCenter, QString("H: %1%").arg(day->humidity()));    font.setBold(false);    font.setPointSize(10.0f * scale);    p->setFont(font);    rect.setRect(width - YAWP_ORIG_PADD / 2.0f - YAWP_ORIG_CURHUMW, YAWP_ORIG_CURHUMY, YAWP_ORIG_CURHUMW, YAWP_ORIG_CURHUMH);    if(m_units == YaWPDay::IMPERIAL)        DRAWTEXT(rect, Qt::AlignHCenter | Qt::AlignVCenter, QString("W: %1 mph %2").arg(YaWPDay::convertDistance(day->units(), m_units, day->windSpeed())).arg(day->windDirection()));    else        DRAWTEXT(rect, Qt::AlignHCenter | Qt::AlignVCenter, QString("W: %1 kmph %2").arg(YaWPDay::convertDistance(day->units(), m_units, day->windSpeed())).arg(day->windDirection()));    //Draw city    font.setBold(false);    font.setPointSize(12.0f * scale);    p->setFont(font);    rect.setRect(YAWP_ORIG_PADD, YAWP_ORIG_CITYY, width - YAWP_ORIG_PADD, YAWP_ORIG_DAYH);    DRAWTEXT(rect, Qt::AlignHCenter | Qt::AlignVCenter, QString("%1").arg(m_message));    //Draw today's emblem    rect.setRect((width - YAWP_ORIG_EMBL) / 2.0f, YAWP_ORIG_PADD/1.8f, YAWP_ORIG_EMBL, YAWP_ORIG_EMBL);    m_svg.paint(p, rect, day->emblem());    //Draw days    for (int i = 0; i < 3; i++) {        day = &(m_days.at(i + 1));        font.setBold(true);        font.setPointSize(16.0f * scale);        p->setFont(font);        rect.setRect(colWidth * i, YAWP_ORIG_DAYY, colWidth, YAWP_ORIG_DAYH);        DRAWTEXT(rect, Qt::AlignHCenter | Qt::AlignVCenter, day->date());        rect.setRect(colWidth * i + colWidth / 2.0f - YAWP_ORIG_EMBS / 2.0f, YAWP_ORIG_EMBY, YAWP_ORIG_EMBS, YAWP_ORIG_EMBS);        m_svg.paint(p, rect, day->emblem());        font.setPointSize(14.0f * scale);        p->setFont(font);        rect.setRect(colWidth * i, YAWP_ORIG_HIGHY, colWidth, YAWP_ORIG_HIGHH);        DRAWTEXT(rect, Qt::AlignHCenter | Qt::AlignVCenter, QString("H: %1").arg(YaWPDay::convertDegrees(day->units(), m_units, day->high())) + QChar(0xBA));        font.setPointSize(12.0f * scale);        p->setFont(font);        rect.setRect( colWidth * i, YAWP_ORIG_LOWY, colWidth, YAWP_ORIG_LOWH);        DRAWTEXTGREY(rect, Qt::AlignHCenter | Qt::AlignVCenter, QString("L: %1").arg(YaWPDay::convertDegrees(day->units(), m_units, day->low())) + QChar(0xBA));    }            }//-----------------------------------------------------------------------------void YaWP::updateMessage(QString mess) {    m_message = mess;    update();}//-----------------------------------------------------------------------------void YaWP::timeout() {    updateMessage("Connecting...");    m_wservice->update(m_country, m_city);}//-----------------------------------------------------------------------------void YaWP::updateInfo(QObject * service, int exitCode) {    if(exitCode == YaWPWeatherService::SUCCESS) {        if(m_timer->interval() !=  YAWP_MINUTE * m_interval) {            m_timer->setInterval(YAWP_MINUTE * m_interval);        }        m_days = ((YaWPWeatherService *)service)->days();        updateMessage(QString("%1  %2").arg(m_city).arg(m_country));    }        if(exitCode == YaWPWeatherService::DATA_FAILED)        updateMessage("Bad city or country!");        if(exitCode == YaWPWeatherService::CONNECTION_FAILED) {        m_timer->stop();        m_timer->setInterval(YAWP_MINUTE);        updateMessage("Connecting...");        m_timer->start();    }        if(exitCode == YaWPWeatherService::ERROR)        updateMessage("ERROR!");}//-----------------------------------------------------------------------------void YaWP::loadConfig() {    KConfigGroup cfg = config();    m_city = cfg.readEntry("city", m_city);    m_country = cfg.readEntry("country", m_country);    m_theme = cfg.readEntry("theme", m_theme);    //Ensure change    constraintsEvent(Plasma::FormFactorConstraint);    m_units = cfg.readEntry("units", QString("%1").arg((int)m_units)).toInt();    m_interval = cfg.readEntry("interval", QString("%1").arg(m_interval)).toInt();    if(m_interval < 15)        m_interval = 30;    if((int)m_units < 0)        m_units = YaWPDay::METRIC;}//-----------------------------------------------------------------------------void YaWP::saveConfig() {    KConfigGroup cfg = config();    cfg.writeEntry("units", QString("%1").arg((int)m_units));    cfg.writeEntry("city", m_city);    cfg.writeEntry("country", m_country);    cfg.writeEntry("theme", m_theme);    cfg.writeEntry("service", m_service);    cfg.writeEntry("interval", QString("%1").arg(m_interval));}//-----------------------------------------------------------------------------void YaWP::showConfigurationInterface() {    if ( m_cdiag == NULL ) {        m_cdiag = new YaWPConfigDialog( this );        connect(m_cdiag, SIGNAL(save()), this, SLOT(settingsChanged()));    }    if ( m_cdiag != NULL ) {        m_cdiag->setVisible(true);        m_cdiag->setCity(m_city);        m_cdiag->setCountry(m_country);        m_cdiag->setInterval(m_interval);        m_cdiag->setTheme(m_theme);        m_cdiag->setUnits(m_units);    }}//-----------------------------------------------------------------------------void YaWP::settingsChanged() {    if ( m_cdiag == NULL )        return;    m_theme = m_cdiag->theme();    //Ensure change    constraintsEvent(Plasma::FormFactorConstraint);    m_units = m_cdiag->units();    m_cdiag->setVisible(false);    m_city = m_cdiag->city();    m_country = m_cdiag->country();    m_interval = m_cdiag->interval();    if(m_interval < 15)        m_interval = 30;    if((int)m_units < 0)        m_units = YaWPDay::METRIC;    saveConfig();    timeout();}K_EXPORT_PLASMA_APPLET(yawp, YaWP)#include "yawp.moc"

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -