1/*
2 SPDX-FileCopyrightText: 2018 David Edmundson <davidedmundson@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "knumbermodel.h"
8
9#include <QtMath>
10
11#include <cmath>
12
13class KNumberModelPrivate
14{
15public:
16 qreal minimumValue = 0.0;
17 qreal maximumValue = 0.0;
18 qreal stepSize = 1.0;
19 QLocale::NumberOptions formattingOptions = QLocale::DefaultNumberOptions;
20};
21
22KNumberModel::KNumberModel(QObject *parent)
23 : QAbstractListModel(parent)
24 , d(new KNumberModelPrivate)
25{
26}
27
28KNumberModel::~KNumberModel()
29{
30}
31
32void KNumberModel::setMinimumValue(qreal minimumValue)
33{
34 if (minimumValue == d->minimumValue) {
35 return;
36 }
37 beginResetModel();
38 d->minimumValue = minimumValue;
39 endResetModel();
40 Q_EMIT minimumValueChanged();
41}
42
43qreal KNumberModel::minimumValue() const
44{
45 return d->minimumValue;
46}
47
48void KNumberModel::setMaximumValue(qreal maximumValue)
49{
50 if (maximumValue == d->maximumValue) {
51 return;
52 }
53 beginResetModel();
54 d->maximumValue = maximumValue;
55 endResetModel();
56 Q_EMIT maximumValueChanged();
57}
58
59qreal KNumberModel::maximumValue() const
60{
61 return d->maximumValue;
62}
63
64void KNumberModel::setStepSize(qreal stepSize)
65{
66 if (stepSize == d->stepSize) {
67 return;
68 }
69 beginResetModel();
70 d->stepSize = stepSize;
71 endResetModel();
72 Q_EMIT stepSizeChanged();
73}
74
75qreal KNumberModel::stepSize() const
76{
77 return d->stepSize;
78}
79
80void KNumberModel::setFormattingOptions(QLocale::NumberOptions formattingOptions)
81{
82 if (d->formattingOptions == formattingOptions) {
83 return;
84 }
85 d->formattingOptions = formattingOptions;
86
87 if (rowCount() == 0) {
88 return;
89 }
90 dataChanged(topLeft: index(row: 0, column: 0, parent: QModelIndex()), bottomRight: index(row: rowCount(), column: 0, parent: QModelIndex()), roles: QList<int>{DisplayRole});
91 Q_EMIT formattingOptionsChanged();
92}
93
94QLocale::NumberOptions KNumberModel::formattingOptions() const
95{
96 return d->formattingOptions;
97}
98
99qreal KNumberModel::value(const QModelIndex &index) const
100{
101 if (!index.isValid()) {
102 return 0.0;
103 }
104 return d->minimumValue + d->stepSize * index.row();
105}
106
107int KNumberModel::rowCount(const QModelIndex &index) const
108{
109 if (index.parent().isValid()) {
110 return 0;
111 }
112 if (d->stepSize == 0) {
113 return 1;
114 }
115 // 1 initial entry (the minimumValue) + the number of valid steps afterwards
116 return 1 + std::max(a: 0, b: qFloor(v: (d->maximumValue - d->minimumValue) / d->stepSize));
117}
118
119QVariant KNumberModel::data(const QModelIndex &index, int role) const
120{
121 switch (role) {
122 case KNumberModel::DisplayRole: {
123 auto locale = QLocale();
124 locale.setNumberOptions(d->formattingOptions);
125 return QVariant(locale.toString(f: value(index)));
126 }
127 case KNumberModel::ValueRole:
128 return QVariant(value(index));
129 }
130 return QVariant();
131}
132
133QHash<int, QByteArray> KNumberModel::roleNames() const
134{
135 return {{KNumberModel::DisplayRole, QByteArrayLiteral("display")}, {KNumberModel::ValueRole, QByteArrayLiteral("value")}};
136}
137
138#include "moc_knumbermodel.cpp"
139

source code of kitemmodels/src/core/knumbermodel.cpp