QTfrontend/model/roomslistmodel.cpp
changeset 6733 5abbc345a82f
parent 6732 c906dc78091f
child 6736 efad396abee9
--- a/QTfrontend/model/roomslistmodel.cpp	Thu Feb 23 00:46:03 2012 +0400
+++ b/QTfrontend/model/roomslistmodel.cpp	Thu Feb 23 18:41:52 2012 +0400
@@ -49,3 +49,74 @@
 
     return m_data.at(index.row()).at(index.column());
 }
+
+void RoomsListModel::setRoomsList(const QStringList & rooms)
+{
+    if(m_data.size())
+    {
+        beginRemoveRows(QModelIndex(), 0, m_data.size() - 1);
+        m_data.clear();
+        endRemoveRows();
+    }
+
+    for(int i = 0; i < rooms.size(); i += 8)
+    {
+        QStringList l;
+        l.reserve(8);
+        for(int t = 0; t < 8; ++t)
+            l.append(rooms[i + t]);
+
+        m_data.append(roomInfo2RoomRecord(l));
+    }
+
+    beginInsertRows(QModelIndex(), 0, m_data.size() - 1);
+    endInsertRows();
+}
+
+void RoomsListModel::addRoom(const QStringList & info)
+{
+    beginInsertRows(QModelIndex(), 0, 0);
+
+    m_data.prepend(roomInfo2RoomRecord(info));
+
+    endInsertRows();
+}
+
+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())
+        return;
+
+    beginRemoveRows(QModelIndex(), i, i);
+
+    m_data.removeAt(i);
+
+    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())
+        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;
+    result.removeFirst();
+
+    return result;
+}