Be lazy in loading maps list
authorunc0rr
Tue, 03 Dec 2013 23:54:58 +0400
changeset 9744 1ee4842a9c86
parent 9743 6e505ea936ad
child 9745 258c320256dc
Be lazy in loading maps list
QTfrontend/model/MapModel.cpp
QTfrontend/model/MapModel.h
QTfrontend/ui/widget/mapContainer.cpp
QTfrontend/ui/widget/mapContainer.h
QTfrontend/util/DataManager.cpp
--- a/QTfrontend/model/MapModel.cpp	Tue Dec 03 18:09:54 2013 +0100
+++ b/QTfrontend/model/MapModel.cpp	Tue Dec 03 23:54:58 2013 +0400
@@ -32,8 +32,22 @@
 MapModel::MapInfo MapModel::MapInfoMaze = {MapModel::GeneratedMaze, "+maze+", "", 0, "", "", "", false};
 MapModel::MapInfo MapModel::MapInfoDrawn = {MapModel::HandDrawnMap, "+drawn+", "", 0, "", "", "", false};
 
-void MapModel::loadMaps(MapType maptype)
+
+MapModel::MapModel(MapType maptype, QObject *parent) : QStandardItemModel(parent)
 {
+    m_maptype = maptype;
+    m_loaded = false;
+}
+
+bool MapModel::loadMaps()
+{
+    if(m_loaded)
+        return false;
+
+    m_loaded = true;
+
+    qDebug("[LAZINESS] MapModel::loadMaps()");
+
     // this method resets the contents of this model (important to know for views).
     beginResetModel();
 
@@ -75,7 +89,7 @@
             MapType type = isMission ? MissionMap : StaticMap;
 
             // if we're supposed to ignore this type, continue
-            if (type != maptype) continue;
+            if (type != m_maptype) continue;
 
             // load map info from file
             QTextStream input(&mapCfgFile);
@@ -149,15 +163,19 @@
     QStandardItemModel::appendColumn(mapList);
 
     endResetModel();
+
+    return true;
 }
 
-bool MapModel::mapExists(const QString & map) const
+bool MapModel::mapExists(const QString & map)
 {
     return findMap(map) >= 0;
 }
 
-int MapModel::findMap(const QString & map) const
+int MapModel::findMap(const QString & map)
 {
+    loadMaps();
+
     return m_mapIndexes.value(map, -1);
 }
 
--- a/QTfrontend/model/MapModel.h	Tue Dec 03 18:09:54 2013 +0100
+++ b/QTfrontend/model/MapModel.h	Tue Dec 03 23:54:58 2013 +0400
@@ -67,12 +67,14 @@
             bool dlc; ///< True if this map was not packaged with the game
         };
 
+        MapModel(MapType maptype, QObject *parent = 0);
+
         /**
          * @brief Searches maps in model to find out if one exists
          * @param map map of which to check existence
          * @return true if it exists
          */
-        bool mapExists(const QString & map) const;
+        bool mapExists(const QString & map);
 
         /**
          * @brief Finds a map index (column, row) for a map name
@@ -86,7 +88,7 @@
          * @param map map of which to find index
          * @return int of index, or -1 if map not found
          */
-        int findMap(const QString & map) const;
+        int findMap(const QString & map);
 
         /**
          * @brief Finds and returns a map item for a map name
@@ -98,16 +100,16 @@
         // Static MapInfos for drawn and generated maps
         static MapInfo MapInfoRandom, MapInfoMaze, MapInfoDrawn;
 
-    public slots:
-        /// Reloads the maps using the DataManager.
-        /// Accepts two map types: StaticMap or MissionMap.
-        void loadMaps(MapType maptype);
+        /// Loads the maps
+        bool loadMaps();
 
 
     private:
         /// map index lookup table. QPair<int, int> contains: <column, index>
         //QHash<QString, QPair<int, int> > m_mapIndexes;
         QHash<QString, int> m_mapIndexes;
+        MapType m_maptype;
+        bool m_loaded;
 
         /**
          * @brief Creates a QStandardItem, that holds the map info and item appearance.
--- a/QTfrontend/ui/widget/mapContainer.cpp	Tue Dec 03 18:09:54 2013 +0100
+++ b/QTfrontend/ui/widget/mapContainer.cpp	Tue Dec 03 23:54:58 2013 +0400
@@ -57,6 +57,8 @@
 {
     // don't show preview anything until first show event
     m_previewEnabled = false;
+    m_missionsViewSetup = false;
+    m_staticViewSetup = false;
 
     hhSmall.load(":/res/hh_small.png");
     hhLimit = 18;
@@ -161,28 +163,14 @@
     /* Static maps list */
 
     staticMapList = new QListView;
-    staticMapList->setModel(m_staticMapModel);
     rightLayout->addWidget(staticMapList, 1);
-    staticMapList->setEditTriggers(QAbstractItemView::NoEditTriggers);
     m_childWidgets << staticMapList;
-    QItemSelectionModel * staticSelectionModel = staticMapList->selectionModel();
-    connect(staticSelectionModel,
-            SIGNAL(currentRowChanged(const QModelIndex &, const QModelIndex &)),
-            this,
-            SLOT(staticMapChanged(const QModelIndex &, const QModelIndex &)));
 
     /* Mission maps list */
 
