| 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 |
| 21 | * \inmodule KItemModels |
| 22 | * \brief Creates a model of entries from N to M with rows at a given interval. |
| 23 | * |
| 24 | * The model contains two roles: |
| 25 | * \list |
| 26 | * \li display - the number represented as a string |
| 27 | * \li value - the actual value as a number |
| 28 | * \endlist |
| 29 | * \since 5.65 |
| 30 | */ |
| 31 | class KITEMMODELS_EXPORT KNumberModel : public QAbstractListModel |
| 32 | { |
| 33 | Q_OBJECT |
| 34 | |
| 35 | /*! |
| 36 | * \property KNumberModel::minimumValue |
| 37 | * |
| 38 | * The minimum value for the model |
| 39 | * |
| 40 | * The default value is \c 1.0. |
| 41 | */ |
| 42 | Q_PROPERTY(qreal minimumValue READ minimumValue WRITE setMinimumValue NOTIFY minimumValueChanged) |
| 43 | /*! |
| 44 | * \property KNumberModel::maximumValue |
| 45 | * |
| 46 | * The maximum value for the model |
| 47 | * |
| 48 | * The default value is \c 1.0. |
| 49 | * |
| 50 | * \note If \c maximumValue is a multiple of \c stepSize added to \c minimumValue |
| 51 | * it will be included. Otherwise it will not be reached. |
| 52 | * 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. |
| 53 | */ |
| 54 | Q_PROPERTY(qreal maximumValue READ maximumValue WRITE setMaximumValue NOTIFY maximumValueChanged) |
| 55 | /*! |
| 56 | * \property KNumberModel::stepSize |
| 57 | * |
| 58 | * Step between listed entries |
| 59 | * |
| 60 | * The default value is \c 1.0. |
| 61 | */ |
| 62 | Q_PROPERTY(qreal stepSize READ stepSize WRITE setStepSize NOTIFY stepSizeChanged) |
| 63 | /*! |
| 64 | * \property KNumberModel::formattingOptions |
| 65 | * |
| 66 | * Defines the string representation of the number, |
| 67 | * e.g. "1,000" or "1000". |
| 68 | * |
| 69 | * Default is \c QLocale::Default. |
| 70 | */ |
| 71 | Q_PROPERTY(QLocale::NumberOptions formattingOptions READ formattingOptions WRITE setFormattingOptions NOTIFY formattingOptionsChanged) |
| 72 | |
| 73 | public: |
| 74 | /*! |
| 75 | * |
| 76 | */ |
| 77 | explicit KNumberModel(QObject *parent = nullptr); |
| 78 | ~KNumberModel() override; |
| 79 | |
| 80 | /*! |
| 81 | * \value DisplayRole |
| 82 | * \value ValueRole |
| 83 | */ |
| 84 | enum Roles { |
| 85 | DisplayRole = Qt::DisplayRole, |
| 86 | ValueRole = Qt::UserRole, |
| 87 | }; |
| 88 | |
| 89 | void setMinimumValue(qreal minimumValue); |
| 90 | qreal minimumValue() const; |
| 91 | |
| 92 | void setMaximumValue(qreal maximumValue); |
| 93 | qreal maximumValue() const; |
| 94 | |
| 95 | void setStepSize(qreal stepSize); |
| 96 | qreal stepSize() const; |
| 97 | |
| 98 | void setFormattingOptions(QLocale::NumberOptions options); |
| 99 | QLocale::NumberOptions formattingOptions() const; |
| 100 | |
| 101 | /*! |
| 102 | * Returns the value represented at the given index. |
| 103 | */ |
| 104 | qreal value(const QModelIndex &index) const; |
| 105 | |
| 106 | int rowCount(const QModelIndex &index = QModelIndex()) const override; |
| 107 | QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; |
| 108 | QHash<int, QByteArray> roleNames() const override; |
| 109 | |
| 110 | Q_SIGNALS: |
| 111 | void minimumValueChanged(); |
| 112 | void maximumValueChanged(); |
| 113 | void stepSizeChanged(); |
| 114 | void formattingOptionsChanged(); |
| 115 | |
| 116 | private: |
| 117 | std::unique_ptr<KNumberModelPrivate> const d; |
| 118 | }; |
| 119 | |
| 120 | #endif |
| 121 | |