diff options
author | Max Christian Pohle | 2015-11-23 04:34:28 +0100 |
---|---|---|
committer | Max Christian Pohle | 2015-11-23 04:34:28 +0100 |
commit | 8e6f79403ff5242dc4b4e547d2eeb6a75766894a (patch) | |
tree | 4a07737d1bc451bde60198af1ce4314d601f3bb4 /mainwindow.cpp | |
parent | 5bf6cd920935500e16f0753e55c0a3593f7bd768 (diff) | |
download | qspacestatus-8e6f79403ff5242dc4b4e547d2eeb6a75766894a.tar.bz2 qspacestatus-8e6f79403ff5242dc4b4e547d2eeb6a75766894a.zip |
various changes
gui improvements
generic programming
bug fixes
Diffstat (limited to 'mainwindow.cpp')
-rw-r--r-- | mainwindow.cpp | 114 |
1 files changed, 91 insertions, 23 deletions
diff --git a/mainwindow.cpp b/mainwindow.cpp index 55a9769..9385cd6 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp | |||
@@ -15,9 +15,9 @@ MainWindow::MainWindow(QWidget *parent) : | |||
15 | this->qsystemtrayicon->setContextMenu(ui->menuMenu1); | 15 | this->qsystemtrayicon->setContextMenu(ui->menuMenu1); |
16 | this->qsystemtrayicon->setToolTip(this->windowTitle()); | 16 | this->qsystemtrayicon->setToolTip(this->windowTitle()); |
17 | this->connect(this->qsystemtrayicon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(slotActivated(QSystemTrayIcon::ActivationReason))); | 17 | this->connect(this->qsystemtrayicon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(slotActivated(QSystemTrayIcon::ActivationReason))); |
18 | this->updateStatus(); | ||
19 | this->updateSpaceList(); | ||
20 | 18 | ||
19 | this->updateSpaceList(); | ||
20 | this->updateStatus(); | ||
21 | 21 | ||
22 | 22 | ||
23 | this->timer = new QTimer(); | 23 | this->timer = new QTimer(); |
@@ -25,10 +25,15 @@ MainWindow::MainWindow(QWidget *parent) : | |||
25 | // update every five minutes... | 25 | // update every five minutes... |
26 | this->timer->start(60000 * 5); | 26 | this->timer->start(60000 * 5); |
27 | 27 | ||
28 | //this->pixmap = new QPixmap(120, 120); | ||
29 | //this->currentIcon = new QIcon(this->pixmap); | ||
28 | } | 30 | } |
29 | 31 | ||
30 | MainWindow::~MainWindow() | 32 | MainWindow::~MainWindow() |
31 | { | 33 | { |
34 | qDebug() << "unloading application"; | ||
35 | delete this->qsystemtrayicon; | ||
36 | delete this->timer; | ||
32 | delete ui; | 37 | delete ui; |
33 | } | 38 | } |
34 | 39 | ||
@@ -40,6 +45,7 @@ void MainWindow::updateSpaceList() | |||
40 | QUrl url("http://spaceapi.net/directory.json"); | 45 | QUrl url("http://spaceapi.net/directory.json"); |
41 | QJsonObject json = QJsonDocument::fromJson(this->download(url)).object(); | 46 | QJsonObject json = QJsonDocument::fromJson(this->download(url)).object(); |
42 | 47 | ||
48 | ui->comboBox->setDisabled(true); | ||
43 | QJsonObject::Iterator it; | 49 | QJsonObject::Iterator it; |
44 | for(it=json.begin(); it!=json.end(); it++) | 50 | for(it=json.begin(); it!=json.end(); it++) |
45 | { | 51 | { |
@@ -48,7 +54,18 @@ void MainWindow::updateSpaceList() | |||
48 | if(it.key() == defaultSpace) | 54 | if(it.key() == defaultSpace) |
49 | ui->comboBox->setCurrentIndex(ui->comboBox->count()-1); | 55 | ui->comboBox->setCurrentIndex(ui->comboBox->count()-1); |
50 | } | 56 | } |
57 | ui->comboBox->setDisabled(false); | ||
58 | } | ||
51 | 59 | ||
60 | QJsonObject json_find(QJsonObject json, QStringList list) | ||
61 | { | ||
62 | QJsonObject retval = json; | ||
63 | foreach(const QString str, list) | ||
64 | if(retval.contains(str)) | ||
65 | retval = retval[str].toObject(); | ||
66 | else | ||
67 | return QJsonObject(); | ||
68 | return retval; | ||
52 | } | 69 | } |
53 | 70 | ||
54 | // lets not talk about memory leaks. yes, they are there. but it works somehow. | 71 | // lets not talk about memory leaks. yes, they are there. but it works somehow. |
@@ -59,14 +76,60 @@ void MainWindow::updateStatus() | |||
59 | ui->label_url_json->setText(ui->comboBox->currentData().toString()); | 76 | ui->label_url_json->setText(ui->comboBox->currentData().toString()); |
60 | 77 | ||
61 | QUrl url(ui->comboBox->currentData().toString()); | 78 | QUrl url(ui->comboBox->currentData().toString()); |
62 | QByteArray rawData = this->download(url); | 79 | QJsonDocument doc = QJsonDocument::fromJson(this->download(url)); |
63 | //qDebug() << rawData; | 80 | //ui->plainTextEdit->setPlainText(doc.toJson(QJsonDocument::Indented)); |
64 | QJsonDocument doc = QJsonDocument::fromJson(rawData); | ||
65 | ui->plainTextEdit->setPlainText(doc.toJson(QJsonDocument::Indented)); | ||
66 | 81 | ||
67 | //qDebug() << doc.toJson(QJsonDocument::Indented); | 82 | //qDebug() << doc.toJson(QJsonDocument::Indented); |
68 | QJsonObject json = doc.object(); | 83 | QJsonObject json = doc.object(); |
84 | |||
85 | foreach (QLabel *label, this->findChildren<QLabel*>()) | ||
86 | { | ||
87 | if(label->statusTip().isEmpty()) | ||
88 | continue; | ||
89 | |||
90 | label->clear(); | ||
91 | //label->setText(""); | ||
92 | QStringList list = label->statusTip().split("/"); | ||
93 | QString name = list.takeLast(); | ||
94 | |||
95 | |||
96 | QJsonObject obj = json_find(json, list); | ||
97 | if(!obj.isEmpty()) | ||
98 | { | ||
99 | if(obj.value(name).isString()) | ||
100 | { | ||
101 | QString result = obj.value(name).toString(); | ||
102 | if(name == "logo" || result.endsWith(".png") || result.endsWith(".jpg")) | ||
103 | { | ||
104 | label->setPixmap( | ||
105 | QPixmap::fromImage( | ||
106 | QImage::fromData( | ||
107 | this->download( | ||
108 | QUrl(result)))).scaled(120, 120, Qt::KeepAspectRatio)); | ||
109 | } | ||
110 | else if(result.contains("http")) | ||
111 | { | ||
112 | label->setText("<b>" + name + ":</b><br/><a href=\"" + result + "\">" + result + "</a> "); | ||
113 | label->setOpenExternalLinks(true); | ||
114 | } | ||
115 | else | ||
116 | label->setText("<b>" + name + ":</b><br/>" + result); | ||
117 | } | ||
118 | else | ||
119 | { | ||
120 | if(obj.value(name).isBool()) | ||
121 | label->setText("<b>" + name + "</b>:<br/>" + (obj.value(name).toBool() ? "true" : "false")); | ||
122 | else if(name == "lastchange") | ||
123 | label->setText("<b>" + name + ":</b><br/>" + QDateTime::fromTime_t(obj.value(name).toInt()).toLocalTime().toString()); | ||
124 | } | ||
125 | |||
126 | } | ||
127 | |||
128 | qDebug() << "NAME: " << name; | ||
129 | } | ||
130 | |||
69 | QString strUrl; | 131 | QString strUrl; |
132 | |||
70 | strUrl = json["state"].toObject() | 133 | strUrl = json["state"].toObject() |
71 | ["icon"].toObject() | 134 | ["icon"].toObject() |
72 | [json["state"].toObject()["open"].toBool() ? "open" : "closed"].toString(); | 135 | [json["state"].toObject()["open"].toBool() ? "open" : "closed"].toString(); |
@@ -77,18 +140,22 @@ void MainWindow::updateStatus() | |||
77 | 140 | ||
78 | 141 | ||
79 | qDebug() << "downloading: " << strUrl; | 142 | qDebug() << "downloading: " << strUrl; |
80 | this->imageArray = this->download(QUrl(strUrl)); | 143 | this->qsystemtrayicon->setIcon(QIcon( |
81 | //qDebug("test: " + imageArray.size()); | 144 | QPixmap::fromImage( |
82 | QImage image = QImage::fromData(imageArray); | 145 | QImage::fromData( |
83 | 146 | this->download( | |
84 | this->currentIcon = new QIcon(QPixmap::fromImage(image)); | 147 | QUrl(strUrl)))).scaled(120, 120, Qt::KeepAspectRatio))); |
85 | this->qsystemtrayicon->setIcon(*currentIcon); | 148 | |
86 | 149 | if(this->qsystemtrayicon->icon().isNull()) | |
87 | //QPixmap* pixmap = new QPixmap("/home/max/folder.png"); | 150 | { |
88 | QUrl logoURL(json["logo"].toString()); | 151 | if(json["state"].toObject().contains("open")) |
89 | //qDebug() << "logo: " << logoURL.toString(); | 152 | if(json["state"].toObject()["open"].toBool()) |
90 | QPixmap pixmap = QPixmap::fromImage(QImage::fromData(this->download(logoURL))); | 153 | this->qsystemtrayicon->setIcon(QIcon::fromTheme("face-smile")); |
91 | ui->label_icon->setPixmap(pixmap.scaled(120, 120, Qt::KeepAspectRatio)); | 154 | else |
155 | this->qsystemtrayicon->setIcon(QIcon::fromTheme("face-sad")); | ||
156 | else | ||
157 | this->qsystemtrayicon->setIcon(QIcon::fromTheme("face-sick")); | ||
158 | } | ||
92 | } | 159 | } |
93 | 160 | ||
94 | void MainWindow::saveSettings() | 161 | void MainWindow::saveSettings() |
@@ -96,8 +163,6 @@ void MainWindow::saveSettings() | |||
96 | this->qsettings.setValue("hackerspace", ui->comboBox->currentText()); | 163 | this->qsettings.setValue("hackerspace", ui->comboBox->currentText()); |
97 | } | 164 | } |
98 | 165 | ||
99 | |||
100 | |||
101 | QByteArray MainWindow::download(QUrl url) | 166 | QByteArray MainWindow::download(QUrl url) |
102 | { | 167 | { |
103 | qDebug() << "downloading: " << url.toString(); | 168 | qDebug() << "downloading: " << url.toString(); |
@@ -164,14 +229,17 @@ void MainWindow::slotActivated(QSystemTrayIcon::ActivationReason reason) | |||
164 | void MainWindow::exitApplication() | 229 | void MainWindow::exitApplication() |
165 | { | 230 | { |
166 | qDebug() << "exit now!"; | 231 | qDebug() << "exit now!"; |
167 | exit(0); | 232 | QApplication::quit(); |
168 | } | 233 | } |
169 | 234 | ||
170 | void MainWindow::on_comboBox_currentIndexChanged(const QString &arg1) | 235 | void MainWindow::on_comboBox_currentIndexChanged(const QString &arg1) |
171 | { | 236 | { |
172 | qDebug() << arg1; | 237 | qDebug() << arg1; |
173 | this->updateStatus(); | 238 | if(ui->comboBox->isEnabled()) |
174 | this->saveSettings(); | 239 | { |
240 | this->updateStatus(); | ||
241 | this->saveSettings(); | ||
242 | } | ||
175 | } | 243 | } |
176 | 244 | ||
177 | 245 | ||