From 8e6f79403ff5242dc4b4e547d2eeb6a75766894a Mon Sep 17 00:00:00 2001 From: Max Christian Pohle Date: Mon, 23 Nov 2015 04:34:28 +0100 Subject: various changes gui improvements generic programming bug fixes --- build/linux/qspacestatus | Bin 77200 -> 78208 bytes img/qspace.blend | Bin 0 -> 94001 bytes img/qspacestatus.png | Bin 0 -> 162022 bytes main.cpp | 1 + mainwindow.cpp | 114 +++++++++++++++---- mainwindow.h | 17 ++- mainwindow.ui | 289 ++++++++++++++++++++++++++++++++++++++++++----- qspace.qrc | 5 + qspaceapi.pro | 6 +- 9 files changed, 372 insertions(+), 60 deletions(-) create mode 100644 img/qspace.blend create mode 100644 img/qspacestatus.png create mode 100644 qspace.qrc diff --git a/build/linux/qspacestatus b/build/linux/qspacestatus index 6598427..291fbee 100755 Binary files a/build/linux/qspacestatus and b/build/linux/qspacestatus differ diff --git a/img/qspace.blend b/img/qspace.blend new file mode 100644 index 0000000..37a979f Binary files /dev/null and b/img/qspace.blend differ diff --git a/img/qspacestatus.png b/img/qspacestatus.png new file mode 100644 index 0000000..cf870aa Binary files /dev/null and b/img/qspacestatus.png differ diff --git a/main.cpp b/main.cpp index 9b39d7c..0e12450 100644 --- a/main.cpp +++ b/main.cpp @@ -9,6 +9,7 @@ int main(int argc, char *argv[]) QApplication a(argc, argv); a.setOrganizationName("CoderOnline"); a.setQuitOnLastWindowClosed(false); + MainWindow w; 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) : this->qsystemtrayicon->setContextMenu(ui->menuMenu1); this->qsystemtrayicon->setToolTip(this->windowTitle()); this->connect(this->qsystemtrayicon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(slotActivated(QSystemTrayIcon::ActivationReason))); - this->updateStatus(); - this->updateSpaceList(); + this->updateSpaceList(); + this->updateStatus(); this->timer = new QTimer(); @@ -25,10 +25,15 @@ MainWindow::MainWindow(QWidget *parent) : // update every five minutes... this->timer->start(60000 * 5); + //this->pixmap = new QPixmap(120, 120); + //this->currentIcon = new QIcon(this->pixmap); } MainWindow::~MainWindow() { + qDebug() << "unloading application"; + delete this->qsystemtrayicon; + delete this->timer; delete ui; } @@ -40,6 +45,7 @@ void MainWindow::updateSpaceList() QUrl url("http://spaceapi.net/directory.json"); QJsonObject json = QJsonDocument::fromJson(this->download(url)).object(); + ui->comboBox->setDisabled(true); QJsonObject::Iterator it; for(it=json.begin(); it!=json.end(); it++) { @@ -48,7 +54,18 @@ void MainWindow::updateSpaceList() if(it.key() == defaultSpace) ui->comboBox->setCurrentIndex(ui->comboBox->count()-1); } + ui->comboBox->setDisabled(false); +} +QJsonObject json_find(QJsonObject json, QStringList list) +{ + QJsonObject retval = json; + foreach(const QString str, list) + if(retval.contains(str)) + retval = retval[str].toObject(); + else + return QJsonObject(); + return retval; } // lets not talk about memory leaks. yes, they are there. but it works somehow. @@ -59,14 +76,60 @@ void MainWindow::updateStatus() ui->label_url_json->setText(ui->comboBox->currentData().toString()); QUrl url(ui->comboBox->currentData().toString()); - QByteArray rawData = this->download(url); - //qDebug() << rawData; - QJsonDocument doc = QJsonDocument::fromJson(rawData); - ui->plainTextEdit->setPlainText(doc.toJson(QJsonDocument::Indented)); + QJsonDocument doc = QJsonDocument::fromJson(this->download(url)); + //ui->plainTextEdit->setPlainText(doc.toJson(QJsonDocument::Indented)); //qDebug() << doc.toJson(QJsonDocument::Indented); QJsonObject json = doc.object(); + + foreach (QLabel *label, this->findChildren()) + { + if(label->statusTip().isEmpty()) + continue; + + label->clear(); + //label->setText(""); + QStringList list = label->statusTip().split("/"); + QString name = list.takeLast(); + + + QJsonObject obj = json_find(json, list); + if(!obj.isEmpty()) + { + if(obj.value(name).isString()) + { + QString result = obj.value(name).toString(); + if(name == "logo" || result.endsWith(".png") || result.endsWith(".jpg")) + { + label->setPixmap( + QPixmap::fromImage( + QImage::fromData( + this->download( + QUrl(result)))).scaled(120, 120, Qt::KeepAspectRatio)); + } + else if(result.contains("http")) + { + label->setText("" + name + ":
" + result + " "); + label->setOpenExternalLinks(true); + } + else + label->setText("" + name + ":
" + result); + } + else + { + if(obj.value(name).isBool()) + label->setText("" + name + ":
" + (obj.value(name).toBool() ? "true" : "false")); + else if(name == "lastchange") + label->setText("" + name + ":
" + QDateTime::fromTime_t(obj.value(name).toInt()).toLocalTime().toString()); + } + + } + + qDebug() << "NAME: " << name; + } + QString strUrl; + strUrl = json["state"].toObject() ["icon"].toObject() [json["state"].toObject()["open"].toBool() ? "open" : "closed"].toString(); @@ -77,18 +140,22 @@ void MainWindow::updateStatus() qDebug() << "downloading: " << strUrl; - this->imageArray = this->download(QUrl(strUrl)); - //qDebug("test: " + imageArray.size()); - QImage image = QImage::fromData(imageArray); - - this->currentIcon = new QIcon(QPixmap::fromImage(image)); - this->qsystemtrayicon->setIcon(*currentIcon); - - //QPixmap* pixmap = new QPixmap("/home/max/folder.png"); - QUrl logoURL(json["logo"].toString()); - //qDebug() << "logo: " << logoURL.toString(); - QPixmap pixmap = QPixmap::fromImage(QImage::fromData(this->download(logoURL))); - ui->label_icon->setPixmap(pixmap.scaled(120, 120, Qt::KeepAspectRatio)); + this->qsystemtrayicon->setIcon(QIcon( + QPixmap::fromImage( + QImage::fromData( + this->download( + QUrl(strUrl)))).scaled(120, 120, Qt::KeepAspectRatio))); + + if(this->qsystemtrayicon->icon().isNull()) + { + if(json["state"].toObject().contains("open")) + if(json["state"].toObject()["open"].toBool()) + this->qsystemtrayicon->setIcon(QIcon::fromTheme("face-smile")); + else + this->qsystemtrayicon->setIcon(QIcon::fromTheme("face-sad")); + else + this->qsystemtrayicon->setIcon(QIcon::fromTheme("face-sick")); + } } void MainWindow::saveSettings() @@ -96,8 +163,6 @@ void MainWindow::saveSettings() this->qsettings.setValue("hackerspace", ui->comboBox->currentText()); } - - QByteArray MainWindow::download(QUrl url) { qDebug() << "downloading: " << url.toString(); @@ -164,14 +229,17 @@ void MainWindow::slotActivated(QSystemTrayIcon::ActivationReason reason) void MainWindow::exitApplication() { qDebug() << "exit now!"; - exit(0); + QApplication::quit(); } void MainWindow::on_comboBox_currentIndexChanged(const QString &arg1) { qDebug() << arg1; - this->updateStatus(); - this->saveSettings(); + if(ui->comboBox->isEnabled()) + { + this->updateStatus(); + this->saveSettings(); + } } diff --git a/mainwindow.h b/mainwindow.h index 0ff1884..305b79b 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -9,6 +9,11 @@ #include #include +#include +#include +#include +#include + #include #include @@ -20,6 +25,8 @@ #include #include +#include + namespace Ui { class MainWindow; } @@ -33,14 +40,14 @@ public: ~MainWindow(); private: - Ui::MainWindow *ui; + Ui::MainWindow* ui; + QIcon* currentIcon; QSystemTrayIcon* qsystemtrayicon; - QByteArray download(QUrl url); - QIcon* currentIcon; - QByteArray imageArray; - void saveSettings(); QSettings qsettings;\ QTimer* timer; + void saveSettings(); + QByteArray download(QUrl url); + private slots: void slotActivated(QSystemTrayIcon::ActivationReason); diff --git a/mainwindow.ui b/mainwindow.ui index 4bd49e4..be54347 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -6,8 +6,8 @@ 0 0 - 952 - 497 + 927 + 532 @@ -23,11 +23,11 @@ - MainWindow + qhackerstatus - - folder.pngfolder.png + + :/icons/img/qspacestatus.png:/icons/img/qspacestatus.png @@ -41,6 +41,9 @@ 0 + + true + QComboBox::InsertAlphabetically @@ -79,7 +82,8 @@ refresh - + + .. false @@ -103,9 +107,9 @@ - + - 25 + 0 25 @@ -117,30 +121,248 @@ 25 - - - - 140 - 120 - - - - TextLabel + + + 0 - - Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop + + 0 - - - - - - false + + 0 - - true + + 0 - + + + + QLayout::SetMinAndMaxSize + + + 0 + + + 0 + + + + + + 0 + 0 + + + + + 120 + 120 + + + + logo + + + TextLabel + + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + 10 + + + QLayout::SetMaximumSize + + + 50 + + + + + + 0 + 0 + + + + url + + + url + + + Qt::RichText + + + Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop + + + true + + + + + + + location/address + + + location/address + + + + + + + contact/email + + + contact/email + + + + + + + contact/phone + + + contact/phone + + + + + + + contact/facebook + + + contact/facebook + + + + + + + contact/jabber + + + contact/jabber + + + + + + + contact/twitter + + + contact/twitter + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + 50 + + + 0 + + + + + state/open + + + state/open + + + + + + + state/lastchange + + + state/lastchange + + + + + + + state/trigger_person + + + state/trigger_person + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + @@ -154,7 +376,7 @@ 0 0 - 952 + 927 29 @@ -190,9 +412,16 @@ Update Status + + + Hackerspace + + - + + + actionExit diff --git a/qspace.qrc b/qspace.qrc new file mode 100644 index 0000000..ebd432b --- /dev/null +++ b/qspace.qrc @@ -0,0 +1,5 @@ + + + img/qspacestatus.png + + diff --git a/qspaceapi.pro b/qspaceapi.pro index 2cc1ade..67e018a 100644 --- a/qspaceapi.pro +++ b/qspaceapi.pro @@ -26,6 +26,8 @@ DISTFILES += \ folder.png \ packages/arch/PKGBUILD \ packages/arch/Makefile \ - share/xdg/qspacestatus.desktop + share/xdg/qspacestatus.desktop \ + img/qspace.blend -RESOURCES += +RESOURCES += \ + qspace.qrc -- cgit v1.2.3