From 5e69e8130c9f207e5f8e3222841e7771c56ccd24 Mon Sep 17 00:00:00 2001 From: Arno Date: Sun, 9 Apr 2017 04:10:32 +0200 Subject: Show volume as tray icon message Use a QTimer to prevent the messages to pile up on every volume change. Only show it when the timer isn't running. The timeout value of 500msecs is totally arbitrary. The mStarting thingy prevents showing the message twice while constructing (setupGui and readSettings). --- playerwidget.cpp | 34 +++++++++++++++++++++++++--------- playerwidget.h | 5 +++++ 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/playerwidget.cpp b/playerwidget.cpp index 1e4a7c6..c476f93 100644 --- a/playerwidget.cpp +++ b/playerwidget.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -35,8 +36,10 @@ #include "helper.h" PlayerWidget::PlayerWidget(QWidget *parent) : QWidget(parent), mDurSecs(0), mPlayListLength(0){ + mStarting = true; setupGui(); createActions(); + mStarting = false; } PlayerWidget::~PlayerWidget(){ @@ -53,6 +56,9 @@ void PlayerWidget::setupGui(){ mTrayIcon = new QSystemTrayIcon(this); mTrayIcon->setIcon(QIcon(":/stop.png")); mTrayIcon->show(); + mVolumeTimer = new QTimer(this); + mVolumeTimer->setSingleShot(true); + connect(mVolumeTimer, &QTimer::timeout, this, &PlayerWidget::showVolume); //THE view mView = new BeetView; @@ -633,7 +639,7 @@ void PlayerWidget::doPlay(){ emit playModeChanged(tr("Playing")); emit setWinTitle(mCurWinTitle); mTrayIcon->setIcon(QIcon(":/play.png")); - mTrayIcon->setToolTip(mCurWinTitle); + mTrayIcon->setToolTip(mCurToolTip); return; } int playListCount = mPlayListModel->rowCount(); @@ -727,6 +733,12 @@ void PlayerWidget::volumeDown(){ adjustVolume(-2); } +void PlayerWidget::showVolume(){ + int volume = mVolumeSlider->value(); + QString msg = QString(tr("Volume: %1%")).arg(volume, 3, 10, QChar('0')); + mTrayIcon->showMessage(msg, QString(), QSystemTrayIcon::Information, 2000); +} + void PlayerWidget::adjustVolume(int by){ int curVol = mVolumeSlider->value(); int newVol = curVol + by; @@ -971,7 +983,7 @@ void PlayerWidget::playCurrent(const QModelIndex &index){ QString fullPath = index.data(FullPathRole).toString(); play(fullPath); mTrayIcon->setIcon(QIcon(":/play.png")); - mTrayIcon->setToolTip(mCurWinTitle); + mTrayIcon->setToolTip(mCurToolTip); } void PlayerWidget::play(const QString &fullPath){ @@ -1004,13 +1016,11 @@ void PlayerWidget::play(const QString &fullPath){ mCurrentTE->append(QString("%1 %2:%3").arg(tr("Length:"), -20).arg(minutes, 2, 10, QChar('0')).arg(seconds, 2, 10, QChar('0'))); QString msg = QString("File: %1").arg(fullPath); emit message(msg); - mCurWinTitle = QString(tr("%1 [%2 - %3 - %4]")).arg(qApp->applicationName()).arg(artist).arg(album).arg(title); + mCurWinTitle = QString(tr("%1 - [%2] - [%3] - [%4]")).arg(qApp->applicationName()).arg(artist).arg(album).arg(title); + mCurToolTip = QString(tr("%1: [%2] - [%3]")).arg(artist).arg(album).arg(title); + mTrayIcon->setToolTip(mCurToolTip); + mTrayIcon->showMessage(QString(tr("Now Playing:")), mCurToolTip, QSystemTrayIcon::Information, 5000); emit setWinTitle(mCurWinTitle); - QString toolStr; - toolStr.append(""); - toolStr.append(QString("

%1

").arg(artist)); - toolStr.append(QString("

%1

").arg(title)); - toolStr.append(""); mPlayer->play(); mPlayA->setChecked(true); @@ -1020,7 +1030,11 @@ void PlayerWidget::play(const QString &fullPath){ void PlayerWidget::volumeChanged(int volume){ QString s = QString("%1 %").arg(volume, 3, 10, QChar('0')); mVolumePos->setText(s); - QString tool = QString(tr("Volume: %1")).arg(s); + if(!mStarting){ + if(!mVolumeTimer->isActive()){ + mVolumeTimer->start(500); + } + } } void PlayerWidget::mute(bool triggered){ @@ -1096,11 +1110,13 @@ void PlayerWidget::expandRecursive(const QModelIndex &idx){ } void PlayerWidget::readSettings(){ + mStarting = true; QSettings s; int vol = s.value("volume").toInt(); mVolumeSlider->setValue(vol); QString dir = s.value("folderdir", QDir::homePath()).toString(); mCurDir = dir; + mStarting = false; } void PlayerWidget::writeSettings(){ diff --git a/playerwidget.h b/playerwidget.h index f42a882..6ef5dd1 100644 --- a/playerwidget.h +++ b/playerwidget.h @@ -14,6 +14,7 @@ class QTextEdit; class QMediaPlayer; class QToolBar; class QAction; +class QTimer; class BeetPlayerProxy; class BeetView; class QSystemTrayIcon; @@ -45,6 +46,7 @@ class PlayerWidget : public QWidget { void doDeleteFiles(); void volumeUp(); void volumeDown(); + void showVolume(); void doFilter(); void clearFilter(); void reindex(); @@ -118,7 +120,10 @@ class PlayerWidget : public QWidget { quint64 mPlayListLength; QString mCurDir; QString mCurWinTitle; + QString mCurToolTip; QSystemTrayIcon *mTrayIcon; + QTimer *mVolumeTimer; + bool mStarting; }; #endif // PLAYERWIDGET_H -- cgit v1.2.3-70-g09d2