QTfrontend/model/roomslistmodel.cpp
author sheepluva
Sun, 02 Oct 2016 00:42:29 +0200
changeset 11858 38fd32724a61
parent 11752 3c66845947a8
child 12474 42da9d8d82ab
permissions -rw-r--r--
and while I'm at it, fix seduction... kinda ♥
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
11046
47a8c19ecb60 more copyright fixes
sheepluva
parents: 10745
diff changeset
     3
 * Copyright (c) 2004-2015 Andrey Korotaev <unC0Rr@gmail.com>
6966
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
10108
c68cf030eded update FSF address. note: two sdl include files (by Sam Lantinga) still have the old FSF address in their copyright - but I ain't gonna touch their copyright headers
sheepluva
parents: 9998
diff changeset
    16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
6966
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
6983
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
    24
#include <QBrush>
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
    25
#include <QColor>
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
    26
#include <QIcon>
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
    27
7258
722e8a0d89dc - Move colorsModel to appropriate place
unc0rr
parents: 6995
diff changeset
    28
#include "roomslistmodel.h"
722e8a0d89dc - Move colorsModel to appropriate place
unc0rr
parents: 6995
diff changeset
    29
#include "MapModel.h"
722e8a0d89dc - Move colorsModel to appropriate place
unc0rr
parents: 6995
diff changeset
    30
6732
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    31
RoomsListModel::RoomsListModel(QObject *parent) :
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
    32
    QAbstractTableModel(parent),
9702
27006953d901 - Column for script in rooms list
unc0rr
parents: 9080
diff changeset
    33
    c_nColumns(9)
6732
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
    m_headerData =
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    36
    QStringList()
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
    37
     << tr("In progress")
6732
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    38
     << tr("Room Name")
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    39
     << tr("C")
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    40
     << tr("T")
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    41
     << tr("Owner")
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    42
     << tr("Map")
9702
27006953d901 - Column for script in rooms list
unc0rr
parents: 9080
diff changeset
    43
     << tr("Script")
6732
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    44
     << tr("Rules")
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    45
     << tr("Weapons");
6983
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
    46
8377
869f80966a77 GCI2012: Improve Game Configuration Widget
dag10
parents: 7258
diff changeset
    47
    m_staticMapModel = DataManager::instance().staticMapModel();
869f80966a77 GCI2012: Improve Game Configuration Widget
dag10
parents: 7258
diff changeset
    48
    m_missionMapModel = DataManager::instance().missionMapModel();
6732
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
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
    51
6732
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    52
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
    53
{
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    54
    if(orientation == Qt::Vertical || role != Qt::DisplayRole)
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    55
        return QVariant();
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    56
    else
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    57
        return QVariant(m_headerData.at(section));
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
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
    60
6732
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    61
int RoomsListModel::rowCount(const QModelIndex & parent) const
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    62
{
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    63
    if(parent.isValid())
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    64
        return 0;
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    65
    else
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    66
        return m_data.size();
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    67
}
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    68
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
    69
6732
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    70
int RoomsListModel::columnCount(const QModelIndex & parent) const
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
    if(parent.isValid())
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    73
        return 0;
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    74
    else
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
    75
        return c_nColumns;
6732
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    76
}
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    77
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
    78
