KItemModels

knumbermodel.cpp
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;
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 }
38 d->minimumValue = minimumValue;
40 Q_EMIT minimumValueChanged();
41}
42
44{
45 return d->minimumValue;
46}
47
48void KNumberModel::setMaximumValue(qreal maximumValue)
49{
50 if (maximumValue == d->maximumValue) {
51 return;
52 }
54 d->maximumValue = maximumValue;
56 Q_EMIT maximumValueChanged();
57}
58
60{
61 return d->maximumValue;
62}
63
64void KNumberModel::setStepSize(qreal stepSize)
65{
66 if (stepSize == d->stepSize) {
67 return;
68 }
70 d->stepSize = stepSize;
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(index(0, 0, QModelIndex()), index(rowCount(), 0, QModelIndex()), QList<int>{DisplayRole});
91 Q_EMIT formattingOptionsChanged();
92}
93
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(0, qFloor((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(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"
qreal minimumValue
The minimum value for the model.
QLocale::NumberOptions formattingOptions
Defines the string representation of the number, e.g.
qreal value(const QModelIndex &index) const
Returns the value represented at the given index.
qreal stepSize
Step between listed entries.
qreal maximumValue
The maximum value for the model.
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList< int > &roles)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
bool isValid() const const
QModelIndex parent() const const
int row() const const
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:07:00 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.