qmlfrontend/players_model.h
changeset 15047 773beead236f
parent 10753 e56db5d988ef
equal deleted inserted replaced
15046:0730c68fdf97 15047:773beead236f
       
     1 #ifndef PLAYERSLISTMODEL_H
       
     2 #define PLAYERSLISTMODEL_H
       
     3 
       
     4 #include <QAbstractListModel>
       
     5 #include <QFont>
       
     6 #include <QHash>
       
     7 #include <QIcon>
       
     8 #include <QModelIndex>
       
     9 #include <QSet>
       
    10 
       
    11 class PlayersListModel : public QAbstractListModel {
       
    12   Q_OBJECT
       
    13 
       
    14  public:
       
    15   enum StateFlag {
       
    16     Ready = Qt::UserRole,
       
    17     ServerAdmin = Qt::UserRole + 1,
       
    18     RoomAdmin = Qt::UserRole + 2,
       
    19     Registered = Qt::UserRole + 3,
       
    20     Friend = Qt::UserRole + 4,
       
    21     Ignore = Qt::UserRole + 5,
       
    22     InGame = Qt::UserRole + 6,
       
    23     InRoom = Qt::UserRole + 7,
       
    24     Contributor = Qt::UserRole + 8
       
    25     // if you add a role that will affect the player icon,
       
    26     // then also add it to the flags Qlist in updateIcon()!
       
    27   };
       
    28 
       
    29   enum SpecialRoles {
       
    30     SortRole = Qt::UserRole + 100,
       
    31     RoomFilterRole = Qt::UserRole + 101
       
    32   };
       
    33 
       
    34   explicit PlayersListModel(QObject *parent = 0);
       
    35 
       
    36   int rowCount(const QModelIndex &parent = QModelIndex()) const override;
       
    37 
       
    38   QVariant data(const QModelIndex &index, int role) const override;
       
    39   bool setData(const QModelIndex &index, const QVariant &value,
       
    40                int role = Qt::DisplayRole) override;
       
    41 
       
    42   void setFlag(const QString &nickname, StateFlag flagType, bool isSet);
       
    43   bool isFlagSet(const QString &nickname, StateFlag flagType);
       
    44 
       
    45   bool insertRows(int row, int count,
       
    46                   const QModelIndex &parent = QModelIndex()) override;
       
    47   bool removeRows(int row, int count,
       
    48                   const QModelIndex &parent = QModelIndex()) override;
       
    49 
       
    50   QModelIndex nicknameIndex(const QString &nickname);
       
    51 
       
    52  public slots:
       
    53   void addPlayer(const QString &nickname, bool notify);
       
    54   void removePlayer(const QString &nickname, const QString &msg = QString());
       
    55   void playerJoinedRoom(const QString &nickname, bool notify);
       
    56   void playerLeftRoom(const QString &nickname);
       
    57   void resetRoomFlags();
       
    58   void setNickname(const QString &nickname);
       
    59 
       
    60  signals:
       
    61   void nickAdded(const QString &nick, bool notifyNick);
       
    62   void nickRemoved(const QString &nick);
       
    63   void nickAddedLobby(const QString &nick, bool notifyNick);
       
    64   void nickRemovedLobby(const QString &nick, const QString &message);
       
    65 
       
    66  private:
       
    67   QHash<quint32, QIcon> &m_icons();
       
    68   using DataEntry = QHash<int, QVariant>;
       
    69   QList<DataEntry> m_data;
       
    70   QSet<QString> m_friendsSet, m_ignoredSet;
       
    71   QString m_nickname;
       
    72   QFont m_fontInRoom;
       
    73 
       
    74   void updateIcon(const QModelIndex &index);
       
    75   void updateSortData(const QModelIndex &index);
       
    76   void loadSet(QSet<QString> &set, const QString &fileName);
       
    77   void saveSet(const QSet<QString> &set, const QString &fileName);
       
    78   void checkFriendIgnore(const QModelIndex &mi);
       
    79   bool isFriend(const QString &nickname);
       
    80   bool isIgnored(const QString &nickname);
       
    81 };
       
    82 
       
    83 #endif  // PLAYERSLISTMODEL_H