roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps
authorsheepluva
Tue, 01 May 2012 21:24:04 +0200
changeset 6983 ede55af89e78
parent 6982 8d41d22a291d
child 6984 3de40b10eafa
roomslistmodel: prettier names for map +rnd+ etc.; point out unavailable maps some other small tweaks and fixes
QTfrontend/model/MapModel.cpp
QTfrontend/model/MapModel.h
QTfrontend/model/roomslistmodel.cpp
QTfrontend/model/roomslistmodel.h
QTfrontend/ui/widget/gamecfgwidget.cpp
QTfrontend/ui/widget/hedgehogerWidget.cpp
QTfrontend/ui/widget/mapContainer.cpp
--- a/QTfrontend/model/MapModel.cpp	Tue May 01 19:56:55 2012 +0200
+++ b/QTfrontend/model/MapModel.cpp	Tue May 01 21:24:04 2012 +0200
@@ -148,18 +148,35 @@
     items.append(separator.clone());
     items.append(staticMaps);
 
+
+    // create row-index lookup table
+
+    m_mapIndexes.clear();
+
+    int count = items.size();
+
+    for (int i = 0; i < count; i++)
+    {
+        QStandardItem * si = items.at(i);
+        QVariant v = si->data(Qt::UserRole + 1);
+        if (v.canConvert<MapInfo>())
+            m_mapIndexes.insert(v.value<MapInfo>().name, i);
+    }
+
+
     // store start-index and count of relevant types
-    typeLoc.insert(GeneratedMap, QPair<int,int>(0, 1));
-    typeLoc.insert(GeneratedMaze, QPair<int,int>(1, 1));
-    typeLoc.insert(HandDrawnMap, QPair<int,int>(2, 1));
+
+    m_typeLoc.insert(GeneratedMap, QPair<int,int>(0, 1));
+    m_typeLoc.insert(GeneratedMaze, QPair<int,int>(1, 1));
+    m_typeLoc.insert(HandDrawnMap, QPair<int,int>(2, 1));
     // mission maps
     int startIdx = genMaps.size() + 2; // start after genMaps and 2 separators
-    int count = missionMaps.size();
-    typeLoc.insert(MissionMap, QPair<int,int>(startIdx, count));
+    count = missionMaps.size();
+    m_typeLoc.insert(MissionMap, QPair<int,int>(startIdx, count));
     // static maps
     startIdx += count + 1; // start after missions and 2 separators
     count = staticMaps.size();
-    typeLoc.insert(StaticMap, QPair<int,int>(startIdx, count));
+    m_typeLoc.insert(StaticMap, QPair<int,int>(startIdx, count));
 
     // store list contents in the item model
     QStandardItemModel::appendColumn(items);
@@ -169,18 +186,10 @@
 }
 
 
-int MapModel::mapCount(MapType type) const
-{
-    // return the count for this type
-    // fetch it from the second int in typeLoc, return 0 if no entry
-    return typeLoc.value(type, QPair<int,int>(0,0)).second;
-}
-
-
 int MapModel::randomMap(MapType type) const
 {
     // return a random index for this type or -1 if none available
-    QPair<int,int> loc = typeLoc.value(type, QPair<int,int>(-1,0));
+    QPair<int,int> loc = m_typeLoc.value(type, QPair<int,int>(-1,0));
 
     int startIdx = loc.first;
     int count = loc.second;
@@ -220,3 +229,10 @@
 
     return item;
 }
+
+
+int MapModel::indexOf(const QString & map) const
+{
+    return m_mapIndexes.value(map, -1);
+}
+
--- a/QTfrontend/model/MapModel.h	Tue May 01 19:56:55 2012 +0200
+++ b/QTfrontend/model/MapModel.h	Tue May 01 21:24:04 2012 +0200
@@ -27,6 +27,7 @@
 #include <QStandardItemModel>
 #include <QStringList>
 #include <QTextStream>
+#include <QHash>
 #include <QMap>
 #include <QIcon>
 #include <QComboBox>
@@ -65,11 +66,11 @@
         };
 
         /**
-         * @brief Returns the number of available maps of a specified type.
-         * @param type map type to get the count of.
-         * @return count of maps that have the specified type.
+         * @brief Returns the row-index of the given map.
+         * @param map map of which to get the row-index of.
+         * @return row-index of map or -1 if not available.
          */
-        int mapCount(MapType type) const;
+        int indexOf(const QString & map) const;
 
         /**
          * @brief Returns the row-index of a random map with a specified type.
@@ -78,7 +79,6 @@
          */
         int randomMap(MapType type) const;
 
-
     public slots:
         /// Reloads the maps using the DataManager.
         void loadMaps();
@@ -86,7 +86,10 @@
 
     private:
         /// start-index and map count for each map-type.
