QTfrontend/model/roomslistmodel.cpp
author unc0rr
Thu, 23 Feb 2012 18:41:52 +0400
changeset 6733 5abbc345a82f
parent 6732 c906dc78091f
child 6736 efad396abee9
permissions -rw-r--r--
Handle ROOM* commands in rooms list model
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()
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
     8
     << tr("Room Name")
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
     9
     << tr("C")
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    10
     << tr("T")
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    11
     << tr("Owner")
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    12
     << tr("Map")
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    13
     << tr("Rules")
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    14
     << tr("Weapons");
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    15
}
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
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
    18
{
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    19
    if(orientation == Qt::Vertical || role != Qt::DisplayRole)
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    20
        return QVariant();
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    21
    else
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    22
        return QVariant(m_headerData.at(section));
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    23
}
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
int RoomsListModel::rowCount(const QModelIndex & parent) const
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    26
{
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    27
    if(parent.isValid())
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    28
        return 0;
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    29
    else
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    30
        return m_data.size();
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    31
}
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
int RoomsListModel::columnCount(const QModelIndex & parent) const
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    34
{
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    35
    if(parent.isValid())
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    36
        return 0;
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    37
    else
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    38
        return 7;
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    39
}
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
QVariant RoomsListModel::data(const QModelIndex &index, int role) const
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    42
{
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    43
    if (!index.isValid() || index.row() < 0
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    44
            || index.row() >= m_data.size()
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    45
            || index.column() >= 7
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    46
            || (role != Qt::EditRole && role != Qt::DisplayRole)
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    47
       )
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    48
        return QVariant();
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    49
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    50
    return m_data.at(index.row()).at(index.column());
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    51
}
6733
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    52
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    53
void RoomsListModel::setRoomsList(const QStringList & rooms)
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    54
{
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    55
    if(m_data.size())
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    56
    {
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    57
        beginRemoveRows(QModelIndex(), 0, m_data.size() - 1);
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    58
        m_data.clear();
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    59
        endRemoveRows();
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    60
    }
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
    for(int i = 0; i < rooms.size(); i += 8)
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    63
    {
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    64
        QStringList l;
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    65
        l.reserve(8);
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    66
        for(int t = 0; t < 8; ++t)
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    67
            l.append(rooms[i + t]);
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    68
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    69
        m_data.append(roomInfo2RoomRecord(l));
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    70
    }
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
    beginInsertRows(QModelIndex(), 0, m_data.size() - 1);
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    73
    endInsertRows();
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    74
}
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
void RoomsListModel::addRoom(const QStringList & info)
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    77
{
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    78
    beginInsertRows(QModelIndex(), 0, 0);
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    79
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    80
    m_data.prepend(roomInfo2RoomRecord(info));
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    81
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    82
    endInsertRows();
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    83
}
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
void RoomsListModel::removeRoom(const QString & name)
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    86
{
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    87
    int i = 0;
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    88
    while(i < m_data.size() && m_data[i].at(0) != name)
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    89
        ++i;
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    90
    if(i >= m_data.size())
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    91
        return;
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    92
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    93
    beginRemoveRows(QModelIndex(), i, i);
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    94
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    95
    m_data.removeAt(i);
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    96
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    97
    endRemoveRows();
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    98
}
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
void RoomsListModel::updateRoom(const QString & name, const QStringList & info)
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   101
{
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   102
    int i = 0;
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   103
    while(i < m_data.size() && m_data[i].at(0) != name)
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   104
        ++i;
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   105
    if(i >= m_data.size())
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   106
        return;
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   107
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
    m_data[i] = roomInfo2RoomRecord(info);
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   110
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   111
    emit dataChanged(index(i, 0), index(i, columnCount(QModelIndex()) - 1));
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   112
}
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
QStringList RoomsListModel::roomInfo2RoomRecord(const QStringList & info)
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   115
{
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   116
    QStringList result;
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   117
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   118
    result = info;
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   119
    result.removeFirst();
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
}