Create systray tooltip workaround for KDE

This commit is contained in:
Jonas Kvinge 2019-02-13 20:04:05 +01:00
parent 65615495d9
commit d3b3c309fa
7 changed files with 62 additions and 26 deletions

View File

@ -6,8 +6,9 @@
<file>schema/schema-3.sql</file> <file>schema/schema-3.sql</file>
<file>schema/device-schema.sql</file> <file>schema/device-schema.sql</file>
<file>style/strawberry.css</file> <file>style/strawberry.css</file>
<file>misc/playing-tooltip.html</file> <file>html/playing-tooltip-plain.html</file>
<file>misc/oauthsuccess.html</file> <file>html/playing-tooltip-table.html</file>
<file>html/oauthsuccess.html</file>
<file>pictures/strawberry.png</file> <file>pictures/strawberry.png</file>
<file>pictures/strawberry-faded.png</file> <file>pictures/strawberry-faded.png</file>
<file>pictures/nomusic.png</file> <file>pictures/nomusic.png</file>

View File

@ -0,0 +1,8 @@
<h4>%appName</h4>
<p>
%image<br />
%titleKey: %titleValue<br />
%artistKey: %artistValue<br />
%albumKey: %albumValue<br />
%lengthKey: %lengthValue<br />
</p>

View File

