Commit 10110d76 authored by Michael Pyne's avatar Michael Pyne
Browse files

Fix use of undefined behavior in status label.

The event filter doesn't check that the received event is a QMouseEvent
but it is possible (and actually happened) to receive other events from
Qt.

But in this case we don't even need an event filter, we're just handling
mouse events and Qt provides entirely usable methods for that already.
parent ab73b938
......@@ -25,8 +25,7 @@
#include <QMouseEvent>
#include <QLabel>
#include <QIcon>
#include <QFrame>
#include <QEvent>
#include <QToolButton>
#include <QPushButton>
#include <QStatusBar>
......@@ -67,18 +66,17 @@ StatusLabel::StatusLabel(const PlaylistInterface &currentPlaylist, QStatusBar *p
m_trackLabel->setTextFormat(Qt::PlainText);
parent->addPermanentWidget(m_trackLabel);
m_itemTimeLabel = new QLabel(this);
m_itemTimeLabel = new QToolButton(this);
QFontMetrics fontMetrics(font());
m_itemTimeLabel->setAlignment(Qt::AlignCenter);
m_itemTimeLabel->setMinimumWidth(fontMetrics.boundingRect("000:00 / 000:00").width());
m_itemTimeLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
m_itemTimeLabel->setFrameStyle(QFrame::Box | QFrame::Sunken);
m_itemTimeLabel->installEventFilter(this);
m_itemTimeLabel->setAutoRaise(true);
connect(m_itemTimeLabel, &QAbstractButton::clicked, this, [this]() {
m_showTimeRemaining = !m_showTimeRemaining;
updateTime();
});
parent->addPermanentWidget(m_itemTimeLabel);
setItemTotalTime(0);
setItemCurrentTime(0);
QPushButton *jumpButton = new QPushButton(this);
jumpButton->setIcon(QIcon::fromTheme("go-jump"));
jumpButton->setFlat(true);
......@@ -87,7 +85,6 @@ StatusLabel::StatusLabel(const PlaylistInterface &currentPlaylist, QStatusBar *p
connect(jumpButton, &QPushButton::clicked, action("showPlaying"), &QAction::trigger);
parent->addPermanentWidget(jumpButton);
installEventFilter(this);
slotCurrentPlaylistHasChanged(currentPlaylist);
}
......@@ -103,6 +100,7 @@ void StatusLabel::slotPlayingItemHasChanged(const FileHandle &file)
setItemCurrentTime(0);
m_trackLabel->setText(tag->artist() + mid + tag->title());
updateTime();
}
void StatusLabel::slotCurrentPlaylistHasChanged(const PlaylistInterface &currentPlaylist)
......@@ -117,6 +115,23 @@ void StatusLabel::slotCurrentPlaylistHasChanged(const PlaylistInterface &current
QStringLiteral(" - ") +
formatTime(qint64(1000) * currentPlaylist.time())
);
updateTime();
}
////////////////////////////////////////////////////////////////////////////////
// protected methods
////////////////////////////////////////////////////////////////////////////////
void StatusLabel::mouseReleaseEvent(QMouseEvent *ev)
{
if(ev->button() != Qt::LeftButton) {
return;
}
m_showTimeRemaining = !m_showTimeRemaining;
updateTime();
ev->accept();
}
////////////////////////////////////////////////////////////////////////////////
......@@ -134,25 +149,4 @@ void StatusLabel::updateTime()
m_itemTimeLabel->setText(timeString);
}
bool StatusLabel::eventFilter(QObject *o, QEvent *e)
{
if(!o || !e)
return false;
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(e);
if(e->type() == QEvent::MouseButtonRelease &&
mouseEvent->button() == Qt::LeftButton)
{
if(o == m_itemTimeLabel) {
m_showTimeRemaining = !m_showTimeRemaining;
updateTime();
}
else
action("showPlaying")->trigger();
return true;
}
return false;
}
// vim: set et sw=4 tw=0 sta:
......@@ -17,13 +17,14 @@
#ifndef JUK_STATUSLABEL_H
#define JUK_STATUSLABEL_H
#include <QStatusBar>
#include <QWidget>
class KSqueezedTextLabel;
class QEvent;
class QLabel;
class QStatusBar;
class QToolButton;
class FileHandle;
class PlaylistInterface;
......@@ -46,13 +47,15 @@ public slots:
void setItemTotalTime(qint64 time_msec) { m_itemTotalTime = time_msec; }
void setItemCurrentTime(qint64 time_msec) { m_itemCurrentTime = time_msec; updateTime(); }
protected:
void mouseReleaseEvent(QMouseEvent *) override;
private:
void updateTime();
virtual bool eventFilter(QObject *o, QEvent *e) override;
KSqueezedTextLabel *m_playlistLabel = nullptr;
QLabel *m_trackLabel = nullptr;
QLabel *m_itemTimeLabel = nullptr;
QToolButton *m_itemTimeLabel = nullptr;
int m_itemTotalTime = 0;
int m_itemCurrentTime = 0;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment