File indexing completed on 2024-05-12 05:39:51

0001 /***************************************************************************
0002  *  Copyright (C) 2020 by Renaud Guezennec                               *
0003  *   http://www.rolisteam.org/contact                                      *
0004  *                                                                         *
0005  *   This software is free software; you can redistribute it and/or modify *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 #ifndef PARTICIPANTMODEL_H
0021 #define PARTICIPANTMODEL_H
0022 
0023 #include <QAbstractItemModel>
0024 #include <QPointer>
0025 #include <core_global.h>
0026 class Player;
0027 
0028 class CORE_EXPORT ParticipantItem
0029 {
0030 public:
0031     ParticipantItem(const QString& name);
0032     ParticipantItem(Player* player);
0033 
0034     bool isLeaf() const;
0035     QString name() const;
0036     QString uuid() const;
0037     Player* player() const;
0038 
0039     int indexOf(ParticipantItem* child);
0040     int childCount() const;
0041     ParticipantItem* childAt(int pos);
0042 
0043     void appendChild(ParticipantItem* item);
0044     void removeChild(ParticipantItem* item);
0045 
0046     ParticipantItem* parent() const;
0047     void setParent(ParticipantItem* parent);
0048 
0049 private:
0050     QString m_name;
0051     QPointer<Player> m_player;
0052     ParticipantItem* m_parent= nullptr;
0053     QList<ParticipantItem*> m_children;
0054 };
0055 
0056 class PlayerModel;
0057 class CORE_EXPORT ParticipantModel : public QAbstractItemModel
0058 {
0059     Q_OBJECT
0060 
0061 public:
0062     enum Permission
0063     {
0064         readWrite= 0,
0065         readOnly,
0066         hidden
0067     };
0068     Q_ENUM(Permission)
0069     explicit ParticipantModel(const QString& ownerId, PlayerModel* model, QObject* parent= nullptr);
0070 
0071     // Header:
0072     QVariant headerData(int section, Qt::Orientation orientation, int role= Qt::DisplayRole) const override;
0073 
0074     // Basic functionality
0075     QModelIndex index(int row, int column, const QModelIndex& parent= QModelIndex()) const override;
0076     QModelIndex parent(const QModelIndex& index) const override;
0077     int rowCount(const QModelIndex& parent= QModelIndex()) const override;
0078     int columnCount(const QModelIndex& parent= QModelIndex()) const override;
0079     Qt::ItemFlags flags(const QModelIndex& index) const override;
0080     QVariant data(const QModelIndex& index, int role= Qt::DisplayRole) const override;
0081 
0082     QString getOwner() const;
0083     void setOwner(const QString& owner);
0084 
0085     ParticipantModel::Permission permissionFor(const QModelIndex& index);
0086     ParticipantModel::Permission permissionFor(const QString& id);
0087 
0088     void saveModel(QJsonObject& root);
0089     void loadModel(QJsonObject& root);
0090 
0091 public slots:
0092     virtual void addNewPlayer(Player*);
0093     virtual void removePlayer(Player*);
0094     int promotePlayer(const QModelIndex& index);
0095     int demotePlayer(const QModelIndex& index);
0096     void setPlayerInto(const QModelIndex& index, ParticipantModel::Permission level);
0097     void setPlayerPermission(const QString& id, ParticipantModel::Permission level);
0098     void initModel();
0099 
0100 signals:
0101     void userReadPermissionChanged(QString, bool);
0102     void userWritePermissionChanged(QString, bool);
0103 
0104 private:
0105     QPointer<PlayerModel> m_playerList;
0106     std::unique_ptr<ParticipantItem> m_root;
0107     QString m_ownerId;
0108 };
0109 
0110 #endif // PARTICIPANTMODEL_H