1 | /* |
2 | SPDX-FileCopyrightText: 2018 David Edmundson <davidedmundson@kde.org> |
3 | |
4 | SPDX-License-Identifier: LGPL-2.0-or-later |
5 | */ |
6 | |
7 | #ifndef KNUMBERMODEL_H |
8 | #define KNUMBERMODEL_H |
9 | |
10 | #include <QAbstractListModel> |
11 | #include <QLocale> |
12 | |
13 | #include "kitemmodels_export.h" |
14 | |
15 | #include <memory> |
16 | |
17 | class KNumberModelPrivate; |
18 | |
19 | /** |
20 | * @class KNumberModel knumbermodel.h KNumberModel |
21 | * |
22 | * Creates a model of entries from N to M with rows at a given interval |
23 | * |
24 | * The model contains two roles: |
25 | * @li display - the number represented as a string |
26 | * @li value - the actual value as a number |
27 | * |
28 | * @since 5.65 |
29 | */ |
30 | class KITEMMODELS_EXPORT KNumberModel : public QAbstractListModel |
31 | { |
32 | Q_OBJECT |
33 | |
34 | /** |
35 | * The minimum value for the model |
36 | * |
37 | * The default value is @c 1.0. |
38 | */ |
39 | Q_PROPERTY(qreal minimumValue READ minimumValue WRITE setMinimumValue NOTIFY minimumValueChanged) |
40 | /** |
41 | * The maximum value for the model |
42 | * |
43 | * The default value is @c 1.0. |
44 | * |
45 | * @note If @c maximumValue is a multiple of @c stepSize added to @c minimumValue |
46 | * it will be included. Otherwise it will not be reached. |
47 | * E.g. in a model with a @c minimumValue of 0.0, a @c maximumValue of 1.0 and a @c stepSize of 0.3, the final row will be 0.9. |
48 | */ |
49 | Q_PROPERTY(qreal maximumValue READ maximumValue WRITE setMaximumValue NOTIFY maximumValueChanged) |
50 | /** |
51 | * Step between listed entries |
52 | * |
53 | * The default value is @c 1.0. |
54 | */ |
55 | Q_PROPERTY(qreal stepSize READ stepSize WRITE setStepSize NOTIFY stepSizeChanged) |
56 | /** |
57 | * Defines the string representation of the number, |
58 | * e.g. "1,000" or "1000". |
59 | * |
60 | * Default is @c QLocale::Default. |
61 | */ |
62 | Q_PROPERTY(QLocale::NumberOptions formattingOptions READ formattingOptions WRITE setFormattingOptions NOTIFY formattingOptionsChanged) |
63 | |
64 | public: |
65 | explicit KNumberModel(QObject *parent = nullptr); |
66 | ~KNumberModel() override; |
67 | |
68 | enum Roles { |
69 | DisplayRole = Qt::DisplayRole, |
70 | ValueRole = Qt::UserRole, |
71 | }; |
72 | |
73 | void setMinimumValue(qreal minimumValue); |
74 | qreal minimumValue() const; |
75 | |
76 | void setMaximumValue(qreal maximumValue); |
77 | qreal maximumValue() const; |
78 | |
79 | void setStepSize(qreal stepSize); |
80 | qreal stepSize() const; |
81 | |
82 | void setFormattingOptions(QLocale::NumberOptions options); |
83 | QLocale::NumberOptions formattingOptions() const; |
84 | |
85 | /** |
86 | * Returns the value represented at the given index. |
87 | */ |
88 | qreal value(const QModelIndex &index) const; |
89 | |
90 | int rowCount(const QModelIndex &index = QModelIndex()) const override; |
91 | QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; |
92 | QHash<int, QByteArray> roleNames() const override; |
93 | |
94 | Q_SIGNALS: |
95 | void minimumValueChanged(); |
96 | void maximumValueChanged(); |
97 | void stepSizeChanged(); |
98 | void formattingOptionsChanged(); |
99 | |
100 | private: |
101 | std::unique_ptr<KNumberModelPrivate> const d; |
102 | }; |
103 | |
104 | #endif |
105 | |