QTfrontend/model/roomslistmodel.cpp
author sheepluva
Mon, 30 Apr 2012 20:12:43 +0200
changeset 6966 eda4f63bec41
parent 6757 ed9b3a567a3d
child 6973 582ca7e8e338
permissions -rw-r--r--
add missing copyright headers to roomslistmodel
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6966
eda4f63bec41 add missing copyright headers to roomslistmodel
sheepluva
parents: 6757
diff changeset
     1
/*
eda4f63bec41 add missing copyright headers to roomslistmodel
sheepluva
parents: 6757
diff changeset
     2
 * Hedgewars, a free turn based strategy game
eda4f63bec41 add missing copyright headers to roomslistmodel
sheepluva
parents: 6757
diff changeset
     3
 * Copyright (c) 2004-2012 Andrey Korotaev <unC0Rr@gmail.com>
eda4f63bec41 add missing copyright headers to roomslistmodel
sheepluva
parents: 6757
diff changeset
     4
 *
eda4f63bec41 add missing copyright headers to roomslistmodel
sheepluva
parents: 6757
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
eda4f63bec41 add missing copyright headers to roomslistmodel
sheepluva
parents: 6757
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
eda4f63bec41 add missing copyright headers to roomslistmodel
sheepluva
parents: 6757
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
eda4f63bec41 add missing copyright headers to roomslistmodel
sheepluva
parents: 6757
diff changeset
     8
 *
eda4f63bec41 add missing copyright headers to roomslistmodel
sheepluva
parents: 6757
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
eda4f63bec41 add missing copyright headers to roomslistmodel
sheepluva
parents: 6757
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
eda4f63bec41 add missing copyright headers to roomslistmodel
sheepluva
parents: 6757
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
eda4f63bec41 add missing copyright headers to roomslistmodel
sheepluva
parents: 6757
diff changeset
    12
 * GNU General Public License for more details.
eda4f63bec41 add missing copyright headers to roomslistmodel
sheepluva
parents: 6757
diff changeset
    13
 *
eda4f63bec41 add missing copyright headers to roomslistmodel
sheepluva
parents: 6757
diff changeset
    14
 * You should have received a copy of the GNU General Public License
eda4f63bec41 add missing copyright headers to roomslistmodel
sheepluva
parents: 6757
diff changeset
    15
 * along with this program; if not, write to the Free Software
eda4f63bec41 add missing copyright headers to roomslistmodel
sheepluva
parents: 6757
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
eda4f63bec41 add missing copyright headers to roomslistmodel
sheepluva
parents: 6757
diff changeset
    17
 */
eda4f63bec41 add missing copyright headers to roomslistmodel
sheepluva
parents: 6757
diff changeset
    18
eda4f63bec41 add missing copyright headers to roomslistmodel
sheepluva
parents: 6757
diff changeset
    19
/**
eda4f63bec41 add missing copyright headers to roomslistmodel
sheepluva
parents: 6757
diff changeset
    20
 * @file
eda4f63bec41 add missing copyright headers to roomslistmodel
sheepluva
parents: 6757
diff changeset
    21
 * @brief RoomsListModel class implementation
eda4f63bec41 add missing copyright headers to roomslistmodel
sheepluva
parents: 6757
diff changeset
    22
 */
eda4f63bec41 add missing copyright headers to roomslistmodel
sheepluva
parents: 6757
diff changeset
    23
6732
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    24
#include "roomslistmodel.h"
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
RoomsListModel::RoomsListModel(QObject *parent) :
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    27
    QAbstractTableModel(parent)
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    28
{
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    29
    m_headerData =
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    30
    QStringList()
6736
efad396abee9 Bring back functionality to "Join" button
unc0rr
parents: 6733
diff changeset
    31
     << QString()
6732
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    32
     << tr("Room Name")
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    33
     << tr("C")
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    34
     << tr("T")
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    35
     << tr("Owner")
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    36
     << tr("Map")
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    37
     << tr("Rules")
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    38
     << tr("Weapons");
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::headerData(int section, Qt::Orientation orientation, 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(orientation == Qt::Vertical || role != Qt::DisplayRole)
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    44
        return QVariant();
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    45
    else
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    46
        return QVariant(m_headerData.at(section));
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
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    49
int RoomsListModel::rowCount(const QModelIndex & parent) const
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
    if(parent.isValid())
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    52
        return 0;
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    53
    else
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    54
        return m_data.size();
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    55
}
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    56
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    57
int RoomsListModel::columnCount(const QModelIndex & parent) const
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    58
{
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    59
    if(parent.isValid())
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    60
        return 0;
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    61
    else
6736
efad396abee9 Bring back functionality to "Join" button
unc0rr
parents: 6733
diff changeset
    62
        return 8;
6732
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    63
}
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    64
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    65
QVariant RoomsListModel::data(const QModelIndex &index, int role) const
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    66
{
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    67
    if (!index.isValid() || index.row() < 0
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    68
            || index.row() >= m_data.size()
6736
efad396abee9 Bring back functionality to "Join" button
unc0rr
parents: 6733
diff changeset
    69
            || index.column() >= 8
6732
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    70
            || (role != Qt::EditRole && role != Qt::DisplayRole)
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    71
       )
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    72
        return QVariant();
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    73
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    74
    return m_data.at(index.row()).at(index.column());
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    75
}
6733
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::setRoomsList(const QStringList & rooms)
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
    if(m_data.size())
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
        beginRemoveRows(QModelIndex(), 0, m_data.size() - 1);
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    82
        m_data.clear();
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    83
        endRemoveRows();
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
    for(int i = 0; i < rooms.size(); i += 8)
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
        QStringList l;
6757
ed9b3a567a3d comment out reserve to maintain old Qt compat
nemo
parents: 6736
diff changeset
    89
        //l.reserve(8);  not really that useful an optimisation and causes problems w/ old Qt.  Harmless to leave it out.
6733
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    90
        for(int t = 0; t < 8; ++t)
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    91
            l.append(rooms[i + t]);
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
        m_data.append(roomInfo2RoomRecord(l));
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
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    96
    beginInsertRows(QModelIndex(), 0, m_data.size() - 1);
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
    97
    endInsertRows();
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::addRoom(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
    beginInsertRows(QModelIndex(), 0, 0);
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   103
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   104
    m_data.prepend(roomInfo2RoomRecord(info));
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   105
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   106
    endInsertRows();
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
void RoomsListModel::removeRoom(const QString & name)
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
    int i = 0;
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   112
    while(i < m_data.size() && m_data[i].at(0) != name)
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   113
        ++i;
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   114
    if(i >= m_data.size())
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   115
        return;
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
    beginRemoveRows(QModelIndex(), i, i);
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
    m_data.removeAt(i);
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
    endRemoveRows();
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   122
}
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   123
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   124
void RoomsListModel::updateRoom(const QString & name, const QStringList & info)
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   125
{
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   126
    int i = 0;
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   127
    while(i < m_data.size() && m_data[i].at(0) != name)
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   128
        ++i;
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   129
    if(i >= m_data.size())
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   130
        return;
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   131
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   132
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   133
    m_data[i] = roomInfo2RoomRecord(info);
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   134
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   135
    emit dataChanged(index(i, 0), index(i, columnCount(QModelIndex()) - 1));
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   136
}
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   137
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   138
QStringList RoomsListModel::roomInfo2RoomRecord(const QStringList & info)
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   139
{
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   140
    QStringList result;
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   141
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   142
    result = info;
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   143
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   144
    return result;
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   145
}