Change theme to Nature if changing from background-only or hidden theme to a generated map type
authorWuzzy <Wuzzy2@mail.ru>
Tue, 23 Apr 2019 15:32:04 +0200
changeset 14849 8ed0c3761640
parent 14848 266f973745b8
child 14850 f56936207a65
Change theme to Nature if changing from background-only or hidden theme to a generated map type
ChangeLog.txt
QTfrontend/model/ThemeFilterProxyModel.cpp
QTfrontend/model/ThemeFilterProxyModel.h
QTfrontend/model/ThemeModel.cpp
QTfrontend/model/ThemeModel.h
QTfrontend/ui/widget/mapContainer.cpp
--- a/ChangeLog.txt	Tue Apr 23 13:22:12 2019 +0200
+++ b/ChangeLog.txt	Tue Apr 23 15:32:04 2019 +0200
@@ -92,6 +92,7 @@
  + Restructure credits page
  + More intelligent automatic mission selection in campaign screen
  * Fix force-locked schemes getting unlocked when changing map types
+ * Fix possible to select background-only or hidden themes indirectly by changing map type
 
 Sounds and voicepacks:
  + sndYoohoo has been split to sndYoohoo and sndKiss
--- a/QTfrontend/model/ThemeFilterProxyModel.cpp	Tue Apr 23 13:22:12 2019 +0200
+++ b/QTfrontend/model/ThemeFilterProxyModel.cpp	Tue Apr 23 15:32:04 2019 +0200
@@ -29,6 +29,7 @@
 {
     isFilteringDLC = false;
     isFilteringHidden = false;
+    isFilteringBackground = false;
 }
 
 bool ThemeFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex & sourceParent) const
@@ -43,13 +44,15 @@
         searchOkay = in != -1;
     }
 
-    if(isFilteringDLC || isFilteringHidden)
+    if(isFilteringDLC || isFilteringHidden || isFilteringBackground)
     {
         bool isDLC = index.data(ThemeModel::IsDlcRole).toBool();
         bool isHidden = index.data(ThemeModel::IsHiddenRole).toBool();
+        bool isBackground = index.data(ThemeModel::IsBackgroundThemeRole).toBool();
 
         return ( ((isFilteringDLC && !isDLC) || !isFilteringDLC) &&
-                 ((isFilteringHidden && !isHidden) || !isFilteringHidden) ) &&
+                 ((isFilteringHidden && !isHidden) || !isFilteringHidden) &&
+                 ((isFilteringBackground && !isBackground) || !isFilteringBackground) ) &&
                searchOkay;
     }
     else
@@ -69,3 +72,9 @@
     isFilteringHidden = enable;
     invalidateFilter();
 }
+
+void ThemeFilterProxyModel::setFilterBackground(bool enable)
+{
+    isFilteringBackground = enable;
+    invalidateFilter();
+};
--- a/QTfrontend/model/ThemeFilterProxyModel.h	Tue Apr 23 13:22:12 2019 +0200
+++ b/QTfrontend/model/ThemeFilterProxyModel.h	Tue Apr 23 15:32:04 2019 +0200
@@ -37,6 +37,7 @@
         ThemeFilterProxyModel(QObject *parent = 0);
         void setFilterDLC(bool enabled);
         void setFilterHidden(bool enabled);
+        void setFilterBackground(bool enabled);
 
     protected:
         bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
@@ -44,6 +45,7 @@
     private:
         bool isFilteringDLC;
         bool isFilteringHidden;
+        bool isFilteringBackground;
 };
 
 #endif // HEDGEWARS_THEMEFILTERPROXYMODEL_H
