Automatically expand the nodes in the magnatune database when searching

This commit is contained in:
David Sansome 2010-05-16 23:44:33 +00:00
parent 4b9fd35db1
commit 84c8caea55
9 changed files with 143 additions and 53 deletions

View File

@ -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,

View File

@ -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();

View File

@ -25,7 +25,6 @@
#include <QSortFilterProxyModel>
#include <QSettings>
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 ; i<children ; ++i) {
if (!RecursivelyExpand(model()->index(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())

View File

@ -17,8 +17,9 @@
#ifndef LIBRARYVIEW_H
#define LIBRARYVIEW_H
#include "widgets/autoexpandingtreeview.h"
#include <QStyledItemDelegate>
#include <QTreeView>
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_;

View File

@ -22,9 +22,10 @@
#include <QContextMenuEvent>
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 &current, const QModelIndex&) {
emit CurrentIndexChanged(current);
}
void RadioView::setModel(QAbstractItemModel *model) {
AutoExpandingTreeView::setModel(model);
if (MergedProxyModel* merged_model = qobject_cast<MergedProxyModel*>(model)) {
connect(merged_model,
SIGNAL(SubModelReset(QModelIndex,QAbstractItemModel*)),
SLOT(RecursivelyExpand(QModelIndex)));
}
}

View File

@ -17,9 +17,9 @@
#ifndef RADIOVIEW_H
#define RADIOVIEW_H
#include <QTreeView>
#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 &current, const QModelIndex &previous);
void setModel(QAbstractItemModel *model);
signals:
void CurrentIndexChanged(const QModelIndex& index);

View File

@ -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

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#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 ; i<children ; ++i) {
if (!RecursivelyExpand(model()->index(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));
}

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#ifndef AUTOEXPANDINGTREEVIEW_H
#define AUTOEXPANDINGTREEVIEW_H
#include <QTreeView>
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