Move ConversationsSortFilterProxyModel class from conversationlistmodel files to its own files

## Summary

Currently `OurSortFilterProxyModel` class is implemented in the same file with `ConversationListModel` class, which would likely grow in the future.

This MR separates `OurSortFilterProxyModel` class into its own separate file renaming it to `ConversationsSortFilterProxyModel`

https://invent.kde.org/kde/kdeconnect-kde/-/merge_requests/229
This commit is contained in:
Aniket Kumar 2020-03-29 18:25:07 +00:00 committed by Simon Redman
parent 77e08ec70a
commit 520d2a5695
6 changed files with 138 additions and 77 deletions

View file

@ -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})

View file

@ -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)

View file

@ -24,53 +24,12 @@
#include <QStandardItemModel>
#include <QLoggingCategory>
#include <QQmlParserStatus>
#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
{

View file

@ -0,0 +1,65 @@
/**
* Copyright (C) 2018 Aleix Pol Gonzalez <aleixpol@kde.org>
* Copyright (C) 2018 Simon Redman <simon@ergotech.com>
*
* 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 <https://www.gnu.org/licenses/>.
*/
#include "conversationssortfilterproxymodel.h"
#include "conversationlistmodel.h"
#include <QString>
#include <QLoggingCategory>
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;
}

View file

@ -0,0 +1,70 @@
/**
* Copyright (C) 2018 Aleix Pol Gonzalez <aleixpol@kde.org>
* Copyright (C) 2018 Simon Redman <simon@ergotech.com>
*
* 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 <https://www.gnu.org/licenses/>.
*/
#ifndef CONVERSATIONSSORTFILTERPROXYMODEL_H
#define CONVERSATIONSSORTFILTERPROXYMODEL_H
#include <QSortFilterProxyModel>
#include <QLoggingCategory>
#include <QQmlParserStatus>
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

View file

@ -21,6 +21,7 @@
#include "conversationmodel.h"
#include "conversationlistmodel.h"
#include "conversationssortfilterproxymodel.h"
#include "kdeconnect-version.h"
#include <QApplication>
@ -62,7 +63,7 @@ int main(int argc, char *argv[])
KDBusService service(KDBusService::Unique);
qmlRegisterType<OurSortFilterProxyModel>("org.kde.kdeconnect.sms", 1, 0, "QSortFilterProxyModel");
qmlRegisterType<ConversationsSortFilterProxyModel>("org.kde.kdeconnect.sms", 1, 0, "QSortFilterProxyModel");
qmlRegisterType<ConversationModel>("org.kde.kdeconnect.sms", 1, 0, "ConversationModel");
qmlRegisterType<ConversationListModel>("org.kde.kdeconnect.sms", 1, 0, "ConversationListModel");