-        QMap<MapType, QPair<int,int> > typeLoc;
+        QMap<MapType, QPair<int,int> > m_typeLoc;
+
+        /// map index lookup table
+        QHash<QString, int> m_mapIndexes;
 
         /**
          * @brief Creates a QStandardItem, that holds the map info and item appearance.
--- a/QTfrontend/model/roomslistmodel.cpp	Tue May 01 19:56:55 2012 +0200
+++ b/QTfrontend/model/roomslistmodel.cpp	Tue May 01 21:24:04 2012 +0200
@@ -23,6 +23,8 @@
 
 #include "roomslistmodel.h"
 
+#include <QBrush>
+#include <QColor>
 #include <QIcon>
 
 RoomsListModel::RoomsListModel(QObject *parent) :
@@ -39,6 +41,8 @@
      << tr("Map")
      << tr("Rules")
      << tr("Weapons");
+
+    m_mapModel = DataManager::instance().mapModel();
 }
 
 
@@ -73,20 +77,30 @@
 {
     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))
-       )
+
+    // invalid index
+    if (!index.isValid())
+        return QVariant();
+
+    // invalid row
+    if ((row < 0) || (row >= m_data.size()))
         return QVariant();
 
+    // invalid column
+    if ((column < 0) || (column >= c_nColumns))
+        return QVariant();
+
+    // not a role we have data for
+    if (role != Qt::DisplayRole)
+        // only decorate name column
+        if ((role != Qt::DecorationRole) || (column != 1))
+            // only dye map column
+            if ((role != Qt::ForegroundRole) || (column != 5))
+                return QVariant();
+
     // 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");
 
@@ -98,10 +112,42 @@
 
     QString content = m_data.at(row).at(column);
 
-    if (column == 0)
-        return QVariant(!content.isEmpty());
+    if (role == Qt::DisplayRole)
+    {
+        // supply in progress flag as bool
+        if (column == 0)
+            return QVariant(!content.isEmpty());
+
+        // display room names
+        if (column == 5)
+        {
+            // special names
+            if (content[0] == '+')
+            {
+                if (content == "+rnd+") return tr("Random Map");
+                if (content == "+maze+") return tr("Random Maze");
+                if (content == "+drawn+") return tr("Hand-drawn");
+            }
 
-    return content;
+            // prefix ? if map not available
+            if ((m_mapModel->indexOf(content) < 0))
+                return QString ("? %1").arg(content);
+        }
+
+        return content;
+    }
+
+    // dye map names red if map not available
+    if (role == Qt::ForegroundRole)
+    {
+        if ((m_mapModel->indexOf(content) < 0))
+            return QBrush(QColor("darkred"));
+        else
+            return QVariant();
+    }
+
+    Q_ASSERT(false);
+    return QVariant();
 }
 
 
--- a/QTfrontend/model/roomslistmodel.h	Tue May 01 19:56:55 2012 +0200
+++ b/QTfrontend/model/roomslistmodel.h	Tue May 01 21:24:04 2012 +0200
@@ -27,6 +27,8 @@
 #include <QAbstractTableModel>
 #include <QStringList>
 
+#include "DataManager.h"
+
 class RoomsListModel : public QAbstractTableModel
 {
     Q_OBJECT
@@ -49,6 +51,7 @@
     const int c_nColumns;
     QList<QStringList> m_data;
     QStringList m_headerData;
+    MapModel * m_mapModel;
 
     QStringList roomInfo2RoomRecord(const QStringList & info);
 };
--- a/QTfrontend/ui/widget/gamecfgwidget.cpp	Tue May 01 19:56:55 2012 +0200
+++ b/QTfrontend/ui/widget/gamecfgwidget.cpp	Tue May 01 21:24:04 2012 +0200
@@ -57,6 +57,7 @@
     GBoxOptionsLayout->addWidget(Scripts, 1, 1);
 
     Scripts->setModel(DataManager::instance().gameStyleModel());
+    m_curScript = Scripts->currentText();
     connect(Scripts, SIGNAL(currentIndexChanged(int)), this, SLOT(scriptChanged(int)));
 
     QWidget *SchemeWidget = new QWidget(GBoxOptions);
--- a/QTfrontend/ui/widget/hedgehogerWidget.cpp	Tue May 01 19:56:55 2012 +0200
+++ b/QTfrontend/ui/widget/hedgehogerWidget.cpp	Tue May 01 21:24:04 2012 +0200
@@ -105,8 +105,7 @@
     }
 
     QPainter painter(this);
-    const QFont font("MS Shell Dlg", 12);
-    painter.setFont(font);
-    painter.drawText(this->width() - 12, 24, QString::number(numItems));
+    painter.setFont(QFont("MS Shell Dlg", 10, QFont::Bold));
+    painter.drawText(this->width() - 12, 23, QString::number(numItems));
 
 }
--- a/QTfrontend/ui/widget/mapContainer.cpp	Tue May 01 19:56:55 2012 +0200
+++ b/QTfrontend/ui/widget/mapContainer.cpp	Tue May 01 21:24:04 2012 +0200
@@ -376,24 +376,9 @@
 
 void HWMapContainer::intSetMap(const QString & map)
 {
-    int id = 0;
-    for(int i = 0; i < chooseMap->count(); i++)
-    {
-        QVariant data = chooseMap->itemData(i, Qt::UserRole + 1);
-        // skip separators etc
-        if (!data.isValid())
-            continue;
-        Q_ASSERT(data.canConvert<MapModel::MapInfo>());
-        MapModel::MapInfo mapInfo = data.value<MapModel::MapInfo>();
+    int id = m_mapModel->indexOf(map);
 
-        if (mapInfo.name == map)
-        {
-            id = i;
-            break;
-        }
-    }
-
-    if(id > 0)
+    if(id >= 0)
     {
         if (pMap)
         {