Using QSortFilterProxyModel, so searching and sorting works now

This commit is contained in:
Scharel Clemens 2019-11-14 00:47:38 +01:00
parent 2ff9b06c99
commit 5f246cce20
9 changed files with 64 additions and 67 deletions

View file

@ -40,7 +40,7 @@ ApplicationWindow
property int autoSyncInterval: value("autoSyncInterval", 0, Number)
property int previewLineCount: value("previewLineCount", 4, Number)
property bool favoritesOnTop: value("favoritesOnTop", true, Boolean)
property string sortBy: value("sortBy", "modified", String)
property string sortBy: value("sortBy", "prettyDate", String)
property bool showSeparator: value("showSeparator", false, Boolean)
property bool useMonoFont: value("useMonoFont", false, Boolean)
property bool useCapitalX: value("useCapitalX", false, Boolean)

View file

@ -9,8 +9,19 @@ Page {
property NotesModel notesModel: notesApi.model()
Connections {
target: appSettings
onSortByChanged: notesModel.sortRole = notesModel.sortingRole(appSettings.sortBy)
onFavoritesOnTopChanged: notesModel.favoritesOnTop = appSettings.favoritesOnTop
onSortByChanged: {
if (appSettings.sortBy == "none")
notesModel.invalidate()
else
notesModel.sortRole = notesModel.roleFromName(appSettings.sortBy)
}
onFavoritesOnTopChanged: {
notesModel.favoritesOnTop = appSettings.favoritesOnTop
}
}
Component.onCompleted: {
notesModel.favoritesOnTop = appSettings.favoritesOnTop
notesModel.sortRole = notesModel.roleFromName(appSettings.sortBy)
}
onStatusChanged: {
@ -217,7 +228,7 @@ Page {
section.criteria: appSettings.sortBy === "title" ? ViewSection.FirstCharacter : ViewSection.FullString
section.labelPositioning: appSettings.sortBy === "title" ? ViewSection.CurrentLabelAtStart | ViewSection.NextLabelAtEnd : ViewSection.InlineLabels
section.delegate: SectionHeader {
text: appSettings.sortBy === "modified" ? new Date(section) : section
text: section
}
BusyIndicator {
@ -254,7 +265,7 @@ Page {
ViewPlaceholder {
id: noSearchPlaceholder
enabled: notesList.count === 0 && searchField.text !== ""
enabled: notesList.count === 0 && notesModel.filterRegExp !== ""
text: qsTr("No result")
hintText: qsTr("Try another query")
}

View file

@ -145,7 +145,7 @@ Page {
ComboBox {
id: sortByComboBox
property var criteria: [
{ role: "modified", text: qsTr("Last edited") },
{ role: "prettyDate", text: qsTr("Last edited") },
{ role: "category", text: qsTr("Category") },
{ role: "title", text: qsTr("Title alphabetically") },
{ role: "none", text: qsTr("No sorting") }

View file

@ -13,7 +13,7 @@ Note::Note(QObject *parent) : QObject(parent) {
connect(this, SIGNAL(etagChanged(QString)), this, SIGNAL(noteChanged()));
connect(this, SIGNAL(errorChanged(bool)), this, SIGNAL(noteChanged()));
connect(this, SIGNAL(errorMessageChanged(QString)), this, SIGNAL(noteChanged()));
connect(this, SIGNAL(dateStringChanged(QString)), this, SIGNAL(noteChanged()));
connect(this, SIGNAL(prettyDateChanged(QString)), this, SIGNAL(noteChanged()));
}
Note::Note(const Note& note, QObject *parent) : QObject(parent) {
@ -57,7 +57,7 @@ bool Note::same(const Note &note) const {
return m_id == note.id();
}
QString Note::dateString() const {
QString Note::prettyDate() const {
QDateTime date;
QString dateString;
date.setTime_t(m_modified);

View file

@ -23,7 +23,7 @@ public:
Q_PROPERTY(uint modified READ modified WRITE setModified NOTIFY modifiedChanged)
uint modified() const { return m_modified; }
void setModified(uint modified) { if (modified != m_modified) { m_modified = modified; emit modifiedChanged(modified); emit dateStringChanged(dateString()); } }
void setModified(uint modified) { if (modified != m_modified) { m_modified = modified; emit modifiedChanged(modified); emit prettyDateChanged(prettyDate()); } }
Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged)
QString title() const { return m_title; }
@ -53,8 +53,9 @@ public:
QString errorMessage() const { return m_errorMessage; }
void setErrorMessage(QString errorMessage) { if (errorMessage != m_errorMessage) { m_errorMessage = errorMessage; emit errorMessageChanged(errorMessage); } }
Q_PROPERTY(QString dateString READ dateString NOTIFY dateStringChanged)
QString dateString() const;
Q_PROPERTY(QString prettyDate READ prettyDate NOTIFY prettyDateChanged)
QString prettyDate() const;
static Note fromjson(const QJsonObject& jobj);
signals:
@ -67,7 +68,7 @@ signals:
void etagChanged(QString etag);
void errorChanged(bool error);
void errorMessageChanged(QString errorMessage);
void dateStringChanged(QString date);
void prettyDateChanged(QString date);
void noteChanged();
private:

View file

@ -91,7 +91,7 @@ private:
QNetworkRequest m_request;
QVector<QNetworkReply*> m_replies;
NotesModel* mp_model;
NotesProxyModel* mp_modelProxy; // TODO: use!
NotesProxyModel* mp_modelProxy;
};
#endif // NOTESAPI_H

View file

@ -8,7 +8,7 @@
NotesProxyModel::NotesProxyModel(QObject *parent) {
m_favoritesOnTop = true;
connect(this, SIGNAL(favoritesOnTopChanged(bool)), this, SLOT(resort()));
//connect(this, SIGNAL(favoritesOnTopChanged(bool)), this, SLOT(resort()));
}
NotesProxyModel::~NotesProxyModel() {
@ -21,38 +21,26 @@ void NotesProxyModel::setFavoritesOnTop(bool favoritesOnTop) {
m_favoritesOnTop = favoritesOnTop;
emit favoritesOnTopChanged(m_favoritesOnTop);
}
sort();
}
QHash<int, QByteArray> NotesProxyModel::sortingNames() const {
return QHash<int, QByteArray> {
{ModifiedRole, roleNames()[ModifiedRole]},
{CategoryRole, roleNames()[CategoryRole]},
{TitleRole, roleNames()[TitleRole]},
{noSorting, "none"}
};
}
QList<int> NotesProxyModel::sortingRoles() const {
return sortingNames().keys();
}
int NotesProxyModel::sortingRole(const QString &name) const {
return sortingNames().key(name.toLocal8Bit());
int NotesProxyModel::roleFromName(const QString &name) const {
return roleNames().key(name.toLocal8Bit());
}
bool NotesProxyModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const {
QAbstractItemModel* source = sourceModel();
if (m_favoritesOnTop && source->data(source_left, NotesModel::FavoriteRole) != source->data(source_right, NotesModel::FavoriteRole))
if (m_favoritesOnTop && source->data(source_left, NotesModel::FavoriteRole).toBool() != source->data(source_right, NotesModel::FavoriteRole).toBool())
return source->data(source_left, NotesModel::FavoriteRole).toBool();
else if (sortRole() == NotesModel::PrettyDateRole)
return source->data(source_left, NotesModel::ModifiedRole).toInt() >= source->data(source_right, NotesModel::ModifiedRole).toInt();
else
return source->data(source_left, sortRole()) < source->data(source_right, sortRole());
return QSortFilterProxyModel::lessThan(source_left, source_right);
}
void NotesProxyModel::resort() {
if (sortRole() == ModifiedRole)
sort(Qt::DescendingOrder);
else
sort(Qt::AscendingOrder);
void NotesProxyModel::sort() {
invalidate();
QSortFilterProxyModel::sort(0);
}
NotesModel::NotesModel(QObject *parent) {
@ -185,7 +173,9 @@ QHash<int, QByteArray> NotesModel::roleNames() const {
{NotesModel::FavoriteRole, "favorite"},
{NotesModel::EtagRole, "etag"},
{NotesModel::ErrorRole, "error"},
{NotesModel::ErrorMessageRole, "errorMessage"}
{NotesModel::ErrorMessageRole, "errorMessage"},
{NotesModel::PrettyDateRole, "prettyDate"},
{NotesModel::NoneRole, "none"}
};
}
@ -213,6 +203,7 @@ QVariant NotesModel::data(const QModelIndex &index, int role) const {
else if (role == EtagRole) return m_notes[index.row()].etag();
else if (role == ErrorRole) return m_notes[index.row()].error();
else if (role == ErrorMessageRole) return m_notes[index.row()].errorMessage();
else if (role == PrettyDateRole) return m_notes[index.row()].prettyDate();
return QVariant();
}

View file

@ -16,21 +16,13 @@ public:
bool favoritesOnTop() const { return m_favoritesOnTop; }
void setFavoritesOnTop(bool favoritesOnTop);
enum SortingCriteria {
ModifiedRole,
CategoryRole,
TitleRole,
noSorting = Qt::UserRole + 9
};
QHash<int, QByteArray> sortingNames() const;
Q_INVOKABLE QList<int> sortingRoles() const;
Q_INVOKABLE int sortingRole(const QString &name) const;
Q_INVOKABLE void sort();
Q_INVOKABLE int roleFromName(const QString &name) const;
protected:
virtual bool lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const;
private slots:
void resort();
signals:
void favoritesOnTopChanged(bool favoritesOnTop);
@ -57,7 +49,9 @@ public:
FavoriteRole = Qt::UserRole + 5,
EtagRole = Qt::UserRole + 6,
ErrorRole = Qt::UserRole + 7,
ErrorMessageRole = Qt::UserRole + 8
ErrorMessageRole = Qt::UserRole + 8,
PrettyDateRole = Qt::UserRole + 9,
NoneRole = Qt::UserRole + 10
};
QHash<int, QByteArray> roleNames() const;

View file

@ -262,97 +262,97 @@
<context>
<name>NotesPage</name>
<message>
<location filename="../qml/pages/NotesPage.qml" line="36"/>
<location filename="../qml/pages/NotesPage.qml" line="47"/>
<source>Settings</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="40"/>
<location filename="../qml/pages/NotesPage.qml" line="51"/>
<source>Add note</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="45"/>
<location filename="../qml/pages/NotesPage.qml" line="56"/>
<source>Reload</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="45"/>
<location filename="../qml/pages/NotesPage.qml" line="56"/>
<source>Updating...</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="51"/>
<location filename="../qml/pages/NotesPage.qml" line="62"/>
<source>Last update</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="54"/>
<location filename="../qml/pages/NotesPage.qml" line="65"/>
<source>never</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="64"/>
<location filename="../qml/pages/NotesPage.qml" line="75"/>
<source>Nextcloud Notes</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="203"/>
<location filename="../qml/pages/NotesPage.qml" line="214"/>
<source>Modified</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="206"/>
<location filename="../qml/pages/NotesPage.qml" line="217"/>
<source>Delete</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="208"/>
<location filename="../qml/pages/NotesPage.qml" line="219"/>
<source>Deleting note</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="238"/>
<location filename="../qml/pages/NotesPage.qml" line="249"/>
<source>Loading notes...</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="244"/>
<location filename="../qml/pages/NotesPage.qml" line="255"/>
<source>No account yet</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="245"/>
<location filename="../qml/pages/NotesPage.qml" line="256"/>
<source>Got to the settings to add an account</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="251"/>
<location filename="../qml/pages/NotesPage.qml" line="262"/>
<source>No notes yet</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="252"/>
<location filename="../qml/pages/NotesPage.qml" line="263"/>
<source>Pull down to add a note</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="258"/>
<location filename="../qml/pages/NotesPage.qml" line="269"/>
<source>No result</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="259"/>
<location filename="../qml/pages/NotesPage.qml" line="270"/>
<source>Try another query</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="265"/>
<location filename="../qml/pages/NotesPage.qml" line="276"/>
<source>An error occurred</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/NotesPage.qml" line="276"/>
<location filename="../qml/pages/NotesPage.qml" line="287"/>
<source>Open the settings to configure your Nextcloud accounts</source>
<translation type="unfinished"></translation>
</message>