some tweaks/fixes/etc to the map preview and reload stuff
authorsheepluva
Thu, 03 May 2012 19:00:17 +0200
changeset 7018 6a1f46c026bf
parent 7017 19a434fc91fc
child 7019 333afe233886
child 7020 846cea23aea2
some tweaks/fixes/etc to the map preview and reload stuff also fixed an old bug of a "stuck random/drawn map preview" when changing maps quickly
QTfrontend/ui/widget/mapContainer.cpp
--- a/QTfrontend/ui/widget/mapContainer.cpp	Thu May 03 14:21:53 2012 +0200
+++ b/QTfrontend/ui/widget/mapContainer.cpp	Thu May 03 19:00:17 2012 +0200
@@ -77,9 +77,6 @@
     chooseMap->setEditable(false);
     chooseMap->setModel(m_mapModel);
 
-    // update model views after model changes (to e.g. re-adjust separators)
-    connect(&DataManager::instance(), SIGNAL(updated()), this, SLOT(updateModelViews()));
-
     mapLayout->addWidget(chooseMap, 1, 1);
 
     QLabel * lblMap = new QLabel(tr("Map"), mapWidget);
@@ -183,9 +180,12 @@
 
     chooseMap->setCurrentIndex(0);
     mapChanged(0);
-    connect(chooseMap, SIGNAL(currentIndexChanged(int)), this, SLOT(mapChanged(int)));
+    // use signal "activated" rather than currentIndexChanged
+    // because index is somtimes changed a few times in a row programmatically
+    connect(chooseMap, SIGNAL(activated(int)), this, SLOT(mapChanged(int)));
 
-    updateModelViews();
+    // update model views after model changes (to e.g. re-adjust separators)
+    connect(&DataManager::instance(), SIGNAL(updated()), this, SLOT(updateModelViews()));
 }
 
 void HWMapContainer::setImage(const QImage newImage)
@@ -213,18 +213,24 @@
 
 void HWMapContainer::mapChanged(int index)
 {
+    if (chooseMap->currentIndex() != index)
+        chooseMap->setCurrentIndex(index);
+
     if (index < 0)
+    {
+        m_mapInfo.type = MapModel::Invalid;
+        updatePreview();
         return;
+    }
 
     Q_ASSERT(chooseMap->itemData(index, Qt::UserRole + 1).canConvert<MapModel::MapInfo>());
     m_mapInfo = chooseMap->itemData(index, Qt::UserRole + 1).value<MapModel::MapInfo>();
-    m_curMap = chooseMap->currentText();
+    m_curMap = m_mapInfo.name;
 
     switch(m_mapInfo.type)
     {
         case MapModel::GeneratedMap:
             mapgen = MAPGEN_REGULAR;
-            updatePreview();
             gbThemes->show();
             lblFilter->show();
             cbTemplateFilter->show();
@@ -233,7 +239,6 @@
             break;
         case MapModel::GeneratedMaze:
             mapgen = MAPGEN_MAZE;
-            updatePreview();
             gbThemes->show();
             lblFilter->hide();
             cbTemplateFilter->hide();
@@ -242,7 +247,6 @@
             break;
         case MapModel::HandDrawnMap:
             mapgen = MAPGEN_DRAWN;
-            updatePreview();
             gbThemes->show();
             lblFilter->hide();
             cbTemplateFilter->hide();
@@ -251,7 +255,6 @@
             break;
         default:
             mapgen = MAPGEN_MAP;
-            updatePreview();
             gbThemes->hide();
             lblFilter->hide();
             cbTemplateFilter->hide();
@@ -266,7 +269,9 @@
         m_theme = lvThemes->currentIndex().data().toString();
         emit themeChanged(m_theme);
     }
-    emit mapChanged(m_mapInfo.name);
+
+    updatePreview();
+    emit mapChanged(m_curMap);
     emit mapgenChanged(mapgen);
 }
 
