qmlfrontend/rooms_model.cpp
changeset 15052 773beead236f
parent 12479 42da9d8d82ab
equal deleted inserted replaced
15051:0730c68fdf97 15052:773beead236f
       
     1 /*
       
     2  * Hedgewars, a free turn based strategy game
       
     3  * Copyright (c) 2004-2015 Andrey Korotaev <unC0Rr@gmail.com>
       
     4  *
       
     5  * This program is free software; you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation; version 2 of the License
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program; if not, write to the Free Software
       
    16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
       
    17  */
       
    18 
       
    19 /**
       
    20  * @file
       
    21  * @brief RoomsListModel class implementation
       
    22  */
       
    23 
       
    24 #include <QBrush>
       
    25 #include <QColor>
       
    26 #include <QIcon>
       
    27 
       
    28 #include "rooms_model.h"
       
    29 
       
    30 RoomsListModel::RoomsListModel(QObject *parent)
       
    31     : QAbstractTableModel(parent), c_nColumns(9) {
       
    32   m_headerData = QStringList();
       
    33   m_headerData << tr("In progress");
       
    34   m_headerData << tr("Room Name");
       
    35   //: Caption of the column for the number of connected clients in the list of
       
    36   // rooms
       
    37   m_headerData << tr("C");
       
    38   //: Caption of the column for the number of teams in the list of rooms
       
    39   m_headerData << tr("T");
       
    40   m_headerData << tr("Owner");
       
    41   m_headerData << tr("Map");
       
    42   m_headerData << tr("Script");
       
    43   m_headerData << tr("Rules");
       
    44   m_headerData << tr("Weapons");
       
    45 
       
    46   // m_staticMapModel = DataManager::instance().staticMapModel();
       
    47   // m_missionMapModel = DataManager::instance().missionMapModel();
       
    48 }
       
    49 
       
    50 QVariant RoomsListModel::headerData(int section, Qt::Orientation orientation,
       
    51                                     int role) const {
       
    52   if (orientation == Qt::Vertical || role != Qt::DisplayRole)
       
    53     return QVariant();
       
    54   else
       
    55     return QVariant(m_headerData.at(section));
       
    56 }
       
    57 
       
    58 int RoomsListModel::rowCount(const QModelIndex &parent) const {
       
    59   if (parent.isValid())
       
    60     return 0;
       
    61   else
       
    62     return m_data.size();
       
    63 }
       
    64 
       
    65 int RoomsListModel::columnCount(const QModelIndex &parent) const {
       
    66   if (parent.isValid())
       
    67     return 0;
       
    68   else
       
    69     return c_nColumns;
       
    70 }
       
    71 
       
    72 QVariant RoomsListModel::data(const QModelIndex &index, int role) const {
       
    73   int column = index.column();
       
    74   int row = index.row();
       
    75 
       
    76   // invalid index
       
    77   if (!index.isValid()) return QVariant();
       
    78 
       
    79   // invalid row
       
    80   if ((row < 0) || (row >= m_data.size())) return QVariant();
       
    81 
       
    82   // invalid column
       
    83   if ((column < 0) || (column >= c_nColumns)) return QVariant();
       
    84 
       
    85   // not a role we have data for
       
    86   if (role != Qt::DisplayRole)
       
    87     // only custom-align counters
       
    88     if ((role != Qt::TextAlignmentRole) ||
       
    89         ((column != PlayerCountColumn) && (column != TeamCountColumn)))
       
    90       // only decorate name column
       
    91       if ((role != Qt::DecorationRole) || (column != NameColumn))
       
    92         // only dye map column
       
    93         if ((role != Qt::ForegroundRole) || (column != MapColumn))
       
    94           return QVariant();
       
    95 
       
    96   // decorate room name based on room state
       
    97   if (role == Qt::DecorationRole) {
       
    98     const QIcon roomBusyIcon(":/res/iconDamage.png");
       
    99     const QIcon roomBusyIconGreen(":/res/iconDamageLockG.png");
       
   100     const QIcon roomBusyIconRed(":/res/iconDamageLockR.png");
       
   101     const QIcon roomWaitingIcon(":/res/iconTime.png");
       
   102     const QIcon roomWaitingIconGreen(":/res/iconTimeLockG.png");
       
   103     const QIcon roomWaitingIconRed(":/res/iconTimeLockR.png");
       
   104 
       
   105     QString flags = m_data.at(row).at(StateColumn);
       
   106 
       
   107     if (flags.contains("g")) {
       
   108       if (flags.contains("j"))
       
   109         return QVariant(roomBusyIconRed);
       
   110       else if (flags.contains("p"))
       
   111         return QVariant(roomBusyIconGreen);
       
   112       else
       
   113         return QVariant(roomBusyIcon);
       
   114     } else {
       
   115       if (flags.contains("j"))
       
   116         return QVariant(roomWaitingIconRed);
       
   117       else if (flags.contains("p"))
       
   118         return QVariant(roomWaitingIconGreen);
       
   119       else
       
   120         return QVariant(roomWaitingIcon);
       
   121     }
       
   122   }
       
   123 
       
   124   QString content = m_data.at(row).at(column);
       
   125 
       
   126   if (role == Qt::DisplayRole) {
       
   127     // display room names
       
   128     if (column == 5) {
       
   129       // special names
       
   130       if (content[0] == '+') {
       
   131         if (content == "+rnd+") return tr("Random Map");
       
   132         if (content == "+maze+") return tr("Random Maze");
       
   133         if (content == "+perlin+") return tr("Random Perlin");
       
   134         if (content == "+drawn+") return tr("Hand-drawn");
       
   135         if (content == "+forts+") return tr("Forts");
       
   136       }
       
   137 
       
   138       // prefix ? if map not available
       
   139       /*if (!m_staticMapModel->mapExists(content) &&
       
   140           !m_missionMapModel->mapExists(content))
       
   141         return QString("? %1").arg(content);*/
       
   142     }
       
   143 
       
   144     return content;
       
   145   }
       
   146 
       
   147   // dye map names red if map not available
       
   148   /*if (role == Qt::ForegroundRole) {
       
   149     if (content == "+rnd+" || content == "+maze+" || content == "+perlin+" ||
       
   150         content == "+drawn+" || content == "+forts+" ||
       
   151         m_staticMapModel->mapExists(content) ||
       
   152         m_missionMapModel->mapExists(content))
       
   153       return QVariant();
       
   154     else
       
   155       return QBrush(QColor("darkred"));
       
   156   }*/
       
   157 
       
   158   if (role == Qt::TextAlignmentRole) {
       
   159     return (int)(Qt::AlignHCenter | Qt::AlignVCenter);
       
   160   }
       
   161 
       
   162   Q_ASSERT(false);
       
   163   return QVariant();
       
   164 }
       
   165 
       
   166 void RoomsListModel::setRoomsList(const QStringList &rooms) {
       
   167   beginResetModel();
       
   168 
       
   169   m_data.clear();
       
   170 
       
   171   int nRooms = rooms.size();
       
   172 
       
   173   for (int i = 0; i < nRooms; i += c_nColumns) {
       
   174     QStringList l;
       
   175     l.reserve(c_nColumns);
       
   176 
       
   177     for (int t = 0; t < c_nColumns; t++) {
       
   178       l.append(rooms[i + t]);
       
   179     }
       
   180 
       
   181     m_data.append(l);
       
   182   }
       
   183 
       
   184   endResetModel();
       
   185 }
       
   186 
       
   187 void RoomsListModel::addRoom(const QStringList &info) {
       
   188   beginInsertRows(QModelIndex(), 0, 0);
       
   189 
       
   190   m_data.prepend(info);
       
   191 
       
   192   endInsertRows();
       
   193 }
       
   194 
       
   195 int RoomsListModel::rowOfRoom(const QString &name) {
       
   196   int size = m_data.size();
       
   197 
       
   198   if (size < 1) return -1;
       
   199 
       
   200   int i = 0;
       
   201 
       
   202   // search for record with matching room name
       
   203   while (m_data[i].at(NameColumn) != name) {
       
   204     i++;
       
   205     if (i >= size) return -1;
       
   206   }
       
   207 
       
   208   return i;
       
   209 }
       
   210 
       
   211 void RoomsListModel::removeRoom(const QString &name) {
       
   212   int i = rowOfRoom(name);
       
   213 
       
   214   if (i < 0) return;
       
   215 
       
   216   beginRemoveRows(QModelIndex(), i, i);
       
   217 
       
   218   m_data.removeAt(i);
       
   219 
       
   220   endRemoveRows();
       
   221 }
       
   222 
       
   223 void RoomsListModel::updateRoom(const QString &name, const QStringList &info) {
       
   224   int i = rowOfRoom(name);
       
   225 
       
   226   if (i < 0) return;
       
   227 
       
   228   m_data[i] = info;
       
   229 
       
   230   emit dataChanged(index(i, 0), index(i, columnCount(QModelIndex()) - 1));
       
   231 }