Changeset View
Changeset View
Standalone View
Standalone View
src/core/knumbermodel.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2018 David Edmundson <davidedmundson@kde.org> | ||||
3 | * | ||||
4 | * This library is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU Library General Public | ||||
6 | * License as published by the Free Software Foundation; either | ||||
7 | * version 2 of the License, or (at your option) any later version. | ||||
8 | * | ||||
9 | * This library is distributed in the hope that it will be useful, | ||||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
12 | * Library General Public License for more details. | ||||
13 | * | ||||
14 | * You should have received a copy of the GNU Library General Public License | ||||
15 | * along with this library; see the file COPYING.LIB. If not, write to | ||||
16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
17 | * Boston, MA 02110-1301, USA. | ||||
18 | */ | ||||
19 | | ||||
20 | #include "knumbermodel.h" | ||||
21 | | ||||
22 | #include <QLocale> | ||||
23 | #include <QtMath> | ||||
24 | | ||||
25 | #include <cmath> | ||||
26 | | ||||
27 | class KNumberModelPrivate | ||||
28 | { | ||||
29 | public: | ||||
30 | qreal minimumValue = 0.0; | ||||
31 | qreal maximumValue = 0.0; | ||||
32 | qreal stepSize = 1.0; | ||||
33 | QLocale::NumberOptions formattingOptions = QLocale::DefaultNumberOptions; | ||||
34 | }; | ||||
35 | | ||||
36 | KNumberModel::KNumberModel(QObject *parent): | ||||
37 | QAbstractListModel(parent), | ||||
38 | d(new KNumberModelPrivate) | ||||
39 | {} | ||||
40 | | ||||
41 | KNumberModel::~KNumberModel() | ||||
42 | {} | ||||
43 | | ||||
44 | void KNumberModel::setMinimumValue(qreal minimumValue) | ||||
45 | { | ||||
46 | if (minimumValue == d->minimumValue) { | ||||
47 | return; | ||||
48 | } | ||||
49 | beginResetModel(); | ||||
50 | d->minimumValue = minimumValue; | ||||
51 | endResetModel(); | ||||
52 | emit minimumValueChanged(); | ||||
53 | } | ||||
54 | | ||||
55 | qreal KNumberModel::minimumValue() const | ||||
56 | { | ||||
57 | return d->minimumValue; | ||||
58 | } | ||||
59 | | ||||
60 | void KNumberModel::setMaximumValue(qreal maximumValue) | ||||
61 | { | ||||
62 | if (maximumValue == d->maximumValue) { | ||||
63 | return; | ||||
64 | } | ||||
65 | beginResetModel(); | ||||
66 | d->maximumValue = maximumValue; | ||||
67 | endResetModel(); | ||||
68 | emit maximumValueChanged(); | ||||
69 | } | ||||
70 | | ||||
71 | qreal KNumberModel::maximumValue() const | ||||
72 | { | ||||
73 | return d->maximumValue; | ||||
74 | } | ||||
75 | | ||||
76 | void KNumberModel::setStepSize(qreal stepSize) | ||||
77 | { | ||||
78 | Q_ASSERT(stepSize != 0); | ||||
79 | if (stepSize == d->stepSize) { | ||||
80 | return; | ||||
81 | } | ||||
82 | beginResetModel(); | ||||
83 | d->stepSize = stepSize; | ||||
84 | endResetModel(); | ||||
85 | emit stepSizeChanged(); | ||||
86 | } | ||||
87 | | ||||
88 | qreal KNumberModel::stepSize() const | ||||
89 | { | ||||
90 | return d->stepSize; | ||||
91 | } | ||||
92 | | ||||
93 | void KNumberModel::setFormattingOptions(QLocale::NumberOptions formattingOptions) | ||||
94 | { | ||||
95 | if (d->formattingOptions == formattingOptions) { | ||||
96 | return; | ||||
97 | } | ||||
98 | d->formattingOptions = formattingOptions; | ||||
99 | | ||||
100 | if (rowCount() == 0) { | ||||
101 | return; | ||||
102 | } | ||||
103 | dataChanged(index(0, 0, QModelIndex()), index(rowCount(), 0, QModelIndex()), QVector<int>{DisplayRole}); | ||||
104 | emit formattingOptionsChanged(); | ||||
105 | } | ||||
106 | | ||||
107 | QLocale::NumberOptions KNumberModel::formattingOptions() const | ||||
108 | { | ||||
109 | return d->formattingOptions; | ||||
110 | } | ||||
111 | | ||||
112 | qreal KNumberModel::value(const QModelIndex &index) const | ||||
113 | { | ||||
114 | if (!index.isValid()) { | ||||
115 | return 0.0; | ||||
116 | } | ||||
117 | return d->minimumValue + d->stepSize * index.row(); | ||||
118 | } | ||||
119 | | ||||
120 | int KNumberModel::rowCount(const QModelIndex &index) const | ||||
121 | { | ||||
122 | if (index.parent().isValid()) { | ||||
123 | return 0; | ||||
124 | } | ||||
125 | //1 initial entry (the minimumValue) + the number of valid steps afterwards | ||||
126 | return 1 + std::max(0, qFloor((d->maximumValue - d->minimumValue) / d->stepSize)); | ||||
127 | } | ||||
128 | | ||||
129 | QVariant KNumberModel::data(const QModelIndex &index, int role) const | ||||
130 | { | ||||
131 | switch(role) { | ||||
132 | case KNumberModel::DisplayRole: { | ||||
133 | auto locale = QLocale::system(); | ||||
134 | locale.setNumberOptions(d->formattingOptions); | ||||
135 | return QVariant(locale.toString(value(index))); | ||||
136 | } | ||||
137 | case KNumberModel::ValueRole: | ||||
138 | return QVariant(value(index)); | ||||
139 | } | ||||
140 | return QVariant(); | ||||
141 | } | ||||
142 | | ||||
143 | QHash<int, QByteArray> KNumberModel::roleNames() const | ||||
144 | { | ||||
145 | return {{KNumberModel::DisplayRole, QByteArrayLiteral("display")}, | ||||
146 | {KNumberModel::ValueRole, QByteArrayLiteral("value")}}; | ||||
147 | } |