Roles can now be declared read-only. First read-only role: IdRole
This commit is contained in:
@ -25,8 +25,11 @@ enum UserRoles {
|
|||||||
|
|
||||||
static UserRoles DEFAULT_ROLE = NameRole;
|
static UserRoles DEFAULT_ROLE = NameRole;
|
||||||
// TODO ?rename USER_FACING_ROLES -> MAIN_ROLES ?
|
// TODO ?rename USER_FACING_ROLES -> MAIN_ROLES ?
|
||||||
static QList<UserRoles> USER_FACING_ROLES = {NameRole, DescriptionRole, InfoRole,
|
static QList<UserRoles> USER_FACING_ROLES = {NameRole, DescriptionRole, InfoRole, TypeRole,
|
||||||
TypeRole, AmountRole, FactorRole};
|
AmountRole, FactorRole, IdRole};
|
||||||
|
|
||||||
|
static QList<UserRoles> READ_ONLY_ROLES = {IdRole};
|
||||||
|
|
||||||
static QHash<int, QByteArray> ROLE_NAMES = {
|
static QHash<int, QByteArray> ROLE_NAMES = {
|
||||||
{NameRole, "name"}, {DescriptionRole, "description"},
|
{NameRole, "name"}, {DescriptionRole, "description"},
|
||||||
{InfoRole, "info"}, {TypeRole, "type"},
|
{InfoRole, "info"}, {TypeRole, "type"},
|
||||||
@ -71,6 +74,9 @@ static UserRoles GET_ROLE_FOR_COLUMN(const int column) {
|
|||||||
case 5:
|
case 5:
|
||||||
return FactorRole;
|
return FactorRole;
|
||||||
break;
|
break;
|
||||||
|
case 6:
|
||||||
|
return IdRole;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
qWarning() << QString("No role found for column %1! Returning 'NameRole'...").arg(column);
|
qWarning() << QString("No role found for column %1! Returning 'NameRole'...").arg(column);
|
||||||
return NameRole;
|
return NameRole;
|
||||||
|
|||||||
@ -40,10 +40,19 @@ TableModel::TableModel(QUndoStack* undoStack, QObject* parent)
|
|||||||
, m_undoStack(undoStack) {}
|
, m_undoStack(undoStack) {}
|
||||||
|
|
||||||
Qt::ItemFlags TableModel::flags(const QModelIndex& index) const {
|
Qt::ItemFlags TableModel::flags(const QModelIndex& index) const {
|
||||||
|
Qt::ItemFlags result = QAbstractTableModel::flags(index);
|
||||||
if (!index.isValid()) {
|
if (!index.isValid()) {
|
||||||
return QAbstractTableModel::flags(index);
|
return result;
|
||||||
}
|
}
|
||||||
return Qt::ItemIsEditable | QAbstractTableModel::flags(index);
|
|
||||||
|
const int column = index.column();
|
||||||
|
const int roleForColumn = GET_ROLE_FOR_COLUMN(column);
|
||||||
|
/// roles which aren't editable by the user
|
||||||
|
if (READ_ONLY_ROLES.contains(roleForColumn)) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result | Qt::ItemIsEditable;
|
||||||
}
|
}
|
||||||
|
|
||||||
QHash<int, QByteArray> TableModel::roleNames() const { return ROLE_NAMES; }
|
QHash<int, QByteArray> TableModel::roleNames() const { return ROLE_NAMES; }
|
||||||
@ -341,8 +350,6 @@ int TableModel::getAppropriateRoleForIndex(const QModelIndex& index, const int r
|
|||||||
}
|
}
|
||||||
|
|
||||||
QModelIndex TableModel::searchItemIndex(const ModelItemValues givenItemValues) const {
|
QModelIndex TableModel::searchItemIndex(const ModelItemValues givenItemValues) const {
|
||||||
// iterate over indexes to search item : see searchItem(...);
|
|
||||||
// for (const shared_ptr<ModelItem>& item : m_items) {
|
|
||||||
for (int row = 0; row < rowCount(); ++row) {
|
for (int row = 0; row < rowCount(); ++row) {
|
||||||
qDebug() << "Processing item at row" << row << "...";
|
qDebug() << "Processing item at row" << row << "...";
|
||||||
QModelIndex itemIndex = index(row, 0);
|
QModelIndex itemIndex = index(row, 0);
|
||||||
|
|||||||
Reference in New Issue
Block a user