QTfrontend/model/roomslistmodel.cpp
author unc0rr
Thu, 23 Feb 2012 23:13:48 +0400
changeset 6736 efad396abee9
parent 6733 5abbc345a82f
child 6757 ed9b3a567a3d
permissions -rw-r--r--
Bring back functionality to "Join" button
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6732
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
     1
#include "roomslistmodel.h"
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
     2
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
     3
RoomsListModel::RoomsListModel(QObject *parent) :
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
     4
    QAbstractTableModel(parent)
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
     5
{
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
     6
    m_headerData =
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
     7
    QStringList()
6736
efad396abee9 Bring back functionality to "Join" button
unc0rr
parents: 6733
diff changeset
     8
     << QString()
6732
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
     9
     << tr("Room Name")
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    10
     << tr("C")
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    11
     << tr("T")
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    12
     << tr("Owner")
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    13
     << tr("Map")
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    14
     << tr("Rules")
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    15
     << tr("Weapons");
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    16
}
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    17
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    18
QVariant RoomsListModel::headerData(int section, Qt::Orientation orientation, int role) const
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    19
{
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    20
    if(orientation == Qt::Vertical || role != Qt::DisplayRole)
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    21
        return QVariant();
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    22
    else
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    23
        return QVariant(m_headerData.at(section));
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    24
}
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    25
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    26
int RoomsListModel::rowCount(const QModelIndex & parent) const
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    27
{
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    28
    if(parent.isValid())
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    29
        return 0;
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    30
    else
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    31
        return m_data.size();
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    32
}
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    33
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    34
int RoomsListModel::columnCount(const QModelIndex & parent) const
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    35
{
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    36
    if(parent.isValid())
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    37
        return 0;
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    38
    else
6736
efad396abee9 Bring back functionality to "Join" button
unc0rr
parents: 6733
diff changeset
    39
        return 8;
6732
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    40
}
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    41
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    42
QVariant RoomsListModel::data(const QModelIndex &index, int role) const
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    43
{
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    44
    if (!index.isValid() || index.row() < 0
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    45
            || index.row() >= m_data.size()
6736
efad396abee9 Bring back functionality to "Join" button
unc0rr
parents: 6733
diff changeset
    46
            || index.column() >= 8
6732
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    47
            || (role != Qt::EditRole && role != Qt::DisplayRole)
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    48
       )
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    49
        return QVariant();
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    50
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    51
    return m_data.at(index.row()).at(index.column());
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    52
}
6733
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    53
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    54
void RoomsListModel::setRoomsList(const QStringList & rooms)
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    55
{
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    56
    if(m_data.size())
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    57
    {
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    58
        beginRemoveRows(QModelIndex(), 0, m_data.size() - 1);
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    59
        m_data.clear();
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    60
        endRemoveRows();
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    61
    }
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    62
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    63
    for(int i = 0; i < rooms.size(); i += 8)
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    64
    {
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    65
        QStringList l;
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    66
        l.reserve(8);
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    67
        for(int t = 0; t < 8; ++t)
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    68
            l.append(rooms[i + t]);
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    69
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    70
        m_data.append(roomInfo2RoomRecord(l));
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    71
    }
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    72
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    73
    beginInsertRows(QModelIndex(), 0, m_data.size() - 1);
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    74
    endInsertRows();
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    75
}
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    76
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    77
void RoomsListModel::addRoom(const QStringList & info)
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    78
{
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    79
    beginInsertRows(QModelIndex(), 0, 0);
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    80
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    81
    m_data.prepend(roomInfo2RoomRecord(info));
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    82
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    83
    endInsertRows();
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    84
}
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    85
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    86
void RoomsListModel::removeRoom(const QString & name)
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    87
{
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    88
    int i = 0;
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    89
    while(i < m_data.size() && m_data[i].at(0) != name)
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    90
        ++i;
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    91
    if(i >= m_data.size())
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    92
        return;
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    93
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    94
    beginRemoveRows(QModelIndex(), i, i);
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    95
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    96
    m_data.removeAt(i);
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    97
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    98
    endRemoveRows();
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    99
}
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   100
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   101
void RoomsListModel::updateRoom(const QString & name, const QStringList & info)
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   102
{
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   103
    int i = 0;
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   104
    while(i < m_data.size() && m_data[i].at(0) != name)
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   105
        ++i;
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   106
    if(i >= m_data.size())
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   107
        return;
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   108
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   109
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   110
    m_data[i] = roomInfo2RoomRecord(info);
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   111
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   112
    emit dataChanged(index(i, 0), index(i, columnCount(QModelIndex()) - 1));
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   113
}
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   114
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   115
QStringList RoomsListModel::roomInfo2RoomRecord(const QStringList & info)
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   116
{
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   117
    QStringList result;
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   118
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   119
    result = info;
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   120
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   121
    return result;
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   122
}