/* * SPDX-FileCopyrightText: 2020-2021 Han Young * * SPDX-License-Identifier: GPL-3.0-or-later */ #pragma once #include #include #include #include #include #include #include class UnitModel : public QAbstractListModel { Q_OBJECT QML_ELEMENT QML_SINGLETON Q_PROPERTY(QStringList typeList READ typeList NOTIFY typeListChanged) Q_PROPERTY(QString result READ result NOTIFY resultChanged) Q_PROPERTY(QString value READ value WRITE setValue NOTIFY valueChanged) Q_PROPERTY(int currentIndex READ currentIndex WRITE setCurrentIndex NOTIFY currentIndexChanged) Q_PROPERTY(int fromUnitIndex READ fromUnitIndex WRITE setFromUnitIndex NOTIFY unitIndexChanged) Q_PROPERTY(int toUnitIndex READ toUnitIndex WRITE setToUnitIndex NOTIFY unitIndexChanged) public: explicit UnitModel(QObject *parent = nullptr); ~UnitModel(); int rowCount(const QModelIndex &parent) const override; QVariant data(const QModelIndex &index, int role) const override; QHash roleNames() const override; const QString &value() const { return m_value; } void setValue(QString value); const QString &result() const { return m_result; } int currentIndex() const { return m_currentIndex; } int fromUnitIndex() const { return m_fromUnitIndex; } int toUnitIndex() const { return m_toUnitIndex; } const QStringList &typeList() const { return m_units; } void setCurrentIndex(int i); void setFromUnitIndex(int i); void setToUnitIndex(int i); Q_SIGNALS: void typeListChanged(); void valueChanged(); void resultChanged(); void currentIndexChanged(); void unitIndexChanged(); private Q_SLOTS: void calculateResult(); private: int m_currentIndex = 0; int m_fromUnitIndex = 0; int m_toUnitIndex = 1; QString m_value, m_result; QStringList m_units; std::vector m_unitIDs; static const std::vector> categoryAndEnum; };