qmlfrontend/rooms_model.cpp
author Wuzzy <Wuzzy2@mail.ru>
Fri, 31 May 2019 11:08:54 +0200
changeset 15079 424e3b132dd3
parent 15047 773beead236f
permissions -rw-r--r--
Lua: Add IsHogAlive
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
15047
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    28
#include "rooms_model.h"
7258
722e8a0d89dc - Move colorsModel to appropriate place
unc0rr
parents: 6995
diff changeset
    29
15047
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    30
RoomsListModel::RoomsListModel(QObject *parent)
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    31
    : QAbstractTableModel(parent), c_nColumns(9) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    32
  m_headerData = QStringList();
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    33
  m_headerData << tr("In progress");
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    34
  m_headerData << tr("Room Name");
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    35
  //: Caption of the column for the number of connected clients in the list of
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    36
  // rooms
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    37
  m_headerData << tr("C");
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    38
  //: Caption of the column for the number of teams in the list of rooms
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    39
  m_headerData << tr("T");
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    40
  m_headerData << tr("Owner");
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    41
  m_headerData << tr("Map");
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    42
  m_headerData << tr("Script");
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    43
  m_headerData << tr("Rules");
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    44
  m_headerData << tr("Weapons");
6983
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
    45
15047
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    46
  // m_staticMapModel = DataManager::instance().staticMapModel();
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    47
  // m_missionMapModel = DataManager::instance().missionMapModel();
6732
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
15047
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    50
QVariant RoomsListModel::headerData(int section, Qt::Orientation orientation,
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    51
                                    int role) const {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    52
  if (orientation == Qt::Vertical || role != Qt::DisplayRole)
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    53
    return QVariant();
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    54
  else
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    55
    return QVariant(m_headerData.at(section));
6732
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
15047
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    58
int RoomsListModel::rowCount(const QModelIndex &parent) const {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    59
  if (parent.isValid())
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    60
    return 0;
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    61
  else
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    62
    return m_data.size();
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
15047
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    65
int RoomsListModel::columnCount(const QModelIndex &parent) const {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    66
  if (parent.isValid())
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    67
    return 0;
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    68
  else
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    69
    return c_nColumns;
6732
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    70
}
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    71
15047
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    72
QVariant RoomsListModel::data(const QModelIndex &index, int role) const {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    73
  int column = index.column();
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    74
  int row = index.row();
6983
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
    75
15047
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    76
  // invalid index
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    77
  if (!index.isValid()) return QVariant();
6983
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
    78
15047
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    79
  // invalid row
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    80
  if ((row < 0) || (row >= m_data.size())) return QVariant();
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    81
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    82
  // invalid column
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    83
  if ((column < 0) || (column >= c_nColumns)) return QVariant();
6732
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
    84
15047
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    85
  // not a role we have data for
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    86
  if (role != Qt::DisplayRole)
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    87
    // only custom-align counters
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    88
    if ((role != Qt::TextAlignmentRole) ||
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    89
        ((column != PlayerCountColumn) && (column != TeamCountColumn)))
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    90
      // only decorate name column
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    91
      if ((role != Qt::DecorationRole) || (column != NameColumn))
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    92
        // only dye map column
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    93
        if ((role != Qt::ForegroundRole) || (column != MapColumn))
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    94
          return QVariant();
6983
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
    95
15047
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    96
  // decorate room name based on room state
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    97
  if (role == Qt::DecorationRole) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    98
    const QIcon roomBusyIcon(":/res/iconDamage.png");
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
    99
    const QIcon roomBusyIconGreen(":/res/iconDamageLockG.png");
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   100
    const QIcon roomBusyIconRed(":/res/iconDamageLockR.png");
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   101
    const QIcon roomWaitingIcon(":/res/iconTime.png");
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   102
    const QIcon roomWaitingIconGreen(":/res/iconTimeLockG.png");
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   103
    const QIcon roomWaitingIconRed(":/res/iconTimeLockR.png");
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   104
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   105
    QString flags = m_data.at(row).at(StateColumn);
6983
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
   106
15047
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   107
    if (flags.contains("g")) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   108
      if (flags.contains("j"))
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   109
        return QVariant(roomBusyIconRed);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   110
      else if (flags.contains("p"))
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   111
        return QVariant(roomBusyIconGreen);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   112
      else
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   113
        return QVariant(roomBusyIcon);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   114
    } else {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   115
      if (flags.contains("j"))
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   116
        return QVariant(roomWaitingIconRed);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   117
      else if (flags.contains("p"))
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   118
        return QVariant(roomWaitingIconGreen);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   119
      else
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   120
        return QVariant(roomWaitingIcon);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   121
    }
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   122
  }
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   123
15047
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   124
  QString content = m_data.at(row).at(column);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   125
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   126
  if (role == Qt::DisplayRole) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   127
    // display room names
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   128
    if (column == 5) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   129
      // special names
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   130
      if (content[0] == '+') {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   131
        if (content == "+rnd+") return tr("Random Map");
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   132
        if (content == "+maze+") return tr("Random Maze");
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   133
        if (content == "+perlin+") return tr("Random Perlin");
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   134
        if (content == "+drawn+") return tr("Hand-drawn");
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   135
        if (content == "+forts+") return tr("Forts");
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   136
      }
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   137
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   138
      // prefix ? if map not available
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   139
      /*if (!m_staticMapModel->mapExists(content) &&
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   140
          !m_missionMapModel->mapExists(content))
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   141
        return QString("? %1").arg(content);*/
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   142
    }
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   143
15047
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   144
    return content;
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   145
  }
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   146
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   147
  // dye map names red if map not available
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   148
  /*if (role == Qt::ForegroundRole) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   149
    if (content == "+rnd+" || content == "+maze+" || content == "+perlin+" ||
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   150
        content == "+drawn+" || content == "+forts+" ||
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   151
        m_staticMapModel->mapExists(content) ||
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   152
        m_missionMapModel->mapExists(content))
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   153
      return QVariant();
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   154
    else
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   155
      return QBrush(QColor("darkred"));
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   156
  }*/
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   157
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   158
  if (role == Qt::TextAlignmentRole) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   159
    return (int)(Qt::AlignHCenter | Qt::AlignVCenter);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   160
  }
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   161
15047
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   162
  Q_ASSERT(false);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   163
  return QVariant();
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   164
}
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   165
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   166
void RoomsListModel::setRoomsList(const QStringList &rooms) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   167
  beginResetModel();
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   168
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   169
  m_data.clear();
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   170
15047
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   171
  int nRooms = rooms.size();
