summaryrefslogtreecommitdiff
path: root/taglistmodel.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'taglistmodel.cpp')
-rw-r--r--taglistmodel.cpp76
1 files changed, 76 insertions, 0 deletions
diff --git a/taglistmodel.cpp b/taglistmodel.cpp
new file mode 100644
index 0000000..e3ba006
--- /dev/null
+++ b/taglistmodel.cpp
@@ -0,0 +1,76 @@
+#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());
+}