FluentUI/src/FluTableSortProxyModel.cpp

76 lines
2.1 KiB
C++
Raw Normal View History

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