From 881aa3083aac75153f8fcc9548d48951a01f8fd2 Mon Sep 17 00:00:00 2001 From: Anthony Bilinski Date: Mon, 22 Apr 2019 03:56:22 -0700 Subject: [PATCH] fix(status): use enum as UI property instead of untranslated string Translation of getStatusTitle introduced in 15d72a9610bc6e4e2e93a9c36ee6536b0aa13429, breaking asset path from string. --- src/model/status.cpp | 38 +++++++++++++++++++++++--------------- src/model/status.h | 1 + src/widget/widget.cpp | 22 ++++++++-------------- 3 files changed, 32 insertions(+), 29 deletions(-) diff --git a/src/model/status.cpp b/src/model/status.cpp index 189c03a8e..292ad97c3 100644 --- a/src/model/status.cpp +++ b/src/model/status.cpp @@ -47,24 +47,32 @@ namespace Status return QStringLiteral(""); } + QString getAssetSuffix(Status status) + { + switch (status) { + case Status::Online: + return "online"; + case Status::Away: + return "away"; + case Status::Busy: + return "busy"; + case Status::Offline: + return "offline"; + case Status::Blocked: + return "blocked"; + } + assert(false); + return QStringLiteral(""); + } + QString getIconPath(Status status, bool event) { const QString eventSuffix = event ? QStringLiteral("_notification") : QString(); - - switch (status) { - case Status::Online: - return ":/img/status/online" + eventSuffix + ".svg"; - case Status::Away: - return ":/img/status/away" + eventSuffix + ".svg"; - case Status::Busy: - return ":/img/status/busy" + eventSuffix + ".svg"; - case Status::Offline: - return ":/img/status/offline" + eventSuffix + ".svg"; - case Status::Blocked: - return ":/img/status/blocked.svg"; + const QString statusSuffix = getAssetSuffix(status); + if (status == Status::Blocked) { + return ":/img/status/" + statusSuffix + ".svg"; + } else { + return ":/img/status/" + statusSuffix + eventSuffix + ".svg"; } - qWarning() << "Status unknown"; - assert(false); - return QString{}; } } // namespace Status diff --git a/src/model/status.h b/src/model/status.h index 21958957d..b80c014cf 100644 --- a/src/model/status.h +++ b/src/model/status.h @@ -37,6 +37,7 @@ namespace Status QString getIconPath(Status status, bool event = false); QString getTitle(Status status); + QString getAssetSuffix(Status status); } #endif // STATUS_H diff --git a/src/widget/widget.cpp b/src/widget/widget.cpp index 1571e9373..230dcaa3c 100644 --- a/src/widget/widget.cpp +++ b/src/widget/widget.cpp @@ -437,15 +437,9 @@ void Widget::updateIcons() return; } - QString status; - if (eventIcon) { - status = QStringLiteral("event"); - } else { - status = ui->statusButton->property("status").toString(); - if (!status.length()) { - status = QStringLiteral("offline"); - } - } + const QString assetSuffix = eventIcon ? "event" : + Status::getAssetSuffix(static_cast(ui->statusButton->property("status").toInt())); + // Some builds of Qt appear to have a bug in icon loading: // QIcon::hasThemeIcon is sometimes unaware that the icon returned @@ -474,11 +468,11 @@ void Widget::updateIcons() } QIcon ico; - if (!hasThemeIconBug && QIcon::hasThemeIcon("qtox-" + status)) { - ico = QIcon::fromTheme("qtox-" + status); + if (!hasThemeIconBug && QIcon::hasThemeIcon("qtox-" + assetSuffix)) { + ico = QIcon::fromTheme("qtox-" + assetSuffix); } else { QString color = settings.getLightTrayIcon() ? "light" : "dark"; - QString path = ":/img/taskbar/" + color + "/taskbar_" + status + ".svg"; + QString path = ":/img/taskbar/" + color + "/taskbar_" + assetSuffix + ".svg"; QSvgRenderer renderer(path); // Prepare a QImage with desired characteritisc @@ -649,7 +643,7 @@ void Widget::onBadProxyCore() void Widget::onStatusSet(Status::Status status) { - ui->statusButton->setProperty("status", getTitle(status)); + ui->statusButton->setProperty("status", static_cast(status)); ui->statusButton->setIcon(prepareIcon(getIconPath(status), icon_size, icon_size)); updateIcons(); } @@ -1968,7 +1962,7 @@ void Widget::onUserAwayCheck() { #ifdef QTOX_PLATFORM_EXT uint32_t autoAwayTime = settings.getAutoAwayTime() * 60 * 1000; - bool online = ui->statusButton->property("status").toString() == "online"; + bool online = static_cast(ui->statusButton->property("status").toInt()) == Status::Status::Online; bool away = autoAwayTime && Platform::getIdleTime() >= autoAwayTime; if (online && away) {