diff options
author | Max Christian Pohle | 2015-11-18 04:12:48 +0100 |
---|---|---|
committer | Max Christian Pohle | 2015-11-18 04:12:48 +0100 |
commit | de6cb045fd2b237e58320f0da7f3a365ca4d2ace (patch) | |
tree | 2bc3a13c3308aadaa0e92c556d587c60f8d718de | |
download | qspacestatus-de6cb045fd2b237e58320f0da7f3a365ca4d2ace.tar.bz2 qspacestatus-de6cb045fd2b237e58320f0da7f3a365ca4d2ace.zip |
downloads+parses json and displays systemtray icon
can be activated by a single click and shows the corresponding window.
-rw-r--r-- | folder.png | bin | 0 -> 16980 bytes | |||
-rw-r--r-- | main.cpp | 10 | ||||
-rw-r--r-- | mainwindow.cpp | 95 | ||||
-rw-r--r-- | mainwindow.h | 44 | ||||
-rw-r--r-- | mainwindow.ui | 76 | ||||
-rw-r--r-- | qtapp.pro | 23 |
6 files changed, 248 insertions, 0 deletions
diff --git a/folder.png b/folder.png new file mode 100644 index 0000000..b472e50 --- /dev/null +++ b/folder.png | |||
Binary files 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 @@ | |||
1 | #include "mainwindow.h" | ||
2 | #include <QApplication> | ||
3 | |||
4 | int main(int argc, char *argv[]) | ||
5 | { | ||
6 | QApplication a(argc, argv); | ||
7 | a.setQuitOnLastWindowClosed(false); | ||
8 | MainWindow w; | ||
9 | return a.exec(); | ||
10 | } | ||
diff --git a/mainwindow.cpp b/mainwindow.cpp new file mode 100644 index 0000000..0dd8a3e --- /dev/null +++ b/mainwindow.cpp | |||
@@ -0,0 +1,95 @@ | |||
1 | #include "mainwindow.h" | ||
2 | #include "ui_mainwindow.h" | ||
3 | #include <QDebug> | ||
4 | |||
5 | MainWindow::MainWindow(QWidget *parent) : | ||
6 | QMainWindow(parent), | ||
7 | ui(new Ui::MainWindow) | ||
8 | |||
9 | { | ||
10 | ui->setupUi(this); | ||
11 | |||
12 | QIcon* icon = new QIcon("folder.png"); | ||
13 | this->qsystemtrayicon = new QSystemTrayIcon(*icon, this); | ||
14 | this->setWindowIcon(*icon); | ||
15 | |||
16 | this->qsystemtrayicon->show(); | ||
17 | this->qsystemtrayicon->setContextMenu(ui->menuMenu1); | ||
18 | //this->connect(this, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(systray_clicked(QSystemTrayIcon::ActivationReason))); | ||
19 | this->connect(qsystemtrayicon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(slotActivated(QSystemTrayIcon::ActivationReason))); | ||
20 | |||
21 | //this->qsystemtrayicon->installEventFilter(this); | ||
22 | |||
23 | //this->qsystemtrayicon->setIcon(this->windowIcon()); | ||
24 | this->qsystemtrayicon->setToolTip(this->windowTitle()); | ||
25 | |||
26 | QUrl url("http://hackerspace-bielefeld.de/spacestatus/status.json"); | ||
27 | QByteArray rawData = this->download(url); | ||
28 | QJsonDocument doc = QJsonDocument::fromJson(rawData); | ||
29 | //qDebug() << rawData; | ||
30 | //qDebug() << doc.toJson(QJsonDocument::Compact); | ||
31 | |||
32 | QJsonObject json = doc.object(); | ||
33 | |||
34 | QString strUrl; | ||
35 | if(json["state/open"].toBool()) | ||
36 | { | ||
37 | strUrl = json["state"].toObject()["icon"].toObject()["open"].toString(); | ||
38 | this->qsystemtrayicon->showMessage("Hackerspace Bielefeld", "Der Space ist nun geƶffnet!"); | ||
39 | } | ||
40 | else | ||
41 | { | ||
42 | strUrl = json["state"].toObject()["icon"].toObject()["closed"].toString(); | ||
43 | this->qsystemtrayicon->showMessage("Hackerspace Bielefeld", "Der Space ist nun geschlossen!"); | ||
44 | } | ||
45 | |||
46 | qDebug() << "downloading: " << strUrl; | ||
47 | QByteArray imageArray = this->download(QUrl(strUrl)); | ||
48 | //qDebug("test: " + imageArray.size()); | ||
49 | QImage image = QImage::fromData(imageArray); | ||
50 | |||
51 | this->currentIcon = new QIcon(QPixmap::fromImage(image)); | ||
52 | this->setWindowIcon(*currentIcon); | ||
53 | this->qsystemtrayicon->setIcon(*currentIcon); | ||
54 | this->setWindowTitle("closed"); | ||
55 | |||
56 | qDebug() << json["state", "open"]; | ||
57 | } | ||
58 | |||
59 | MainWindow::~MainWindow() | ||
60 | { | ||
61 | delete ui; | ||
62 | } | ||
63 | |||
64 | QByteArray MainWindow::download(QUrl url) | ||
65 | { | ||
66 | QNetworkAccessManager manager; | ||
67 | QNetworkReply *response = manager.get(QNetworkRequest(QUrl(url))); | ||
68 | QEventLoop event; | ||
69 | connect(response,SIGNAL(finished()),&event,SLOT(quit())); | ||
70 | event.exec(); | ||
71 | |||
72 | return response->readAll(); | ||
73 | } | ||
74 | |||
75 | void MainWindow::slotActivated(QSystemTrayIcon::ActivationReason reason) | ||
76 | { | ||
77 | if(reason == QSystemTrayIcon::Trigger) | ||
78 | { | ||
79 | qDebug() << "trigger"; | ||
80 | this->setWindowState(Qt::WindowNoState); | ||
81 | this->show(); | ||
82 | this->activateWindow(); | ||
83 | |||
84 | } | ||
85 | else | ||
86 | { | ||
87 | qDebug() << "activate(reason): " << reason; | ||
88 | } | ||
89 | } | ||
90 | |||
91 | void MainWindow::exitApplication() | ||
92 | { | ||
93 | qDebug() << "exit now!"; | ||
94 | exit(0); | ||
95 | } | ||
diff --git a/mainwindow.h b/mainwindow.h new file mode 100644 index 0000000..11c1616 --- /dev/null +++ b/mainwindow.h | |||
@@ -0,0 +1,44 @@ | |||
1 | #ifndef MAINWINDOW_H | ||
2 | #define MAINWINDOW_H | ||
3 | |||
4 | #include <QMainWindow> | ||
5 | #include <QSystemTrayIcon> | ||
6 | |||
7 | #include <QJsonDocument> | ||
8 | #include <QJsonObject> | ||
9 | #include <QByteArray> | ||
10 | |||
11 | |||
12 | #include <QString> | ||
13 | #include <QObject> | ||
14 | |||
15 | #include <QtNetwork/QNetworkAccessManager> | ||
16 | #include <QtNetwork/QNetworkRequest> | ||
17 | #include <QtNetwork/QNetworkReply> | ||
18 | |||
19 | |||
20 | namespace Ui { | ||
21 | class MainWindow; | ||
22 | } | ||
23 | |||
24 | class MainWindow : public QMainWindow | ||
25 | { | ||
26 | Q_OBJECT | ||
27 | |||
28 | public: | ||
29 | explicit MainWindow(QWidget *parent = 0); | ||
30 | ~MainWindow(); | ||
31 | |||
32 | private: | ||
33 | Ui::MainWindow *ui; | ||
34 | QSystemTrayIcon* qsystemtrayicon; | ||
35 | QByteArray download(QUrl url); | ||
36 | QIcon* currentIcon; | ||
37 | |||
38 | private slots: | ||
39 | void slotActivated(QSystemTrayIcon::ActivationReason); | ||
40 | void exitApplication(); | ||
41 | |||
42 | }; | ||
43 | |||
44 | #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 @@ | |||
1 | <?xml version="1.0" encoding="UTF-8"?> | ||
2 | <ui version="4.0"> | ||
3 | <class>MainWindow</class> | ||
4 | <widget class="QMainWindow" name="MainWindow"> | ||
5 | <property name="geometry"> | ||
6 | <rect> | ||
7 | <x>0</x> | ||
8 | <y>0</y> | ||
9 | <width>646</width> | ||
10 | <height>486</height> | ||
11 | </rect> | ||
12 | </property> | ||
13 | <property name="windowTitle"> | ||
14 | <string>MainWindow</string> | ||
15 | </property> | ||
16 | <property name="windowIcon"> | ||
17 | <iconset> | ||
18 | <normaloff>folder.png</normaloff>folder.png</iconset> | ||
19 | </property> | ||
20 | <widget class="QWidget" name="centralWidget"/> | ||
21 | <widget class="QMenuBar" name="menuBar"> | ||
22 | <property name="geometry"> | ||
23 | <rect> | ||
24 | <x>0</x> | ||
25 | <y>0</y> | ||
26 | <width>646</width> | ||
27 | <height>29</height> | ||
28 | </rect> | ||
29 | </property> | ||
30 | <widget class="QMenu" name="menuMenu1"> | ||
31 | <property name="title"> | ||
32 | <string>menu1</string> | ||
33 | </property> | ||
34 | <addaction name="actionExit"/> | ||
35 | </widget> | ||
36 | <addaction name="menuMenu1"/> | ||
37 | </widget> | ||
38 | <widget class="QToolBar" name="mainToolBar"> | ||
39 | <attribute name="toolBarArea"> | ||
40 | <enum>TopToolBarArea</enum> | ||
41 | </attribute> | ||
42 | <attribute name="toolBarBreak"> | ||
43 | <bool>false</bool> | ||
44 | </attribute> | ||
45 | </widget> | ||
46 | <widget class="QStatusBar" name="statusBar"/> | ||
47 | <action name="actionExit"> | ||
48 | <property name="text"> | ||
49 | <string>exit</string> | ||
50 | </property> | ||
51 | </action> | ||
52 | </widget> | ||
53 | <layoutdefault spacing="6" margin="11"/> | ||
54 | <resources/> | ||
55 | <connections> | ||
56 | <connection> | ||
57 | <sender>actionExit</sender> | ||
58 | <signal>triggered()</signal> | ||
59 | <receiver>MainWindow</receiver> | ||
60 | <slot>exitApplication()</slot> | ||
61 | <hints> | ||
62 | <hint type="sourcelabel"> | ||
63 | <x>-1</x> | ||
64 | <y>-1</y> | ||
65 | </hint> | ||
66 | <hint type="destinationlabel"> | ||
67 | <x>322</x> | ||
68 | <y>242</y> | ||
69 | </hint> | ||
70 | </hints> | ||
71 | </connection> | ||
72 | </connections> | ||
73 | <slots> | ||
74 | <slot>exitApplication()</slot> | ||
75 | </slots> | ||
76 | </ui> | ||
diff --git a/qtapp.pro b/qtapp.pro new file mode 100644 index 0000000..db84b23 --- /dev/null +++ b/qtapp.pro | |||
@@ -0,0 +1,23 @@ | |||
1 | #------------------------------------------------- | ||
2 | # | ||
3 | # Project created by QtCreator 2015-11-17T21:16:22 | ||
4 | # | ||
5 | #------------------------------------------------- | ||
6 | |||
7 | QT += core gui network | ||
8 | |||
9 | greaterThan(QT_MAJOR_VERSION, 4): QT += widgets | ||
10 | |||
11 | TARGET = qtapp | ||
12 | TEMPLATE = app | ||
13 | |||
14 | |||
15 | SOURCES += main.cpp\ | ||
16 | mainwindow.cpp | ||
17 | |||
18 | HEADERS += mainwindow.h | ||
19 | |||
20 | FORMS += mainwindow.ui | ||
21 | |||
22 | DISTFILES += \ | ||
23 | folder.png | ||