]> Some of my projects - localmylist.git/commitdiff
Add filtering to the MyList view
authorAPTX <marek321@gmail.com>
Mon, 23 Jul 2012 00:52:10 +0000 (02:52 +0200)
committerAPTX <marek321@gmail.com>
Mon, 23 Jul 2012 00:52:10 +0000 (02:52 +0200)
localmylist/localmylist.pro
localmylist/mylistmodel.cpp
localmylist/mylistnode.cpp
localmylist/mylistnode.h
management-gui/mainwindow.cpp
management-gui/mainwindow.h
management-gui/mainwindow.ui
management-gui/management-gui.pro
management-gui/mylistfiltermodel.cpp [new file with mode: 0644]
management-gui/mylistfiltermodel.h [new file with mode: 0644]

index a4f827a1c1bec69bba2abc71d3de82d32de0ea5d..636cb64615c4d15c1ef8db50959cb1d71932d31d 100644 (file)
@@ -26,7 +26,7 @@ SOURCES += \
        unknownfilelookuptask.cpp \
        renameutils.cpp \
        directorywatcher.cpp \
-       addrelatedepisodestask.cpp \
+       addrelatedepisodestask.cpp
 
 HEADERS += \
        localmylist_global.h \
index d8c7362fc74ac120e15ffa0b68c545f79ffe75d5..d21430299792cd2ac1b21b6ad851a533f679eec1 100644 (file)
@@ -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<MyListNode *>(parent.internalPointer())->fetchMore();
+               node = static_cast<MyListNode *>(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";
index 9f546f67c0050608cf3803fdf362c77f18c3f876..c7deae359c5d6287dc4619fa0a04f3cefbbec2a9 100644 (file)
@@ -20,7 +20,6 @@ MyListNode::MyListNode(NodeType type, int id, const QList<QVariant> &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();
 }
 
index 423e04c4194a150b260601ae1dfa39d111832d5a..b7ba096969e83e55c686317dd86c692a13384f3a 100644 (file)
@@ -19,8 +19,6 @@ public:
        MyListNode(NodeType type = Root, int m_id = 0, const QList<QVariant> &data = QList<QVariant>(), 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<QVariant> itemData;
        MyListNode *parentItem;
 
-       static const int LIMIT = 100;
+       static const int LIMIT = 200;
 };
 
 class MyListAnimeNode : public MyListNode
index 185526b9a5e0db536fbd9641c978eb973f60b34f..cf49fe73288a6ad09206c2a672972110ba4b3aab 100644 (file)
@@ -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);
+}
index 83dbd2b378c30e12efdf4a83bfc9a4e9421b6ced..ee8774cdd1ce022cdb7fc46290dd8cb7ca6c3ca0 100644 (file)
@@ -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
index 82464043559de7f59ec618cde3df3275b8b5f83a..94f10ff397d4359637aa0e3cfeeb14c44c43691a 100644 (file)
@@ -15,6 +15,9 @@
   </property>
   <widget class="QWidget" name="centralWidget">
    <layout class="QVBoxLayout" name="verticalLayout">
+    <item>
+     <widget class="QLineEdit" name="filterInput"/>
+    </item>
     <item>
      <widget class="MyListView" name="myListView"/>
     </item>
index 92704563d08eae7b6bb34057059a9b8832acf70b..d0dbf4a3880292581e2d04a763853a424ee6787f 100644 (file)
@@ -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 (file)
index 0000000..09bdae6
--- /dev/null
@@ -0,0 +1,18 @@
+#include "mylistfiltermodel.h"
+
+#include <QDebug>
+
+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 (file)
index 0000000..aae9c61
--- /dev/null
@@ -0,0 +1,16 @@
+#ifndef MYLISTFILTERMODEL_H
+#define MYLISTFILTERMODEL_H
+
+#include <QSortFilterProxyModel>
+
+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