@@ -293,13 +298,6 @@
 
 void HWMapContainer::askForGeneratedPreview()
 {
-    if (pMap)
-    {
-        disconnect(pMap, 0, this, SLOT(setImage(const QImage)));
-        disconnect(pMap, 0, this, SLOT(setHHLimit(int)));
-        pMap = 0;
-    }
-
     pMap = new HWMap();
     connect(pMap, SIGNAL(ImageReceived(const QImage)), this, SLOT(setImage(const QImage)));
     connect(pMap, SIGNAL(HHLimitReceived(int)), this, SLOT(setHHLimit(int)));
@@ -342,7 +340,7 @@
 QString HWMapContainer::getCurrentMap() const
 {
     if(chooseMap->currentIndex() < MAPGEN_MAP) return QString();
-    return(m_mapInfo.name);
+    return(m_curMap);
 }
 
 QString HWMapContainer::getCurrentTheme() const
@@ -397,28 +395,17 @@
 
 void HWMapContainer::intSetMap(const QString & map)
 {
+    m_curMap = map;
+
     int id = m_mapModel->indexOf(map);
 
-    if (pMap)
-    {
-        disconnect(pMap, 0, this, SLOT(setImage(const QImage)));
-        disconnect(pMap, 0, this, SLOT(setHHLimit(int)));
-        pMap = 0;
-    }
-
-    chooseMap->setCurrentIndex(id);
-
-    if(id < 0)
-    {
-        m_mapInfo.type = MapModel::Invalid;
-        m_curMap = map;
-    }
+    mapChanged(id);
 }
 
-void HWMapContainer::setMap(const QString &map)
+void HWMapContainer::setMap(const QString & map)
 {
-    intSetMap(map);
-    updatePreview();
+    if ((m_mapInfo.type == MapModel::Invalid) || (map != m_mapInfo.name))
+        intSetMap(map);
 }
 
 void HWMapContainer::setTheme(const QString & theme)
@@ -447,10 +434,10 @@
         case MapModel::StaticMap:
             // get random map of same type
             idx = m_mapModel->randomMap(m_mapInfo.type);
-            chooseMap->setCurrentIndex(idx);
+            mapChanged(idx);
             break;
         case MapModel::Invalid:
-            chooseMap->setCurrentIndex(0);
+            mapChanged(0);
     }
 }
 
@@ -477,7 +464,8 @@
 void HWMapContainer::setTemplateFilter(int filter)
 {
     intSetTemplateFilter(filter);
-    updatePreview();
+    if (m_mapInfo.type == MapModel::GeneratedMap)
+        updatePreview();
 }
 
 MapGenerator HWMapContainer::get_mapgen(void) const
@@ -499,23 +487,52 @@
 void HWMapContainer::setMazeSize(int size)
 {
     intSetMazeSize(size);
-    updatePreview();
+    if (m_mapInfo.type == MapModel::GeneratedMaze)
+        updatePreview();
 }
 
 void HWMapContainer::intSetMapgen(MapGenerator m)
 {
-    mapgen = m;
+    if (mapgen != m)
+    {
+        mapgen = m;
 
-    if(m != MAPGEN_MAP)
-        chooseMap->setCurrentIndex(m);
+        switch (m)
+        {
+            case MAPGEN_REGULAR:
+                m_mapInfo.type = MapModel::GeneratedMap;
+                break;
+            case MAPGEN_MAZE:
+                m_mapInfo.type = MapModel::GeneratedMaze;
+                break;
+            case MAPGEN_DRAWN:
+                m_mapInfo.type = MapModel::HandDrawnMap;
+                break;
+            case MAPGEN_MAP:
+                switch (m_mapInfo.type)
+                {
+                    case MapModel::GeneratedMap:
+                    case MapModel::GeneratedMaze:
+                    case MapModel::HandDrawnMap:
+                        m_mapInfo.type = MapModel::Invalid;
+                    default:
+                        break;
+                }
+                break;
+        }
 
-    emit mapgenChanged(m);
+        if(m != MAPGEN_MAP)
+            chooseMap->setCurrentIndex(m);
+
+        emit mapgenChanged(m);
+    }
 }
 
 void HWMapContainer::setMapgen(MapGenerator m)
 {
     intSetMapgen(m);
-    updatePreview();
+    if(m != MAPGEN_MAP)
+        updatePreview();
 }
 
 void HWMapContainer::setDrawnMapData(const QByteArray & ar)
@@ -562,6 +579,13 @@
 
 void HWMapContainer::updatePreview()
 {
+    if (pMap)
+    {
+        disconnect(pMap, 0, this, SLOT(setImage(const QImage)));
+        disconnect(pMap, 0, this, SLOT(setHHLimit(int)));
+        pMap = 0;
+    }
+
     QPixmap failIcon;
 
     switch(m_mapInfo.type)
@@ -600,13 +624,12 @@
 
 void HWMapContainer::setAllMapParameters(const QString &map, MapGenerator m, int mazesize, const QString &seed, int tmpl)
 {
-    intSetMap(map);
     intSetMapgen(m);
     intSetMazeSize(mazesize);
     intSetSeed(seed);
     intSetTemplateFilter(tmpl);
-
-    updatePreview();
+    // this one last because it will refresh the preview
+    intSetMap(map);
 }
 
 
@@ -624,9 +647,10 @@
     }
 
     // restore map selection
-    if (!m_curMap.isEmpty())
-        setMap(m_curMap);
-
+    if ((!m_curMap.isEmpty()) && (chooseMap->currentIndex() < 0))
+        intSetMap(m_curMap);
+    else
+        updatePreview();
 }