summaryrefslogtreecommitdiff
path: root/taglistmodel.cpp
blob: 9cf4fb76f0b13ec28a8f8c94558bc40105c2e0b8 (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
#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(1).toInt() < res_b.captured(1).toInt())
        return true;

    if (res_a.captured(1) != res_b.captured(1))
        return false;

    if (res_a.captured(2) < res_b.captured(2))
        return true;

    if (res_a.captured(2) != res_b.captured(2))
        return false;

    if (res_a.captured(3).isEmpty() && !res_b.captured(3).isEmpty())
        return true;

    if (res_a.captured(3).toInt() < res_b.captured(3).toInt())
        return true;

    return false;
}

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