diff --git a/src/core/mergedproxymodel.cpp b/src/core/mergedproxymodel.cpp index e2c5cd93e..355b60429 100644 --- a/src/core/mergedproxymodel.cpp +++ b/src/core/mergedproxymodel.cpp @@ -133,6 +133,8 @@ void MergedProxyModel::SubModelReset() { beginInsertRows(proxy_parent, 0, count-1); endInsertRows(); } + + emit SubModelReset(proxy_parent, submodel); } QModelIndex MergedProxyModel::GetActualSourceParent(const QModelIndex& source_parent, diff --git a/src/core/mergedproxymodel.h b/src/core/mergedproxymodel.h index 96523591c..44ed651e7 100644 --- a/src/core/mergedproxymodel.h +++ b/src/core/mergedproxymodel.h @@ -72,6 +72,9 @@ class MergedProxyModel : public QAbstractProxyModel { QModelIndex mapToSource(const QModelIndex &proxyIndex) const; void setSourceModel(QAbstractItemModel *sourceModel); + signals: + void SubModelReset(const QModelIndex& root, QAbstractItemModel* model); + private slots: void SourceModelReset(); void SubModelReset(); diff --git a/src/library/libraryview.cpp b/src/library/libraryview.cpp index e7b1d1348..409d03aa3 100644 --- a/src/library/libraryview.cpp +++ b/src/library/libraryview.cpp @@ -25,7 +25,6 @@ #include #include -const int LibraryView::kRowsToShow = 50; const char* LibraryView::kSettingsGroup = "LibraryView"; LibraryItemDelegate::LibraryItemDelegate(QObject *parent) @@ -75,17 +74,14 @@ void LibraryItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &o } LibraryView::LibraryView(QWidget* parent) - : QTreeView(parent), + : AutoExpandingTreeView(parent), library_(NULL), total_song_count_(-1), - auto_open_(true), nomusic_(":nomusic.png"), context_menu_(new QMenu(this)) { setItemDelegate(new LibraryItemDelegate(this)); - connect(this, SIGNAL(expanded(QModelIndex)), SLOT(ItemExpanded(QModelIndex))); - load_ = context_menu_->addAction(QIcon(":/media-playback-start.png"), tr("Load"), this, SLOT(Load())); add_to_playlist_ = context_menu_->addAction(QIcon(":/media-playback-start.png"), @@ -103,7 +99,7 @@ void LibraryView::ReloadSettings() { QSettings s; s.beginGroup(kSettingsGroup); - auto_open_ = s.value("auto_open", true).toBool(); + SetAutoOpen(s.value("auto_open", true).toBool()); } void LibraryView::SetLibrary(LibraryModel *library) { @@ -122,16 +118,6 @@ void LibraryView::TotalSongCountUpdated(int count) { unsetCursor(); } -void LibraryView::reset() { - QTreeView::reset(); - - // Expand nodes in the tree until we have about 50 rows visible in the view - if (auto_open_) { - int rows = model()->rowCount(rootIndex()); - RecursivelyExpand(rootIndex(), &rows); - } -} - void LibraryView::paintEvent(QPaintEvent* event) { QTreeView::paintEvent(event); QPainter p(viewport()); @@ -169,30 +155,6 @@ void LibraryView::mouseReleaseEvent(QMouseEvent* e) { } } -bool LibraryView::RecursivelyExpand(const QModelIndex& index, int* count) { - if (model()->canFetchMore(index)) - model()->fetchMore(index); - - int children = model()->rowCount(index); - if (*count + children > kRowsToShow) - return false; - - expand(index); - *count += children; - - for (int i=0 ; iindex(i, 0, index), count)) - return false; - } - - return true; -} - -void LibraryView::ItemExpanded(const QModelIndex& index) { - if (model()->rowCount(index) == 1 && auto_open_) - expand(model()->index(0, 0, index)); -} - void LibraryView::contextMenuEvent(QContextMenuEvent *e) { context_menu_index_ = indexAt(e->pos()); if (!context_menu_index_.isValid()) diff --git a/src/library/libraryview.h b/src/library/libraryview.h index 9c1497d31..6a9d05ffa 100644 --- a/src/library/libraryview.h +++ b/src/library/libraryview.h @@ -17,8 +17,9 @@ #ifndef LIBRARYVIEW_H #define LIBRARYVIEW_H +#include "widgets/autoexpandingtreeview.h" + #include -#include class LibraryModel; @@ -28,7 +29,7 @@ class LibraryItemDelegate : public QStyledItemDelegate { void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const; }; -class LibraryView : public QTreeView { +class LibraryView : public AutoExpandingTreeView { Q_OBJECT public: @@ -48,16 +49,12 @@ class LibraryView : public QTreeView { void AddToPlaylist(const QModelIndex& index); protected: - // QAbstractItemView - void reset(); - // QWidget void paintEvent(QPaintEvent* event); void mouseReleaseEvent(QMouseEvent* e); void contextMenuEvent(QContextMenuEvent* e); private slots: - void ItemExpanded(const QModelIndex& index); void Load(); void AddToPlaylist(); void ShowInVarious(); @@ -65,15 +62,11 @@ class LibraryView : public QTreeView { private: void RecheckIsEmpty(); - bool RecursivelyExpand(const QModelIndex& index, int* count); void ShowInVarious(bool on); private: - static const int kRowsToShow; - LibraryModel* library_; int total_song_count_; - bool auto_open_; QPixmap nomusic_; diff --git a/src/radio/radioview.cpp b/src/radio/radioview.cpp index 9355fe4cb..c749ed652 100644 --- a/src/radio/radioview.cpp +++ b/src/radio/radioview.cpp @@ -22,9 +22,10 @@ #include RadioView::RadioView(QWidget *parent) - : QTreeView(parent) + : AutoExpandingTreeView(parent) { setItemDelegate(new LibraryItemDelegate(this)); + SetExpandOnReset(false); } void RadioView::contextMenuEvent(QContextMenuEvent* e) { @@ -44,3 +45,13 @@ void RadioView::contextMenuEvent(QContextMenuEvent* e) { void RadioView::currentChanged(const QModelIndex ¤t, const QModelIndex&) { emit CurrentIndexChanged(current); } + +void RadioView::setModel(QAbstractItemModel *model) { + AutoExpandingTreeView::setModel(model); + + if (MergedProxyModel* merged_model = qobject_cast(model)) { + connect(merged_model, + SIGNAL(SubModelReset(QModelIndex,QAbstractItemModel*)), + SLOT(RecursivelyExpand(QModelIndex))); + } +} diff --git a/src/radio/radioview.h b/src/radio/radioview.h index 9f14df373..dc49faff5 100644 --- a/src/radio/radioview.h +++ b/src/radio/radioview.h @@ -17,9 +17,9 @@ #ifndef RADIOVIEW_H #define RADIOVIEW_H -#include +#include "widgets/autoexpandingtreeview.h" -class RadioView : public QTreeView { +class RadioView : public AutoExpandingTreeView { Q_OBJECT public: @@ -30,6 +30,7 @@ class RadioView : public QTreeView { // QTreeView void currentChanged(const QModelIndex ¤t, const QModelIndex &previous); + void setModel(QAbstractItemModel *model); signals: void CurrentIndexChanged(const QModelIndex& index); diff --git a/src/widgets/CMakeLists.txt b/src/widgets/CMakeLists.txt index 007d93027..adfbc21ba 100644 --- a/src/widgets/CMakeLists.txt +++ b/src/widgets/CMakeLists.txt @@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 2.6) include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}) set(SOURCES + autoexpandingtreeview.cpp busyindicator.cpp equalizerslider.cpp fileview.cpp @@ -19,6 +20,7 @@ set(SOURCES ) set(HEADERS + autoexpandingtreeview.h busyindicator.h equalizerslider.h fileview.h diff --git a/src/widgets/autoexpandingtreeview.cpp b/src/widgets/autoexpandingtreeview.cpp new file mode 100644 index 000000000..c4c7abada --- /dev/null +++ b/src/widgets/autoexpandingtreeview.cpp @@ -0,0 +1,65 @@ +/* This file is part of Clementine. + + Clementine is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Clementine is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Clementine. If not, see . +*/ + +#include "autoexpandingtreeview.h" + +const int AutoExpandingTreeView::kRowsToShow = 50; + +AutoExpandingTreeView::AutoExpandingTreeView(QWidget *parent) + : QTreeView(parent), + auto_open_(true), + expand_on_reset_(true) +{ + connect(this, SIGNAL(expanded(QModelIndex)), SLOT(ItemExpanded(QModelIndex))); +} + +void AutoExpandingTreeView::reset() { + QTreeView::reset(); + + // Expand nodes in the tree until we have about 50 rows visible in the view + if (auto_open_ && expand_on_reset_) { + RecursivelyExpand(rootIndex()); + } +} + +void AutoExpandingTreeView::RecursivelyExpand(const QModelIndex &index) { + int rows = model()->rowCount(index); + RecursivelyExpand(index, &rows); +} + +bool AutoExpandingTreeView::RecursivelyExpand(const QModelIndex& index, int* count) { + if (model()->canFetchMore(index)) + model()->fetchMore(index); + + int children = model()->rowCount(index); + if (*count + children > kRowsToShow) + return false; + + expand(index); + *count += children; + + for (int i=0 ; iindex(i, 0, index), count)) + return false; + } + + return true; +} + +void AutoExpandingTreeView::ItemExpanded(const QModelIndex& index) { + if (model()->rowCount(index) == 1 && auto_open_) + expand(model()->index(0, 0, index)); +} diff --git a/src/widgets/autoexpandingtreeview.h b/src/widgets/autoexpandingtreeview.h new file mode 100644 index 000000000..219a8ba39 --- /dev/null +++ b/src/widgets/autoexpandingtreeview.h @@ -0,0 +1,51 @@ +/* This file is part of Clementine. + + Clementine is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Clementine is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Clementine. If not, see . +*/ + +#ifndef AUTOEXPANDINGTREEVIEW_H +#define AUTOEXPANDINGTREEVIEW_H + +#include + +class AutoExpandingTreeView : public QTreeView { + Q_OBJECT + + public: + AutoExpandingTreeView(QWidget* parent = 0); + + static const int kRowsToShow; + + void SetAutoOpen(bool v) { auto_open_ = v; } + void SetExpandOnReset(bool v) { expand_on_reset_ = v; } + + public slots: + void RecursivelyExpand(const QModelIndex &index); + + protected: + // QAbstractItemView + void reset(); + + private slots: + void ItemExpanded(const QModelIndex& index); + + private: + bool RecursivelyExpand(const QModelIndex& index, int* count); + + private: + bool auto_open_; + bool expand_on_reset_; +}; + +#endif // AUTOEXPANDINGTREEVIEW_H