Commit 37d5a87a authored by Nicolas Fella's avatar Nicolas Fella

Use KNumberModel from KItemModels

parent 974c1aae
......@@ -15,7 +15,7 @@ include(KDECMakeSettings)
include(KDEClangFormat)
find_package(Qt5 REQUIRED COMPONENTS Gui Qml Quick)
find_package(KF5 REQUIRED COMPONENTS I18n)
find_package(KF5 REQUIRED COMPONENTS I18n ItemModels)
if (ANDROID)
# runtime dependencies are build-time dependencies on Android
......
......@@ -18,6 +18,7 @@ target_link_libraries(ktrip PRIVATE Qt5::Qml Qt5::Gui KF5::I18n KPublicTransport
if (ANDROID)
target_link_libraries(ktrip PRIVATE
KF5::Kirigami2
KF5::ItemModels
Qt5::Svg
OpenSSL::SSL
Qt5::AndroidExtras
......
set(dateandtime_SRCS
lib/timezonemodel.cpp
lib/timezonesi18n.cpp
lib/knumbermodel.cpp
lib/plugin.cpp)
add_definitions(-DTRANSLATION_DOMAIN=\"kirigami_dateandtime\")
......
......@@ -22,6 +22,7 @@ import QtQuick.Layouts 1.2
import QtQuick.Controls 2.3
import org.kde.kirigami 2.4 as Kirigami
import org.kde.kitemmodels 1.0
import org.kde.kirigamiaddons.dateandtime 0.1
......@@ -115,7 +116,7 @@ RowLayout {
id: daySelectTouchComponent
Tumbler {
delegate: tumberDelegate
model: NumberModel {
model: KNumberModel {
minimumValue: 1
maximumValue: 31
}
......@@ -141,7 +142,7 @@ RowLayout {
id: monthSelectTouchComponent
Tumbler {
delegate: tumberDelegate
model: NumberModel {
model: KNumberModel {
minimumValue: 1
maximumValue: 12
}
......@@ -166,7 +167,7 @@ RowLayout {
id: yearSelectTouchComponent
Tumbler {
delegate: tumberDelegate
model: NumberModel {
model: KNumberModel {
minimumValue: 1970
maximumValue: 2100
}
......
/*
* Copyright (C) 2018 David Edmundson <davidedmundson@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "knumbermodel.h"
#include <QLocale>
#include <QtMath>
#include <cmath>
class KNumberModelPrivate
{
public:
qreal minimumValue = 0.0;
qreal maximumValue = 0.0;
qreal stepSize = 1.0;
QLocale::NumberOptions formattingOptions = QLocale::DefaultNumberOptions;
};
KNumberModel::KNumberModel(QObject *parent)
: QAbstractListModel(parent)
, d(new KNumberModelPrivate)
{
}
KNumberModel::~KNumberModel()
{
}
void KNumberModel::setMinimumValue(qreal minimumValue)
{
if (minimumValue == d->minimumValue) {
return;
}
beginResetModel();
d->minimumValue = minimumValue;
endResetModel();
emit minimumValueChanged();
}
qreal KNumberModel::minimumValue() const
{
return d->minimumValue;
}
void KNumberModel::setMaximumValue(qreal maximumValue)
{
if (maximumValue == d->maximumValue) {
return;
}
beginResetModel();
d->maximumValue = maximumValue;
endResetModel();
emit maximumValueChanged();
}
qreal KNumberModel::maximumValue() const
{
return d->maximumValue;
}
void KNumberModel::setStepSize(qreal stepSize)
{
Q_ASSERT(stepSize != 0);
if (stepSize == d->stepSize) {
return;
}
beginResetModel();
d->stepSize = stepSize;
endResetModel();
emit stepSizeChanged();
}
qreal KNumberModel::stepSize() const
{
return d->stepSize;
}
void KNumberModel::setFormattingOptions(QLocale::NumberOptions formattingOptions)
{
if (d->formattingOptions == formattingOptions) {
return;
}
d->formattingOptions = formattingOptions;
if (rowCount() == 0) {
return;
}
dataChanged(index(0, 0, QModelIndex()), index(rowCount(), 0, QModelIndex()), QVector<int> {DisplayRole});
emit formattingOptionsChanged();
}
QLocale::NumberOptions KNumberModel::formattingOptions() const
{
return d->formattingOptions;
}
qreal KNumberModel::value(const QModelIndex &index) const
{
if (!index.isValid()) {
return 0.0;
}
return d->minimumValue + d->stepSize * index.row();
}
int KNumberModel::rowCount(const QModelIndex &index) const
{
if (index.parent().isValid()) {
return 0;
}
// 1 initial entry (the minimumValue) + the number of valid steps afterwards
return 1 + std::max(0, qFloor((d->maximumValue - d->minimumValue) / d->stepSize));
}
QVariant KNumberModel::data(const QModelIndex &index, int role) const
{
switch (role) {
case KNumberModel::DisplayRole: {
auto locale = QLocale::system();
locale.setNumberOptions(d->formattingOptions);
return QVariant(locale.toString(value(index)));
}
case KNumberModel::ValueRole:
return QVariant(value(index));
}
return QVariant();
}
QHash<int, QByteArray> KNumberModel::roleNames() const
{
return {{KNumberModel::DisplayRole, QByteArrayLiteral("display")}, {KNumberModel::ValueRole, QByteArrayLiteral("value")}};
}
/*
* Copyright (C) 2018 David Edmundson <davidedmundson@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef KNUMBERMODEL_H
#define KNUMBERMODEL_H
#include <QAbstractListModel>
#include <QLocale>
#include <QScopedPointer>
// #include "kitemmodels_export.h"
class KNumberModelPrivate;
/**
* Creates a model of entries from N to M with rows at a given interval
*
* The model contains two Roles
* display, the number represented as a string
* value, the actual value as a number
*
* @since 5.NUMBERMODEL_VERSION
*/
class KNumberModel : public QAbstractListModel
{
Q_OBJECT
Q_PROPERTY(qreal minimumValue READ minimumValue WRITE setMinimumValue NOTIFY minimumValueChanged)
Q_PROPERTY(qreal maximumValue READ maximumValue WRITE setMaximumValue NOTIFY maximumValueChanged)
Q_PROPERTY(qreal stepSize READ stepSize WRITE setStepSize NOTIFY stepSizeChanged)
Q_PROPERTY(QLocale::NumberOptions formattingOptions READ formattingOptions WRITE setFormattingOptions NOTIFY formattingOptionsChanged)
public:
KNumberModel(QObject *parent = nullptr);
~KNumberModel() override;
enum Roles { DisplayRole = Qt::DisplayRole, ValueRole = Qt::UserRole };
/**
* The minimum value for the model.
* The default value is 1.
*/
void setMinimumValue(qreal minimumValue);
qreal minimumValue() const;
/**
* The maximum value in this model.
* The default value is 1.
*
* @note If max is a multiple of @arg step it will be included. Otherwise it will not be reached
* 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.
*
*/
void setMaximumValue(qreal maximumValue);
qreal maximumValue() const;
/*
* Step between listed entries
* The default value is 1.0
*/
void setStepSize(qreal stepSize);
qreal stepSize() const;
/**
* Define how the string representation of the number should be presented
* i.e "1,000" or "1000"
* Default is QLocale::Default
*/
void setFormattingOptions(QLocale::NumberOptions options);
QLocale::NumberOptions formattingOptions() const;
/**
* Returns the value represented at the given index.
*/
qreal value(const QModelIndex &index) const;
int rowCount(const QModelIndex &index = QModelIndex()) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
QHash<int, QByteArray> roleNames() const override;
Q_SIGNALS:
void minimumValueChanged();
void maximumValueChanged();
void stepSizeChanged();
void formattingOptionsChanged();
private:
QScopedPointer<KNumberModelPrivate> d;
};
#endif
......@@ -21,7 +21,6 @@
#include <QQmlEngine>
#include <QQmlExtensionPlugin>
#include "knumbermodel.h"
#include "timezonemodel.h"
class KirigamiAddonsDataAndTimePlugin : public QQmlExtensionPlugin
......@@ -40,7 +39,6 @@ void KirigamiAddonsDataAndTimePlugin::registerTypes(const char *uri)
{
qmlRegisterType<TimeZoneModel>(uri, 0, 1, "TimeZoneModel");
qmlRegisterType<TimeZoneFilterProxy>(uri, 0, 1, "TimeZoneFilterModel");
qmlRegisterType<KNumberModel>(uri, 0, 1, "NumberModel");
}
#include "plugin.moc"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment