QTfrontend/model/roomslistmodel.cpp
changeset 6973 582ca7e8e338
parent 6966 eda4f63bec41
child 6983 ede55af89e78
equal deleted inserted replaced
6972:1c267028a071 6973:582ca7e8e338
    21  * @brief RoomsListModel class implementation
    21  * @brief RoomsListModel class implementation
    22  */
    22  */
    23 
    23 
    24 #include "roomslistmodel.h"
    24 #include "roomslistmodel.h"
    25 
    25 
       
    26 #include <QIcon>
       
    27 
    26 RoomsListModel::RoomsListModel(QObject *parent) :
    28 RoomsListModel::RoomsListModel(QObject *parent) :
    27     QAbstractTableModel(parent)
    29     QAbstractTableModel(parent),
       
    30     c_nColumns(8)
    28 {
    31 {
    29     m_headerData =
    32     m_headerData =
    30     QStringList()
    33     QStringList()
    31      << QString()
    34      << tr("In progress")
    32      << tr("Room Name")
    35      << tr("Room Name")
    33      << tr("C")
    36      << tr("C")
    34      << tr("T")
    37      << tr("T")
    35      << tr("Owner")
    38      << tr("Owner")
    36      << tr("Map")
    39      << tr("Map")
    37      << tr("Rules")
    40      << tr("Rules")
    38      << tr("Weapons");
    41      << tr("Weapons");
    39 }
    42 }
    40 
    43 
       
    44 
    41 QVariant RoomsListModel::headerData(int section, Qt::Orientation orientation, int role) const
    45 QVariant RoomsListModel::headerData(int section, Qt::Orientation orientation, int role) const
    42 {
    46 {
    43     if(orientation == Qt::Vertical || role != Qt::DisplayRole)
    47     if(orientation == Qt::Vertical || role != Qt::DisplayRole)
    44         return QVariant();
    48         return QVariant();
    45     else
    49     else
    46         return QVariant(m_headerData.at(section));
    50         return QVariant(m_headerData.at(section));
    47 }
    51 }
    48 
    52 
       
    53 
    49 int RoomsListModel::rowCount(const QModelIndex & parent) const
    54 int RoomsListModel::rowCount(const QModelIndex & parent) const
    50 {
    55 {
    51     if(parent.isValid())
    56     if(parent.isValid())
    52         return 0;
    57         return 0;
    53     else
    58     else
    54         return m_data.size();
    59         return m_data.size();
    55 }
    60 }
    56 
    61 
       
    62 
    57 int RoomsListModel::columnCount(const QModelIndex & parent) const
    63 int RoomsListModel::columnCount(const QModelIndex & parent) const
    58 {
    64 {
    59     if(parent.isValid())
    65     if(parent.isValid())
    60         return 0;
    66         return 0;
    61     else
    67     else
    62         return 8;
    68         return c_nColumns;
    63 }
    69 }
       
    70 
    64 
    71 
    65 QVariant RoomsListModel::data(const QModelIndex &index, int role) const
    72 QVariant RoomsListModel::data(const QModelIndex &index, int role) const
    66 {
    73 {
    67     if (!index.isValid() || index.row() < 0
    74     int column = index.column();
    68             || index.row() >= m_data.size()
    75     int row = index.row();
    69             || index.column() >= 8
    76     
    70             || (role != Qt::EditRole && role != Qt::DisplayRole)
    77     if (!index.isValid() || (row < 0)
       
    78             || (row >= m_data.size())
       
    79             || (column >= c_nColumns)
       
    80             || ((role != Qt::DecorationRole) && (role != Qt::DisplayRole))
    71        )
    81        )
    72         return QVariant();
    82         return QVariant();
    73 
    83 
    74     return m_data.at(index.row()).at(index.column());
    84     // decorate room name based on room state
    75 }
    85     if (role == Qt::DecorationRole)
       
    86     {
       
    87         if (column != 1)
       
    88             return QVariant();
       
    89 
       
    90         const QIcon roomBusyIcon(":/res/iconDamage.png");
       
    91         const QIcon roomWaitingIcon(":/res/iconTime.png");
       
    92 
       
    93         if (m_data.at(row).at(0).isEmpty())
       
    94             return QVariant(roomWaitingIcon);
       
    95         else
       
    96             return QVariant(roomBusyIcon);
       
    97     }
       
    98 
       
    99     QString content = m_data.at(row).at(column);
       
   100 
       
   101     if (column == 0)
       
   102         return QVariant(!content.isEmpty());
       
   103 
       
   104     return content;
       
   105 }
       
   106 
    76 
   107 
    77 void RoomsListModel::setRoomsList(const QStringList & rooms)
   108 void RoomsListModel::setRoomsList(const QStringList & rooms)
    78 {
   109 {
    79     if(m_data.size())
   110     beginResetModel();
    80     {
   111 
    81         beginRemoveRows(QModelIndex(), 0, m_data.size() - 1);
   112     m_data.clear();
    82         m_data.clear();
   113 
    83         endRemoveRows();
   114     int nRooms = rooms.size();
    84     }
   115 
    85 
   116     for (int i = 0; i < nRooms; i += c_nColumns)
    86     for(int i = 0; i < rooms.size(); i += 8)
       
    87     {
   117     {
    88         QStringList l;
   118         QStringList l;
    89         //l.reserve(8);  not really that useful an optimisation and causes problems w/ old Qt.  Harmless to leave it out.
   119 
    90         for(int t = 0; t < 8; ++t)
   120 #if QT_VERSION >= QT_VERSION_CHECK(4, 7, 0)
       
   121         l.reserve(c_nColumns);  // small optimisation not supported in old Qt
       
   122 #endif
       
   123 
       
   124         for (int t = 0; t < c_nColumns; t++)
       
   125         {
    91             l.append(rooms[i + t]);
   126             l.append(rooms[i + t]);
       
   127         }
    92 
   128 
    93         m_data.append(roomInfo2RoomRecord(l));
   129         m_data.append(roomInfo2RoomRecord(l));
    94     }
   130     }
    95 
   131 
    96     beginInsertRows(QModelIndex(), 0, m_data.size() - 1);
   132     endResetModel();
       
   133 }
       
   134 
       
   135 
       
   136 void RoomsListModel::addRoom(const QStringList & info)
       
   137 {
       
   138     beginInsertRows(QModelIndex(), 0, 0);
       
   139 
       
   140     m_data.prepend(roomInfo2RoomRecord(info));
       
   141 
    97     endInsertRows();
   142     endInsertRows();
    98 }
   143 }
    99 
   144 
   100 void RoomsListModel::addRoom(const QStringList & info)
   145 
   101 {
   146 int RoomsListModel::rowOfRoom(const QString & name)
   102     beginInsertRows(QModelIndex(), 0, 0);
   147 {
   103 
   148     int size = m_data.size();
   104     m_data.prepend(roomInfo2RoomRecord(info));
   149 
   105 
   150     if (size < 1)
   106     endInsertRows();
   151         return -1;
   107 }
   152 
       
   153     int i = 0;
       
   154 
       
   155     // search for record with matching room name
       
   156     while(m_data[i].at(1) != name)
       
   157     {
       
   158         i++;
       
   159         if(i >= size)
       
   160             return -1;
       
   161     }
       
   162 
       
   163     return i;
       
   164 }
       
   165 
   108 
   166 
   109 void RoomsListModel::removeRoom(const QString & name)
   167 void RoomsListModel::removeRoom(const QString & name)
   110 {
   168 {
   111     int i = 0;
   169     int i = rowOfRoom(name);
   112     while(i < m_data.size() && m_data[i].at(0) != name)
   170 
   113         ++i;
   171     if (i < 0)
   114     if(i >= m_data.size())
       
   115         return;
   172         return;
   116 
   173 
   117     beginRemoveRows(QModelIndex(), i, i);
   174     beginRemoveRows(QModelIndex(), i, i);
   118 
   175 
   119     m_data.removeAt(i);
   176     m_data.removeAt(i);
   120 
   177 
   121     endRemoveRows();
   178     endRemoveRows();
   122 }
   179 }
   123 
   180 
       
   181 
   124 void RoomsListModel::updateRoom(const QString & name, const QStringList & info)
   182 void RoomsListModel::updateRoom(const QString & name, const QStringList & info)
   125 {
   183 {
   126     int i = 0;
   184     int i = rowOfRoom(name);
   127     while(i < m_data.size() && m_data[i].at(0) != name)
   185 
   128         ++i;
   186     if (i < 0)
   129     if(i >= m_data.size())
       
   130         return;
   187         return;
   131 
   188 
   132 
       
   133     m_data[i] = roomInfo2RoomRecord(info);
   189     m_data[i] = roomInfo2RoomRecord(info);
   134 
   190 
   135     emit dataChanged(index(i, 0), index(i, columnCount(QModelIndex()) - 1));
   191     emit dataChanged(index(i, 0), index(i, columnCount(QModelIndex()) - 1));
   136 }
   192 }
   137 
   193 
       
   194 
   138 QStringList RoomsListModel::roomInfo2RoomRecord(const QStringList & info)
   195 QStringList RoomsListModel::roomInfo2RoomRecord(const QStringList & info)
   139 {
   196 {
   140     QStringList result;
   197     QStringList result;
   141 
   198 
   142     result = info;
   199     result = info;
   143 
   200 
       
   201     // for matters of less memory usage and quicker access store
       
   202     // the boolean string as either "t" or empty
       
   203     if (info[0].toLower() == "true")
       
   204         result[0] = "t";
       
   205     else
       
   206         result[0] = QString();
       
   207 
   144     return result;
   208     return result;
   145 }
   209 }