From 5f246cce20c3170caa417b486bb3813d10966abc Mon Sep 17 00:00:00 2001 From: Scharel Clemens Date: Thu, 14 Nov 2019 00:47:38 +0100 Subject: [PATCH] Using QSortFilterProxyModel, so searching and sorting works now --- qml/harbour-nextcloudnotes.qml | 2 +- qml/pages/NotesPage.qml | 19 ++++++++++--- qml/pages/SettingsPage.qml | 2 +- src/note.cpp | 4 +-- src/note.h | 9 +++--- src/notesapi.h | 2 +- src/notesmodel.cpp | 39 ++++++++++---------------- src/notesmodel.h | 16 ++++------- translations/harbour-nextcloudnotes.ts | 38 ++++++++++++------------- 9 files changed, 64 insertions(+), 67 deletions(-) diff --git a/qml/harbour-nextcloudnotes.qml b/qml/harbour-nextcloudnotes.qml index a2053a2..ca2d97e 100644 --- a/qml/harbour-nextcloudnotes.qml +++ b/qml/harbour-nextcloudnotes.qml @@ -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) diff --git a/qml/pages/NotesPage.qml b/qml/pages/NotesPage.qml index 8466f89..c5261d7 100644 --- a/qml/pages/NotesPage.qml +++ b/qml/pages/NotesPage.qml @@ -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") } diff --git a/qml/pages/SettingsPage.qml b/qml/pages/SettingsPage.qml index 19f81d0..60a7175 100644 --- a/qml/pages/SettingsPage.qml +++ b/qml/pages/SettingsPage.qml @@ -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") } diff --git a/src/note.cpp b/src/note.cpp index c82e106..c3d19b1 100644 --- a/src/note.cpp +++ b/src/note.cpp @@ -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 ¬e) const { return m_id == note.id(); } -QString Note::dateString() const { +QString Note::prettyDate() const { QDateTime date; QString dateString; date.setTime_t(m_modified); diff --git a/src/note.h b/src/note.h index dc6cb60..f3cadc4 100644 --- a/src/note.h +++ b/src/note.h @@ -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: diff --git a/src/notesapi.h b/src/notesapi.h index cea0a61..2121e57 100644 --- a/src/notesapi.h +++ b/src/notesapi.h @@ -91,7 +91,7 @@ private: QNetworkRequest m_request; QVector m_replies; NotesModel* mp_model; - NotesProxyModel* mp_modelProxy; // TODO: use! + NotesProxyModel* mp_modelProxy; }; #endif // NOTESAPI_H diff --git a/src/notesmodel.cpp b/src/notesmodel.cpp index 8081144..7f7ed06 100644 --- a/src/notesmodel.cpp +++ b/src/notesmodel.cpp @@ -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 NotesProxyModel::sortingNames() const { - return QHash { - {ModifiedRole, roleNames()[ModifiedRole]}, - {CategoryRole, roleNames()[CategoryRole]}, - {TitleRole, roleNames()[TitleRole]}, - {noSorting, "none"} - }; -} - -QList 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 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(); } diff --git a/src/notesmodel.h b/src/notesmodel.h index 18cd6b2..4d517a0 100644 --- a/src/notesmodel.h +++ b/src/notesmodel.h @@ -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 sortingNames() const; - Q_INVOKABLE QList 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 roleNames() const; diff --git a/translations/harbour-nextcloudnotes.ts b/translations/harbour-nextcloudnotes.ts index e457a5f..6a8ccaf 100644 --- a/translations/harbour-nextcloudnotes.ts +++ b/translations/harbour-nextcloudnotes.ts @@ -262,97 +262,97 @@ NotesPage - + Settings - + Add note - + Reload - + Updating... - + Last update - + never - + Nextcloud Notes - + Modified - + Delete - + Deleting note - + Loading notes... - + No account yet - + Got to the settings to add an account - + No notes yet - + Pull down to add a note - + No result - + Try another query - + An error occurred - + Open the settings to configure your Nextcloud accounts