summaryrefslogtreecommitdiff
path: root/builtin/qtcpp/templates/structmodel.cpp
blob: 2759dae83d3f5871e58c551c994d560a44c09f79 (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
{# Copyright (c) Pelagicore AB 2016 #}
{% set class = 'Qml{0}Model'.format(struct) %}
/****************************************************************************
** This is an auto-generated file.
** Do not edit! All changes made to it will be lost.
****************************************************************************/

#include "{{class|lower}}.h"

{{class}}::{{class}}(QObject *parent)
    : QAbstractListModel(parent)
{
    {% for field in struct.fields %}
    m_roleNames.insert(Roles::{{field|upperfirst}}, QByteArray("{{field}}"));
    {% endfor %}
}

int {{class}}::count() const
{
    return m_data.count();
}

Qml{{struct}} {{class}}::get(int index)
{
    return m_data.value(index);
}

int {{class}}::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)
    return m_data.count();
}

QVariant {{class}}::data(const QModelIndex &index, int role) const
{
    if(index.row() < 0 || index.row() >= count()) {
        return QVariant();
    }
    const Qml{{struct}} &{{struct|lower}} = m_data.at(index.row());
    switch(role) {
    {% for field in struct.fields %}
    case Roles::{{field|upperfirst}}:
        return QVariant::fromValue({{struct|lower}}.{{field}}());
    {% endfor %}
    }
    return QVariant();
}

QHash<int, QByteArray> {{class}}::roleNames() const
{
    return m_roleNames;
}


void {{class}}::insert(int row, const Qml{{struct}} &{{struct|lower}})
{
    if (row < 0)
        row = 0;
    if (row >= m_data.count())
        row = m_data.count();

    beginInsertRows(QModelIndex(), row, row);
    m_data.insert(row, {{struct|lower}});
    endInsertRows();
    emit countChanged(count());
}

void {{class}}::reset(const QList<Qml{{struct}}> data)
{
    beginResetModel();
    m_data = data;
    endResetModel();
}

void {{class}}::append(const Qml{{struct}} &{{struct|lower}})
{
    insert(m_data.count(), {{struct|lower}});
}

void {{class}}::update(int row, const Qml{{struct}} &{{struct|lower}})
{
    if(row < 0 || row >= m_data.count()) {
        return;
    }
    m_data[row] = {{struct|lower}};
    const QModelIndex &index = createIndex(row, 0);
    emit dataChanged(index, index);
}

void {{class}}::remove(int row)
{
    if(row < 0 || row >= m_data.count()) {
        return;
    }
    beginRemoveRows(QModelIndex(), row, row);
    m_data.removeAt(row);
    endRemoveRows();
}

void {{class}}::clear()
{
    beginResetModel();
    m_data.clear();
    endResetModel();
}