QTfrontend/model/playerslistmodel.cpp
author unc0rr
Wed, 17 Oct 2012 23:09:16 +0400
changeset 7764 28613382e3f3
parent 7744 75e1d0c0ba72
child 7765 1e162c1d6dc7
permissions -rw-r--r--
- Show unready status in icon - Fix bug with search for player in model by nick performing "starts with" test
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>
7732
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
     4
#include <QFile>
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
     5
#include <QTextStream>
7727
bd252cacabe8 Bring icons back
unc0rr
parents: 7725
diff changeset
     6
#include <QDebug>
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
     7
7723
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
     8
#include "playerslistmodel.h"
7732
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
     9
#include "hwconsts.h"
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
PlayersListModel::PlayersListModel(QObject *parent) :
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    12
    QAbstractListModel(parent)
7723
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
    13
{
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
    14
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
    15
}
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
    16
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    17
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    18
int PlayersListModel::rowCount(const QModelIndex &parent) const
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    19
{
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    20
    if(parent.isValid())
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    21
        return 0;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    22
    else
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    23
        return m_data.size();
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
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    26
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    27
QVariant PlayersListModel::data(const QModelIndex &index, int role) const
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    28
{
7727
bd252cacabe8 Bring icons back
unc0rr
parents: 7725
diff changeset
    29
    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
    30
        return QVariant(QVariant::Invalid);
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
    return m_data.at(index.row()).value(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
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    35
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    36
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
    37
{
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    38
    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
    39
        return false;
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
    m_data[index.row()].insert(role, value);
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
    emit dataChanged(index, index);
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
    return true;
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
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
bool PlayersListModel::insertRow(int row, const QModelIndex &parent)
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
    return insertRows(row, 1, 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
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    54
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    55
bool PlayersListModel::insertRows(int row, int count, const QModelIndex &parent)
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
    if(parent.isValid() || row > rowCount() || row < 0 || count < 1)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    58
        return false;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    59
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    60
    beginInsertRows(parent, row, row + count - 1);
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
    for(int i = 0; i < count; ++i)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    63
        m_data.insert(row, DataEntry());
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
    endInsertRows();
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
    return true;
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
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    70
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    71
bool PlayersListModel::removeRows(int row, int count, const QModelIndex &parent)
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
    if(parent.isValid() || row + count > rowCount() || row < 0 || count < 1)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    74
        return false;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    75
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    76
    beginRemoveRows(parent, row, row + count - 1);
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
    for(int i = 0; i < count; ++i)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    79
        m_data.removeAt(row);
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
    endRemoveRows();
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
    return true;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    84
}
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    85
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    86
7723
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
    87
void PlayersListModel::addPlayer(const QString & nickname)
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
    88
{
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    89
    insertRow(rowCount());
7723
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
    90
7728
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
    91
    QModelIndex mi = index(rowCount() - 1);
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
    92
    setData(mi, nickname);
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    93
7732
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
    94
    checkFriendIgnore(mi);
7723
ca05b576ec74 Start moving to players list model.
unc0rr
parents:
diff changeset
    95
}
7725
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
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    98
void PlayersListModel::removePlayer(const QString & nickname)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
    99
{
7764
28613382e3f3 - Show unready status in icon
unc0rr
parents: 7744
diff changeset
   100
    QModelIndexList mil = match(index(0), Qt::DisplayRole, nickname, 1, Qt::MatchExactly);
7725
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
    if(mil.size())
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   103
        removeRow(mil[0].row());
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
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   106
7731
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   107
void PlayersListModel::playerJoinedRoom(const QString & nickname)
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   108
{
7764
28613382e3f3 - Show unready status in icon
unc0rr
parents: 7744
diff changeset
   109
    QModelIndexList mil = match(index(0), Qt::DisplayRole, nickname, 1, Qt::MatchExactly);
7731
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   110
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   111
    if(mil.size())
7764
28613382e3f3 - Show unready status in icon
unc0rr
parents: 7744
diff changeset
   112
    {
7731
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   113
        setData(mil[0], "1", RoomFilterRole);
7764
28613382e3f3 - Show unready status in icon
unc0rr
parents: 7744
diff changeset
   114
        updateIcon(mil[0]);
28613382e3f3 - Show unready status in icon
unc0rr
parents: 7744
diff changeset
   115
    }
7731
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   116
}
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   117
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   118
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   119
void PlayersListModel::playerLeftRoom(const QString & nickname)
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   120
{
7764
28613382e3f3 - Show unready status in icon
unc0rr
parents: 7744
diff changeset
   121
    QModelIndexList mil = match(index(0), Qt::DisplayRole, nickname, 1, Qt::MatchExactly);
7731
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   122
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   123
    if(mil.size())
7764
28613382e3f3 - Show unready status in icon
unc0rr
parents: 7744
diff changeset
   124
    {
7731
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   125
        setData(mil[0], "0", RoomFilterRole);
7764
28613382e3f3 - Show unready status in icon
unc0rr
parents: 7744
diff changeset
   126
        updateIcon(mil[0]);
28613382e3f3 - Show unready status in icon
unc0rr
parents: 7744
diff changeset
   127
    }
7731
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   128
}
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   129
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   130
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   131
void PlayersListModel::setFlag(const QString &nickname, StateFlag flagType, bool isSet)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   132
{
7764
28613382e3f3 - Show unready status in icon
unc0rr
parents: 7744
diff changeset
   133
    QModelIndexList mil = match(index(0), Qt::DisplayRole, nickname, 1, Qt::MatchExactly);
7725
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
    if(mil.size())
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   136
    {
7727
bd252cacabe8 Bring icons back
unc0rr
parents: 7725
diff changeset
   137
        setData(mil[0], isSet, flagType);
7728
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
   138
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
   139
        if(flagType == Friend || flagType == ServerAdmin
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
   140
                || flagType == Ignore || flagType == RoomAdmin)
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
   141
            updateSortData(mil[0]);
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
   142
7737
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   143
        if(flagType == Friend)
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   144
        {
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   145
            if(isSet)
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   146
                m_friendsSet.insert(nickname.toLower());
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   147
            else
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   148
                m_friendsSet.remove(nickname.toLower());
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   149
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   150
            saveSet(m_friendsSet, "friends");
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   151
        }
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   152
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   153
        if(flagType == Ignore)
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   154
        {
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   155
            if(isSet)
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   156
                m_ignoredSet.insert(nickname.toLower());
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   157
            else
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   158
                m_ignoredSet.remove(nickname.toLower());
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   159
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   160
            saveSet(m_ignoredSet, "ignore");
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   161
        }
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   162
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   163
        updateIcon(mil[0]);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   164
    }
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   165
}
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
7737
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   168
bool PlayersListModel::isFlagSet(const QString & nickname, StateFlag flagType)
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   169
{
7764
28613382e3f3 - Show unready status in icon
unc0rr
parents: 7744
diff changeset
   170
    QModelIndexList mil = match(index(0), Qt::DisplayRole, nickname, 1, Qt::MatchExactly);
7737
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   171
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   172
    if(mil.size())
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   173
        return mil[0].data(flagType).toBool();
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   174
    else
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   175
        return false;
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   176
}
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   177
7731
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   178
void PlayersListModel::resetRoomFlags()
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   179
{
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   180
    for(int i = rowCount() - 1; i >= 0; --i)
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   181
    {
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   182
        QModelIndex mi = index(i);
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   183
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   184
        if(mi.data(RoomFilterRole).toString() == "1")
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   185
        {
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   186
            setData(mi, "0", RoomFilterRole);
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   187
            setData(mi, false, RoomAdmin);
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   188
            setData(mi, false, Ready);
7737
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   189
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   190
            updateSortData(mi);
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   191
            updateIcon(mi);
7731
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   192
        }
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   193
    }
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   194
}
262228c64f15 Room players list
unc0rr
parents: 7728
diff changeset
   195
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   196
void PlayersListModel::updateIcon(const QModelIndex & index)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   197
{
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   198
    quint32 iconNum = 0;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   199
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   200
    QList<bool> flags;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   201
    flags
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   202
        << index.data(Ready).toBool()
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   203
        << index.data(ServerAdmin).toBool()
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   204
        << index.data(RoomAdmin).toBool()
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   205
        << index.data(Registered).toBool()
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   206
        << index.data(Friend).toBool()
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   207
        << index.data(Ignore).toBool()
7764
28613382e3f3 - Show unready status in icon
unc0rr
parents: 7744
diff changeset
   208
        << (index.data(RoomFilterRole).toString() == "1")
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   209
        ;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   210
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   211
    for(int i = flags.size() - 1; i >= 0; --i)
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   212
        if(flags[i])
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   213
            iconNum |= 1 << i;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   214
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   215
    if(m_icons().contains(iconNum))
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   216
    {
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   217
        setData(index, m_icons().value(iconNum), Qt::DecorationRole);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   218
    }
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   219
    else
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   220
    {
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   221
        QPixmap result(24, 16);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   222
        result.fill(Qt::transparent);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   223
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   224
        QPainter painter(&result);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   225
7764
28613382e3f3 - Show unready status in icon
unc0rr
parents: 7744
diff changeset
   226
        if(index.data(RoomFilterRole).toString() == "1")
28613382e3f3 - Show unready status in icon
unc0rr
parents: 7744
diff changeset
   227
            if(index.data(Ready).toBool())
28613382e3f3 - Show unready status in icon
unc0rr
parents: 7744
diff changeset
   228
                painter.drawPixmap(0, 0, 16, 16, QPixmap(":/res/chat/lamp.png"));
28613382e3f3 - Show unready status in icon
unc0rr
parents: 7744
diff changeset
   229
            else
28613382e3f3 - Show unready status in icon
unc0rr
parents: 7744
diff changeset
   230
                painter.drawPixmap(0, 0, 16, 16, QPixmap(":/res/chat/lamp_off.png"));
7725
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   231
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   232
        QString mainIconName(":/res/chat/");
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   233
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   234
        if(index.data(RoomAdmin).toBool())
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   235
            mainIconName += "roomadmin";
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   236
        else if(index.data(ServerAdmin).toBool())
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   237
            mainIconName += "serveradmin";
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   238
        else
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   239
            mainIconName += "hedgehog";
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   240
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   241
        if(!index.data(Registered).toBool())
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   242
            mainIconName += "_gray";
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   243
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   244
        painter.drawPixmap(8, 0, 16, 16, QPixmap(mainIconName + ".png"));
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   245
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   246
        if(index.data(Ignore).toBool())
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   247
            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
   248
        else
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   249
        if(index.data(Friend).toBool())
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   250
            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
   251
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   252
        painter.end();
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   253
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   254
        QIcon icon(result);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   255
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   256
        setData(index, icon, Qt::DecorationRole);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   257
        m_icons().insert(iconNum, icon);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   258
    }
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   259
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   260
    if(index.data(Ignore).toBool())
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   261
        setData(index, Qt::gray, Qt::ForegroundRole);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   262
    else
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   263
    if(index.data(Friend).toBool())
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   264
        setData(index, Qt::green, Qt::ForegroundRole);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   265
    else
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   266
        setData(index, QBrush(QColor(0xff, 0xcc, 0x00)), Qt::ForegroundRole);
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   267
}
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   268
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   269
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   270
QHash<quint32, QIcon> & PlayersListModel::m_icons()
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   271
{
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   272
    static QHash<quint32, QIcon> iconsCache;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   273
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   274
    return iconsCache;
4ad05a478c6c Further work on moving to players list model
unc0rr
parents: 7723
diff changeset
   275
}
7728
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
   276
7732
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   277
7728
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
   278
void PlayersListModel::updateSortData(const QModelIndex & index)
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
   279
{
7744
75e1d0c0ba72 - Nicks starting from not-letter char go to bottom of the list
unc0rr
parents: 7737
diff changeset
   280
    QString result = QString("%1%2%3%4%5%6")
75e1d0c0ba72 - Nicks starting from not-letter char go to bottom of the list
unc0rr
parents: 7737
diff changeset
   281
            // room admins go first, then server admins, then friends
7728
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
   282
            .arg(1 - index.data(RoomAdmin).toInt())
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
   283
            .arg(1 - index.data(ServerAdmin).toInt())
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
   284
            .arg(1 - index.data(Friend).toInt())
7744
75e1d0c0ba72 - Nicks starting from not-letter char go to bottom of the list
unc0rr
parents: 7737
diff changeset
   285
            // ignored at bottom
7728
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
   286
            .arg(index.data(Ignore).toInt())
7744
75e1d0c0ba72 - Nicks starting from not-letter char go to bottom of the list
unc0rr
parents: 7737
diff changeset
   287
            // keep nicknames starting from non-letter character at bottom within group
75e1d0c0ba72 - Nicks starting from not-letter char go to bottom of the list
unc0rr
parents: 7737
diff changeset
   288
            // assume there are no empty nicks in list
75e1d0c0ba72 - Nicks starting from not-letter char go to bottom of the list
unc0rr
parents: 7737
diff changeset
   289
            .arg(index.data(Qt::DisplayRole).toString().at(0).isLetter() ? 0 : 1)
75e1d0c0ba72 - Nicks starting from not-letter char go to bottom of the list
unc0rr
parents: 7737
diff changeset
   290
            // sort ignoring case
7728
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
   291
            .arg(index.data(Qt::DisplayRole).toString().toLower())
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
   292
            ;
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
   293
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
   294
    setData(index, result, SortRole);
664738870325 Bring back sorting
unc0rr
parents: 7727
diff changeset
   295
}
7732
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   296
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   297
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   298
void PlayersListModel::setNickname(const QString &nickname)
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   299
{
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   300
    m_nickname = nickname;
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   301
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   302
    loadSet(m_friendsSet, "friends");
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   303
    loadSet(m_ignoredSet, "ignore");
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   304
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   305
    for(int i = rowCount() - 1; i >= 0; --i)
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   306
        checkFriendIgnore(index(i));
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   307
}
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   308
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   309
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   310
void PlayersListModel::checkFriendIgnore(const QModelIndex &mi)
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   311
{
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   312
    setData(mi, m_friendsSet.contains(mi.data().toString().toLower()), Friend);
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   313
    setData(mi, m_ignoredSet.contains(mi.data().toString().toLower()), Ignore);
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   314
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   315
    updateIcon(mi);
7744
75e1d0c0ba72 - Nicks starting from not-letter char go to bottom of the list
unc0rr
parents: 7737
diff changeset
   316
    updateSortData(mi);
7732
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   317
}
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   318
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   319
void PlayersListModel::loadSet(QSet<QString> & set, const QString & suffix)
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   320
{
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   321
    set.clear();
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   322
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   323
    QString fileName = QString("%1/%2_%3.txt").arg(cfgdir->absolutePath(), m_nickname.toLower(), suffix);
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   324
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   325
    QFile txt(fileName);
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   326
    if(!txt.open(QIODevice::ReadOnly))
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   327
        return;
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   328
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   329
    QTextStream stream(&txt);
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   330
    stream.setCodec("UTF-8");
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   331
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   332
    while(!stream.atEnd())
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   333
    {
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   334
        QString str = stream.readLine();
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   335
        if(str.startsWith(";") || str.isEmpty())
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   336
            continue;
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   337
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   338
        set.insert(str.trimmed());
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   339
    }
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   340
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   341
    txt.close();
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   342
}
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   343
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   344
void PlayersListModel::saveSet(const QSet<QString> & set, const QString & suffix)
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   345
{
7737
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   346
    qDebug("saving set");
ff63da8a3202 - Enable chat context menu actions
unc0rr
parents: 7732
diff changeset
   347
7732
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   348
    QString fileName = QString("%1/%2_%3.txt").arg(cfgdir->absolutePath(), m_nickname.toLower(), suffix);
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   349
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   350
    QFile txt(fileName);
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   351
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   352
    // list empty? => rather have no file for the list than an empty one
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   353
    if (set.isEmpty())
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   354
    {
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   355
        if (txt.exists())
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   356
        {
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   357
            // try to remove file, if successful we're done here.
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   358
            if (txt.remove())
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   359
                return;
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   360
        }
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   361
        else
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   362
            // there is no file
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   363
            return;
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   364
    }
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   365
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   366
    if(!txt.open(QIODevice::WriteOnly | QIODevice::Truncate))
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   367
        return;
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   368
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   369
    QTextStream stream(&txt);
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   370
    stream.setCodec("UTF-8");
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   371
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   372
    stream << "; this list is used by Hedgewars - do not edit it unless you know what you're doing!" << endl;
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   373
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   374
    foreach(const QString & nick, set.values())
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   375
        stream << nick << endl;
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   376
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   377
    txt.close();
fad3408fdcc1 Load friends/ignored nicks from file
unc0rr
parents: 7731
diff changeset
   378
}