FluentUI/src/FluTableSortProxyModel.cpp

76 lines
2.6 KiB
C++
Raw Normal View History

2024-03-01 00:19:41 +08:00
#include "FluTableSortProxyModel.h"
#include <QJSValueList>
2024-04-11 14:51:43 +08:00
FluTableSortProxyModel::FluTableSortProxyModel(QSortFilterProxyModel *parent) : QSortFilterProxyModel{parent} {
connect(this, &FluTableSortProxyModel::modelChanged, this, [=] {
2024-04-28 20:22:05 +08:00
setSourceModel(this->model().value<QAbstractTableModel *>());
2024-03-01 00:19:41 +08:00
});
}
2024-04-11 14:51:43 +08:00
bool FluTableSortProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
2024-03-01 23:24:03 +08:00
QJSValue filter = _filter;
2024-04-11 14:51:43 +08:00
if (filter.isUndefined()) {
2024-03-01 23:24:03 +08:00
return true;
}
QJSValueList data;
2024-04-11 14:51:43 +08:00
data << source_row;
2024-03-01 23:24:03 +08:00
return filter.call(data).toBool();
2024-03-01 00:19:41 +08:00
}
2024-04-11 14:51:43 +08:00
bool FluTableSortProxyModel::filterAcceptsColumn(int source_column, const QModelIndex &source_parent) const {
2024-03-01 00:19:41 +08:00
return true;
}
2024-04-11 14:51:43 +08:00
bool FluTableSortProxyModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const {
2024-03-01 00:19:41 +08:00
QJSValue comparator = _comparator;
2024-04-11 14:51:43 +08:00
if (comparator.isUndefined()) {
2024-03-01 00:19:41 +08:00
return true;
}
QJSValueList data;
2024-04-11 14:51:43 +08:00
data << source_left.row();
data << source_right.row();
2024-03-01 00:19:41 +08:00
bool flag = comparator.call(data).toBool();
2024-04-11 14:51:43 +08:00
if (sortOrder() == Qt::AscendingOrder) {
2024-03-01 00:19:41 +08:00
return !flag;
2024-04-11 14:51:43 +08:00
} else {
2024-03-01 00:19:41 +08:00
return flag;
}
}
2024-04-11 14:51:43 +08:00
[[maybe_unused]] void FluTableSortProxyModel::setComparator(const QJSValue &comparator) {
2024-03-01 23:24:03 +08:00
int column = 0;
2024-04-11 14:51:43 +08:00
if (comparator.isUndefined()) {
2024-03-01 23:24:03 +08:00
column = -1;
}
2024-03-01 00:19:41 +08:00
this->_comparator = comparator;
2024-04-11 14:51:43 +08:00
if (sortOrder() == Qt::AscendingOrder) {
sort(column, Qt::DescendingOrder);
} else {
sort(column, Qt::AscendingOrder);
2024-03-01 00:19:41 +08:00
}
}
2024-03-01 23:24:03 +08:00
2024-04-11 14:51:43 +08:00
[[maybe_unused]] void FluTableSortProxyModel::setFilter(const QJSValue &filter) {
2024-03-01 23:24:03 +08:00
this->_filter = filter;
invalidateFilter();
}
2024-04-11 14:51:43 +08:00
[[maybe_unused]] QVariant FluTableSortProxyModel::getRow(int rowIndex) {
2024-03-01 00:19:41 +08:00
QVariant result;
2024-04-28 20:22:05 +08:00
QMetaObject::invokeMethod(_model.value<QAbstractTableModel *>(), "getRow", Q_RETURN_ARG(QVariant, result), Q_ARG(int, mapToSource(index(rowIndex, 0)).row()));
2024-03-01 00:19:41 +08:00
return result;
}
2024-04-11 14:51:43 +08:00
[[maybe_unused]] void FluTableSortProxyModel::setRow(int rowIndex, const QVariant &val) {
2024-04-28 20:22:05 +08:00
QMetaObject::invokeMethod(_model.value<QAbstractTableModel *>(), "setRow", Q_ARG(int, mapToSource(index(rowIndex, 0)).row()), Q_ARG(QVariant, val));
2024-03-01 00:19:41 +08:00
}
2024-04-28 15:56:37 +08:00
[[maybe_unused]] void FluTableSortProxyModel::insertRow(int rowIndex, const QVariant &val) {
2024-04-28 20:22:05 +08:00
QMetaObject::invokeMethod(_model.value<QAbstractTableModel *>(), "insertRow", Q_ARG(int, mapToSource(index(rowIndex, 0)).row()), Q_ARG(QVariant, val));
2024-04-28 15:56:37 +08:00
}
2024-04-11 14:51:43 +08:00
[[maybe_unused]] void FluTableSortProxyModel::removeRow(int rowIndex, int rows) {
2024-04-28 20:22:05 +08:00
QMetaObject::invokeMethod(_model.value<QAbstractTableModel *>(), "removeRow", Q_ARG(int, mapToSource(index(rowIndex, 0)).row()), Q_ARG(int, rows));
2024-03-01 00:19:41 +08:00
}