QTfrontend/model/playerslistmodel.cpp
author unc0rr
Sun, 07 Oct 2012 00:12:46 +0400
changeset 7725 4ad05a478c6c
parent 7723 ca05b576ec74
child 7727 bd252cacabe8
permissions -rw-r--r--
Further work on moving to players list model
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>
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
     4
7723
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
     5
#include "playerslistmodel.h"
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
     6
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
     7
PlayersListModel::PlayersListModel(QObject *parent) :
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
     8
    QAbstractListModel(parent)
7723
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
     9
{
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
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    13
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    14
int PlayersListModel::rowCount(const QModelIndex &parent) const
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    15
{
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    16
    if(parent.isValid())
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    17
        return 0;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    18
    else
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    19
        return m_data.size();
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    20
}
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
QVariant PlayersListModel::data(const QModelIndex &index, int role) const
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    24
{
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    25
    if(!index.isValid())
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    26
        return QVariant(QVariant::Invalid);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    27
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    28
    return m_data.at(index.row()).value(role);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    29
}
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
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
    33
{
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    34
    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
    35
        return false;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    36
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    37
    m_data[index.row()].insert(role, value);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    38
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    39
    emit dataChanged(index, index);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    40
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    41
    return true;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    42
}
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
bool PlayersListModel::insertRow(int row, const QModelIndex &parent)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    46
{
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    47
    return insertRows(row, 1, parent);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    48
}
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
bool PlayersListModel::insertRows(int row, int count, const QModelIndex &parent)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    52
{
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    53
    if(parent.isValid() || row > rowCount() || row < 0 || count < 1)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    54
        return false;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    55
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    56
    beginInsertRows(parent, row, row + count - 1);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    57
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    58
    for(int i = 0; i < count; ++i)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    59
        m_data.insert(row, DataEntry());
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    60
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    61
    endInsertRows();
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    62
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    63
    return true;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    64
}
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
bool PlayersListModel::removeRows(int row, int count, const QModelIndex &parent)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    68
{
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    69
    if(parent.isValid() || row + count > rowCount() || row < 0 || count < 1)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    70
        return false;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    71
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    72
    beginRemoveRows(parent, row, row + count - 1);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    73
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    74
    for(int i = 0; i < count; ++i)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    75
        m_data.removeAt(row);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    76
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    77
    endRemoveRows();
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    78
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    79
    return true;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    80
}
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
7723
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
    83
void PlayersListModel::addPlayer(const QString & nickname)
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
    84
{
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    85
    insertRow(rowCount());
7723
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
    86
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
    87
    setData(index(rowCount() - 1), nickname);
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    88
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    89
    updateIcon(index(rowCount() - 1));
7723
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
    90
}
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    91
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
void PlayersListModel::removePlayer(const QString & nickname)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    94
{
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    95
    QModelIndexList mil = match(index(0, 0), Qt::DisplayRole, nickname);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    96
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    97
    if(mil.size())
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    98
        removeRow(mil[0].row());
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    99
}
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
void PlayersListModel::setFlag(const QString &nickname, StateFlag flagType, bool isSet)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   103
{
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   104
    QModelIndexList mil = match(index(0), Qt::DisplayRole, nickname);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   105
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   106
    if(mil.size())
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   107
    {
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   108
        setData(mil[0], flagType, isSet);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   109
        updateIcon(mil[0]);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   110
    }
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
void PlayersListModel::updateIcon(const QModelIndex & index)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   115
{
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   116
    quint32 iconNum = 0;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   117
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   118
    QList<bool> flags;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   119
    flags
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   120
        << index.data(Ready).toBool()
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   121
        << index.data(ServerAdmin).toBool()
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   122
        << index.data(RoomAdmin).toBool()
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   123
        << index.data(Registered).toBool()
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   124
        << index.data(Friend).toBool()
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   125
        << index.data(Ignore).toBool()
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   126
        ;
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
    for(int i = flags.size() - 1; i >= 0; --i)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   129
        if(flags[i])
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   130
            iconNum |= 1 << i;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   131
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   132
    if(m_icons().contains(iconNum))
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   133
    {
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   134
        setData(index, m_icons().value(iconNum), Qt::DecorationRole);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   135
        qDebug("cached");
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
}