QTfrontend/model/playerslistmodel.cpp
author unc0rr
Sun, 07 Oct 2012 23:33:09 +0400
changeset 7727 bd252cacabe8
parent 7725 4ad05a478c6c
child 7728 664738870325
permissions -rw-r--r--
Bring icons back
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
     1
#include <QModelIndexList>
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
     2
#include <QModelIndex>
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
     3
#include <QPainter>
7727
bd252cacabe8 Bring icons back
unc0rr
parents: 7725
diff changeset
     4
#include <QDebug>
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
     5
7723
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
     6
#include "playerslistmodel.h"
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
     7
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
     8
PlayersListModel::PlayersListModel(QObject *parent) :
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
     9
    QAbstractListModel(parent)
7723
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
    10
{
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
    11
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
    12
}
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
    13
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    14
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    15
int PlayersListModel::rowCount(const QModelIndex &parent) const
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    16
{
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    17
    if(parent.isValid())
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    18
        return 0;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    19
    else
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    20
        return m_data.size();
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    21
}
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    22
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    23
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    24
QVariant PlayersListModel::data(const QModelIndex &index, int role) const
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    25
{
7727
bd252cacabe8 Bring icons back
unc0rr
parents: 7725
diff changeset
    26
    if(!index.isValid() || index.row() < 0 || index.row() >= rowCount() || index.column() != 0)
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    27
        return QVariant(QVariant::Invalid);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    28
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    29
    return m_data.at(index.row()).value(role);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    30
}
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    31
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    32
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    33
bool PlayersListModel::setData(const QModelIndex &index, const QVariant &value, int role)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    34
{
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    35
    if(!index.isValid() || index.row() < 0 || index.row() >= rowCount() || index.column() != 0)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    36
        return false;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    37
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    38
    m_data[index.row()].insert(role, value);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    39
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    40
    emit dataChanged(index, index);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    41
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    42
    return true;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    43
}
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    44
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    45
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    46
bool PlayersListModel::insertRow(int row, const QModelIndex &parent)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    47
{
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    48
    return insertRows(row, 1, parent);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    49
}
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    50
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    51
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    52
bool PlayersListModel::insertRows(int row, int count, const QModelIndex &parent)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    53
{
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    54
    if(parent.isValid() || row > rowCount() || row < 0 || count < 1)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    55
        return false;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    56
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    57
    beginInsertRows(parent, row, row + count - 1);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    58
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    59
    for(int i = 0; i < count; ++i)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    60
        m_data.insert(row, DataEntry());
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    61
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    62
    endInsertRows();
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    63
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    64
    return true;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    65
}
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    66
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    67
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    68
bool PlayersListModel::removeRows(int row, int count, const QModelIndex &parent)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    69
{
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    70
    if(parent.isValid() || row + count > rowCount() || row < 0 || count < 1)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    71
        return false;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    72
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    73
    beginRemoveRows(parent, row, row + count - 1);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    74
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    75
    for(int i = 0; i < count; ++i)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    76
        m_data.removeAt(row);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    77
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    78
    endRemoveRows();
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    79
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    80
    return true;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    81
}
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    82
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    83
7723
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
    84
void PlayersListModel::addPlayer(const QString & nickname)
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
    85
{
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    86
    insertRow(rowCount());
7723
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
    87
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
    88
    setData(index(rowCount() - 1), nickname);
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    89
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    90
    updateIcon(index(rowCount() - 1));
7723
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
    91
}
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    92
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    93
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    94
void PlayersListModel::removePlayer(const QString & nickname)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    95
{
7727
bd252cacabe8 Bring icons back
unc0rr
parents: 7725
diff changeset
    96
    QModelIndexList mil = match(index(0), Qt::DisplayRole, nickname);
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    97
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    98
    if(mil.size())
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    99
        removeRow(mil[0].row());
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   100
}
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   101
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   102
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   103
void PlayersListModel::setFlag(const QString &nickname, StateFlag flagType, bool isSet)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   104
{
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   105
    QModelIndexList mil = match(index(0), Qt::DisplayRole, nickname);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   106
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   107
    if(mil.size())
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   108
    {
7727
bd252cacabe8 Bring icons back
unc0rr
parents: 7725
diff changeset
   109
        setData(mil[0], isSet, flagType);
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   110
        updateIcon(mil[0]);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   111
    }
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   112
}
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   113
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   114
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   115
void PlayersListModel::updateIcon(const QModelIndex & index)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   116
{
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   117
    quint32 iconNum = 0;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   118
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   119
    QList<bool> flags;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   120
    flags
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   121
        << index.data(Ready).toBool()
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   122
        << index.data(ServerAdmin).toBool()
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   123
        << index.data(RoomAdmin).toBool()
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   124
        << index.data(Registered).toBool()
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   125
        << index.data(Friend).toBool()
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   126
        << index.data(Ignore).toBool()
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   127
        ;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   128
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   129
    for(int i = flags.size() - 1; i >= 0; --i)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   130
        if(flags[i])
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   131
            iconNum |= 1 << i;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   132
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   133
    if(m_icons().contains(iconNum))
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   134
    {
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   135
        setData(index, m_icons().value(iconNum), Qt::DecorationRole);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   136
    }
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   137
    else
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   138
    {
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   139
        QPixmap result(24, 16);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   140
        result.fill(Qt::transparent);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   141
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   142
        QPainter painter(&result);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   143
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   144
        if(index.data(Ready).toBool())
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   145
            painter.drawPixmap(0, 0, 16, 16, QPixmap(":/res/chat/lamp.png"));
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   146
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   147
        QString mainIconName(":/res/chat/");
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   148
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   149
        if(index.data(RoomAdmin).toBool())
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   150
            mainIconName += "roomadmin";
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   151
        else if(index.data(ServerAdmin).toBool())
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   152
            mainIconName += "serveradmin";
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   153
        else
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   154
            mainIconName += "hedgehog";
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   155
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   156
        if(!index.data(Registered).toBool())
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   157
            mainIconName += "_gray";
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   158
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   159
        painter.drawPixmap(8, 0, 16, 16, QPixmap(mainIconName + ".png"));
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   160
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   161
        if(index.data(Ignore).toBool())
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   162
            painter.drawPixmap(8, 0, 16, 16, QPixmap(":/res/chat/ignore.png"));
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   163
        else
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   164
        if(index.data(Friend).toBool())
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   165
            painter.drawPixmap(8, 0, 16, 16, QPixmap(":/res/chat/friend.png"));
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   166
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   167
        painter.end();
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   168
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   169
        QIcon icon(result);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   170
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   171
        setData(index, icon, Qt::DecorationRole);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   172
        m_icons().insert(iconNum, icon);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   173
    }
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   174
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   175
    if(index.data(Ignore).toBool())
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   176
        setData(index, Qt::gray, Qt::ForegroundRole);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   177
    else
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   178
    if(index.data(Friend).toBool())
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   179
        setData(index, Qt::green, Qt::ForegroundRole);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   180
    else
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   181
        setData(index, QBrush(QColor(0xff, 0xcc, 0x00)), Qt::ForegroundRole);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   182
}
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   183
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   184
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   185
QHash<quint32, QIcon> & PlayersListModel::m_icons()
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   186
{
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   187
    static QHash<quint32, QIcon> iconsCache;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   188
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   189
    return iconsCache;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   190
}