summaryrefslogtreecommitdiff
path: root/tagproptablemodel.cpp
blob: e015987e9cb2ea325efad8d2a012e8a8b47d8c6c (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "tagproptablemodel.h"


TagPropTableModel::TagPropTableModel(SQLiteSaveFile *backend, bool showBaseData)
    : backend(backend)
    , showBaseData(showBaseData)
{
    if (backend) {
        connect(backend, &SQLiteSaveFile::tagChange, this, &TagPropTableModel::tagChange);
        connect(backend, &SQLiteSaveFile::fileReload, [=]() {
            showTag(Tag());
        });
    }
}

int TagPropTableModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    if (!tag_cached.isValid())
        return 0;

    if (showBaseData)
        return 3 + tag_cached.metadata.size();
    else
        return tag_cached.metadata.size();
}

int TagPropTableModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return 2;
}

QVariant TagPropTableModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    if (role != Qt::DisplayRole && role != Qt::EditRole)
        return QVariant();

    int row = index.row();
    bool label = index.column() == 0;
    if (showBaseData) {

        switch (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")         : QString("%1, %2").arg(tag_cached.anchor.x()).arg(tag_cached.anchor.y()); break;
        }

        row -= 3;
    }

    return label ? tag_keys[row] : tag_cached.metadata[tag_keys[row]];
}

QVariant TagPropTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (orientation != Qt::Horizontal)
        return QVariant();

    if (role != Qt::DisplayRole)
        return QVariant();

    if (section == 0)
        return "Property";
    else
        return "Value";
}

Qt::ItemFlags TagPropTableModel::flags(const QModelIndex &index) const
{
    if (index.row() == 0)
        return Qt::NoItemFlags;

    if (index.column() == 0) {
        if (showBaseData && index.row() < 3)
            return Qt::ItemIsEnabled;

        return Qt::ItemIsEnabled | Qt::ItemIsEditable;
    }

    if (showBaseData) {
        if (index.row() == 1)
            return Qt::ItemIsEnabled | Qt::ItemIsEditable;

        if (index.row() == 2) /* anchor */
            return Qt::ItemIsEnabled;
    }

    return Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsEnabled;
}

bool TagPropTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (!index.isValid())
        return false;

    if (role != Qt::EditRole)
        return false;

    int row = index.row();
    if (showBaseData) {
        if (row == 1) {
            tag_cached.name = value.toString();

            if (backend)
                backend->updateTag(tag_cached);
            return true;

        } else if (row < 3) {
            return false;
        } else {
            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[row]];
        tag_cached.metadata.remove(tag_keys[row]);
    } else {
        tag_cached.metadata[tag_keys[row]] = value.toString();
    }

    if (backend)
        backend->updateTag(tag_cached);
    return true;
}

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";
        showTag(Tag());
    }
}


void TagPropTableModel::showTag(const Tag &tag)
{
    beginResetModel();
    tag_cached = tag;
    tag_keys = tag_cached.metadata.keys();
    tag_keys.sort();
    endResetModel();
}