From 602ec861270583d24abd3190ce1f76cf8a8fad96 Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 <0xFEEDC0DE64@gmail.com> Date: Tue, 19 Dec 2017 21:34:44 +0100 Subject: [PATCH 1/2] Added WeatherWidget --- plugins/weatherplugin/weatherplugin.cpp | 10 ++++++++++ plugins/weatherplugin/weatherplugin.h | 1 + plugins/weatherplugin/weatherplugin.pro | 6 ++++-- plugins/weatherplugin/weatherwidget.cpp | 10 ++++++++++ plugins/weatherplugin/weatherwidget.h | 19 +++++++++++++++++++ 5 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 plugins/weatherplugin/weatherwidget.cpp create mode 100644 plugins/weatherplugin/weatherwidget.h diff --git a/plugins/weatherplugin/weatherplugin.cpp b/plugins/weatherplugin/weatherplugin.cpp index c30cc4c..674d9fb 100644 --- a/plugins/weatherplugin/weatherplugin.cpp +++ b/plugins/weatherplugin/weatherplugin.cpp @@ -1,9 +1,19 @@ #include "weatherplugin.h" #include +#include + +#include "mainwindow.h" + +#include "weatherwidget.h" WeatherPlugin::WeatherPlugin(QObject *parent) : ZeiterfassungPlugin(parent) { qDebug() << "called"; } + +void WeatherPlugin::attachTo(MainWindow &mainWindow) +{ + mainWindow.statusBar()->addWidget(new WeatherWidget(mainWindow)); +} diff --git a/plugins/weatherplugin/weatherplugin.h b/plugins/weatherplugin/weatherplugin.h index f8abc9b..11ba9b0 100644 --- a/plugins/weatherplugin/weatherplugin.h +++ b/plugins/weatherplugin/weatherplugin.h @@ -15,6 +15,7 @@ public: explicit WeatherPlugin(QObject *parent = Q_NULLPTR); // ZeiterfassungPlugin interface + void attachTo(MainWindow &mainWindow) Q_DECL_OVERRIDE; }; #endif // WEATHERPLUGIN_H diff --git a/plugins/weatherplugin/weatherplugin.pro b/plugins/weatherplugin/weatherplugin.pro index b67c6dd..3a9d5a9 100644 --- a/plugins/weatherplugin/weatherplugin.pro +++ b/plugins/weatherplugin/weatherplugin.pro @@ -14,8 +14,10 @@ DEPENDPATH += $$PWD/../../zeiterfassunglib DEFINES += QT_DEPRECATED_WARNINGS QT_DISABLE_DEPRECATED_BEFORE=0x060000 QT_MESSAGELOGCONTEXT -HEADERS += weatherplugin.h +HEADERS += weatherplugin.h \ + weatherwidget.h -SOURCES += weatherplugin.cpp +SOURCES += weatherplugin.cpp \ + weatherwidget.cpp OTHER_FILES += weatherplugin.json diff --git a/plugins/weatherplugin/weatherwidget.cpp b/plugins/weatherplugin/weatherwidget.cpp new file mode 100644 index 0000000..b055298 --- /dev/null +++ b/plugins/weatherplugin/weatherwidget.cpp @@ -0,0 +1,10 @@ +#include "weatherwidget.h" + +#include "mainwindow.h" + +WeatherWidget::WeatherWidget(MainWindow &mainWindow) : + QLabel(&mainWindow), + m_mainWindow(mainWindow) +{ + setText(tr("Weather")); +} diff --git a/plugins/weatherplugin/weatherwidget.h b/plugins/weatherplugin/weatherwidget.h new file mode 100644 index 0000000..e04b530 --- /dev/null +++ b/plugins/weatherplugin/weatherwidget.h @@ -0,0 +1,19 @@ +#ifndef WEATHERWIDGET_H +#define WEATHERWIDGET_H + +#include + +class MainWindow; + +class WeatherWidget : public QLabel +{ + Q_OBJECT + +public: + explicit WeatherWidget(MainWindow &mainWindow); + +private: + MainWindow &m_mainWindow; +}; + +#endif // WEATHERWIDGET_H -- 2.50.1 From 0f51884018f4abff4c23c1ec27303eb99a8d621a Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 <0xFEEDC0DE64@gmail.com> Date: Tue, 19 Dec 2017 22:01:06 +0100 Subject: [PATCH 2/2] Implemented WeatherWidget with API --- plugins/weatherplugin/weatherwidget.cpp | 84 ++++++++++++++++++++++++- plugins/weatherplugin/weatherwidget.h | 9 +++ 2 files changed, 92 insertions(+), 1 deletion(-) diff --git a/plugins/weatherplugin/weatherwidget.cpp b/plugins/weatherplugin/weatherwidget.cpp index b055298..76efa1a 100644 --- a/plugins/weatherplugin/weatherwidget.cpp +++ b/plugins/weatherplugin/weatherwidget.cpp @@ -1,10 +1,92 @@ #include "weatherwidget.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + #include "mainwindow.h" +#include "zeiterfassungsettings.h" +#include "zeiterfassungapi.h" WeatherWidget::WeatherWidget(MainWindow &mainWindow) : QLabel(&mainWindow), m_mainWindow(mainWindow) { - setText(tr("Weather")); + setFrameShape(QFrame::Panel); + setFrameShadow(QFrame::Sunken); + + connect(&m_mainWindow, &MainWindow::refreshEverything, this, &WeatherWidget::refresh); + + refresh(); +} + +void WeatherWidget::refresh() +{ + setText(tr("Loading...")); + + auto url = m_mainWindow.settings().value(QStringLiteral("WeatherPlugin/url"), + QStringLiteral("http://api.openweathermap.org/data/2.5/weather?q=Graz,AT&units=metric&APPID=40f6c892c6162680c6c9235169dc9f83")).toString(); + + m_reply = std::unique_ptr(m_mainWindow.erfassung().manager()->get(QNetworkRequest(QUrl(url)))); + connect(m_reply.get(), &QNetworkReply::finished, this, &WeatherWidget::finished); +} + +void WeatherWidget::finished() +{ + if(m_reply->error() != QNetworkReply::NoError) + { + qWarning() << m_reply->errorString(); + setText(tr("Request failed")); + goto after; + } + + { + QJsonParseError error; + + auto document = QJsonDocument::fromJson(m_reply->readAll(), &error); + if(error.error != QJsonParseError::NoError) + { + qWarning() << error.errorString(); + setText(tr("Parsing failed")); + goto after; + } + + if(!document.isObject()) + { + setText(tr("Not an json obj")); + goto after; + } + + auto obj = document.object(); + + if(!obj.contains(QStringLiteral("weather"))) + { + qWarning() << "no weather" << obj; + setText("No weater found"); + goto after; + } + + if(!obj.contains(QStringLiteral("main"))) + { + qWarning() << "no main" << obj; + setText("No main found"); + goto after; + } + + auto weaterObj = obj.value(QStringLiteral("weather")).toArray(); + auto mainObj = obj.value(QStringLiteral("main")).toObject(); + + setText(tr("%0 (%1°C)").arg(weaterObj.first().toObject().value(QStringLiteral("main")).toString()) + .arg(mainObj.value(QStringLiteral("temp")).toDouble())); + } + + after: + m_reply = Q_NULLPTR; } diff --git a/plugins/weatherplugin/weatherwidget.h b/plugins/weatherplugin/weatherwidget.h index e04b530..17532c5 100644 --- a/plugins/weatherplugin/weatherwidget.h +++ b/plugins/weatherplugin/weatherwidget.h @@ -1,7 +1,10 @@ #ifndef WEATHERWIDGET_H #define WEATHERWIDGET_H +#include + #include +#include class MainWindow; @@ -12,8 +15,14 @@ class WeatherWidget : public QLabel public: explicit WeatherWidget(MainWindow &mainWindow); +private Q_SLOTS: + void refresh(); + void finished(); + private: MainWindow &m_mainWindow; + + std::unique_ptr m_reply; }; #endif // WEATHERWIDGET_H -- 2.50.1