#include "taglistmodel.h" TagListModel::TagListModel(SQLiteSaveFile &backend) : backend(backend) , cached_tags(backend.getAllTags()) { qDebug() << "connecting TagListModel" << &backend; 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(); } bool collateTagNames(QString a, QString b) { QRegularExpression name_re("^(\\d*)(.*?)(\\d*)$"); auto res_a = name_re.match(a), res_b = name_re.match(b); if (res_a.captured(1).isEmpty() && !res_b.captured(1).isEmpty()) return true; if (res_a.captured().toInt() < res_b.captured().toInt()) return true; /* FIXME TODO */ } void TagListModel::reloadTags() { qDebug() << "TagListModel::reloadTags()"; beginResetModel(); cached_tags = backend.getAllTags(); std::sort(cached_tags.begin(), cached_tags.end(), [](const Tag &a, const Tag &b) { return collateTagNames(a.name, b.name); }); endResetModel(); } QVariant TagListModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); if (role != Qt::DisplayRole && role != Qt::EditRole) return QVariant(); auto rv = cached_tags.at(index.row()).name; return rv; } 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"); } QModelIndex TagListModel::indexOf(const Tag &t) const { return index(cached_tags.indexOf(t)); } 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()); }