File indexing completed on 2024-05-26 05:54:03

0001 /*
0002  * Copyright 2019 Patrick José Pereira <patrickjp@kde.org>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Lesser General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2.1 of the License, or (at your option) version 3, or any
0008  * later version accepted by the membership of KDE e.V. (or its
0009  * successor approved by the membership of KDE e.V.), which shall
0010  * act as a proxy defined in Section 6 of version 3 of the license.
0011  *
0012  * This library is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015  * Lesser General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU Lesser General Public
0018  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0019  */
0020 
0021 #pragma once
0022 
0023 #include <QAbstractListModel>
0024 
0025 /**
0026  * @brief ParameterModel for qml interface.
0027  *
0028  */
0029 class ParameterModel : public QAbstractListModel
0030 {
0031     Q_OBJECT
0032 
0033     Q_PROPERTY(int count READ rowCount NOTIFY countChanged)
0034 
0035 public:
0036     /**
0037      * @brief Construct a new ParameterModel.
0038      *
0039      * @param parent
0040      */
0041     ParameterModel(QObject *parent = nullptr);
0042 
0043     /**
0044      * @brief Load description file
0045      *
0046      * @param path
0047      */
0048     void loadDescriptionFile(const QString &path);
0049 
0050     /**
0051      * @brief Roles
0052      *
0053      */
0054     enum Roles {
0055         Name = Qt::UserRole, // ACRO_BAL_PITCH
0056         HumanName, // Acro Balance Pitch
0057         Description, // rate at which pitch angle returns...
0058         Index, // 0
0059         Value, // X
0060         ValidValues, // { 0: "Disabled", "0.1": "Very Low", ... }
0061         Bitmask, // True/False
0062         Units, // deg
0063         Increment, // 0.1
0064         Range, // [0, 3]
0065         UserType, // Advanced/Standard
0066     };
0067     Q_ENUM(Roles)
0068 
0069     /**
0070      * @brief Return data.
0071      *
0072      * @param index
0073      * @param role
0074      * @return QVariant
0075      */
0076     QVariant data(const QModelIndex &index, int role) const override;
0077 
0078     /**
0079      * @brief Get role names.
0080      *
0081      * @return QHash<int, QByteArray>
0082      */
0083     QHash<int, QByteArray> roleNames() const override;
0084 
0085     /**
0086      * @brief Return ParameterModel size
0087      *
0088      * @param parent
0089      * @return int
0090      */
0091     int rowCount(const QModelIndex &parent = QModelIndex()) const override
0092     {
0093         Q_UNUSED(parent);
0094         return m_vectors[ParameterModel::Roles::Name].size();
0095     };
0096 
0097 Q_SIGNALS:
0098     void countChanged();
0099 
0100     /**
0101      * @brief Update parameter
0102      *
0103      * @param parameter
0104      */
0105     void update(const QMap<ParameterModel::Roles, QVariant> &parameter);
0106 
0107 private:
0108     Q_DISABLE_COPY(ParameterModel)
0109 
0110     /**
0111      * @brief Do update inside ParameterModel thread.
0112      *
0113      * @param parameter
0114      */
0115     void doUpdate(const QMap<ParameterModel::Roles, QVariant> &parameter);
0116 
0117     void initializeRoleNames();
0118 
0119     QHash<int, QByteArray> m_roleNames;
0120     QHash<int, QVector<QVariant>> m_vectors;
0121 };