aboutsummaryrefslogtreecommitdiffstats
path: root/tests/manual/tableview/abstracttablemodel/main.cpp
blob: 6b7d38c9ec84be44689bc245629bd959c6962e86 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright (C) 2018 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QAbstractTableModel>
#include <QSet>
#include <QDebug>

typedef QPair<QString, bool> CellData;

class TestTableModel : public QAbstractTableModel
{
    Q_OBJECT
    Q_PROPERTY(int rowCount READ rowCount WRITE setRowCount NOTIFY rowCountChanged)
    Q_PROPERTY(int columnCount READ columnCount WRITE setColumnCount NOTIFY columnCountChanged)

public:
    TestTableModel(QObject *parent = nullptr) : QAbstractTableModel(parent) { }

    int rowCount(const QModelIndex & = QModelIndex()) const override { return m_rows; }
    void setRowCount(int count) {
        beginResetModel();
        m_rows = count;
        rebuildModel();
        emit rowCountChanged();
        endResetModel();
    }

    int columnCount(const QModelIndex & = QModelIndex()) const override { return m_cols; }
    void setColumnCount(int count) {
        beginResetModel();
        m_cols = count;
        rebuildModel();
        emit columnCountChanged();
        endResetModel();
    }

    int indexValue(const QModelIndex &index) const
    {
        return index.row() + (index.column() * rowCount());
    }

    void rebuildModel()
    {
        m_modelData = QVector<QVector<CellData>>(m_cols);
        for (int x = 0; x < m_cols; ++x) {
            m_modelData[x] = QVector<CellData>(m_rows);
            for (int y = 0; y < m_rows; ++y)
                m_modelData[x][y] = qMakePair(QString("%1, %2").arg(x).arg(y), false);
        }
    }

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

        switch (role) {
        case Qt::DisplayRole:
            return m_modelData[index.column()][index.row()].first;
        case Qt::CheckStateRole:
            return m_modelData[index.column()][index.row()].second;
        default:
            return QVariant();
        }

        return QVariant();
    }

    bool setData(const QModelIndex &index, const QVariant &value,
                 int role = Qt::EditRole) override
    {
        switch (role) {
        case Qt::DisplayRole: {
            QString text = value.toString();
            auto &cellData = m_modelData[index.column()][index.row()];
            if (text == cellData.first)
                return false;
            cellData.first = text;
            break; }
        case Qt::CheckStateRole: {
            bool checked = value.toBool();
            auto &cellData = m_modelData[index.column()][index.row()];
            if (checked == cellData.second)
                return false;

            cellData.second = checked;
            break; }
        default:
            return QAbstractTableModel::setData(index, value, role);
        }

        emit dataChanged(index, index, {role});
        return true;
    }

    QHash<int, QByteArray> roleNames() const override
    {
        return {
            {Qt::DisplayRole, "display"},
            {Qt::CheckStateRole, "checked"}
        };
    }

    Q_INVOKABLE void insertRows(int row, int count)
    {
        insertRows(row, count, QModelIndex());
    }

    Q_INVOKABLE void removeRows(int row, int count)
    {
        removeRows(row, count, QModelIndex());
    }

    Q_INVOKABLE void insertColumns(int column, int count)
    {
        insertColumns(column, count, QModelIndex());
    }

    Q_INVOKABLE void removeColumns(int column, int count)
    {
        removeColumns(column, count, QModelIndex());
    }

    bool insertRows(int row, int count, const QModelIndex &) override
    {
        if (row > m_rows)
            return false;

        beginInsertRows(QModelIndex(), row, row + count - 1);

        m_rows += count;

        for (int y = 0; y < count; ++y) {
            for (int x = 0; x < m_cols; ++x)
                m_modelData[x].insert(row, qMakePair(QStringLiteral("added"), false));
        }

        endInsertRows();

        return true;
    }

    bool removeRows(int row, int count, const QModelIndex &) override
    {
        if (row + count > m_rows)
            count = m_rows - row;
        if (count < 1)
            return false;

        beginRemoveRows(QModelIndex(), row, row + count - 1);

        m_rows -= count;

        for (int y = 0; y < count; ++y) {
            for (int x = 0; x < m_cols; ++x)
                m_modelData[x].remove(row);
        }

        endRemoveRows();

        return true;
    }

    bool insertColumns(int column, int count, const QModelIndex &) override
    {
        if (column > m_cols)
            return false;

        beginInsertColumns(QModelIndex(), column, column + count - 1);

        m_cols += count;

        for (int x = 0; x < count; ++x) {
            const int c = column + x;
            m_modelData.insert(c, QVector<CellData>(m_rows));
            for (int y = 0; y < m_rows; ++y)
                m_modelData[c][y] = qMakePair(QStringLiteral("added"), false);
        }

        endInsertColumns();

        return true;
    }

    bool removeColumns(int column, int count, const QModelIndex &) override
    {
        if (column + count > m_cols)
            count = m_cols - column;
        if (count < 1)
            return false;

        beginRemoveColumns(QModelIndex(), column, column + count - 1);

        m_cols -= count;

        for (int x = 0; x < count; ++x)
            m_modelData.remove(column + x);

        endRemoveColumns();

        return true;
    }

    Qt::ItemFlags flags(const QModelIndex &index) const override
    {
        Q_UNUSED(index)
        return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
    }

signals:
    void rowCountChanged();
    void columnCountChanged();

private:
    int m_rows = 0;
    int m_cols = 0;

    QVector<QVector<CellData>> m_modelData;
};

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    qmlRegisterType<TestTableModel>("TestTableModel", 0, 1, "TestTableModel");

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    return app.exec();
}

#include "main.moc"