2024-04-28 18:38:37 +08:00
|
|
|
#include "FluTableModel.h"
|
|
|
|
|
2024-04-28 20:22:05 +08:00
|
|
|
FluTableModel::FluTableModel(QObject *parent) : QAbstractTableModel{parent} {
|
2024-04-28 18:38:37 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int FluTableModel::rowCount(const QModelIndex &parent) const {
|
|
|
|
return _rows.count();
|
|
|
|
}
|
|
|
|
|
|
|
|
int FluTableModel::columnCount(const QModelIndex &parent) const {
|
|
|
|
return this->_columnSource.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant FluTableModel::data(const QModelIndex &index, int role) const {
|
|
|
|
switch (role) {
|
2024-04-28 20:22:05 +08:00
|
|
|
case FluTableModel::RowModel:
|
|
|
|
return QVariant::fromValue(_rows.at(index.row()));
|
|
|
|
case FluTableModel::ColumnModel:
|
|
|
|
return QVariant::fromValue(_columnSource.at(index.column()));
|
|
|
|
default:
|
|
|
|
break;
|
2024-04-28 18:38:37 +08:00
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
QHash<int, QByteArray> FluTableModel::roleNames() const {
|
|
|
|
return {
|
2024-04-28 20:22:05 +08:00
|
|
|
{FluTableModel::RowModel, "rowModel"},
|
|
|
|
{FluTableModel::ColumnModel, "columnModel"}
|
2024-04-28 18:38:37 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-04-28 20:22:05 +08:00
|
|
|
void FluTableModel::clear() {
|
2024-04-28 18:38:37 +08:00
|
|
|
beginResetModel();
|
|
|
|
this->_rows.clear();
|
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
|
2024-04-28 20:22:05 +08:00
|
|
|
QVariant FluTableModel::getRow(int rowIndex) {
|
2024-04-28 18:38:37 +08:00
|
|
|
return _rows.at(rowIndex);
|
|
|
|
}
|
|
|
|
|
2024-04-28 20:22:05 +08:00
|
|
|
void FluTableModel::setRow(int rowIndex, QVariant row) {
|
|
|
|
_rows.replace(rowIndex, row.toMap());
|
2024-04-28 18:38:37 +08:00
|
|
|
Q_EMIT dataChanged(index(rowIndex, 0), index(rowIndex, columnCount() - 1));
|
|
|
|
}
|
|
|
|
|
2024-04-28 20:22:05 +08:00
|
|
|
void FluTableModel::insertRow(int rowIndex, QVariant row) {
|
2024-04-28 18:38:37 +08:00
|
|
|
beginInsertRows(QModelIndex(), rowIndex, rowIndex);
|
2024-04-28 20:22:05 +08:00
|
|
|
_rows.insert(rowIndex, row.toMap());
|
2024-04-28 18:38:37 +08:00
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
|
2024-04-28 20:22:05 +08:00
|
|
|
void FluTableModel::removeRow(int rowIndex, int rows) {
|
2024-04-28 18:38:37 +08:00
|
|
|
beginRemoveRows(QModelIndex(), rowIndex, rowIndex + rows - 1);
|
|
|
|
_rows = _rows.mid(0, rowIndex) + _rows.mid(rowIndex + rows);
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
|
|
|
|
2024-04-28 20:22:05 +08:00
|
|
|
void FluTableModel::appendRow(QVariant row) {
|
|
|
|
insertRow(rowCount(), row);
|
2024-04-28 18:38:37 +08:00
|
|
|
}
|