Load themes lazily
authorunc0rr
Wed, 04 Dec 2013 00:19:42 +0400
changeset 9745 258c320256dc
parent 9744 1ee4842a9c86
child 9746 64abf9862562
Load themes lazily
QTfrontend/model/ThemeModel.cpp
QTfrontend/model/ThemeModel.h
QTfrontend/ui/widget/mapContainer.cpp
QTfrontend/util/DataManager.cpp
--- a/QTfrontend/model/ThemeModel.cpp	Tue Dec 03 23:54:58 2013 +0400
+++ b/QTfrontend/model/ThemeModel.cpp	Wed Dec 04 00:19:42 2013 +0400
@@ -29,6 +29,8 @@
     QAbstractListModel(parent)
 {
     m_data = QList<QMap<int, QVariant> >();
+
+    m_themesLoaded = false;
 }
 
 int ThemeModel::rowCount(const QModelIndex &parent) const
@@ -36,7 +38,11 @@
     if(parent.isValid())
         return 0;
     else
+    {
+        if(!m_themesLoaded)
+            loadThemes();
         return m_data.size();
+    }
 }
 
 
@@ -45,13 +51,21 @@
     if(index.column() > 0 || index.row() >= m_data.size())
         return QVariant();
     else
+    {
+        if(!m_themesLoaded)
+            loadThemes();
+
         return m_data.at(index.row()).value(role);
+    }
 }
 
 
-void ThemeModel::loadThemes()
+void ThemeModel::loadThemes() const
 {
-    beginResetModel();
+    qDebug("[LAZINESS ThemeModel::loadThemes()]");
+
+    m_themesLoaded = true;
+
 
     DataManager & datamgr = DataManager::instance();
 
@@ -94,7 +108,4 @@
 
         m_data.append(dataset);
     }
-
-
-    endResetModel();
 }
--- a/QTfrontend/model/ThemeModel.h	Tue Dec 03 23:54:58 2013 +0400
+++ b/QTfrontend/model/ThemeModel.h	Wed Dec 04 00:19:42 2013 +0400
@@ -45,14 +45,11 @@
         int rowCount(const QModelIndex &parent = QModelIndex()) const;
         QVariant data(const QModelIndex &index, int role) const;
 
+    private:
+        mutable QList<QMap<int, QVariant> > m_data;
+        mutable bool m_themesLoaded;
 
-    public slots:
-        /// reloads the themes from the DataManager
-        void loadThemes();
-
-
-    private:
-        QList<QMap<int, QVariant> > m_data;
+        void loadThemes() const;
 };
 
 #endif // HEDGEWARS_THEMEMODEL_H
--- a/QTfrontend/ui/widget/mapContainer.cpp	Tue Dec 03 23:54:58 2013 +0400
+++ b/QTfrontend/ui/widget/mapContainer.cpp	Wed Dec 04 00:19:42 2013 +0400
@@ -252,7 +252,6 @@
     staticMapChanged(m_staticMapModel->index(0, 0));
     missionMapChanged(m_missionMapModel->index(0, 0));
     changeMapType(MapModel::GeneratedMap);
-    setRandomTheme();
 }
 
 void HWMapContainer::setImage(const QImage newImage)
@@ -603,6 +602,7 @@
 {
     if (!m_previewEnabled) {
         m_previewEnabled = true;
+        setRandomTheme();
         updatePreview();
     }
     QWidget::showEvent(event);
--- a/QTfrontend/util/DataManager.cpp	Tue Dec 03 23:54:58 2013 +0400
+++ b/QTfrontend/util/DataManager.cpp	Wed Dec 04 00:19:42 2013 +0400
@@ -115,7 +115,6 @@
 {
     if (m_themeModel == NULL) {
         m_themeModel = new ThemeModel();
-        m_themeModel->loadThemes();
     }
     return m_themeModel;
 }