-    missionMapList = new QListView;
-    missionMapList->setModel(m_missionMapModel);
-    missionMapList->setEditTriggers(QAbstractItemView::NoEditTriggers);
+    missionMapList = new QListView(this);
     rightLayout->addWidget(missionMapList, 1);
     m_childWidgets << missionMapList;
-    QItemSelectionModel * missionSelectionModel = missionMapList->selectionModel();
-    connect(missionSelectionModel,
-            SIGNAL(currentRowChanged(const QModelIndex &, const QModelIndex &)),
-            this,
-            SLOT(missionMapChanged(const QModelIndex &, const QModelIndex &)));
 
     /* Map load and edit buttons */
 
@@ -742,6 +730,7 @@
             btnEditMap->show();
             break;
         case MapModel::MissionMap:
+            setupMissionMapsView();
             mapgen = MAPGEN_MAP;
             missionMapChanged(newMap.isValid() ? newMap : missionMapList->currentIndex());
             lblMapList->setText(tr("Mission:"));
@@ -752,6 +741,7 @@
             emit mapChanged(m_curMap);
             break;
         case MapModel::StaticMap:
+            setupStaticMapsView();
             mapgen = MAPGEN_MAP;
             staticMapChanged(newMap.isValid() ? newMap : staticMapList->currentIndex());
             lblMapList->setText(tr("Map:"));
@@ -951,3 +941,35 @@
     btnTheme->setIcon(QIcon());
     btnTheme->setText(tr("Theme: %1").arg(name));
 }
+
+void HWMapContainer::setupMissionMapsView()
+{
+    if(m_missionsViewSetup) return;
+    m_missionsViewSetup = true;
+
+    m_missionMapModel->loadMaps();
+    missionMapList->setModel(m_missionMapModel);
+    missionMapList->setEditTriggers(QAbstractItemView::NoEditTriggers);
+    QItemSelectionModel * missionSelectionModel = missionMapList->selectionModel();
+    connect(missionSelectionModel,
+            SIGNAL(currentRowChanged(const QModelIndex &, const QModelIndex &)),
+            this,
+            SLOT(missionMapChanged(const QModelIndex &, const QModelIndex &)));
+    missionSelectionModel->setCurrentIndex(m_missionMapModel->index(0, 0), QItemSelectionModel::Clear | QItemSelectionModel::SelectCurrent);
+}
+
+void HWMapContainer::setupStaticMapsView()
+{
+    if(m_staticViewSetup) return;
+    m_staticViewSetup = true;
+
+    m_staticMapModel->loadMaps();
+    staticMapList->setModel(m_staticMapModel);
+    staticMapList->setEditTriggers(QAbstractItemView::NoEditTriggers);
+    QItemSelectionModel * staticSelectionModel = staticMapList->selectionModel();
+    connect(staticSelectionModel,
+            SIGNAL(currentRowChanged(const QModelIndex &, const QModelIndex &)),
+            this,
+            SLOT(staticMapChanged(const QModelIndex &, const QModelIndex &)));
+    staticSelectionModel->setCurrentIndex(m_staticMapModel->index(0, 0), QItemSelectionModel::Clear | QItemSelectionModel::SelectCurrent);
+}
--- a/QTfrontend/ui/widget/mapContainer.h	Tue Dec 03 18:09:54 2013 +0100
+++ b/QTfrontend/ui/widget/mapContainer.h	Tue Dec 03 23:54:58 2013 +0400
@@ -151,6 +151,8 @@
         bool m_master;
         QList<QWidget *> m_childWidgets;
         bool m_previewEnabled;
+        bool m_missionsViewSetup;
+        bool m_staticViewSetup;
 
         void intSetSeed(const QString & seed);
         void intSetMap(const QString & map);
@@ -163,6 +165,8 @@
         void changeMapType(MapModel::MapType type, const QModelIndex & newMap = QModelIndex());
         void updatePreview();
         void updateThemeButtonSize();
+        void setupMissionMapsView();
+        void setupStaticMapsView();
 
         MapModel::MapInfo m_mapInfo;
         int m_themeID;
--- a/QTfrontend/util/DataManager.cpp	Tue Dec 03 18:09:54 2013 +0100
+++ b/QTfrontend/util/DataManager.cpp	Tue Dec 03 23:54:58 2013 +0400
@@ -98,8 +98,7 @@
 MapModel * DataManager::staticMapModel()
 {
     if (m_staticMapModel == NULL) {
-        m_staticMapModel = new MapModel();
-        m_staticMapModel->loadMaps(MapModel::StaticMap);
+        m_staticMapModel = new MapModel(MapModel::StaticMap, this);
     }
     return m_staticMapModel;
 }
@@ -107,8 +106,7 @@
 MapModel * DataManager::missionMapModel()
 {
     if (m_missionMapModel == NULL) {
-        m_missionMapModel = new MapModel();
-        m_missionMapModel->loadMaps(MapModel::MissionMap);
+        m_missionMapModel = new MapModel(MapModel::MissionMap, this);
     }
     return m_missionMapModel;
 }