Automatically scroll to the currently playing song whenever (it changes || the window is shown) && (the user hasn't done anything for 1 minute). Fixes issue #124

This commit is contained in:
David Sansome 2010-04-08 22:59:02 +00:00
parent f10873e9b8
commit 0cd8f42ea5
2 changed files with 55 additions and 0 deletions

View File

@ -31,6 +31,7 @@
const char* PlaylistView::kSettingsGroup = "Playlist";
const int PlaylistView::kGlowIntensitySteps = 32;
const int PlaylistView::kAutoscrollGraceTimeout = 60; // seconds
PlaylistView::PlaylistView(QWidget *parent)
@ -38,6 +39,8 @@ PlaylistView::PlaylistView(QWidget *parent)
glow_enabled_(false),
glow_timer_(new QTimer(this)),
glow_intensity_step_(0),
inhibit_autoscroll_timer_(new QTimer(this)),
inhibit_autoscroll_(false),
row_height_(-1),
currenttrack_play_(":currenttrack_play.png"),
currenttrack_pause_(":currenttrack_pause.png")
@ -48,6 +51,10 @@ PlaylistView::PlaylistView(QWidget *parent)
connect(header(), SIGNAL(sectionResized(int,int,int)), SLOT(SaveGeometry()));
connect(header(), SIGNAL(sectionMoved(int,int,int)), SLOT(SaveGeometry()));
inhibit_autoscroll_timer_->setInterval(kAutoscrollGraceTimeout * 1000);
inhibit_autoscroll_timer_->setSingleShot(true);
connect(inhibit_autoscroll_timer_, SIGNAL(timeout()), SLOT(InhibitAutoscrollTimeout()));
glow_timer_->setInterval(1500 / kGlowIntensitySteps);
connect(glow_timer_, SIGNAL(timeout()), SLOT(GlowIntensityChanged()));
@ -211,6 +218,7 @@ void PlaylistView::hideEvent(QHideEvent*) {
void PlaylistView::showEvent(QShowEvent*) {
if (glow_enabled_)
glow_timer_->start();
MaybeAutoscroll();
}
bool CompareSelectionRanges(const QItemSelectionRange& a, const QItemSelectionRange& b) {
@ -320,3 +328,39 @@ void PlaylistView::closeEditor(QWidget* editor, QAbstractItemDelegate::EndEditHi
QTreeView::closeEditor(editor, hint);
}
}
void PlaylistView::mousePressEvent(QMouseEvent *event) {
QTreeView::mousePressEvent(event);
inhibit_autoscroll_ = true;
inhibit_autoscroll_timer_->start();
}
void PlaylistView::scrollContentsBy(int dx, int dy) {
QTreeView::scrollContentsBy(dx, dy);
inhibit_autoscroll_ = true;
inhibit_autoscroll_timer_->start();
}
void PlaylistView::InhibitAutoscrollTimeout() {
// For 1 minute after the user clicks on or scrolls the playlist we promise
// not to automatically scroll the view to keep up with a track change.
inhibit_autoscroll_ = false;
}
void PlaylistView::dataChanged(const QModelIndex&, const QModelIndex&) {
MaybeAutoscroll();
}
void PlaylistView::MaybeAutoscroll() {
Playlist* playlist = qobject_cast<Playlist*>(model());
Q_ASSERT(playlist);
if (inhibit_autoscroll_)
return;
if (playlist->current_index() == -1)
return;
QModelIndex current = playlist->index(playlist->current_index(), 0);
scrollTo(current, QAbstractItemView::PositionAtCenter);
}

View File

@ -53,19 +53,27 @@ class PlaylistView : public QTreeView {
protected:
void hideEvent(QHideEvent* event);
void showEvent(QShowEvent* event);
void mousePressEvent(QMouseEvent *event);
void scrollContentsBy(int dx, int dy);
protected slots:
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight);
private slots:
void LoadGeometry();
void SaveGeometry();
void GlowIntensityChanged();
void InhibitAutoscrollTimeout();
private:
void ReloadBarPixmaps();
QList<QPixmap> LoadBarPixmap(const QString& filename);
void MaybeAutoscroll();
private:
static const char* kSettingsGroup;
static const int kGlowIntensitySteps;
static const int kAutoscrollGraceTimeout;
QList<int> GetEditableColumns();
QModelIndex NextEditableIndex(const QModelIndex& current);
@ -77,6 +85,9 @@ class PlaylistView : public QTreeView {
QModelIndex last_current_item_;
QRect last_glow_rect_;
QTimer* inhibit_autoscroll_timer_;
bool inhibit_autoscroll_;
int row_height_; // Used to invalidate the currenttrack_bar pixmaps
QList<QPixmap> currenttrack_bar_left_;
QList<QPixmap> currenttrack_bar_mid_;