@ -26,14 +26,18 @@
#include <QAction> #include <QAction>
#include <QIODevice> #include <QIODevice>
#include <QFile> #include <QFile>
#include <QLabel>
#include <QMenu> #include <QMenu>
#include <QIcon> #include <QIcon>
#include <QString> #include <QString>
#include <QtEvents> #include <QtEvents>
#include <QSettings> #include <QSettings>
#include "core/logging.h"
#include "song.h" #include "song.h"
#include "iconloader.h" #include "iconloader.h"
#include "utilities.h"
#include "systemtrayicon.h" #include "systemtrayicon.h"
#include "qtsystemtrayicon.h" #include "qtsystemtrayicon.h"
@ -53,18 +57,29 @@ QtSystemTrayIcon::QtSystemTrayIcon(QObject *parent)
action_stop_after_this_track_(nullptr), action_stop_after_this_track_(nullptr),
action_mute_(nullptr) { action_mute_(nullptr) {
app_name_[0] = app_name_[0].toUpper();
tray_->setIcon(normal_icon_); tray_->setIcon(normal_icon_);
tray_->installEventFilter(this); tray_->installEventFilter(this);
ClearNowPlaying(); ClearNowPlaying();
QFile pattern_file(":/misc/playing-tooltip.html"); #ifndef Q_OS_WIN
de_ = Utilities::DesktopEnvironment().toLower();
QFile pattern_file;
if (de_ == "kde") {
pattern_file.setFileName(":/html/playing-tooltip-plain.html");
}
else {
pattern_file.setFileName(":/html/playing-tooltip-table.html");
}
pattern_file.open(QIODevice::ReadOnly); pattern_file.open(QIODevice::ReadOnly);
pattern_ = QString::fromLatin1(pattern_file.readAll()); pattern_ = QString::fromLatin1(pattern_file.readAll());
pattern_file.close();
#endif
connect(tray_, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), SLOT(Clicked(QSystemTrayIcon::ActivationReason))); connect(tray_, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), SLOT(Clicked(QSystemTrayIcon::ActivationReason)));
app_name_[0] = app_name_[0].toUpper();
} }
QtSystemTrayIcon::~QtSystemTrayIcon() { QtSystemTrayIcon::~QtSystemTrayIcon() {
@ -223,36 +238,43 @@ void QtSystemTrayIcon::SetNowPlaying(const Song &song, const QString &image_path
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
// Windows doesn't support HTML in tooltips, so just show something basic // Windows doesn't support HTML in tooltips, so just show something basic
tray_->setToolTip(song.PrettyTitleWithArtist()); tray_->setToolTip(song.PrettyTitleWithArtist());
return; #else
#endif
int columns = image_path == nullptr ? 1 : 2; int columns = image_path == nullptr ? 1 : 2;
QString clone = pattern_; QString tooltip(pattern_);
clone.replace("%columns" , QString::number(columns)); tooltip.replace("%columns" , QString::number(columns));
clone.replace("%appName" , app_name_); tooltip.replace("%appName" , app_name_);
clone.replace("%titleKey" , tr("Title") % ":"); tooltip.replace("%titleKey" , tr("Title") % ":");
clone.replace("%titleValue" , song.PrettyTitle().toHtmlEscaped()); tooltip.replace("%titleValue" , song.PrettyTitle().toHtmlEscaped());
clone.replace("%artistKey" , tr("Artist") % ":"); tooltip.replace("%artistKey" , tr("Artist") % ":");
clone.replace("%artistValue", song.artist().toHtmlEscaped()); tooltip.replace("%artistValue", song.artist().toHtmlEscaped());
clone.replace("%albumKey" , tr("Album") % ":"); tooltip.replace("%albumKey" , tr("Album") % ":");
clone.replace("%albumValue" , song.album().toHtmlEscaped()); tooltip.replace("%albumValue" , song.album().toHtmlEscaped());
clone.replace("%lengthKey" , tr("Length") % ":"); tooltip.replace("%lengthKey" , tr("Length") % ":");
clone.replace("%lengthValue", song.PrettyLength().toHtmlEscaped()); tooltip.replace("%lengthValue", song.PrettyLength().toHtmlEscaped());
if(columns == 2) { if (columns == 2) {
QString final_path = image_path.startsWith("file://") ? image_path.mid(7) : image_path; QString final_path = image_path.startsWith("file://") ? image_path.mid(7) : image_path;
clone.replace("%image", " <td> <img src=\"" % final_path % "\" /> </td>"); if (de_ == "kde") {
tooltip.replace("%image", "<img src=\"" % final_path % "\" />");
}
else {
tooltip.replace("%image", " <td> <img src=\"" % final_path % "\" /> </td>");
}
} }
else { else {
clone.replace("%image", ""); tooltip.replace("<td>%image</td>", "");
tooltip.replace("%image", "");
} }
// TODO: we should also repaint this // TODO: we should also repaint this
tray_->setToolTip(clone); tray_->setToolTip(tooltip);
#endif
} }

View File

@ -27,6 +27,7 @@
#include <QObject> #include <QObject>
#include <QSystemTrayIcon> #include <QSystemTrayIcon>
#include <QLabel>
#include <QString> #include <QString>
#include <QPixmap> #include <QPixmap>
#include <QAction> #include <QAction>
@ -55,7 +56,7 @@ class QtSystemTrayIcon : public SystemTrayIcon {
void SetNowPlaying(const Song &song, const QString &image_path); void SetNowPlaying(const Song &song, const QString &image_path);
void ClearNowPlaying(); void ClearNowPlaying();
protected: protected:
// SystemTrayIcon // SystemTrayIcon
void UpdateIcon(); void UpdateIcon();
void SetPaused(); void SetPaused();
@ -66,10 +67,10 @@ protected:
// QObject // QObject
bool eventFilter(QObject *, QEvent *); bool eventFilter(QObject *, QEvent *);
private slots: private slots:
void Clicked(QSystemTrayIcon::ActivationReason); void Clicked(QSystemTrayIcon::ActivationReason);
private: private:
QSystemTrayIcon *tray_; QSystemTrayIcon *tray_;
QMenu *menu_; QMenu *menu_;
QString app_name_; QString app_name_;
@ -82,7 +83,11 @@ private:
QAction *action_stop_after_this_track_; QAction *action_stop_after_this_track_;
QAction *action_mute_; QAction *action_mute_;
#ifndef Q_OS_WIN
QString de_;
QString pattern_; QString pattern_;
#endif
}; };
#endif // QTSYSTEMTRAYICON_H #endif // QTSYSTEMTRAYICON_H

View File

@ -68,7 +68,7 @@ void LocalRedirectServer::ReadyRead(QTcpSocket* socket, QByteArray buffer) {
void LocalRedirectServer::WriteTemplate(QTcpSocket* socket) const { void LocalRedirectServer::WriteTemplate(QTcpSocket* socket) const {
QFile page_file(":/misc/oauthsuccess.html"); QFile page_file(":/html/oauthsuccess.html");
page_file.open(QIODevice::ReadOnly); page_file.open(QIODevice::ReadOnly);
QString page_data = QString::fromUtf8(page_file.readAll()); QString page_data = QString::fromUtf8(page_file.readAll());