diff --git a/gui/albumspage.cpp b/gui/albumspage.cpp index ee1b2f1c4..0ff16d917 100644 --- a/gui/albumspage.cpp +++ b/gui/albumspage.cpp @@ -137,12 +137,6 @@ void AlbumsPage::itemActivated(const QModelIndex &) void AlbumsPage::searchItems() { proxy.setFilterGenre(0==genreCombo->currentIndex() ? QString() : genreCombo->currentText()); - - if (view->searchText().isEmpty()) { - proxy.setFilterRegExp(QString()); - return; - } - proxy.setFilterRegExp(view->searchText()); } diff --git a/gui/folderpage.cpp b/gui/folderpage.cpp index 6797e9581..c719db99a 100644 --- a/gui/folderpage.cpp +++ b/gui/folderpage.cpp @@ -88,11 +88,6 @@ void FolderPage::clear() void FolderPage::searchItems() { - if (view->searchText().isEmpty()) { - proxy.setFilterRegExp(""); - return; - } - proxy.setFilterRegExp(view->searchText()); } diff --git a/models/albumsproxymodel.cpp b/models/albumsproxymodel.cpp index 90ff3e997..b1e5cf132 100644 --- a/models/albumsproxymodel.cpp +++ b/models/albumsproxymodel.cpp @@ -23,7 +23,8 @@ #include "albumsproxymodel.h" -AlbumsProxyModel::AlbumsProxyModel(QObject *parent) : QSortFilterProxyModel(parent) +AlbumsProxyModel::AlbumsProxyModel(QObject *parent) + : ProxyModel(parent) { setDynamicSortFilter(true); setFilterCaseSensitivity(Qt::CaseInsensitive); @@ -67,6 +68,9 @@ bool AlbumsProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &source if (filterGenre.isEmpty() && filterRegExp().isEmpty()) { return true; } + if (!isChildOfRoot(sourceParent)) { + return true; + } const QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent); AlbumsModel::Item *item = static_cast(index.internalPointer()); diff --git a/models/albumsproxymodel.h b/models/albumsproxymodel.h index be36fbe4b..2a612e95d 100644 --- a/models/albumsproxymodel.h +++ b/models/albumsproxymodel.h @@ -24,10 +24,10 @@ #ifndef ALBUMSPROXYMODEL_H #define ALBUMSPROXYMODEL_H -#include +#include "proxymodel.h" #include "albumsmodel.h" -class AlbumsProxyModel : public QSortFilterProxyModel +class AlbumsProxyModel : public ProxyModel { public: AlbumsProxyModel(QObject *parent = 0); diff --git a/models/dirviewproxymodel.cpp b/models/dirviewproxymodel.cpp index 3a7ec8f38..00de4a9fb 100644 --- a/models/dirviewproxymodel.cpp +++ b/models/dirviewproxymodel.cpp @@ -28,7 +28,8 @@ #include "dirviewitemfile.h" #include "dirviewproxymodel.h" -DirViewProxyModel::DirViewProxyModel(QObject *parent) : QSortFilterProxyModel(parent) +DirViewProxyModel::DirViewProxyModel(QObject *parent) + : ProxyModel(parent) { setDynamicSortFilter(true); setFilterCaseSensitivity(Qt::CaseInsensitive); @@ -75,6 +76,9 @@ bool DirViewProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourc if (filterRegExp().isEmpty()) { return true; } + if (!isChildOfRoot(sourceParent)) { + return true; + } const QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent); DirViewItem *item = static_cast(index.internalPointer()); diff --git a/models/dirviewproxymodel.h b/models/dirviewproxymodel.h index b777017e1..fcdccef88 100644 --- a/models/dirviewproxymodel.h +++ b/models/dirviewproxymodel.h @@ -26,11 +26,11 @@ #ifndef DIRVIEWPROXYMODEL_H #define DIRVIEWPROXYMODEL_H -#include +#include "proxymodel.h" class DirViewItem; -class DirViewProxyModel : public QSortFilterProxyModel +class DirViewProxyModel : public ProxyModel { Q_OBJECT diff --git a/models/musiclibraryproxymodel.cpp b/models/musiclibraryproxymodel.cpp index 3de781095..dc7bb447e 100644 --- a/models/musiclibraryproxymodel.cpp +++ b/models/musiclibraryproxymodel.cpp @@ -31,7 +31,7 @@ #include "musiclibraryproxymodel.h" MusicLibraryProxyModel::MusicLibraryProxyModel(QObject *parent) - : QSortFilterProxyModel(parent), + : ProxyModel(parent), _filterField(3) { setDynamicSortFilter(true); @@ -139,6 +139,9 @@ bool MusicLibraryProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex & if (_filterGenre.isEmpty() && filterRegExp().isEmpty()) { return true; } + if (!isChildOfRoot(sourceParent)) { + return true; + } const QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent); const MusicLibraryItem * const item = static_cast(index.internalPointer()); diff --git a/models/musiclibraryproxymodel.h b/models/musiclibraryproxymodel.h index 8ae8504ef..d4776062d 100644 --- a/models/musiclibraryproxymodel.h +++ b/models/musiclibraryproxymodel.h @@ -27,11 +27,11 @@ #ifndef MUSIC_LIBRARY_SORT_FILTER_MODEL_H #define MUSIC_LIBRARY_SORT_FILTER_MODEL_H -#include +#include "proxymodel.h" class MusicLibraryItem; -class MusicLibraryProxyModel : public QSortFilterProxyModel +class MusicLibraryProxyModel : public ProxyModel { Q_OBJECT @@ -44,12 +44,13 @@ public: void setFilterGenre(const QString &genre); private: - QString _filterGenre; - int _filterField; - bool filterAcceptsArtist(const MusicLibraryItem * const item) const; bool filterAcceptsAlbum(const MusicLibraryItem * const item) const; bool filterAcceptsSong(const MusicLibraryItem * const item) const; + +private: + QString _filterGenre; + int _filterField; }; #endif diff --git a/models/playlistsproxymodel.cpp b/models/playlistsproxymodel.cpp index 2c333eebf..88a814cbc 100644 --- a/models/playlistsproxymodel.cpp +++ b/models/playlistsproxymodel.cpp @@ -27,7 +27,8 @@ #include "playlistsproxymodel.h" #include "playlistsmodel.h" -PlaylistsProxyModel::PlaylistsProxyModel(QObject *parent) : QSortFilterProxyModel(parent) +PlaylistsProxyModel::PlaylistsProxyModel(QObject *parent) + : ProxyModel(parent) { setDynamicSortFilter(true); setFilterCaseSensitivity(Qt::CaseInsensitive); @@ -40,6 +41,9 @@ bool PlaylistsProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sou if (filterRegExp().isEmpty()) { return true; } + if (!isChildOfRoot(sourceParent)) { + return true; + } const QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent); PlaylistsModel::Item *item = static_cast(index.internalPointer()); diff --git a/models/playlistsproxymodel.h b/models/playlistsproxymodel.h index 7b55ad867..2da2c19b8 100644 --- a/models/playlistsproxymodel.h +++ b/models/playlistsproxymodel.h @@ -26,9 +26,9 @@ #ifndef PLAYLISTSPROXYMODEL_H #define PLAYLISTSPROXYMODEL_H -#include +#include "proxymodel.h" -class PlaylistsProxyModel : public QSortFilterProxyModel +class PlaylistsProxyModel : public ProxyModel { Q_OBJECT diff --git a/models/proxymodel.h b/models/proxymodel.h new file mode 100644 index 000000000..8ad3eb714 --- /dev/null +++ b/models/proxymodel.h @@ -0,0 +1,62 @@ +/* + * Cantata + * + * Copyright (c) 2011 Craig Drummond + * + * ---- + * + * This program 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 2 of the License, or + * (at your option) any later version. + * + * This program 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 this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifndef PROXYMODEL_H +#define PROXYMODEL_H + +#include +#include + +class ProxyModel : public QSortFilterProxyModel +{ +public: + ProxyModel(QObject *parent) + : QSortFilterProxyModel(parent) { + } + virtual ~ProxyModel() { + } + + void setRootIndex(const QModelIndex &idx) { + rootIndex=idx.isValid() ? mapToSource(idx) : idx; + } + + bool isChildOfRoot(const QModelIndex &idx) const { + if (!rootIndex.isValid()) { + return true; + } + + QModelIndex i=idx; + while(i.isValid()) { + if (i==rootIndex) { + return true; + } + i=i.parent(); + } + return false; + } + +private: + QModelIndex rootIndex; +}; + +#endif diff --git a/models/streamsproxymodel.cpp b/models/streamsproxymodel.cpp index cd775f655..5c48fab27 100644 --- a/models/streamsproxymodel.cpp +++ b/models/streamsproxymodel.cpp @@ -23,7 +23,8 @@ #include "streamsproxymodel.h" #include "streamsmodel.h" -StreamsProxyModel::StreamsProxyModel(QObject *parent) : QSortFilterProxyModel(parent) +StreamsProxyModel::StreamsProxyModel(QObject *parent) + : ProxyModel(parent) { setDynamicSortFilter(true); setFilterCaseSensitivity(Qt::CaseInsensitive); @@ -31,3 +32,14 @@ StreamsProxyModel::StreamsProxyModel(QObject *parent) : QSortFilterProxyModel(pa setSortLocaleAware(true); sort(0); } + +bool StreamsProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const +{ + if (filterRegExp().isEmpty()) { + return true; + } + if (!isChildOfRoot(sourceParent)) { + return true; + } + return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent); +} diff --git a/models/streamsproxymodel.h b/models/streamsproxymodel.h index 865f7716f..4e545a462 100644 --- a/models/streamsproxymodel.h +++ b/models/streamsproxymodel.h @@ -24,12 +24,13 @@ #ifndef STREAMSPROXYMODEL_H #define STREAMSPROXYMODEL_H -#include +#include "proxymodel.h" -class StreamsProxyModel : public QSortFilterProxyModel +class StreamsProxyModel : public ProxyModel { public: StreamsProxyModel(QObject *parent = 0); + bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const; }; #endif diff --git a/widgets/itemview.cpp b/widgets/itemview.cpp index 41d3f8383..31880c246 100644 --- a/widgets/itemview.cpp +++ b/widgets/itemview.cpp @@ -24,6 +24,7 @@ #include "itemview.h" #include "mainwindow.h" #include "covers.h" +#include "proxymodel.h" #include #include #include @@ -393,11 +394,13 @@ void ItemView::setMode(Mode m) if (Mode_Tree==mode) { treeView->setModel(itemModel); listView->setModel(0); + itemModel->setRootIndex(QModelIndex()); } else { treeView->setModel(0); listView->setModel(itemModel); setLevel(0); listView->setRootIndex(QModelIndex()); + itemModel->setRootIndex(QModelIndex()); if (Mode_IconTop!=mode) { listView->setGridSize(listGridSize); listView->setViewMode(QListView::ListMode); @@ -471,7 +474,7 @@ QAbstractItemView * ItemView::view() const return Mode_Tree==mode ? (QAbstractItemView *)treeView : (QAbstractItemView *)listView; } -void ItemView::setModel(QAbstractItemModel *m) +void ItemView::setModel(ProxyModel *m) { itemModel=m; view()->setModel(m); @@ -563,6 +566,7 @@ void ItemView::backActivated() return; } setLevel(currentLevel-1); + itemModel->setRootIndex(listView->rootIndex().parent()); listView->setRootIndex(listView->rootIndex().parent()); listView->scrollTo(prevTopIndex, QAbstractItemView::PositionAtTop); } @@ -624,6 +628,7 @@ void ItemView::itemActivated(const QModelIndex &index) listSearch->setPlaceholderText(tr("Search %1...").arg(index.data(Qt::DisplayRole).toString())); #endif listView->setRootIndex(index); + itemModel->setRootIndex(index); listView->scrollToTop(); } } diff --git a/widgets/itemview.h b/widgets/itemview.h index a50d58a74..f20f1a0ef 100644 --- a/widgets/itemview.h +++ b/widgets/itemview.h @@ -31,6 +31,8 @@ class QAction; class KPixmapSequenceOverlayPainter; #endif +class ProxyModel; + class ItemView : public QWidget, public Ui::ItemView { Q_OBJECT @@ -60,7 +62,7 @@ public: Mode viewMode() const { return mode; } void setLevel(int level); QAbstractItemView * view() const; - void setModel(QAbstractItemModel *m); + void setModel(ProxyModel *m); void clearSelection() { view()->selectionModel()->clearSelection(); } QModelIndexList selectedIndexes() const; QString searchText() const; @@ -92,7 +94,7 @@ private: QAction * getAction(const QModelIndex &index); private: - QAbstractItemModel *itemModel; + ProxyModel *itemModel; QAction *backAction; QAction *act1; QAction *act2;