File indexing completed on 2024-05-12 17:21:07
0001 /* 0002 * SPDX-FileCopyrightText: 2020-2021 Han Young <hanyoung@protonmail.com> 0003 * 0004 * SPDX-License-Identifier: GPL-3.0-or-later 0005 */ 0006 #pragma once 0007 0008 #include <QAbstractListModel> 0009 #include <QObject> 0010 #include <kunitconversion/unit.h> 0011 #include <kunitconversion/converter.h> 0012 #include <tuple> 0013 0014 class UnitModel : public QAbstractListModel 0015 { 0016 Q_OBJECT 0017 Q_PROPERTY(QStringList typeList READ typeList NOTIFY typeListChanged) 0018 Q_PROPERTY(QString result READ result NOTIFY resultChanged) 0019 Q_PROPERTY(QString value READ value WRITE setValue NOTIFY valueChanged) 0020 Q_PROPERTY(int currentIndex READ currentIndex WRITE setCurrentIndex NOTIFY currentIndexChanged) 0021 Q_PROPERTY(int fromUnitIndex READ fromUnitIndex WRITE setFromUnitIndex NOTIFY unitIndexChanged) 0022 Q_PROPERTY(int toUnitIndex READ toUnitIndex WRITE setToUnitIndex NOTIFY unitIndexChanged) 0023 public: 0024 static UnitModel *inst() 0025 { 0026 static UnitModel singleton; 0027 return &singleton; 0028 } 0029 int rowCount(const QModelIndex &parent) const override; 0030 QVariant data(const QModelIndex &index, int role) const override; 0031 QHash<int, QByteArray> roleNames() const override; 0032 const QString &value() const 0033 { 0034 return m_value; 0035 } 0036 void setValue(QString value); 0037 const QString &result() const 0038 { 0039 return m_result; 0040 } 0041 int currentIndex() const 0042 { 0043 return m_currentIndex; 0044 } 0045 int fromUnitIndex() const 0046 { 0047 return m_fromUnitIndex; 0048 } 0049 int toUnitIndex() const 0050 { 0051 return m_toUnitIndex; 0052 } 0053 const QStringList &typeList() const 0054 { 0055 return m_units; 0056 } 0057 void setCurrentIndex(int i); 0058 void setFromUnitIndex(int i); 0059 void setToUnitIndex(int i); 0060 0061 Q_SIGNALS: 0062 void typeListChanged(); 0063 void valueChanged(); 0064 void resultChanged(); 0065 void currentIndexChanged(); 0066 void unitIndexChanged(); 0067 0068 private Q_SLOTS: 0069 void calculateResult(); 0070 0071 private: 0072 UnitModel(); 0073 int m_currentIndex = 0; 0074 int m_fromUnitIndex = 0; 0075 int m_toUnitIndex = 1; 0076 QString m_value, m_result; 0077 QStringList m_units; 0078 std::vector<KUnitConversion::UnitId> m_unitIDs; 0079 static const std::vector<std::tuple<QString, KUnitConversion::CategoryId>> categoryAndEnum; 0080 };