Custom Room Sort Order
Add the ability to sort rooms by a custom set of parameters.
This commit is contained in:
104
src/models/roomsortparametermodel.cpp
Normal file
104
src/models/roomsortparametermodel.cpp
Normal file
@@ -0,0 +1,104 @@
|
||||
// SPDX-FileCopyrightText: 2024 James Graham <james.h.graham@protonmail.com>
|
||||
// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
||||
|
||||
#include "roomsortparametermodel.h"
|
||||
|
||||
#include "roomsortparameter.h"
|
||||
|
||||
using namespace Qt::StringLiterals;
|
||||
|
||||
RoomSortParameterModel::RoomSortParameterModel(QObject *parent)
|
||||
: QAbstractListModel(parent)
|
||||
{
|
||||
m_currentParameters = RoomSortParameter::currentParameterList();
|
||||
}
|
||||
|
||||
RoomSortParameterModel::RoomSortParameterModel(QList<RoomSortParameter::Parameter> parameters, QObject *parent)
|
||||
: QAbstractListModel(parent)
|
||||
{
|
||||
m_currentParameters = parameters;
|
||||
}
|
||||
|
||||
QVariant RoomSortParameterModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if (index.row() < 0 || index.row() >= rowCount()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const auto parameter = m_currentParameters.at(index.row());
|
||||
if (role == Name) {
|
||||
return RoomSortParameter::parameterName(parameter);
|
||||
}
|
||||
if (role == Description) {
|
||||
return RoomSortParameter::parameterDescription(parameter);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
int RoomSortParameterModel::rowCount(const QModelIndex &parent) const
|
||||
{
|
||||
Q_UNUSED(parent);
|
||||
return m_currentParameters.size();
|
||||
}
|
||||
|
||||
QHash<int, QByteArray> RoomSortParameterModel::roleNames() const
|
||||
{
|
||||
return {
|
||||
{Name, "name"},
|
||||
{Description, "description"},
|
||||
};
|
||||
}
|
||||
|
||||
void RoomSortParameterModel::addParameter(RoomSortParameter::Parameter parameter)
|
||||
{
|
||||
if (m_currentParameters.contains(parameter)) {
|
||||
return;
|
||||
}
|
||||
|
||||
beginInsertRows({}, rowCount(), rowCount());
|
||||
m_currentParameters.append(parameter);
|
||||
endInsertRows();
|
||||
}
|
||||
|
||||
void RoomSortParameterModel::removeRow(int row)
|
||||
{
|
||||
if (rowCount() <= 1 || row < 0 || row >= rowCount()) {
|
||||
return;
|
||||
}
|
||||
|
||||
beginRemoveRows({}, row, row);
|
||||
m_currentParameters.remove(row);
|
||||
endRemoveRows();
|
||||
}
|
||||
|
||||
void RoomSortParameterModel::moveRowUp(int row)
|
||||
{
|
||||
if (row < 1 || row >= rowCount()) {
|
||||
return;
|
||||
}
|
||||
|
||||
beginMoveRows({}, row, row, {}, row - 1);
|
||||
m_currentParameters.move(row, row - 1);
|
||||
endMoveRows();
|
||||
}
|
||||
|
||||
void RoomSortParameterModel::moveRowDown(int row)
|
||||
{
|
||||
if (row < 0 || row >= rowCount() - 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
beginMoveRows({}, row, row, {}, row + 2);
|
||||
m_currentParameters.move(row, row + 1);
|
||||
endMoveRows();
|
||||
}
|
||||
|
||||
void RoomSortParameterModel::saveParameterList()
|
||||
{
|
||||
RoomSortParameter::saveNewParameterList(m_currentParameters);
|
||||
}
|
||||
|
||||
RoomSortParameterModel *RoomSortParameterModel::allParameterModel() const
|
||||
{
|
||||
return new RoomSortParameterModel(RoomSortParameter::allParameterList());
|
||||
}
|
||||
92
src/models/roomsortparametermodel.h
Normal file
92
src/models/roomsortparametermodel.h
Normal file
@@ -0,0 +1,92 @@
|
||||
// SPDX-FileCopyrightText: 2024 James Graham <james.h.graham@protonmail.com>
|
||||
// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QAbstractListModel>
|
||||
#include <QQmlEngine>
|
||||
|
||||
#include <KLazyLocalizedString>
|
||||
#include <qtmetamacros.h>
|
||||
|
||||
#include "enums/roomsortparameter.h"
|
||||
|
||||
/**
|
||||
* @class RoomSortParameterModel
|
||||
*
|
||||
* This model is used to visualize and modify the current sorting priorities.
|
||||
*/
|
||||
class RoomSortParameterModel : public QAbstractListModel
|
||||
{
|
||||
Q_OBJECT
|
||||
QML_ELEMENT
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief Defines the model roles.
|
||||
*/
|
||||
enum Roles {
|
||||
Name = Qt::DisplayRole, /**< The name of the sort parameter. */
|
||||
Description, /**< The description of the sort parameter. */
|
||||
};
|
||||
Q_ENUM(Roles)
|
||||
|
||||
explicit RoomSortParameterModel(QObject *parent = nullptr);
|
||||
explicit RoomSortParameterModel(QList<RoomSortParameter::Parameter> parameters, QObject *parent = nullptr);
|
||||
|
||||
/**
|
||||
* @brief Get the given role value at the given index.
|
||||
*
|
||||
* @sa QAbstractItemModel::data
|
||||
*/
|
||||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||
|
||||
/**
|
||||
* @brief Number of rows in the model.
|
||||
*
|
||||
* @sa QAbstractItemModel::rowCount
|
||||
*/
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
|
||||
/**
|
||||
* @brief Returns a mapping from Role enum values to role names.
|
||||
*
|
||||
* @sa EventRoles, QAbstractItemModel::roleNames()
|
||||
*/
|
||||
QHash<int, QByteArray> roleNames() const override;
|
||||
|
||||
/**
|
||||
* @brief Add the given parameter to the model.
|
||||
*
|
||||
* If the Parameter is already in the model nothing will happen.
|
||||
*/
|
||||
Q_INVOKABLE void addParameter(RoomSortParameter::Parameter parameter);
|
||||
|
||||
/**
|
||||
* @brief Remove the given row from the model.
|
||||
*/
|
||||
Q_INVOKABLE void removeRow(int row);
|
||||
|
||||
/**
|
||||
* @brief Move the given row up one.
|
||||
*/
|
||||
Q_INVOKABLE void moveRowUp(int row);
|
||||
|
||||
/**
|
||||
* @brief Move the given row down one.
|
||||
*/
|
||||
Q_INVOKABLE void moveRowDown(int row);
|
||||
|
||||
/**
|
||||
* @brief Save the current model parameters as a custom sort order.
|
||||
*/
|
||||
Q_INVOKABLE void saveParameterList();
|
||||
|
||||
/**
|
||||
* @brief Return a RoomSortParameterModel with all available parameters.
|
||||
*/
|
||||
Q_INVOKABLE RoomSortParameterModel *allParameterModel() const;
|
||||
|
||||
private:
|
||||
QList<RoomSortParameter::Parameter> m_currentParameters;
|
||||
};
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include "sortfilterroomtreemodel.h"
|
||||
|
||||
#include "enums/roomsortparameter.h"
|
||||
#include "neochatconfig.h"
|
||||
#include "neochatconnection.h"
|
||||
#include "neochatroom.h"
|
||||
@@ -65,10 +66,13 @@ static const QVector<RoomSortParameter::Parameter> lastMessageSortPriorities{
|
||||
RoomSortParameter::LastActive,
|
||||
};
|
||||
|
||||
bool SortFilterRoomTreeModel::prioritiesCmp(const QVector<RoomSortParameter::Parameter> &priorities,
|
||||
const QModelIndex &source_left,
|
||||
const QModelIndex &source_right) const
|
||||
bool SortFilterRoomTreeModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
|
||||
{
|
||||
// Don't sort the top level categories.
|
||||
if (!source_left.parent().isValid() || !source_right.parent().isValid()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto treeModel = dynamic_cast<RoomTreeModel *>(sourceModel());
|
||||
if (treeModel == nullptr) {
|
||||
return false;
|
||||
@@ -80,7 +84,7 @@ bool SortFilterRoomTreeModel::prioritiesCmp(const QVector<RoomSortParameter::Par
|
||||
return false;
|
||||
}
|
||||
|
||||
for (auto sortRole : priorities) {
|
||||
for (auto sortRole : RoomSortParameter::currentParameterList()) {
|
||||
auto result = RoomSortParameter::compareParameter(sortRole, leftRoom, rightRoom);
|
||||
|
||||
if (result != 0) {
|
||||
@@ -90,24 +94,6 @@ bool SortFilterRoomTreeModel::prioritiesCmp(const QVector<RoomSortParameter::Par
|
||||
return false;
|
||||
}
|
||||
|
||||
bool SortFilterRoomTreeModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
|
||||
{
|
||||
// Don't sort the top level categories.
|
||||
if (!source_left.parent().isValid() || !source_right.parent().isValid()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (m_sortOrder) {
|
||||
case SortFilterRoomTreeModel::Alphabetical:
|
||||
return prioritiesCmp(alphabeticalSortPriorities, source_left, source_right);
|
||||
case SortFilterRoomTreeModel::Activity:
|
||||
return prioritiesCmp(activitySortPriorities, source_left, source_right);
|
||||
case SortFilterRoomTreeModel::LastMessage:
|
||||
return prioritiesCmp(lastMessageSortPriorities, source_left, source_right);
|
||||
}
|
||||
return QSortFilterProxyModel::lessThan(source_left, source_right);
|
||||
}
|
||||
|
||||
void SortFilterRoomTreeModel::setFilterText(const QString &text)
|
||||
{
|
||||
m_filterText = text;
|
||||
|
||||
@@ -7,8 +7,7 @@
|
||||
#include <QQmlEngine>
|
||||
#include <QSortFilterProxyModel>
|
||||
|
||||
#include "enums/roomsortparameter.h"
|
||||
#include "models/roomtreemodel.h"
|
||||
class RoomTreeModel;
|
||||
|
||||
/**
|
||||
* @class SortFilterRoomTreeModel
|
||||
@@ -105,6 +104,4 @@ private:
|
||||
Mode m_mode = All;
|
||||
QString m_filterText;
|
||||
QString m_activeSpaceId;
|
||||
|
||||
bool prioritiesCmp(const QVector<RoomSortParameter::Parameter> &priorities, const QModelIndex &left, const QModelIndex &right) const;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user