--- a/QTfrontend/model/ThemeModel.cpp	Tue Apr 23 13:22:12 2019 +0200
+++ b/QTfrontend/model/ThemeModel.cpp	Tue Apr 23 15:32:04 2019 +0200
@@ -124,38 +124,48 @@
     {
         QMap<int, QVariant> dataset;
 
+        // Ignore directories without theme.cfg
+        QFile themeCfgFile(QString("physfs://Themes/%1/theme.cfg").arg(theme));
+        if (!themeCfgFile.open(QFile::ReadOnly))
+        {
+            continue;
+        }
+
         // themes without icon are supposed to be hidden
         QString iconpath = QString("physfs://Themes/%1/icon.png").arg(theme);
-
         if (!QFile::exists(iconpath))
         {
             dataset.insert(IsHiddenRole, true);
         }
         else
         {
-            // themes with the key “hidden” in theme.cfg are hidden, too
-            QFile themeCfgFile(QString("physfs://Themes/%1/theme.cfg").arg(theme));
-            if (themeCfgFile.open(QFile::ReadOnly))
+            QTextStream stream(&themeCfgFile);
+            QString line = stream.readLine();
+            QString key;
+            while (!line.isNull())
             {
-                QTextStream stream(&themeCfgFile);
-                QString line = stream.readLine();
-                QString key;
-                while (!line.isNull())
+                key = QString(line);
+                int equalsPos = line.indexOf('=');
+                key.truncate(equalsPos - 1);
+                key = key.simplified();
+                if (!line.startsWith(';') && key == "hidden")
                 {
-                    key = QString(line);
-                    int equalsPos = line.indexOf('=');
-                    key.truncate(equalsPos - 1);
-                    key = key.simplified();
-                    if (!line.startsWith(';') && key == "hidden")
-                    {
-                        dataset.insert(IsHiddenRole, true);
-                        break;
-                    }
-                    line = stream.readLine();
+                    dataset.insert(IsHiddenRole, true);
+                    break;
                 }
+                line = stream.readLine();
             }
         }
 
+        // Themes without land textures are considered "background themes"
+        // since they cannot be used for generated maps, but they can be used
+        // for image maps.
+        QString landtexpath = QString("physfs://Themes/%1/LandTex.png").arg(theme);
+        if (!QFile::exists(landtexpath))
+        {
+            dataset.insert(IsBackgroundThemeRole, true);
+        }
+
         // detect if theme is dlc
         QString themeDir = PHYSFS_getRealDir(QString("Themes/%1").arg(theme).toLocal8Bit().data());
         bool isDLC = !themeDir.startsWith(datadir->absolutePath());
@@ -179,5 +189,6 @@
         }
 
         m_data.append(dataset);
+        themeCfgFile.close();
     }
 }
--- a/QTfrontend/model/ThemeModel.h	Tue Apr 23 13:22:12 2019 +0200
+++ b/QTfrontend/model/ThemeModel.h	Tue Apr 23 15:32:04 2019 +0200
@@ -41,7 +41,7 @@
         Q_OBJECT
 
     public:
-        enum Roles { ActualNameRole = Qt::UserRole, IsDlcRole, IconPathRole, IsHiddenRole };
+        enum Roles { ActualNameRole = Qt::UserRole, IsDlcRole, IconPathRole, IsHiddenRole, IsBackgroundThemeRole };
         explicit ThemeModel(QObject *parent = 0);
 
         int rowCount(const QModelIndex &parent = QModelIndex()) const;
--- a/QTfrontend/ui/widget/mapContainer.cpp	Tue Apr 23 13:22:12 2019 +0200
+++ b/QTfrontend/ui/widget/mapContainer.cpp	Tue Apr 23 15:32:04 2019 +0200
@@ -1215,6 +1215,15 @@
     {
         if (!selectedTheme.isNull() && !selectedTheme.isEmpty())
         {
+            // Fall back to a default theme if current theme is a background theme or hidden
+            QModelIndexList mdl = m_themeModel->match(m_themeModel->index(0), ThemeModel::ActualNameRole, m_theme);
+            if (mdl.size() > 0)
+            {
+                if ((mdl.at(0).data(ThemeModel::Roles::IsBackgroundThemeRole).toBool() == true) || (mdl.at(0).data(ThemeModel::Roles::IsHiddenRole).toBool() == true))
+                {
+                    selectedTheme = "Nature";
+                }
+            }
             setTheme(selectedTheme);
             emit themeChanged(selectedTheme);
         }