aboutsummaryrefslogtreecommitdiffstats
path: root/src/testmodel.cpp
blob: ef28459612c37c5c156e0a8ebffd548dd4dada22 (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
/****************************************************************************
**
** Copyright (C) 2017 Crimson AS <info@crimson.no>
** Contact: https://www.qt.io/licensing/
**
** This file is part of the qmlbench tool.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "testmodel.h"

#include <QDebug>
#include <cmath>

TestModel::TestModel()
    : m_rowNumber(0)
{
    m_roleNames[StringRole] = "stringRole";
    m_roleNames[ByteArrayRole] = "byteArrayRole";
    m_roleNames[BoolRole] = "boolRole";
    m_roleNames[IntRole] = "intRole";
    m_roleNames[RealRole] = "realRole";
    m_roleNames[UrlRole] = "urlRole";
    m_roleNames[JsValueRole] = "jsValueRole";
    m_roleNames[DateTimeRole] = "dateTimeRole";
    m_roleNames[ColorRole] = "colorRole";
    m_roleNames[SizeRole] = "sizeRole";
    m_roleNames[PointRole] = "pointRole";
    m_roleNames[RectRole] = "rectRole";
}

TestModel::~TestModel()
{

}

int TestModel::rowNumber() const
{
    return m_rowNumber;
}

void TestModel::setRowNumber(int rowNumber)
{
    if (rowNumber == m_rowNumber)
        return;

    beginResetModel();
    m_rowNumber = rowNumber;

    // Only create data we don't already have.
    for (int i = m_strings.size(); i < m_rowNumber; ++i) {
        m_strings.append(QString("Test string %1").arg(i));
        m_byteArrays.append(QByteArray("Test ByteArray ") + QByteArray::number(i));
        m_bools.append(i % 2 == 0 ? true : false);
        m_ints.append(i);
        m_reals.append(i + 0.5);
        m_urls.append(QUrl(QString("http://example.org/some-thing-here/%1").arg(i)));
        m_jsValues.append(QJSValue(i)); // TODO: should we test object types (like strings?)
        m_dateTimes.append(QDateTime(QDate(1995, 5, 20)).addSecs(i));
        m_colors.append(QColor::fromHslF(std::fmod(0.57 * i, 1.0), 0.5, 0.5));
        m_sizes.append(QSize(i, i));
        m_points.append(QPoint(i, i));
        m_rects.append(QRect(0, 0, i, i));
    }

    emit rowNumberChanged();
    endResetModel();
}

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

int TestModel::rowCount(const QModelIndex&) const
{
    return m_rowNumber;
}

QVariant TestModel::data(const QModelIndex &index, int role) const
{
    switch (role) {
    case StringRole:
        return m_strings.at(index.row());
        break;
    case ByteArrayRole:
        return m_byteArrays.at(index.row());
        break;
    case BoolRole:
        return m_bools.at(index.row());
        break;
    case IntRole:
        return m_ints.at(index.row());
        break;
    case RealRole:
        return m_reals.at(index.row());
        break;
    case UrlRole:
        return m_urls.at(index.row());
        break;
    case JsValueRole:
        return QVariant::fromValue(m_jsValues.at(index.row()));
        break;
    case DateTimeRole:
        return m_dateTimes.at(index.row());
        break;
    case ColorRole:
        return m_colors.at(index.row());
        break;
    case SizeRole:
        return m_sizes.at(index.row());
        break;
    case PointRole:
        return m_points.at(index.row());
        break;
    case RectRole:
        return m_rects.at(index.row());
        break;
    }

    return QVariant();
}