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

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