From c6713d0876ce2d99f912151c9884477606909681 Mon Sep 17 00:00:00 2001 From: jaseg Date: Mon, 17 Aug 2020 00:56:17 +0200 Subject: More UI work --- tagproptablemodel.cpp | 99 ++++++++++++++++++++++++++++----------------------- 1 file changed, 54 insertions(+), 45 deletions(-) (limited to 'tagproptablemodel.cpp') diff --git a/tagproptablemodel.cpp b/tagproptablemodel.cpp index 4dc5e6b..e015987 100644 --- a/tagproptablemodel.cpp +++ b/tagproptablemodel.cpp @@ -1,32 +1,28 @@ #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) +TagPropTableModel::TagPropTableModel(SQLiteSaveFile *backend, bool showBaseData) : backend(backend) - , tagIsValid(tagIsValid) + , showBaseData(showBaseData) { - connect(&backend, &SQLiteSaveFile::tagChange, this, &TagPropTableModel::tagChange); - connect(&backend, &SQLiteSaveFile::fileReload, [=]() { - beginResetModel(); - this->tagIsValid = false; - endResetModel(); - }); + if (backend) { + connect(backend, &SQLiteSaveFile::tagChange, this, &TagPropTableModel::tagChange); + connect(backend, &SQLiteSaveFile::fileReload, [=]() { + showTag(Tag()); + }); + } } int TagPropTableModel::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent); - if (!tagIsValid) + if (!tag_cached.isValid()) return 0; - return 3 + tag_cached.metadata.size(); + + if (showBaseData) + return 3 + tag_cached.metadata.size(); + else + return tag_cached.metadata.size(); } int TagPropTableModel::columnCount(const QModelIndex &parent) const @@ -43,16 +39,20 @@ QVariant TagPropTableModel::data(const QModelIndex &index, int role) const if (role != Qt::DisplayRole && role != Qt::EditRole) return QVariant(); + int row = index.row(); bool label = index.column() == 0; + if (showBaseData) { + + switch (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; + } - 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; + row -= 3; } - int idx = index.row() - 3; - return label ? tag_keys[idx] : tag_cached.metadata[tag_keys[idx]]; + return label ? tag_keys[row] : tag_cached.metadata[tag_keys[row]]; } QVariant TagPropTableModel::headerData(int section, Qt::Orientation orientation, int role) const @@ -75,17 +75,19 @@ Qt::ItemFlags TagPropTableModel::flags(const QModelIndex &index) const return Qt::NoItemFlags; if (index.column() == 0) { - if (index.row() < 3) + if (showBaseData && index.row() < 3) return Qt::ItemIsEnabled; return Qt::ItemIsEnabled | Qt::ItemIsEditable; } - if (index.row() == 1) - return Qt::ItemIsEnabled | Qt::ItemIsEditable; + if (showBaseData) { + if (index.row() == 1) + return Qt::ItemIsEnabled | Qt::ItemIsEditable; - if (index.row() == 2) /* anchor */ - return Qt::ItemIsEnabled; + if (index.row() == 2) /* anchor */ + return Qt::ItemIsEnabled; + } return Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsEnabled; } @@ -98,22 +100,32 @@ bool TagPropTableModel::setData(const QModelIndex &index, const QVariant &value, 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]); + int row = index.row(); + if (showBaseData) { + if (row == 1) { + tag_cached.name = value.toString(); + + if (backend) + backend->updateTag(tag_cached); + return true; + + } else if (row < 3) { + return false; } else { - tag_cached.metadata[tag_keys[idx]] = value.toString(); + row -= 3; } } - backend.updateTag(tag_cached); + 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[row]]; + tag_cached.metadata.remove(tag_keys[row]); + } else { + tag_cached.metadata[tag_keys[row]] = value.toString(); + } + + if (backend) + backend->updateTag(tag_cached); return true; } @@ -130,9 +142,7 @@ void TagPropTableModel::tagChange(TagChange change, const Tag &tag) } else if (change == TagChange::DELETED) { dbg << "deleted"; - beginResetModel(); - tagIsValid = false; - endResetModel(); + showTag(Tag()); } } @@ -143,6 +153,5 @@ void TagPropTableModel::showTag(const Tag &tag) tag_cached = tag; tag_keys = tag_cached.metadata.keys(); tag_keys.sort(); - tagIsValid = true; endResetModel(); } -- cgit