diff --git a/src/playlistview.cpp b/src/playlistview.cpp index 737d8cb03..9937f4b68 100644 --- a/src/playlistview.cpp +++ b/src/playlistview.cpp @@ -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(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); +} diff --git a/src/playlistview.h b/src/playlistview.h index 7fa21665b..4a000e315 100644 --- a/src/playlistview.h +++ b/src/playlistview.h @@ -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 LoadBarPixmap(const QString& filename); + void MaybeAutoscroll(); private: static const char* kSettingsGroup; static const int kGlowIntensitySteps; + static const int kAutoscrollGraceTimeout; QList 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 currenttrack_bar_left_; QList currenttrack_bar_mid_;