From 62c9090ec80b63e8d0f5a84a90efe43a7072c8c9 Mon Sep 17 00:00:00 2001 From: APTX Date: Mon, 23 Jul 2012 02:52:10 +0200 Subject: [PATCH] Add filtering to the MyList view --- localmylist/localmylist.pro | 2 +- localmylist/mylistmodel.cpp | 14 +++++++------- localmylist/mylistnode.cpp | 7 ++++++- localmylist/mylistnode.h | 5 ++--- management-gui/mainwindow.cpp | 10 +++++++++- management-gui/mainwindow.h | 5 +++-- management-gui/mainwindow.ui | 3 +++ management-gui/management-gui.pro | 6 ++++-- management-gui/mylistfiltermodel.cpp | 18 ++++++++++++++++++ management-gui/mylistfiltermodel.h | 16 ++++++++++++++++ 10 files changed, 69 insertions(+), 17 deletions(-) create mode 100644 management-gui/mylistfiltermodel.cpp create mode 100644 management-gui/mylistfiltermodel.h diff --git a/localmylist/localmylist.pro b/localmylist/localmylist.pro index a4f827a..636cb64 100644 --- a/localmylist/localmylist.pro +++ b/localmylist/localmylist.pro @@ -26,7 +26,7 @@ SOURCES += \ unknownfilelookuptask.cpp \ renameutils.cpp \ directorywatcher.cpp \ - addrelatedepisodestask.cpp \ + addrelatedepisodestask.cpp HEADERS += \ localmylist_global.h \ diff --git a/localmylist/mylistmodel.cpp b/localmylist/mylistmodel.cpp index d8c7362..d214302 100644 --- a/localmylist/mylistmodel.cpp +++ b/localmylist/mylistmodel.cpp @@ -111,17 +111,17 @@ bool MyListModel::canFetchMore(const QModelIndex &parent) const void MyListModel::fetchMore(const QModelIndex &parent) { - int newrows = 0; + MyListNode *node; if (parent.isValid()) - newrows = static_cast(parent.internalPointer())->fetchMore(); + node = static_cast(parent.internalPointer()); else - newrows = rootItem->fetchMore(); + node = rootItem; - if (!newrows) - return; - - beginInsertRows(parent, rowCount(parent) - newrows, rowCount(parent) - 1); + int nextFetchSize = node->nextFetchSize(); + beginInsertRows(parent, rowCount(parent), rowCount(parent) + nextFetchSize - 1); + int newrows = node->fetchMore(); + Q_ASSERT(newrows == nextFetchSize); endInsertRows(); qDebug() << "added" << newrows << "new rows"; diff --git a/localmylist/mylistnode.cpp b/localmylist/mylistnode.cpp index 9f546f6..c7deae3 100644 --- a/localmylist/mylistnode.cpp +++ b/localmylist/mylistnode.cpp @@ -20,7 +20,6 @@ MyListNode::MyListNode(NodeType type, int id, const QList &data, MyLis return; itemData << "Title" << "Episode / Version" << "Rating / Quality" << "Vote" << "Watched / Renamed"; - } MyListNode::~MyListNode() @@ -86,6 +85,11 @@ int MyListNode::totalRowCount() const return m_totalRowCount; } +int MyListNode::nextFetchSize() const +{ + return qMin(LIMIT, totalRowCount() - childCount()); +} + bool MyListNode::canFetchMore() const { if (m_type != FileLocation && childCount() < totalRowCount()) @@ -117,6 +121,7 @@ int MyListNode::fetchMore() << QObject::tr("%1 of %2").arg(q.value(5).toInt()).arg(q.value(2).toInt()); childItems << new MyListAnimeNode(id, data, this); } + qDebug() << "root" << q.size(); return q.size(); } diff --git a/localmylist/mylistnode.h b/localmylist/mylistnode.h index 423e04c..b7ba096 100644 --- a/localmylist/mylistnode.h +++ b/localmylist/mylistnode.h @@ -19,8 +19,6 @@ public: MyListNode(NodeType type = Root, int m_id = 0, const QList &data = QList(), MyListNode *parent = 0); virtual ~MyListNode(); - void appendChild(MyListNode *child); - MyListNode *child(int row); int childCount() const; int columnCount() const; @@ -29,6 +27,7 @@ public: MyListNode *parent(); int totalRowCount() const; + int nextFetchSize() const; bool canFetchMore() const; virtual int fetchMore(); @@ -50,7 +49,7 @@ protected: QList itemData; MyListNode *parentItem; - static const int LIMIT = 100; + static const int LIMIT = 200; }; class MyListAnimeNode : public MyListNode diff --git a/management-gui/mainwindow.cpp b/management-gui/mainwindow.cpp index 185526b..cf49fe7 100644 --- a/management-gui/mainwindow.cpp +++ b/management-gui/mainwindow.cpp @@ -14,6 +14,7 @@ #include "database.h" #include "settings.h" #include "mylistmodel.h" +#include "mylistfiltermodel.h" #include "unknownfilelookuptask.h" #include "addrelatedepisodestask.h" #include "renamesettingsdialog.h" @@ -46,7 +47,9 @@ MainWindow::MainWindow(QWidget *parent) : MyList::instance()->database()->connect(); myListModel = new MyListModel(this); - ui->myListView->setModel(myListModel); + myListFilterModel = new MyListFilterModel(this); + myListFilterModel->setSourceModel(myListModel); + ui->myListView->setModel(myListFilterModel); ui->myListView->header()->setResizeMode(0, QHeaderView::Stretch); ui->myListView->header()->setStretchLastSection(false); @@ -372,3 +375,8 @@ void MainWindow::dropEvent(QDropEvent *event) } event->acceptProposedAction(); } + +void MainWindow::on_filterInput_textChanged(const QString &filter) +{ + myListFilterModel->setFilterFixedString(filter); +} diff --git a/management-gui/mainwindow.h b/management-gui/mainwindow.h index 83dbd2b..ee8774c 100644 --- a/management-gui/mainwindow.h +++ b/management-gui/mainwindow.h @@ -12,9 +12,9 @@ namespace LocalMyList { class MyListModel; } class QLabel; -class QSortFilterProxyModel; class RenameSettingsDialog; +class MyListFilterModel; class MainWindow : public QMainWindow { @@ -61,6 +61,7 @@ private slots: void on_actionRenameScript_triggered(); void on_actionStartDirectoryWatcher_triggered(); void on_actionAddRelatedEpisodeInfo_triggered(); + void on_filterInput_textChanged(const QString &filter); protected: void dragEnterEvent(QDragEnterEvent *event); @@ -74,7 +75,7 @@ private: QLabel *taskCountLabel; LocalMyList::MyListModel *myListModel; - QSortFilterProxyModel *myListFilterModel; + MyListFilterModel *myListFilterModel; }; #endif // MAINWINDOW_H diff --git a/management-gui/mainwindow.ui b/management-gui/mainwindow.ui index 8246404..94f10ff 100644 --- a/management-gui/mainwindow.ui +++ b/management-gui/mainwindow.ui @@ -15,6 +15,9 @@ + + + diff --git a/management-gui/management-gui.pro b/management-gui/management-gui.pro index 9270456..d0dbf4a 100644 --- a/management-gui/management-gui.pro +++ b/management-gui/management-gui.pro @@ -11,12 +11,14 @@ SOURCES += main.cpp\ mainwindow.cpp \ databaseconnectiondialog.cpp \ mylistview.cpp \ - renamesettingsdialog.cpp + renamesettingsdialog.cpp \ + mylistfiltermodel.cpp HEADERS += mainwindow.h \ databaseconnectiondialog.h \ mylistview.h \ - renamesettingsdialog.h + renamesettingsdialog.h \ + mylistfiltermodel.h FORMS += mainwindow.ui \ databaseconnectiondialog.ui \ diff --git a/management-gui/mylistfiltermodel.cpp b/management-gui/mylistfiltermodel.cpp new file mode 100644 index 0000000..09bdae6 --- /dev/null +++ b/management-gui/mylistfiltermodel.cpp @@ -0,0 +1,18 @@ +#include "mylistfiltermodel.h" + +#include + +MyListFilterModel::MyListFilterModel(QObject *parent) : + QSortFilterProxyModel(parent) +{ + setFilterCaseSensitivity(Qt::CaseInsensitive); +} + +bool MyListFilterModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const +{ + if (source_parent.isValid()) + return true; + + QModelIndex idx = sourceModel()->index(source_row, 0, source_parent); + return sourceModel()->data(idx).toString().contains(filterRegExp()); +} diff --git a/management-gui/mylistfiltermodel.h b/management-gui/mylistfiltermodel.h new file mode 100644 index 0000000..aae9c61 --- /dev/null +++ b/management-gui/mylistfiltermodel.h @@ -0,0 +1,16 @@ +#ifndef MYLISTFILTERMODEL_H +#define MYLISTFILTERMODEL_H + +#include + +class MyListFilterModel : public QSortFilterProxyModel +{ + Q_OBJECT +public: + explicit MyListFilterModel(QObject *parent = 0); + +protected: + bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const; +}; + +#endif // MYLISTFILTERMODEL_H -- 2.52.0