#include "taglistmodel.h" TagListModel::TagListModel(SQLiteSaveFile &backend) : backend(backend) , cached_tags(backend.getAllTags()) { connect(&backend, &SQLiteSaveFile::tagChange, [=](TagChange change, const Tag &tag) { Q_UNUSED(change); Q_UNUSED(tag); reloadTags(); }); connect(&backend, &SQLiteSaveFile::fileReload, [=]() { reloadTags(); }); } int TagListModel::rowCount(const QModelIndex &parent) const { Q_UNUSED(parent); return cached_tags.size(); } void TagListModel::reloadTags() { beginResetModel(); cached_tags = backend.getAllTags(); endResetModel(); } QVariant TagListModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); if (role != Qt::DisplayRole) return QVariant(); return cached_tags.at(index.row()).name; } QVariant TagListModel::headerData(int section, Qt::Orientation orientation, int role) const { assert(section == 0); assert(orientation == Qt::Horizontal); if (role != Qt::DisplayRole) return QVariant(); return QString("Tag"); } Qt::ItemFlags TagListModel::flags(const QModelIndex &index) const { Q_UNUSED(index); /* TODO: Add drag&drop from tag list to graphics view */ return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable; } bool TagListModel::setData(const QModelIndex &index, const QVariant &value, int role) { if (!index.isValid()) return false; if (role != Qt::EditRole) return false; Tag t = cached_tags.at(index.row()); t.name = value.toString(); backend.updateTag(t); return true; } Tag TagListModel::getTag(const QModelIndex &index) const { if (!index.isValid()) return Tag(); return cached_tags.at(index.row()); }