summaryrefslogtreecommitdiff
path: root/taglistmodel.cpp
blob: e3ba006167622ea26e24f8833654a0f00eb0ec28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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());
}