6983
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
   172
15047
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   173
  for (int i = 0; i < nRooms; i += c_nColumns) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   174
    QStringList l;
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   175
    l.reserve(c_nColumns);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   176
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   177
    for (int t = 0; t < c_nColumns; t++) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   178
      l.append(rooms[i + t]);
6983
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
   179
    }
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
   180
15047
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   181
    m_data.append(l);
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   182
  }
6983
ede55af89e78 roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
sheepluva
parents: 6973
diff changeset
   183
15047
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   184
  endResetModel();
6732
c906dc78091f Start switching to rooms list model. To be continued.
unc0rr
parents:
diff changeset
   185
}
6733
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   186
15047
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   187
void RoomsListModel::addRoom(const QStringList &info) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   188
  beginInsertRows(QModelIndex(), 0, 0);
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   189
15047
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   190
  m_data.prepend(info);
6733
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   191
15047
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   192
  endInsertRows();
6733
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   193
}
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   194
15047
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   195
int RoomsListModel::rowOfRoom(const QString &name) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   196
  int size = m_data.size();
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   197
15047
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   198
  if (size < 1) return -1;
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   199
15047
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   200
  int i = 0;
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   201
15047
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   202
  // search for record with matching room name
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   203
  while (m_data[i].at(NameColumn) != name) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   204
    i++;
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   205
    if (i >= size) return -1;
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   206
  }
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   207
15047
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   208
  return i;
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   209
}
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   210
15047
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   211
void RoomsListModel::removeRoom(const QString &name) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   212
  int i = rowOfRoom(name);
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   213
15047
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   214
  if (i < 0) return;
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   215
15047
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   216
  beginRemoveRows(QModelIndex(), i, i);
6733
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   217
15047
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   218
  m_data.removeAt(i);
6733
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   219
15047
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   220
  endRemoveRows();
6733
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   221
}
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   222
15047
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   223
void RoomsListModel::updateRoom(const QString &name, const QStringList &info) {
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   224
  int i = rowOfRoom(name);
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   225
15047
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   226
  if (i < 0) return;
6973
582ca7e8e338 RoomsListModel:
sheepluva
parents: 6966
diff changeset
   227
15047
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   228
  m_data[i] = info;
6733
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   229
15047
773beead236f Add handling of some messages, reuse models from the old frontend
unc0rr
parents: 12474
diff changeset
   230
  emit dataChanged(index(i, 0), index(i, columnCount(QModelIndex()) - 1));
6733
5abbc345a82f Handle ROOM* commands in rooms list model
unc0rr
parents: 6732
diff changeset
   231
}