File: roomslistmodel.cpp

package info (click to toggle)
hedgewars 1.0.2-13
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 219,164 kB
  • sloc: pascal: 54,829; cpp: 27,221; ansic: 22,809; java: 8,210; haskell: 6,797; xml: 3,076; sh: 580; objc: 113; python: 105; makefile: 32
file content (275 lines) | stat: -rw-r--r-- 7,178 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
 * Hedgewars, a free turn based strategy game
 * Copyright (c) 2004-2015 Andrey Korotaev <unC0Rr@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/**
 * @file
 * @brief RoomsListModel class implementation
 */

#include <QBrush>
#include <QColor>
#include <QIcon>

#include "roomslistmodel.h"
#include "MapModel.h"

RoomsListModel::RoomsListModel(QObject *parent) :
    QAbstractTableModel(parent),
    c_nColumns(9)
{
    m_headerData = QStringList();
    m_headerData << tr("In progress");
    m_headerData << tr("Room Name");
    //: Caption of the column for the number of connected clients in the list of rooms
    m_headerData << tr("C");
    //: Caption of the column for the number of teams in the list of rooms
    m_headerData << tr("T");
    m_headerData << tr("Owner");
    m_headerData << tr("Map");
    m_headerData << tr("Script");
    m_headerData << tr("Rules");
    m_headerData << tr("Weapons");

    m_staticMapModel = DataManager::instance().staticMapModel();
    m_missionMapModel = DataManager::instance().missionMapModel();
}


QVariant RoomsListModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if(orientation == Qt::Vertical || role != Qt::DisplayRole)
        return QVariant();
    else
        return QVariant(m_headerData.at(section));
}


int RoomsListModel::rowCount(const QModelIndex & parent) const
{
    if(parent.isValid())
        return 0;
    else
        return m_data.size();
}


int RoomsListModel::columnCount(const QModelIndex & parent) const
{
    if(parent.isValid())
        return 0;
    else
        return c_nColumns;
}


QVariant RoomsListModel::data(const QModelIndex &index, int role) const
{
    int column = index.column();
    int row = index.row();

    // invalid index
    if (!index.isValid())
        return QVariant();

    // invalid row
    if ((row < 0) || (row >= m_data.size()))
        return QVariant();

    // invalid column
    if ((column < 0) || (column >= c_nColumns))
        return QVariant();

    // not a role we have data for
    if (role != Qt::DisplayRole)
        // only custom-align counters
        if ((role != Qt::TextAlignmentRole)
            || ((column != PlayerCountColumn) && (column != TeamCountColumn)))
                // only decorate name column
                if ((role != Qt::DecorationRole) || (column != NameColumn))
                    // only dye map column
                    if ((role != Qt::ForegroundRole) || (column != MapColumn))
                        return QVariant();

    // decorate room name based on room state
    if (role == Qt::DecorationRole)
    {
        const QIcon roomBusyIcon(":/res/iconDamage.png");
        const QIcon roomBusyIconGreen(":/res/iconDamageLockG.png");
        const QIcon roomBusyIconRed(":/res/iconDamageLockR.png");
        const QIcon roomWaitingIcon(":/res/iconTime.png");
        const QIcon roomWaitingIconGreen(":/res/iconTimeLockG.png");
        const QIcon roomWaitingIconRed(":/res/iconTimeLockR.png");

        QString flags = m_data.at(row).at(StateColumn);

        if (flags.contains("g"))
        {
            if (flags.contains("j"))
                return QVariant(roomBusyIconRed);
            else if (flags.contains("p"))
                return QVariant(roomBusyIconGreen);
            else
                return QVariant(roomBusyIcon);
        }
        else
        {
            if (flags.contains("j"))
                return QVariant(roomWaitingIconRed);
            else if (flags.contains("p"))
                return QVariant(roomWaitingIconGreen);
            else
                return QVariant(roomWaitingIcon);
        }
    }

    QString content = m_data.at(row).at(column);

    if (role == Qt::DisplayRole)
    {
        // display room names
        if (column == 5)
        {
            // special names
            if (content[0] == '+')
            {
                if (content == "+rnd+") return tr("Random Map");
                if (content == "+maze+") return tr("Random Maze");
                if (content == "+perlin+") return tr("Random Perlin");
                if (content == "+drawn+") return tr("Hand-drawn");
                if (content == "+forts+") return tr("Forts");
            }

            // prefix ? if map not available
            if (!m_staticMapModel->mapExists(content) &&
                !m_missionMapModel->mapExists(content))
                return QString ("? %1").arg(content);
        }

        return content;
    }

    // dye map names red if map not available
    if (role == Qt::ForegroundRole)
    {
        if (content == "+rnd+" ||
            content == "+maze+" ||
            content == "+perlin+" ||
            content == "+drawn+" ||
            content == "+forts+" ||
            m_staticMapModel->mapExists(content) ||
            m_missionMapModel->mapExists(content))
            return QVariant();
        else
            return QBrush(QColor("darkred"));
    }

    if (role == Qt::TextAlignmentRole)
    {
        return (int)(Qt::AlignHCenter | Qt::AlignVCenter);
    }

    Q_ASSERT(false);
    return QVariant();
}


void RoomsListModel::setRoomsList(const QStringList & rooms)
{
    beginResetModel();

    m_data.clear();

    int nRooms = rooms.size();

    for (int i = 0; i < nRooms; i += c_nColumns)
    {
        QStringList l;

#if QT_VERSION >= QT_VERSION_CHECK(4, 7, 0)
        l.reserve(c_nColumns);  // small optimisation not supported in old Qt
#endif

        for (int t = 0; t < c_nColumns; t++)
        {
            l.append(rooms[i + t]);
        }

        m_data.append(l);
    }

    endResetModel();
}


void RoomsListModel::addRoom(const QStringList & info)
{
    beginInsertRows(QModelIndex(), 0, 0);

    m_data.prepend(info);

    endInsertRows();
}


int RoomsListModel::rowOfRoom(const QString & name)
{
    int size = m_data.size();

    if (size < 1)
        return -1;

    int i = 0;

    // search for record with matching room name
    while(m_data[i].at(NameColumn) != name)
    {
        i++;
        if(i >= size)
            return -1;
    }

    return i;
}


void RoomsListModel::removeRoom(const QString & name)
{
    int i = rowOfRoom(name);

    if (i < 0)
        return;

    beginRemoveRows(QModelIndex(), i, i);

    m_data.removeAt(i);

    endRemoveRows();
}


void RoomsListModel::updateRoom(const QString & name, const QStringList & info)
{
    int i = rowOfRoom(name);

    if (i < 0)
        return;

    m_data[i] = info;

    emit dataChanged(index(i, 0), index(i, columnCount(QModelIndex()) - 1));
}