104 lines
3.0 KiB
C++
104 lines
3.0 KiB
C++
#include "tablemodel.h"
|
|
|
|
#include "modelitem.h"
|
|
|
|
enum UserRoles { NameRole = Qt::UserRole + 1, DescriptionRole, InfoRole, AmountRole, FactorRole };
|
|
static const QHash<int, QByteArray> ROLE_NAMES = {{NameRole, "Name"},
|
|
{DescriptionRole, "Description"},
|
|
{InfoRole, "Info"},
|
|
{AmountRole, "Amount"},
|
|
{FactorRole, "Factor"}};
|
|
|
|
TableModel::TableModel(QObject* parent)
|
|
: QAbstractTableModel{parent} {
|
|
for (int row = 0; row < 23; ++row) {
|
|
QHash<int, QVariant> values;
|
|
values[NameRole] = QString("Item %1").arg(row);
|
|
values[DescriptionRole] = QString("This is item %1").arg(row);
|
|
values[InfoRole] = QString("Info of item %1").arg(row);
|
|
values[AmountRole] = row;
|
|
values[FactorRole] = row * 1.1;
|
|
|
|
shared_ptr<ModelItem> item = make_unique<ModelItem>(values);
|
|
m_items.append(std::move(item));
|
|
}
|
|
}
|
|
|
|
Qt::ItemFlags TableModel::flags(const QModelIndex& index) const {
|
|
return Qt::ItemIsEditable | QAbstractTableModel::flags(index);
|
|
}
|
|
|
|
int TableModel::rowCount(const QModelIndex& parent) const { return m_items.size(); }
|
|
|
|
int TableModel::columnCount(const QModelIndex& parent) const { return ROLE_NAMES.size(); }
|
|
|
|
QVariant TableModel::data(const QModelIndex& index, int role) const {
|
|
const int row = index.row();
|
|
const int column = index.column();
|
|
|
|
if (!index.isValid()) {
|
|
return QVariant();
|
|
}
|
|
if (row >= rowCount(QModelIndex()) || column >= columnCount(QModelIndex())) {
|
|
return QVariant();
|
|
}
|
|
|
|
int roleForColumn = getRoleForColumn(column);
|
|
switch (role) {
|
|
case Qt::DisplayRole:
|
|
return m_items.at(row)->data(roleForColumn);
|
|
}
|
|
|
|
return QVariant();
|
|
}
|
|
|
|
QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const {
|
|
if (role == Qt::DisplayRole) {
|
|
if (orientation == Qt::Horizontal) {
|
|
const int columnRole = getRoleForColumn(section);
|
|
const QString headerName = ROLE_NAMES.value(columnRole);
|
|
return QString("%1").arg(headerName);
|
|
} else {
|
|
return QString("%1").arg(section);
|
|
}
|
|
}
|
|
return QVariant();
|
|
}
|
|
|
|
bool TableModel::setData(const QModelIndex& index, const QVariant& value, int role) {
|
|
if (role == Qt::EditRole) {
|
|
if (!checkIndex(index)) {
|
|
return false;
|
|
}
|
|
int columnRole = getRoleForColumn(index.column());
|
|
shared_ptr<ModelItem> item = m_items.at(index.row());
|
|
return item->setData(value, columnRole);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// bool TableModel::setItemData(const QModelIndex& index, const QMap<int, QVariant>& roles) {}
|
|
|
|
int TableModel::getRoleForColumn(const int column) const {
|
|
switch (column) {
|
|
case 0:
|
|
return NameRole;
|
|
break;
|
|
case 1:
|
|
return DescriptionRole;
|
|
break;
|
|
case 2:
|
|
return InfoRole;
|
|
break;
|
|
case 3:
|
|
return AmountRole;
|
|
break;
|
|
case 4:
|
|
return FactorRole;
|
|
break;
|
|
default:
|
|
return NameRole;
|
|
break;
|
|
}
|
|
}
|