6732
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    79
QVariant RoomsListModel::data(const QModelIndex &index, int role) const
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    80
{
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
    81
    int column = index.column();
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
    82
    int row = index.row();
6983
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
    83
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
    84
    // invalid index
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
    85
    if (!index.isValid())
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
    86
        return QVariant();
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
    87
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
    88
    // invalid row
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
    89
    if ((row < 0) || (row >= m_data.size()))
6732
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    90
        return QVariant();
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    91
6983
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
    92
    // invalid column
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
    93
    if ((column < 0) || (column >= c_nColumns))
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
    94
        return QVariant();
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
    95
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
    96
    // not a role we have data for
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
    97
    if (role != Qt::DisplayRole)
6987
e34415c77342 allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents: 6983
diff changeset
    98
        // only custom-align counters
6993
47830cf50574 room list: replace magic table column indexes with enum. makes future changes to the room list format way easier.
sheepluva
parents: 6987
diff changeset
    99
        if ((role != Qt::TextAlignmentRole)
47830cf50574 room list: replace magic table column indexes with enum. makes future changes to the room list format way easier.
sheepluva
parents: 6987
diff changeset
   100
            || ((column != PlayerCountColumn) && (column != TeamCountColumn)))
47830cf50574 room list: replace magic table column indexes with enum. makes future changes to the room list format way easier.
sheepluva
parents: 6987
diff changeset
   101
                // only decorate name column
47830cf50574 room list: replace magic table column indexes with enum. makes future changes to the room list format way easier.
sheepluva
parents: 6987
diff changeset
   102
                if ((role != Qt::DecorationRole) || (column != NameColumn))
47830cf50574 room list: replace magic table column indexes with enum. makes future changes to the room list format way easier.
sheepluva
parents: 6987
diff changeset
   103
                    // only dye map column
47830cf50574 room list: replace magic table column indexes with enum. makes future changes to the room list format way easier.
sheepluva
parents: 6987
diff changeset
   104
                    if ((role != Qt::ForegroundRole) || (column != MapColumn))
47830cf50574 room list: replace magic table column indexes with enum. makes future changes to the room list format way easier.
sheepluva
parents: 6987
diff changeset
   105
                        return QVariant();
6983
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
   106
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   107
    // decorate room name based on room state
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   108
    if (role == Qt::DecorationRole)
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   109
    {
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   110
        const QIcon roomBusyIcon(":/res/iconDamage.png");
10739
d886f319e7b3 Tired of not knowing which rooms I can't join
nemo
parents: 10511
diff changeset
   111
        const QIcon roomBusyIconGreen(":/res/iconDamageLockG.png");
d886f319e7b3 Tired of not knowing which rooms I can't join
nemo
parents: 10511
diff changeset
   112
        const QIcon roomBusyIconRed(":/res/iconDamageLockR.png");
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   113
        const QIcon roomWaitingIcon(":/res/iconTime.png");
10739
d886f319e7b3 Tired of not knowing which rooms I can't join
nemo
parents: 10511
diff changeset
   114
        const QIcon roomWaitingIconGreen(":/res/iconTimeLockG.png");
d886f319e7b3 Tired of not knowing which rooms I can't join
nemo
parents: 10511
diff changeset
   115
        const QIcon roomWaitingIconRed(":/res/iconTimeLockR.png");
d886f319e7b3 Tired of not knowing which rooms I can't join
nemo
parents: 10511
diff changeset
   116
10745
fab746a3597e premature optimisation bit me again.
nemo
parents: 10741
diff changeset
   117
        QString flags = m_data.at(row).at(StateColumn);
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   118
10739
d886f319e7b3 Tired of not knowing which rooms I can't join
nemo
parents: 10511
diff changeset
   119
        if (flags.contains("g"))
d886f319e7b3 Tired of not knowing which rooms I can't join
nemo
parents: 10511
diff changeset
   120
        {
d886f319e7b3 Tired of not knowing which rooms I can't join
nemo
parents: 10511
diff changeset
   121
            if (flags.contains("j"))
10741
nemo
parents: 10739
diff changeset
   122
                return QVariant(roomBusyIconRed);
nemo
parents: 10739
diff changeset
   123
            else if (flags.contains("p"))
nemo
parents: 10739
diff changeset
   124
                return QVariant(roomBusyIconGreen);
nemo
parents: 10739
diff changeset
   125
            else
nemo
parents: 10739
diff changeset
   126
                return QVariant(roomBusyIcon);
nemo
parents: 10739
diff changeset
   127
        }
nemo
parents: 10739
diff changeset
   128
        else
nemo
parents: 10739
diff changeset
   129
        {
nemo
parents: 10739
diff changeset
   130
            if (flags.contains("j"))
10739
d886f319e7b3 Tired of not knowing which rooms I can't join
nemo
parents: 10511
diff changeset
   131
                return QVariant(roomWaitingIconRed);
d886f319e7b3 Tired of not knowing which rooms I can't join
nemo
parents: 10511
diff changeset
   132
            else if (flags.contains("p"))
d886f319e7b3 Tired of not knowing which rooms I can't join
nemo
parents: 10511
diff changeset
   133
                return QVariant(roomWaitingIconGreen);
d886f319e7b3 Tired of not knowing which rooms I can't join
nemo
parents: 10511
diff changeset
   134
            else
d886f319e7b3 Tired of not knowing which rooms I can't join
nemo
parents: 10511
diff changeset
   135
                return QVariant(roomWaitingIcon);
d886f319e7b3 Tired of not knowing which rooms I can't join
nemo
parents: 10511
diff changeset
   136
        }
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   137
    }
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   138
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   139
    QString content = m_data.at(row).at(column);
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   140
6983
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
   141
    if (role == Qt::DisplayRole)
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
   142
    {
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
   143
        // display room names
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
   144
        if (column == 5)
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
   145
        {
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
   146
            // special names
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
   147
            if (content[0] == '+')
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
   148
            {
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
   149
                if (content == "+rnd+") return tr("Random Map");
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
   150
                if (content == "+maze+") return tr("Random Maze");
10391
ce3ccc45d790 Add separate option for perlin gen
unc0rr
parents: 10108
diff changeset
   151
                if (content == "+perlin+") return tr("Random Perlin");
6983
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
   152
                if (content == "+drawn+") return tr("Hand-drawn");
11744
ac58a063d26a Added "Forts" to map type selection. This makes the mode easier selectable/discoverable. Also the slider can be used to adjust the distance between forts.
sheepluva
parents: 11046
diff changeset
   153
                if (content == "+forts+") return tr("Forts");
6983
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
   154
            }
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   155
6983
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
   156
            // prefix ? if map not available
8377
869f80966a77 GCI2012: Improve Game Configuration Widget
dag10
parents: 7258
diff changeset
   157
            if (!m_staticMapModel->mapExists(content) &&
869f80966a77 GCI2012: Improve Game Configuration Widget
dag10
parents: 7258
diff changeset
   158
                !m_missionMapModel->mapExists(content))
6983
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
   159
                return QString ("? %1").arg(content);
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
   160
        }
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
   161
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
   162
        return content;
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
   163
    }
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
   164
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
   165
    // dye map names red if map not available
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
   166
    if (role == Qt::ForegroundRole)
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
   167
    {
8377
869f80966a77 GCI2012: Improve Game Configuration Widget
dag10
parents: 7258
diff changeset
   168
        if (content == "+rnd+" ||
869f80966a77 GCI2012: Improve Game Configuration Widget
dag10
parents: 7258
diff changeset
   169
            content == "+maze+" ||
10391
ce3ccc45d790 Add separate option for perlin gen
unc0rr
parents: 10108
diff changeset
   170
            content == "+perlin+" ||
8377
869f80966a77 GCI2012: Improve Game Configuration Widget
dag10
parents: 7258
diff changeset
   171
            content == "+drawn+" ||
11752
3c66845947a8 rooms-list: fix "Forts" map type being displayed in dark red
sheepluva
parents: 11744
diff changeset
   172
            content == "+forts+" ||
8377
869f80966a77 GCI2012: Improve Game Configuration Widget
dag10
parents: 7258
diff changeset
   173
            m_staticMapModel->mapExists(content) ||
869f80966a77 GCI2012: Improve Game Configuration Widget
dag10
parents: 7258
diff changeset
   174
            m_missionMapModel->mapExists(content))
869f80966a77 GCI2012: Improve Game Configuration Widget
dag10
parents: 7258
diff changeset
   175
            return QVariant();
869f80966a77 GCI2012: Improve Game Configuration Widget
dag10
parents: 7258
diff changeset
   176
        else
6983
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
   177
            return QBrush(QColor("darkred"));
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
   178
    }
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
   179
6987
e34415c77342 allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents: 6983
diff changeset
   180
    if (role == Qt::TextAlignmentRole)
e34415c77342 allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents: 6983
diff changeset
   181
    {
e34415c77342 allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents: 6983
diff changeset
   182
        return (int)(Qt::AlignHCenter | Qt::AlignVCenter);
e34415c77342 allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents: 6983
diff changeset
   183
    }
e34415c77342 allow custom sorting of roomslist (by clicking on header sections)
sheepluva
parents: 6983
diff changeset
   184
6983
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
   185
    Q_ASSERT(false);
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
   186
    return QVariant();
6732
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
   187
}
6733
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   188
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   189
6733
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   190
void RoomsListModel::setRoomsList(const QStringList & rooms)
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   191
{
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   192
    beginResetModel();
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   193
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   194
    m_data.clear();
6733
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   195
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   196
    int nRooms = rooms.size();
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   197
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   198
    for (int i = 0; i < nRooms; i += c_nColumns)
6733
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   199
    {
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   200
        QStringList l;
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   201
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   202
#if QT_VERSION >= QT_VERSION_CHECK(4, 7, 0)
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   203
        l.reserve(c_nColumns);  // small optimisation not supported in old Qt
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   204
#endif
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   205
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   206
        for (int t = 0; t < c_nColumns; t++)
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   207
        {
6733
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   208
            l.append(rooms[i + t]);
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   209
        }
6733
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   210
10739
d886f319e7b3 Tired of not knowing which rooms I can't join
nemo
parents: 10511
diff changeset
   211
        m_data.append(l);
6733
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   212
    }
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   213
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   214
    endResetModel();
6733
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   215
}
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   216
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   217
6733
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   218
void RoomsListModel::addRoom(const QStringList & info)
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   219
{
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   220
    beginInsertRows(QModelIndex(), 0, 0);
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   221
10739
d886f319e7b3 Tired of not knowing which rooms I can't join
nemo
parents: 10511
diff changeset
   222
    m_data.prepend(info);
6733
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   223
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   224
    endInsertRows();
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   225
}
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   226
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   227
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   228
int RoomsListModel::rowOfRoom(const QString & name)
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   229
{
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   230
    int size = m_data.size();
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   231
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   232
    if (size < 1)
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   233
        return -1;
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   234
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   235
    int i = 0;
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   236
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   237
    // search for record with matching room name
6993
47830cf50574 room list: replace magic table column indexes with enum. makes future changes to the room list format way easier.
sheepluva
parents: 6987
diff changeset
   238
    while(m_data[i].at(NameColumn) != name)
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   239
    {
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   240
        i++;
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   241
        if(i >= size)
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   242
            return -1;
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   243
    }
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   244
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   245
    return i;
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   246
}
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   247
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   248
6733
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   249
void RoomsListModel::removeRoom(const QString & name)
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   250
{
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   251
    int i = rowOfRoom(name);
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   252
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   253
    if (i < 0)
6733
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   254
        return;
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   255
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   256
    beginRemoveRows(QModelIndex(), i, i);
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   257
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   258
    m_data.removeAt(i);
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   259
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   260
    endRemoveRows();
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   261
}
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   262
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   263
6733
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   264
void RoomsListModel::updateRoom(const QString & name, const QStringList & info)
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   265
{
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   266
    int i = rowOfRoom(name);
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   267
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   268
    if (i < 0)
6733
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   269
        return;
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   270
10739
d886f319e7b3 Tired of not knowing which rooms I can't join
nemo
parents: 10511
diff changeset
   271
    m_data[i] = info;
6733
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   272
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   273
    emit dataChanged(index(i, 0), index(i, columnCount(QModelIndex()) - 1));
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   274
}