Changeset View
Changeset View
Standalone View
Standalone View
src/core/knumbermodel.h
- 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 | #ifndef KNUMBERMODEL_H | ||||
21 | #define KNUMBERMODEL_H | ||||
22 | | ||||
23 | #include <QAbstractListModel> | ||||
24 | #include <QScopedPointer> | ||||
25 | #include <QLocale> | ||||
26 | | ||||
27 | #include "kitemmodels_export.h" | ||||
28 | | ||||
29 | class KNumberModelPrivate; | ||||
30 | | ||||
31 | /** | ||||
32 | * Creates a model of entries from N to M with rows at a given interval | ||||
33 | * | ||||
34 | * The model contains two Roles | ||||
35 | * display, the number represented as a string | ||||
36 | * value, the actual value as a number | ||||
37 | * | ||||
38 | * @since 5.NUMBERMODEL_VERSION | ||||
39 | */ | ||||
40 | class KITEMMODELS_EXPORT KNumberModel : public QAbstractListModel | ||||
41 | { | ||||
42 | Q_OBJECT | ||||
43 | Q_PROPERTY (qreal minimumValue READ minimumValue WRITE setMinimumValue NOTIFY minimumValueChanged) | ||||
44 | Q_PROPERTY (qreal maximumValue READ maximumValue WRITE setMaximumValue NOTIFY maximumValueChanged) | ||||
45 | Q_PROPERTY (qreal stepSize READ stepSize WRITE setStepSize NOTIFY stepSizeChanged) | ||||
46 | Q_PROPERTY (QLocale::NumberOptions formattingOptions READ formattingOptions WRITE setFormattingOptions NOTIFY formattingOptionsChanged) | ||||
47 | | ||||
48 | public: | ||||
49 | KNumberModel(QObject *parent = nullptr); | ||||
50 | ~KNumberModel() override; | ||||
51 | | ||||
52 | enum Roles { | ||||
53 | DisplayRole = Qt::DisplayRole, | ||||
54 | ValueRole = Qt::UserRole | ||||
55 | }; | ||||
56 | | ||||
57 | /** | ||||
58 | * The minimum value for the model. | ||||
59 | * The default value is 1. | ||||
60 | */ | ||||
61 | void setMinimumValue(qreal minimumValue); | ||||
62 | qreal minimumValue() const; | ||||
63 | | ||||
64 | /** | ||||
65 | * The maximum value in this model. | ||||
66 | * The default value is 1. | ||||
67 | * | ||||
68 | * @note If max is a multiple of @arg step it will be included. Otherwise it will not be reached | ||||
69 | * i.e in a model with a min of 0 with a maximum of 1 with a step size of 0.3, the final row will be 0.9. | ||||
70 | * | ||||
71 | */ | ||||
72 | void setMaximumValue(qreal maximumValue); | ||||
73 | qreal maximumValue() const; | ||||
74 | | ||||
75 | /* | ||||
76 | * Step between listed entries | ||||
77 | * The default value is 1.0 | ||||
78 | */ | ||||
79 | void setStepSize(qreal stepSize); | ||||
80 | qreal stepSize() const; | ||||
81 | | ||||
82 | /** | ||||
83 | * Define how the string representation of the number should be presented | ||||
84 | * i.e "1,000" or "1000" | ||||
85 | * Default is QLocale::Default | ||||
86 | */ | ||||
87 | void setFormattingOptions(QLocale::NumberOptions options); | ||||
88 | QLocale::NumberOptions formattingOptions() const; | ||||
89 | | ||||
90 | /** | ||||
91 | * Returns the value represnted at the given index. | ||||
92 | */ | ||||
93 | qreal value(const QModelIndex &index) const; | ||||
94 | | ||||
95 | int rowCount(const QModelIndex &index = QModelIndex()) const override; | ||||
96 | QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; | ||||
97 | QHash<int, QByteArray> roleNames() const override; | ||||
98 | | ||||
99 | Q_SIGNALS: | ||||
100 | void minimumValueChanged(); | ||||
101 | void maximumValueChanged(); | ||||
102 | void stepSizeChanged(); | ||||
103 | void formattingOptionsChanged(); | ||||
104 | | ||||
105 | private: | ||||
106 | QScopedPointer<KNumberModelPrivate> d; | ||||
107 | }; | ||||
108 | | ||||
109 | #endif |