File indexing completed on 2024-07-21 06:38:22

0001 /*
0002     SPDX-FileCopyrightText: 2018 David Edmundson <davidedmundson@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KNUMBERMODEL_H
0008 #define KNUMBERMODEL_H
0009 
0010 #include <QAbstractListModel>
0011 #include <QLocale>
0012 
0013 #include "kitemmodels_export.h"
0014 
0015 #include <memory>
0016 
0017 class KNumberModelPrivate;
0018 
0019 /**
0020  * @class KNumberModel knumbermodel.h KNumberModel
0021  *
0022  * Creates a model of entries from N to M with rows at a given interval
0023  *
0024  * The model contains two roles:
0025  * @li display - the number represented as a string
0026  * @li value - the actual value as a number
0027  *
0028  * @since 5.65
0029  */
0030 class KITEMMODELS_EXPORT KNumberModel : public QAbstractListModel
0031 {
0032     Q_OBJECT
0033 
0034     /**
0035      * The minimum value for the model
0036      *
0037      * The default value is @c 1.0.
0038      */
0039     Q_PROPERTY(qreal minimumValue READ minimumValue WRITE setMinimumValue NOTIFY minimumValueChanged)
0040     /**
0041      * The maximum value for the model
0042      *
0043      * The default value is @c 1.0.
0044      *
0045      * @note  If @c maximumValue is a multiple of @c stepSize added to @c minimumValue
0046      * it will be included. Otherwise it will not be reached.
0047      * 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.
0048      */
0049     Q_PROPERTY(qreal maximumValue READ maximumValue WRITE setMaximumValue NOTIFY maximumValueChanged)
0050     /**
0051      * Step between listed entries
0052      *
0053      * The default value is @c 1.0.
0054      */
0055     Q_PROPERTY(qreal stepSize READ stepSize WRITE setStepSize NOTIFY stepSizeChanged)
0056     /**
0057      * Defines the string representation of the number,
0058      * e.g. "1,000" or "1000".
0059      *
0060      * Default is @c QLocale::Default.
0061      */
0062     Q_PROPERTY(QLocale::NumberOptions formattingOptions READ formattingOptions WRITE setFormattingOptions NOTIFY formattingOptionsChanged)
0063 
0064 public:
0065     explicit KNumberModel(QObject *parent = nullptr);
0066     ~KNumberModel() override;
0067 
0068     enum Roles {
0069         DisplayRole = Qt::DisplayRole,
0070         ValueRole = Qt::UserRole,
0071     };
0072 
0073     void setMinimumValue(qreal minimumValue);
0074     qreal minimumValue() const;
0075 
0076     void setMaximumValue(qreal maximumValue);
0077     qreal maximumValue() const;
0078 
0079     void setStepSize(qreal stepSize);
0080     qreal stepSize() const;
0081 
0082     void setFormattingOptions(QLocale::NumberOptions options);
0083     QLocale::NumberOptions formattingOptions() const;
0084 
0085     /**
0086      * Returns the value represented at the given index.
0087      */
0088     qreal value(const QModelIndex &index) const;
0089 
0090     int rowCount(const QModelIndex &index = QModelIndex()) const override;
0091     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0092     QHash<int, QByteArray> roleNames() const override;
0093 
0094 Q_SIGNALS:
0095     void minimumValueChanged();
0096     void maximumValueChanged();
0097     void stepSizeChanged();
0098     void formattingOptionsChanged();
0099 
0100 private:
0101     std::unique_ptr<KNumberModelPrivate> const d;
0102 };
0103 
0104 #endif