Basic model/view action works

This commit is contained in:
jaseg 2020-08-16 17:04:32 +02:00
parent 752f270cf8
commit 2deadc6cfb
15 changed files with 439 additions and 133 deletions

View file

@ -1,26 +1,24 @@
#include "tagproptablemodel.h"
TagPropTableModel::TagPropTableModel(SQLiteSaveFile &backend, const Tag tag)
: backend(backend)
, tag_cached(tag)
, tagIsValid(true)
: TagPropTableModel(backend, true)
{
tag_cached = tag;
tag_keys = tag_cached.metadata.keys();
tag_keys.sort();
connect(&backend, &SQLiteSaveFile::tagChange,
this, &TagPropTableModel::tagChange);
connect(&backend, &SQLiteSaveFile::fileReload,
[=]() {
beginResetModel();
tagIsValid = false;
endResetModel();
});
qDebug() << "connecting TagPropTableModel" << &backend;
}
TagPropTableModel::TagPropTableModel(SQLiteSaveFile &backend)
TagPropTableModel::TagPropTableModel(SQLiteSaveFile &backend, bool tagIsValid)
: backend(backend)
, tagIsValid(false)
, 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
@ -42,7 +40,7 @@ QVariant TagPropTableModel::data(const QModelIndex &index, int role) const
if (!index.isValid())
return QVariant();
if (role != Qt::DisplayRole)
if (role != Qt::DisplayRole && role != Qt::EditRole)
return QVariant();
bool label = index.column() == 0;
@ -50,7 +48,7 @@ QVariant TagPropTableModel::data(const QModelIndex &index, int role) const
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") : tag_cached.anchor; 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;
@ -100,18 +98,19 @@ bool TagPropTableModel::setData(const QModelIndex &index, const QVariant &value,
if (role != Qt::EditRole)
return false;
if (index.row() == 1)
if (index.row() == 1) {
tag_cached.name = value.toString();
else if (index.row() < 3)
} else if (index.row() < 3) {
return false;
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();
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);
@ -120,13 +119,17 @@ bool TagPropTableModel::setData(const QModelIndex &index, const QVariant &value,
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();
@ -140,5 +143,6 @@ void TagPropTableModel::showTag(const Tag &tag)
tag_cached = tag;
tag_keys = tag_cached.metadata.keys();
tag_keys.sort();
tagIsValid = true;
endResetModel();
}