diff --git a/smsapp/CMakeLists.txt b/smsapp/CMakeLists.txt index ba4a0c6f5..eb159a7a7 100644 --- a/smsapp/CMakeLists.txt +++ b/smsapp/CMakeLists.txt @@ -33,6 +33,7 @@ add_executable(kdeconnect-sms main.cpp conversationlistmodel.cpp conversationmodel.cpp + conversationssortfilterproxymodel.cpp ${KCSMS_SRCS}) target_include_directories(kdeconnect-sms PUBLIC ${CMAKE_BINARY_DIR}) diff --git a/smsapp/conversationlistmodel.cpp b/smsapp/conversationlistmodel.cpp index 807368b9d..254a574d5 100644 --- a/smsapp/conversationlistmodel.cpp +++ b/smsapp/conversationlistmodel.cpp @@ -36,41 +36,6 @@ Q_LOGGING_CATEGORY(KDECONNECT_SMS_CONVERSATIONS_LIST_MODEL, "kdeconnect.sms.conv #define INVALID_THREAD_ID -1 #define INVALID_DATE -1 -OurSortFilterProxyModel::OurSortFilterProxyModel() -{ - setFilterRole(ConversationListModel::DateRole); -} - -OurSortFilterProxyModel::~OurSortFilterProxyModel(){} - -void OurSortFilterProxyModel::setOurFilterRole(int role) -{ - setFilterRole(role); -} - -bool OurSortFilterProxyModel::lessThan(const QModelIndex& leftIndex, const QModelIndex& rightIndex) const -{ - QVariant leftDataTimeStamp = sourceModel()->data(leftIndex, ConversationListModel::DateRole); - QVariant rightDataTimeStamp = sourceModel()->data(rightIndex, ConversationListModel::DateRole); - - if (leftDataTimeStamp == rightDataTimeStamp) { - QVariant leftDataName = sourceModel()->data(leftIndex, Qt::DisplayRole); - QVariant rightDataName = sourceModel()->data(rightIndex, Qt::DisplayRole); - return leftDataName.toString().toLower() > rightDataName.toString().toLower(); - } - return leftDataTimeStamp < rightDataTimeStamp; -} - -bool OurSortFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const -{ - QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent); - - if (filterRole() == Qt::DisplayRole) { - return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent); - } - return sourceModel()->data(index, ConversationListModel::DateRole) != INVALID_THREAD_ID; -} - ConversationListModel::ConversationListModel(QObject* parent) : QStandardItemModel(parent) , m_conversationsInterface(nullptr) diff --git a/smsapp/conversationlistmodel.h b/smsapp/conversationlistmodel.h index 6daac5ae7..9575174a5 100644 --- a/smsapp/conversationlistmodel.h +++ b/smsapp/conversationlistmodel.h @@ -24,53 +24,12 @@ #include #include -#include #include "interfaces/conversationmessage.h" #include "interfaces/dbusinterfaces.h" Q_DECLARE_LOGGING_CATEGORY(KDECONNECT_SMS_CONVERSATIONS_LIST_MODEL) - -class OurSortFilterProxyModel : public QSortFilterProxyModel, public QQmlParserStatus -{ - Q_OBJECT - Q_INTERFACES(QQmlParserStatus) - Q_PROPERTY(Qt::SortOrder sortOrder READ sortOrder WRITE setSortOrder) -public: - - Qt::SortOrder sortOrder() const { return m_sortOrder; } - void setSortOrder(Qt::SortOrder sortOrder) { - if (m_sortOrder != sortOrder) { - m_sortOrder = sortOrder; - sortNow(); - } - } - void classBegin() override {} - void componentComplete() override { - m_completed = true; - sortNow(); - } - - Q_INVOKABLE void setOurFilterRole(int role); - - OurSortFilterProxyModel(); - ~OurSortFilterProxyModel(); - -protected: - bool lessThan(const QModelIndex& leftIndex, const QModelIndex& rightIndex) const override; - bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override; - -private: - void sortNow() { - if (m_completed && dynamicSortFilter()) - sort(0, m_sortOrder); - } - - bool m_completed = false; - Qt::SortOrder m_sortOrder = Qt::AscendingOrder; -}; - class ConversationListModel : public QStandardItemModel { diff --git a/smsapp/conversationssortfilterproxymodel.cpp b/smsapp/conversationssortfilterproxymodel.cpp new file mode 100644 index 000000000..4aea66c31 --- /dev/null +++ b/smsapp/conversationssortfilterproxymodel.cpp @@ -0,0 +1,65 @@ +/** + * Copyright (C) 2018 Aleix Pol Gonzalez + * Copyright (C) 2018 Simon Redman + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License or (at your option) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "conversationssortfilterproxymodel.h" +#include "conversationlistmodel.h" + +#include +#include + +Q_LOGGING_CATEGORY(KDECONNECT_SMS_CONVERSATIONS_SORT_FILTER_PROXY_MODEL, "kdeconnect.sms.conversations_sort_filter_proxy") + +#define INVALID_THREAD_ID -1 + +ConversationsSortFilterProxyModel::ConversationsSortFilterProxyModel() +{ + setFilterRole(ConversationListModel::DateRole); +} + +ConversationsSortFilterProxyModel::~ConversationsSortFilterProxyModel(){} + +void ConversationsSortFilterProxyModel::setOurFilterRole(int role) +{ + setFilterRole(role); +} + +bool ConversationsSortFilterProxyModel::lessThan(const QModelIndex& leftIndex, const QModelIndex& rightIndex) const +{ + QVariant leftDataTimeStamp = sourceModel()->data(leftIndex, ConversationListModel::DateRole); + QVariant rightDataTimeStamp = sourceModel()->data(rightIndex, ConversationListModel::DateRole); + + if (leftDataTimeStamp == rightDataTimeStamp) { + QVariant leftDataName = sourceModel()->data(leftIndex, Qt::DisplayRole); + QVariant rightDataName = sourceModel()->data(rightIndex, Qt::DisplayRole); + return leftDataName.toString().toLower() > rightDataName.toString().toLower(); + } + return leftDataTimeStamp < rightDataTimeStamp; +} + +bool ConversationsSortFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const +{ + QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent); + + if (filterRole() == Qt::DisplayRole) { + return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent); + } + return sourceModel()->data(index, ConversationListModel::DateRole) != INVALID_THREAD_ID; +} diff --git a/smsapp/conversationssortfilterproxymodel.h b/smsapp/conversationssortfilterproxymodel.h new file mode 100644 index 000000000..912e68f56 --- /dev/null +++ b/smsapp/conversationssortfilterproxymodel.h @@ -0,0 +1,70 @@ +/** + * Copyright (C) 2018 Aleix Pol Gonzalez + * Copyright (C) 2018 Simon Redman + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License or (at your option) version 3 or any later version + * accepted by the membership of KDE e.V. (or its successor approved + * by the membership of KDE e.V.), which shall act as a proxy + * defined in Section 14 of version 3 of the license. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef CONVERSATIONSSORTFILTERPROXYMODEL_H +#define CONVERSATIONSSORTFILTERPROXYMODEL_H + +#include +#include +#include + +Q_DECLARE_LOGGING_CATEGORY(KDECONNECT_SMS_CONVERSATIONS_SORT_FILTER_PROXY_MODEL) + +class ConversationsSortFilterProxyModel : public QSortFilterProxyModel, public QQmlParserStatus +{ + Q_OBJECT + Q_INTERFACES(QQmlParserStatus) + Q_PROPERTY(Qt::SortOrder sortOrder READ sortOrder WRITE setSortOrder) +public: + + Qt::SortOrder sortOrder() const { return m_sortOrder; } + void setSortOrder(Qt::SortOrder sortOrder) { + if (m_sortOrder != sortOrder) { + m_sortOrder = sortOrder; + sortNow(); + } + } + void classBegin() override {} + void componentComplete() override { + m_completed = true; + sortNow(); + } + + Q_INVOKABLE void setOurFilterRole(int role); + + ConversationsSortFilterProxyModel(); + ~ConversationsSortFilterProxyModel(); + +protected: + bool lessThan(const QModelIndex& leftIndex, const QModelIndex& rightIndex) const override; + bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override; + +private: + void sortNow() { + if (m_completed && dynamicSortFilter()) + sort(0, m_sortOrder); + } + + bool m_completed = false; + Qt::SortOrder m_sortOrder = Qt::AscendingOrder; +}; + +#endif // CONVERSATIONSSORTFILTERPROXYMODEL_H diff --git a/smsapp/main.cpp b/smsapp/main.cpp index 9b9af7033..feb010f63 100644 --- a/smsapp/main.cpp +++ b/smsapp/main.cpp @@ -21,6 +21,7 @@ #include "conversationmodel.h" #include "conversationlistmodel.h" +#include "conversationssortfilterproxymodel.h" #include "kdeconnect-version.h" #include @@ -62,7 +63,7 @@ int main(int argc, char *argv[]) KDBusService service(KDBusService::Unique); - qmlRegisterType("org.kde.kdeconnect.sms", 1, 0, "QSortFilterProxyModel"); + qmlRegisterType("org.kde.kdeconnect.sms", 1, 0, "QSortFilterProxyModel"); qmlRegisterType("org.kde.kdeconnect.sms", 1, 0, "ConversationModel"); qmlRegisterType("org.kde.kdeconnect.sms", 1, 0, "ConversationListModel");