File indexing completed on 2024-06-02 05:26:59

0001 /*
0002     Copyright (c) 2018 Christian Mollekopf <mollekopf@kolabsys.com>
0003 
0004     This library is free software; you can redistribute it and/or modify it
0005     under the terms of the GNU Library General Public License as published by
0006     the Free Software Foundation; either version 2 of the License, or (at your
0007     option) any later version.
0008 
0009     This library is distributed in the hope that it will be useful, but WITHOUT
0010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
0012     License for more details.
0013 
0014     You should have received a copy of the GNU Library General Public License
0015     along with this library; see the file COPYING.LIB.  If not, write to the
0016     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0017     02110-1301, USA.
0018 */
0019 
0020 #pragma once
0021 #include "kube_export.h"
0022 #include <QSharedPointer>
0023 #include <QSortFilterProxyModel>
0024 #include <QSet>
0025 #include <QByteArray>
0026 
0027 namespace Sink {
0028     class Query;
0029 }
0030 
0031 class KUBE_EXPORT EntityModel : public QSortFilterProxyModel
0032 {
0033     Q_OBJECT
0034 
0035     Q_PROPERTY (QString accountId READ accountId WRITE setAccountId)
0036     Q_PROPERTY (QString resourceId READ resourceId WRITE setResourceId)
0037     Q_PROPERTY (QString entityId READ entityId WRITE setEntityId)
0038     Q_PROPERTY (QString type READ type WRITE setType)
0039     Q_PROPERTY (QStringList roles READ roles WRITE setRoles)
0040     Q_PROPERTY (QString sortRole READ sortRole WRITE setSortRole)
0041     Q_PROPERTY (QVariantMap filter READ filter WRITE setFilter)
0042 
0043 public:
0044     enum Status {
0045         NoStatus,
0046         InProgressStatus,
0047         ErrorStatus,
0048         SuccessStatus,
0049     };
0050     Q_ENUMS(Status)
0051     EntityModel(QObject *parent = Q_NULLPTR);
0052     virtual ~EntityModel();
0053 
0054     virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0055     bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
0056 
0057     virtual QHash<int, QByteArray> roleNames() const override;
0058 
0059     void setAccountId(const QString &);
0060     QString accountId() const;
0061 
0062     void setResourceId(const QString &);
0063     QString resourceId() const;
0064 
0065     void setEntityId(const QString &);
0066     QString entityId() const;
0067 
0068     void setType(const QString &);
0069     QString type() const;
0070 
0071     void setRoles(const QStringList &);
0072     QStringList roles() const;
0073 
0074     void setFilter(const QVariantMap &);
0075     QVariantMap filter() const;
0076 
0077     void setSortRole(const QString &);
0078     QString sortRole() const;
0079 
0080     Q_INVOKABLE QVariantMap data(int row) const;
0081 
0082     Q_INVOKABLE int findIndex(const QByteArray &property, const QVariant &value) const;
0083 
0084 signals:
0085     void initialItemsLoaded();
0086 
0087 protected:
0088     bool lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const override;
0089 
0090     virtual void updateQuery();
0091     QHash<int, QByteArray> mRoleNames;
0092 
0093 private:
0094     void runQuery(const Sink::Query &query);
0095     QSharedPointer<QAbstractItemModel> mModel;
0096     QHash<QByteArray, int> mRoles;
0097     QString mAccountId;
0098     QString mResourceId;
0099     QString mEntityId;
0100     QString mType;
0101     QString mSortRole;
0102     QVariantMap mFilter;
0103 };
0104 
0105 class KUBE_EXPORT EntityLoader : public EntityModel {
0106     Q_OBJECT
0107 public:
0108     EntityLoader(QObject *parent = Q_NULLPTR);
0109     virtual ~EntityLoader();
0110 protected:
0111     void updateQuery() override;
0112 };
0113 
0114 class KUBE_EXPORT CheckedEntities : public QObject {
0115     Q_OBJECT
0116     Q_PROPERTY (QSet<QByteArray> checkedEntities READ checkedEntities NOTIFY checkedEntitiesChanged)
0117 public:
0118     bool contains(const QByteArray &) const;
0119     void insert(const QByteArray &);
0120     void remove(const QByteArray &);
0121     QSet<QByteArray> checkedEntities() const;
0122 
0123 signals:
0124     void checkedEntitiesChanged();
0125 
0126 private:
0127     QSet<QByteArray> mCheckedEntities;
0128 };
0129 
0130 class KUBE_EXPORT CheckableEntityModel : public EntityModel {
0131 
0132     Q_OBJECT
0133     Q_PROPERTY (CheckedEntities* checkedEntities READ checkedEntities WRITE setCheckedEntities CONSTANT)
0134 public:
0135     CheckableEntityModel(QObject *parent = Q_NULLPTR);
0136     virtual ~CheckableEntityModel();
0137 
0138     QHash<int, QByteArray> roleNames() const override;
0139     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0140     bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
0141 
0142     CheckedEntities *checkedEntities() const;
0143     void setCheckedEntities(CheckedEntities *);
0144 
0145 private:
0146     CheckedEntities *mCheckedEntities = nullptr;
0147 };