Skip to content

Commit

Permalink
filter library
Browse files Browse the repository at this point in the history
  • Loading branch information
Gibbz committed Jul 13, 2020
1 parent 2fd7d25 commit c6a0344
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 28 deletions.
2 changes: 2 additions & 0 deletions audiobook.pro
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ INCLUDEPATH += \
HEADERS += \
src/book.h \
src/library.h \
src/libraryfilterproxy.h \
src/librarymodel.h \
src/player.h \
src/settings.h \
Expand Down Expand Up @@ -189,6 +190,7 @@ HEADERS += \
SOURCES += \
src/book.cpp \
src/library.cpp \
src/libraryfilterproxy.cpp \
src/librarymodel.cpp \
src/main.cpp \
src/player.cpp \
Expand Down
31 changes: 31 additions & 0 deletions src/libraryfilterproxy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "libraryfilterproxy.h"
#include "librarymodel.h"
#include <QDebug>


LibraryFilterProxy::LibraryFilterProxy(QObject *parent)
: QSortFilterProxyModel(parent)
{

}

bool LibraryFilterProxy::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
// Here we can specify whatever filter logic we need to and can check multiple Roles. Below
// we are simply checking the Value role to see if that value stored there is even, if so include the
// data entry in this proxy model.
if ( this->sourceModel() == nullptr )
return false;

QRegExp reg = filterRegExp();

QModelIndex index0 = sourceModel()->index(sourceRow, 0, sourceParent);
if (!index0.isValid())
return false;

QVariant value_role = index0.data( LibraryModel::TitleRole );
if (value_role.toString().toLower().contains(reg.pattern().toLower()))
return true;

return false;
}
14 changes: 14 additions & 0 deletions src/libraryfilterproxy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef LIBRARYMODELPROXY_H
#define LIBRARYMODELPROXY_H

#include <QObject>
#include <QSortFilterProxyModel>

class LibraryFilterProxy : public QSortFilterProxyModel
{
public:
LibraryFilterProxy(QObject *parent = 0);
bool filterAcceptsRow( int source_row, const QModelIndex& source_parent ) const override;
};

#endif // LIBRARYMODELPROXY_H
17 changes: 3 additions & 14 deletions src/librarymodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,14 @@ QVariant LibraryModel::data(const QModelIndex &index, int role) const
const Book &book = Library::instance()->getLibraryItems().at(index.row());

switch (role) {
case LibraryIndexRole:
return QVariant(index.row());
case TitleRole:
return QVariant(book.title);
case ChaptersRole: {
case ChaptersRole:
return QVariant(book.chapter_titles.count());
}
case DurationRole:
return QVariant(Util::getDisplayTime(book.time));
}
return QVariant();
}



QHash<int, QByteArray> LibraryModel::roleNames() const
{
// map/bind qml to cpp
QHash<int, QByteArray> names;
names[TitleRole] = "title";
names[ChaptersRole] = "chapters";
names[DurationRole] = "duration";
return names;
}
14 changes: 12 additions & 2 deletions src/librarymodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,28 @@ class LibraryModel : public QAbstractListModel

// list of accesable properties/roles/map/bindings
enum {
LibraryIndexRole,
TitleRole,
ChaptersRole,
DurationRole,
};

int rowCount(const QModelIndex &parent = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
void UpdateData();

// hashmap to store properties/roles
virtual QHash<int, QByteArray> roleNames() const override;
// map/bind qml to cpp
virtual QHash<int, QByteArray> roleNames() const override {
QHash<int, QByteArray> names;
names[LibraryIndexRole] = "libraryIndex";
names[TitleRole] = "title";
names[ChaptersRole] = "chapters";
names[DurationRole] = "duration";
return names;
}


void UpdateData();
};

#endif // LIBRARYMODEL_H
12 changes: 10 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
#include <QDebug>
#include <QQmlDebuggingEnabler>
#include <QIcon>
#include <QQmlContext>

#include "settings.h"
#include "library.h"
#include "librarymodel.h"
#include "libraryfilterproxy.h"
#include "player.h"

#ifdef Q_OS_ANDROID
Expand Down Expand Up @@ -38,13 +40,19 @@ int main(int argc, char *argv[])
Library* library = Library::instance();
Player* player = Player::instance();

LibraryModel lm;
LibraryModel *library_model = new LibraryModel();
LibraryFilterProxy *filter_proxy = new LibraryFilterProxy();
filter_proxy->setSourceModel(library_model);

// register types
qmlRegisterSingletonType<Settings>("QSettings", 1, 0, "QSettings", &Settings::qmlInstance);
qmlRegisterSingletonType<Library>("Library", 1, 0, "Library", &Library::qmlInstance);
qmlRegisterSingletonType<Player>("Player", 1, 0, "Player", &Player::qmlInstance);

qmlRegisterType<LibraryModel>("LibraryModel", 1, 0, "LibraryModel");

//qmlRegisterType<LibraryModel>("LibraryModel", 1, 0, "LibraryModel");
engine.rootContext()->setContextProperty("LibraryModel", &lm);
engine.rootContext()->setContextProperty("LibraryFilterProxy", filter_proxy);

// add imports
engine.addImportPath(".");
Expand Down
17 changes: 7 additions & 10 deletions src/qml/LibraryPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import QtQuick 2.11
import QtQuick.Controls 2.4
import QtQuick.Layouts 1.2
import Library 1.0
import LibraryModel 1.0
import Player 1.0
//import LibraryModel 1.0
import "Style"

Page {
Expand All @@ -26,12 +26,8 @@ Page {
focus: true

onTextChanged: {
if (text.length < 2)
return;

library_list.currentIndex = -1;
Library.filter(text);

//library_list.currentIndex = -1;
LibraryFilterProxy.setFilterRegExp(text);
}
}

Expand All @@ -49,14 +45,14 @@ Page {
currentIndex: -1 // no selected default
ScrollBar.vertical: ScrollBar {}

model: LibraryModel {}
model: LibraryFilterProxy

delegate: Component {
id: search_delegate

Rectangle {
id: background
color: ListView.isCurrentItem? Style.library.color_highlight : Style.library.color_background
color: ListView.isCurrentItem? Style.library.color_highlight : Style.library.color_background // TODO: model.isCurrentItem for active item
radius: Style.library.radius_background
implicitHeight: row_layout.height
implicitWidth: parent.width
Expand Down Expand Up @@ -95,7 +91,8 @@ Page {
anchors.fill: parent
onClicked: {
library_list.currentIndex = index;
Library.activeItem = index;
console.log(model.title)
Library.activeItem = model.libraryIndex;
}
}
}
Expand Down

0 comments on commit c6a0344

Please sign in to comment.