From 8d9b42ef914214693ebc31fce2590ab3379c896b Mon Sep 17 00:00:00 2001 From: 0xFEEDC0DE64 <0xFEEDC0DE64@gmail.com> Date: Sat, 23 Dec 2017 11:10:57 +0100 Subject: [PATCH] Fixed #42 --- plugins/devtoolsplugin/logdialog.h | 2 +- plugins/lunchmealplugin/lunchmealdialog.cpp | 40 ++--------------- plugins/lunchmealplugin/lunchmealdialog.h | 11 ++--- plugins/lunchmealplugin/lunchmealwidget.cpp | 48 ++++++++++++++++++++- plugins/lunchmealplugin/lunchmealwidget.h | 7 +++ 5 files changed, 62 insertions(+), 46 deletions(-) diff --git a/plugins/devtoolsplugin/logdialog.h b/plugins/devtoolsplugin/logdialog.h index 52ae161..8f50e2e 100644 --- a/plugins/devtoolsplugin/logdialog.h +++ b/plugins/devtoolsplugin/logdialog.h @@ -12,7 +12,7 @@ class LogDialog : public QDialog Q_OBJECT public: - explicit LogDialog(QWidget *parent = 0); + explicit LogDialog(QWidget *parent = Q_NULLPTR); ~LogDialog(); void setModel(QAbstractItemModel *model); diff --git a/plugins/lunchmealplugin/lunchmealdialog.cpp b/plugins/lunchmealplugin/lunchmealdialog.cpp index 0693f31..2897bc1 100644 --- a/plugins/lunchmealplugin/lunchmealdialog.cpp +++ b/plugins/lunchmealplugin/lunchmealdialog.cpp @@ -2,17 +2,9 @@ #include "ui_lunchmealdialog.h" #include -#include -#include -#include -#include "stripswidget.h" -#include "mainwindow.h" -#include "zeiterfassungsettings.h" -#include "zeiterfassungapi.h" - -LunchMealDialog::LunchMealDialog(StripsWidget &stripsWidget) : - QDialog(&stripsWidget.mainWindow()), +LunchMealDialog::LunchMealDialog(const QDate &date, const QString &content, QWidget *parent) : + QDialog(parent), ui(new Ui::LunchMealDialog) { ui->setupUi(this); @@ -23,35 +15,11 @@ LunchMealDialog::LunchMealDialog(StripsWidget &stripsWidget) : setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint); #endif - ui->labelTitle->setText(tr("Lunch meal for %0").arg(stripsWidget.date().toString(tr("dd.MM.yyyy")))); - - const auto &settings = stripsWidget.mainWindow().settings(); - - auto url = settings.value(QStringLiteral("LunchMealPlugin/url"), - QStringLiteral("https://brunner.ninja/lunch/%0.txt")).toString() - .arg(stripsWidget.date().toString(settings.value(QStringLiteral("LunchMealPlugin/dateFormat"), - QStringLiteral("yyyy-MM-dd")).toString())); - m_reply = std::unique_ptr(stripsWidget.mainWindow().erfassung().manager()->get(QNetworkRequest(QUrl(url)))); - connect(m_reply.get(), &QNetworkReply::finished, this, &LunchMealDialog::finished); + ui->labelTitle->setText(tr("Lunch meal for %0").arg(date.toString(tr("dd.MM.yyyy")))); + ui->labelLunchMeal->setText(content); } LunchMealDialog::~LunchMealDialog() { delete ui; } - -void LunchMealDialog::finished() -{ - if(m_reply->error() != QNetworkReply::NoError) - { - ui->labelLunchMeal->setText(QStringLiteral("%0\n\n%1") - .arg(m_reply->errorString()) - .arg(QString(m_reply->readAll()))); - goto after; - } - - ui->labelLunchMeal->setText(m_reply->readAll()); - - after: - m_reply = Q_NULLPTR; -} diff --git a/plugins/lunchmealplugin/lunchmealdialog.h b/plugins/lunchmealplugin/lunchmealdialog.h index be160cd..c0191ee 100644 --- a/plugins/lunchmealplugin/lunchmealdialog.h +++ b/plugins/lunchmealplugin/lunchmealdialog.h @@ -1,9 +1,8 @@ #pragma once -#include - #include -#include + +class QDate; class StripsWidget; @@ -14,13 +13,9 @@ class LunchMealDialog : public QDialog Q_OBJECT public: - explicit LunchMealDialog(StripsWidget &stripsWidget); + explicit LunchMealDialog(const QDate &date, const QString &content, QWidget *parent = Q_NULLPTR); ~LunchMealDialog(); -private Q_SLOTS: - void finished(); - private: Ui::LunchMealDialog *ui; - std::unique_ptr m_reply; }; diff --git a/plugins/lunchmealplugin/lunchmealwidget.cpp b/plugins/lunchmealplugin/lunchmealwidget.cpp index eacaaf6..f93b4b5 100644 --- a/plugins/lunchmealplugin/lunchmealwidget.cpp +++ b/plugins/lunchmealplugin/lunchmealwidget.cpp @@ -1,9 +1,15 @@ #include "lunchmealwidget.h" #include +#include +#include +#include +#include +#include #include "stripswidget.h" #include "mainwindow.h" +#include "zeiterfassungsettings.h" #include "zeiterfassungapi.h" #include "lunchmealdialog.h" @@ -16,10 +22,50 @@ LunchMealWidget::LunchMealWidget(StripsWidget &stripsWidget) : setText(tr("Lunch meal")); connect(this, &QAbstractButton::pressed, this, &LunchMealWidget::pressedSlot); + + connect(&m_stripsWidget, &StripsWidget::dateChanged, this, &LunchMealWidget::dateChanged); + dateChanged(m_stripsWidget.date()); } void LunchMealWidget::pressedSlot() { - LunchMealDialog dialog(m_stripsWidget); + LunchMealDialog dialog(m_stripsWidget.date(), m_content, &m_stripsWidget.mainWindow()); dialog.exec(); } + +void LunchMealWidget::dateChanged(const QDate &date) +{ + qDebug() << "called"; + + setEnabled(false); + setVisible(false); + + const auto &settings = m_stripsWidget.mainWindow().settings(); + + auto url = settings.value(QStringLiteral("LunchMealPlugin/url"), + QStringLiteral("https://brunner.ninja/lunch/%0.txt")).toString() + .arg(date.toString(settings.value(QStringLiteral("LunchMealPlugin/dateFormat"), QStringLiteral("yyyy-MM-dd")).toString())); + m_reply = std::unique_ptr(m_stripsWidget.mainWindow().erfassung().manager()->get(QNetworkRequest(QUrl(url)))); + connect(m_reply.get(), &QNetworkReply::finished, this, &LunchMealWidget::finished); +} + +void LunchMealWidget::finished() +{ + if(m_reply->error() != QNetworkReply::NoError && + m_reply->error() != QNetworkReply::ContentNotFoundError) + { + QMessageBox::warning(&m_stripsWidget.mainWindow(), tr("Could not load lunch meal!"), + tr("Could not load lunch meal!") % "\n\n" % m_reply->errorString()); + goto after; + } + + if(m_reply->error() == QNetworkReply::NoError) + { + setEnabled(true); + setVisible(true); + m_content = m_reply->readAll(); + } + + after: + m_reply = Q_NULLPTR; +} diff --git a/plugins/lunchmealplugin/lunchmealwidget.h b/plugins/lunchmealplugin/lunchmealwidget.h index 161610c..fb7e3c7 100644 --- a/plugins/lunchmealplugin/lunchmealwidget.h +++ b/plugins/lunchmealplugin/lunchmealwidget.h @@ -1,6 +1,9 @@ #pragma once +#include + #include +#include class StripsWidget; @@ -12,7 +15,11 @@ public: private Q_SLOTS: void pressedSlot(); + void dateChanged(const QDate &date); + void finished(); private: StripsWidget &m_stripsWidget; + std::unique_ptr m_reply; + QString m_content; };