From de6cb045fd2b237e58320f0da7f3a365ca4d2ace Mon Sep 17 00:00:00 2001 From: Max Christian Pohle Date: Wed, 18 Nov 2015 04:12:48 +0100 Subject: downloads+parses json and displays systemtray icon can be activated by a single click and shows the corresponding window. --- folder.png | Bin 0 -> 16980 bytes main.cpp | 10 ++++++ mainwindow.cpp | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mainwindow.h | 44 ++++++++++++++++++++++++++ mainwindow.ui | 76 +++++++++++++++++++++++++++++++++++++++++++++ qtapp.pro | 23 ++++++++++++++ 6 files changed, 248 insertions(+) create mode 100644 folder.png create mode 100644 main.cpp create mode 100644 mainwindow.cpp create mode 100644 mainwindow.h create mode 100644 mainwindow.ui create mode 100644 qtapp.pro diff --git a/folder.png b/folder.png new file mode 100644 index 0000000..b472e50 Binary files /dev/null and b/folder.png differ diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..74e1a60 --- /dev/null +++ b/main.cpp @@ -0,0 +1,10 @@ +#include "mainwindow.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + a.setQuitOnLastWindowClosed(false); + MainWindow w; + return a.exec(); +} diff --git a/mainwindow.cpp b/mainwindow.cpp new file mode 100644 index 0000000..0dd8a3e --- /dev/null +++ b/mainwindow.cpp @@ -0,0 +1,95 @@ +#include "mainwindow.h" +#include "ui_mainwindow.h" +#include + +MainWindow::MainWindow(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::MainWindow) + +{ + ui->setupUi(this); + + QIcon* icon = new QIcon("folder.png"); + this->qsystemtrayicon = new QSystemTrayIcon(*icon, this); + this->setWindowIcon(*icon); + + this->qsystemtrayicon->show(); + this->qsystemtrayicon->setContextMenu(ui->menuMenu1); + //this->connect(this, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(systray_clicked(QSystemTrayIcon::ActivationReason))); + this->connect(qsystemtrayicon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(slotActivated(QSystemTrayIcon::ActivationReason))); + + //this->qsystemtrayicon->installEventFilter(this); + + //this->qsystemtrayicon->setIcon(this->windowIcon()); + this->qsystemtrayicon->setToolTip(this->windowTitle()); + + QUrl url("http://hackerspace-bielefeld.de/spacestatus/status.json"); + QByteArray rawData = this->download(url); + QJsonDocument doc = QJsonDocument::fromJson(rawData); + //qDebug() << rawData; + //qDebug() << doc.toJson(QJsonDocument::Compact); + + QJsonObject json = doc.object(); + + QString strUrl; + if(json["state/open"].toBool()) + { + strUrl = json["state"].toObject()["icon"].toObject()["open"].toString(); + this->qsystemtrayicon->showMessage("Hackerspace Bielefeld", "Der Space ist nun geƶffnet!"); + } + else + { + strUrl = json["state"].toObject()["icon"].toObject()["closed"].toString(); + this->qsystemtrayicon->showMessage("Hackerspace Bielefeld", "Der Space ist nun geschlossen!"); + } + + qDebug() << "downloading: " << strUrl; + QByteArray imageArray = this->download(QUrl(strUrl)); + //qDebug("test: " + imageArray.size()); + QImage image = QImage::fromData(imageArray); + + this->currentIcon = new QIcon(QPixmap::fromImage(image)); + this->setWindowIcon(*currentIcon); + this->qsystemtrayicon->setIcon(*currentIcon); + this->setWindowTitle("closed"); + + qDebug() << json["state", "open"]; +} + +MainWindow::~MainWindow() +{ + delete ui; +} + +QByteArray MainWindow::download(QUrl url) +{ + QNetworkAccessManager manager; + QNetworkReply *response = manager.get(QNetworkRequest(QUrl(url))); + QEventLoop event; + connect(response,SIGNAL(finished()),&event,SLOT(quit())); + event.exec(); + + return response->readAll(); +} + +void MainWindow::slotActivated(QSystemTrayIcon::ActivationReason reason) +{ + if(reason == QSystemTrayIcon::Trigger) + { + qDebug() << "trigger"; + this->setWindowState(Qt::WindowNoState); + this->show(); + this->activateWindow(); + + } + else + { + qDebug() << "activate(reason): " << reason; + } +} + +void MainWindow::exitApplication() +{ + qDebug() << "exit now!"; + exit(0); +} diff --git a/mainwindow.h b/mainwindow.h new file mode 100644 index 0000000..11c1616 --- /dev/null +++ b/mainwindow.h @@ -0,0 +1,44 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include +#include + +#include +#include +#include + + +#include +#include + +#include +#include +#include + + +namespace Ui { +class MainWindow; +} + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + explicit MainWindow(QWidget *parent = 0); + ~MainWindow(); + +private: + Ui::MainWindow *ui; + QSystemTrayIcon* qsystemtrayicon; + QByteArray download(QUrl url); + QIcon* currentIcon; + +private slots: + void slotActivated(QSystemTrayIcon::ActivationReason); + void exitApplication(); + +}; + +#endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui new file mode 100644 index 0000000..812bddc --- /dev/null +++ b/mainwindow.ui @@ -0,0 +1,76 @@ + + + MainWindow + + + + 0 + 0 + 646 + 486 + + + + MainWindow + + + + folder.pngfolder.png + + + + + + 0 + 0 + 646 + 29 + + + + + menu1 + + + + + + + + TopToolBarArea + + + false + + + + + + exit + + + + + + + + actionExit + triggered() + MainWindow + exitApplication() + + + -1 + -1 + + + 322 + 242 + + + + + + exitApplication() + + diff --git a/qtapp.pro b/qtapp.pro new file mode 100644 index 0000000..db84b23 --- /dev/null +++ b/qtapp.pro @@ -0,0 +1,23 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2015-11-17T21:16:22 +# +#------------------------------------------------- + +QT += core gui network + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = qtapp +TEMPLATE = app + + +SOURCES += main.cpp\ + mainwindow.cpp + +HEADERS += mainwindow.h + +FORMS += mainwindow.ui + +DISTFILES += \ + folder.png -- cgit v1.2.3