#include "tagproptablemodel.h" TagPropTableModel::TagPropTableModel(SQLiteSaveFile &backend, const Tag tag) : TagPropTableModel(backend, true) { tag_cached = tag; tag_keys = tag_cached.metadata.keys(); tag_keys.sort(); qDebug() << "connecting TagPropTableModel" << &backend; } TagPropTableModel::TagPropTableModel(SQLiteSaveFile &backend, bool tagIsValid) : backend(backend) , tagIsValid(tagIsValid) { connect(&backend, &SQLiteSaveFile::tagChange, this, &TagPropTableModel::tagChange); connect(&backend, &SQLiteSaveFile::fileReload, [=]() { beginResetModel(); this->tagIsValid = false; endResetModel(); }); } int TagPropTableModel::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent); if (!tagIsValid) return 0; return 3 + tag_cached.metadata.size(); } int TagPropTableModel::columnCount(const QModelIndex &parent) const { Q_UNUSED(parent); return 2; } QVariant TagPropTableModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); if (role != Qt::DisplayRole && role != Qt::EditRole) return QVariant(); bool label = index.column() == 0; switch (index.row()) { case 0: return label ? QVariant("Database ID") : tag_cached.id; break; case 1: return label ? QVariant("Label") : tag_cached.name; break; case 2: return label ? QVariant("Anchor") : QString("%1, %2").arg(tag_cached.anchor.x()).arg(tag_cached.anchor.y()); break; } int idx = index.row() - 3; return label ? tag_keys[idx] : tag_cached.metadata[tag_keys[idx]]; } QVariant TagPropTableModel::headerData(int section, Qt::Orientation orientation, int role) const { if (orientation != Qt::Horizontal) return QVariant(); if (role != Qt::DisplayRole) return QVariant(); if (section == 0) return "Property"; else return "Value"; } Qt::ItemFlags TagPropTableModel::flags(const QModelIndex &index) const { if (index.row() == 0) return Qt::NoItemFlags; if (index.column() == 0) { if (index.row() < 3) return Qt::ItemIsEnabled; return Qt::ItemIsEnabled | Qt::ItemIsEditable; } if (index.row() == 1) return Qt::ItemIsEnabled | Qt::ItemIsEditable; if (index.row() == 2) /* anchor */ return Qt::ItemIsEnabled; return Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsEnabled; } bool TagPropTableModel::setData(const QModelIndex &index, const QVariant &value, int role) { if (!index.isValid()) return false; if (role != Qt::EditRole) return false; if (index.row() == 1) { tag_cached.name = value.toString(); } else if (index.row() < 3) { return false; } else { int idx = index.row() - 3; if (index.column() == 0) { /* key changed */ /* move value to new key and delete old key */ tag_cached.metadata[value.toString()] = tag_cached.metadata[tag_keys[idx]]; tag_cached.metadata.remove(tag_keys[idx]); } else { tag_cached.metadata[tag_keys[idx]] = value.toString(); } } backend.updateTag(tag_cached); return true; } void TagPropTableModel::tagChange(TagChange change, const Tag &tag) { auto dbg = qDebug() << QString("TagPropTableModel::tagChange(%1, %2 \"%3\")").arg(change).arg(tag.id).arg(tag.name); if (tag.id != tag_cached.id) return; assert(change != TagChange::CREATED); if (change == TagChange::CHANGED) { dbg << "changed"; showTag(tag); } else if (change == TagChange::DELETED) { dbg << "deleted"; beginResetModel(); tagIsValid = false; endResetModel(); } } void TagPropTableModel::showTag(const Tag &tag) { beginResetModel(); tag_cached = tag; tag_keys = tag_cached.metadata.keys(); tag_keys.sort(); tagIsValid = true; endResetModel(); }