RoomsListModel:
authorsheepluva
Tue, 01 May 2012 14:57:40 +0200
changeset 6973 582ca7e8e338
parent 6972 1c267028a071
child 6974 2d3beb445d64
RoomsListModel: + in progress icon * fixed room delete and update (name match lookup was done in wrong column)
QTfrontend/model/roomslistmodel.cpp
QTfrontend/model/roomslistmodel.h
--- a/QTfrontend/model/roomslistmodel.cpp	Tue May 01 11:39:24 2012 +0100
+++ b/QTfrontend/model/roomslistmodel.cpp	Tue May 01 14:57:40 2012 +0200
@@ -23,12 +23,15 @@
 
 #include "roomslistmodel.h"
 
+#include <QIcon>
+
 RoomsListModel::RoomsListModel(QObject *parent) :
-    QAbstractTableModel(parent)
+    QAbstractTableModel(parent),
+    c_nColumns(8)
 {
     m_headerData =
     QStringList()
-     << QString()
+     << tr("In progress")
      << tr("Room Name")
      << tr("C")
      << tr("T")
@@ -38,6 +41,7 @@
      << tr("Weapons");
 }
 
+
 QVariant RoomsListModel::headerData(int section, Qt::Orientation orientation, int role) const
 {
     if(orientation == Qt::Vertical || role != Qt::DisplayRole)
@@ -46,6 +50,7 @@
         return QVariant(m_headerData.at(section));
 }
 
+
 int RoomsListModel::rowCount(const QModelIndex & parent) const
 {
     if(parent.isValid())
@@ -54,49 +59,80 @@
         return m_data.size();
 }
 
+
 int RoomsListModel::columnCount(const QModelIndex & parent) const
 {
     if(parent.isValid())
         return 0;
     else
-        return 8;
+        return c_nColumns;
 }
 
+
 QVariant RoomsListModel::data(const QModelIndex &index, int role) const
 {
-    if (!index.isValid() || index.row() < 0
-            || index.row() >= m_data.size()
-            || index.column() >= 8
-            || (role != Qt::EditRole && role != Qt::DisplayRole)
+    int column = index.column();
+    int row = index.row();
+    
+    if (!index.isValid() || (row < 0)
+            || (row >= m_data.size())
+            || (column >= c_nColumns)
+            || ((role != Qt::DecorationRole) && (role != Qt::DisplayRole))
        )
         return QVariant();
 
-    return m_data.at(index.row()).at(index.column());
+    // decorate room name based on room state
+    if (role == Qt::DecorationRole)
+    {
+        if (column != 1)
+            return QVariant();
+
+        const QIcon roomBusyIcon(":/res/iconDamage.png");
+        const QIcon roomWaitingIcon(":/res/iconTime.png");
+
+        if (m_data.at(row).at(0).isEmpty())
+            return QVariant(roomWaitingIcon);
+        else
+            return QVariant(roomBusyIcon);
+    }
+
+    QString content = m_data.at(row).at(column);
+
+    if (column == 0)
+        return QVariant(!content.isEmpty());
+
+    return content;
 }
 
+
 void RoomsListModel::setRoomsList(const QStringList & rooms)
 {
-    if(m_data.size())
-    {
-        beginRemoveRows(QModelIndex(), 0, m_data.size() - 1);
-        m_data.clear();
-        endRemoveRows();
-    }
+    beginResetModel();
+
+    m_data.clear();
 
-    for(int i = 0; i < rooms.size(); i += 8)
+    int nRooms = rooms.size();
+
+    for (int i = 0; i < nRooms; i += c_nColumns)
     {
         QStringList l;
-        //l.reserve(8);  not really that useful an optimisation and causes problems w/ old Qt.  Harmless to leave it out.
-        for(int t = 0; t < 8; ++t)
+
+#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(roomInfo2RoomRecord(l));
     }
 
-    beginInsertRows(QModelIndex(), 0, m_data.size() - 1);
-    endInsertRows();
+    endResetModel();
 }
 
+
 void RoomsListModel::addRoom(const QStringList & info)
 {
     beginInsertRows(QModelIndex(), 0, 0);
@@ -106,12 +142,33 @@
     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(1) != name)
+    {
+        i++;
+        if(i >= size)
+            return -1;
+    }
+
+    return i;
+}
+
+
 void RoomsListModel::removeRoom(const QString & name)
 {
-    int i = 0;
-    while(i < m_data.size() && m_data[i].at(0) != name)
-        ++i;
-    if(i >= m_data.size())
+    int i = rowOfRoom(name);
+
+    if (i < 0)
         return;
 
     beginRemoveRows(QModelIndex(), i, i);
@@ -121,25 +178,32 @@
     endRemoveRows();
 }
 
+
 void RoomsListModel::updateRoom(const QString & name, const QStringList & info)
 {
-    int i = 0;
-    while(i < m_data.size() && m_data[i].at(0) != name)
-        ++i;
-    if(i >= m_data.size())
+    int i = rowOfRoom(name);
+
+    if (i < 0)
         return;
 
-
     m_data[i] = roomInfo2RoomRecord(info);
 
     emit dataChanged(index(i, 0), index(i, columnCount(QModelIndex()) - 1));
 }
 
+
 QStringList RoomsListModel::roomInfo2RoomRecord(const QStringList & info)
 {
     QStringList result;
 
     result = info;
 
+    // for matters of less memory usage and quicker access store
+    // the boolean string as either "t" or empty
+    if (info[0].toLower() == "true")
+        result[0] = "t";
+    else
+        result[0] = QString();
+
     return result;
 }
--- a/QTfrontend/model/roomslistmodel.h	Tue May 01 11:39:24 2012 +0100
+++ b/QTfrontend/model/roomslistmodel.h	Tue May 01 14:57:40 2012 +0200
@@ -43,8 +43,10 @@
     void addRoom(const QStringList & info);
     void removeRoom(const QString & name);
     void updateRoom(const QString & name, const QStringList & info);
+    int rowOfRoom(const QString & name);
 
 private:
+    const int c_nColumns;
     QList<QStringList> m_data;
     QStringList m_headerData;