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 #include "knumbermodel.h"
0008 
0009 #include <QtMath>
0010 
0011 #include <cmath>
0012 
0013 class KNumberModelPrivate
0014 {
0015 public:
0016     qreal minimumValue = 0.0;
0017     qreal maximumValue = 0.0;
0018     qreal stepSize = 1.0;
0019     QLocale::NumberOptions formattingOptions = QLocale::DefaultNumberOptions;
0020 };
0021 
0022 KNumberModel::KNumberModel(QObject *parent)
0023     : QAbstractListModel(parent)
0024     , d(new KNumberModelPrivate)
0025 {
0026 }
0027 
0028 KNumberModel::~KNumberModel()
0029 {
0030 }
0031 
0032 void KNumberModel::setMinimumValue(qreal minimumValue)
0033 {
0034     if (minimumValue == d->minimumValue) {
0035         return;
0036     }
0037     beginResetModel();
0038     d->minimumValue = minimumValue;
0039     endResetModel();
0040     Q_EMIT minimumValueChanged();
0041 }
0042 
0043 qreal KNumberModel::minimumValue() const
0044 {
0045     return d->minimumValue;
0046 }
0047 
0048 void KNumberModel::setMaximumValue(qreal maximumValue)
0049 {
0050     if (maximumValue == d->maximumValue) {
0051         return;
0052     }
0053     beginResetModel();
0054     d->maximumValue = maximumValue;
0055     endResetModel();
0056     Q_EMIT maximumValueChanged();
0057 }
0058 
0059 qreal KNumberModel::maximumValue() const
0060 {
0061     return d->maximumValue;
0062 }
0063 
0064 void KNumberModel::setStepSize(qreal stepSize)
0065 {
0066     if (stepSize == d->stepSize) {
0067         return;
0068     }
0069     beginResetModel();
0070     d->stepSize = stepSize;
0071     endResetModel();
0072     Q_EMIT stepSizeChanged();
0073 }
0074 
0075 qreal KNumberModel::stepSize() const
0076 {
0077     return d->stepSize;
0078 }
0079 
0080 void KNumberModel::setFormattingOptions(QLocale::NumberOptions formattingOptions)
0081 {
0082     if (d->formattingOptions == formattingOptions) {
0083         return;
0084     }
0085     d->formattingOptions = formattingOptions;
0086 
0087     if (rowCount() == 0) {
0088         return;
0089     }
0090     dataChanged(index(0, 0, QModelIndex()), index(rowCount(), 0, QModelIndex()), QList<int>{DisplayRole});
0091     Q_EMIT formattingOptionsChanged();
0092 }
0093 
0094 QLocale::NumberOptions KNumberModel::formattingOptions() const
0095 {
0096     return d->formattingOptions;
0097 }
0098 
0099 qreal KNumberModel::value(const QModelIndex &index) const
0100 {
0101     if (!index.isValid()) {
0102         return 0.0;
0103     }
0104     return d->minimumValue + d->stepSize * index.row();
0105 }
0106 
0107 int KNumberModel::rowCount(const QModelIndex &index) const
0108 {
0109     if (index.parent().isValid()) {
0110         return 0;
0111     }
0112     if (d->stepSize == 0) {
0113         return 1;
0114     }
0115     // 1 initial entry (the minimumValue) + the number of valid steps afterwards
0116     return 1 + std::max(0, qFloor((d->maximumValue - d->minimumValue) / d->stepSize));
0117 }
0118 
0119 QVariant KNumberModel::data(const QModelIndex &index, int role) const
0120 {
0121     switch (role) {
0122     case KNumberModel::DisplayRole: {
0123         auto locale = QLocale();
0124         locale.setNumberOptions(d->formattingOptions);
0125         return QVariant(locale.toString(value(index)));
0126     }
0127     case KNumberModel::ValueRole:
0128         return QVariant(value(index));
0129     }
0130     return QVariant();
0131 }
0132 
0133 QHash<int, QByteArray> KNumberModel::roleNames() const
0134 {
0135     return {{KNumberModel::DisplayRole, QByteArrayLiteral("display")}, {KNumberModel::ValueRole, QByteArrayLiteral("value")}};
0136 }
0137 
0138 #include "moc_knumbermodel.cpp"