Add IsHiddenRole to ThemeModel for hidden themes
authorWuzzy <Wuzzy2@mail.ru>
Sun, 18 Mar 2018 15:24:54 +0100
changeset 13248 b0022324fb4e
parent 13247 4df9d8cedf7f
child 13249 d2b58cf339fe
Add IsHiddenRole to ThemeModel for hidden themes
QTfrontend/model/ThemeFilterProxyModel.cpp
QTfrontend/model/ThemeFilterProxyModel.h
QTfrontend/model/ThemeModel.cpp
QTfrontend/model/ThemeModel.h
QTfrontend/ui/widget/mapContainer.cpp
QTfrontend/ui/widget/themeprompt.cpp
--- a/QTfrontend/model/ThemeFilterProxyModel.cpp	Sun Mar 18 14:38:48 2018 +0100
+++ b/QTfrontend/model/ThemeFilterProxyModel.cpp	Sun Mar 18 15:24:54 2018 +0100
@@ -28,15 +28,20 @@
     : QSortFilterProxyModel(parent)
 {
     isFilteringDLC = false;
+    isFilteringHidden = false;
 }
 
 bool ThemeFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex & sourceParent) const
 {
-    if(isFilteringDLC)
+    if(isFilteringDLC || isFilteringHidden)
     {
         QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
         bool isDLC = index.data(ThemeModel::IsDlcRole).toBool();
-        return !isDLC;
+        bool isHidden = index.data(ThemeModel::IsHiddenRole).toBool();
+        return (
+            ((isFilteringDLC && !isDLC) || !isFilteringDLC) &&
+            ((isFilteringHidden && !isHidden) || !isFilteringHidden));
+
     }
     else
     {
@@ -49,3 +54,9 @@
     isFilteringDLC = enable;
     invalidateFilter();
 }
+
+void ThemeFilterProxyModel::setFilterHidden(bool enable)
+{
+    isFilteringHidden = enable;
+    invalidateFilter();
+}
--- a/QTfrontend/model/ThemeFilterProxyModel.h	Sun Mar 18 14:38:48 2018 +0100
+++ b/QTfrontend/model/ThemeFilterProxyModel.h	Sun Mar 18 15:24:54 2018 +0100
@@ -36,12 +36,14 @@
     public:
         ThemeFilterProxyModel(QObject *parent = 0);
         void setFilterDLC(bool enabled);
+        void setFilterHidden(bool enabled);
 
     protected:
         bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
 
     private:
         bool isFilteringDLC;
+        bool isFilteringHidden;
 };
 
 #endif // HEDGEWARS_THEMEFILTERPROXYMODEL_H
--- a/QTfrontend/model/ThemeModel.cpp	Sun Mar 18 14:38:48 2018 +0100
+++ b/QTfrontend/model/ThemeModel.cpp	Sun Mar 18 15:24:54 2018 +0100
@@ -33,8 +33,11 @@
     m_themesLoaded = false;
 
     m_filteredNoDLC = NULL;
+    m_filteredNoHidden = NULL;
+    m_filteredNoDLCOrHidden = NULL;
 }
 
+// Filters out DLC themes, e.g. themes which do not come by default
 ThemeFilterProxyModel * ThemeModel::withoutDLC()
 {
     if (m_filteredNoDLC == NULL)
@@ -46,6 +49,32 @@
     return m_filteredNoDLC;
 }
 
+// Filters out hidden themes, these are themes which are not supposed to be
+// seen by the user.
+ThemeFilterProxyModel * ThemeModel::withoutHidden()
+{
+    if (m_filteredNoHidden == NULL)
+    {
+        m_filteredNoHidden = new ThemeFilterProxyModel(this);
+        m_filteredNoHidden->setSourceModel(this);
+        m_filteredNoHidden->setFilterHidden(true);
+    }
+    return m_filteredNoHidden;
+}
+
+// Combination of the two above for convenience
+ThemeFilterProxyModel * ThemeModel::withoutDLCOrHidden()
+{
+    if (m_filteredNoDLCOrHidden == NULL)
+    {
+        m_filteredNoDLCOrHidden = new ThemeFilterProxyModel(this);
+        m_filteredNoDLCOrHidden->setSourceModel(this);
+        m_filteredNoDLCOrHidden->setFilterDLC(true);
+        m_filteredNoDLCOrHidden->setFilterHidden(true);
+    }
+    return m_filteredNoDLCOrHidden;
+}
+
 int ThemeModel::rowCount(const QModelIndex &parent) const
 {
     if(parent.isValid())
@@ -93,13 +122,13 @@
 
     foreach (QString theme, themes)
     {
+        QMap<int, QVariant> dataset;
+
         // themes without icon are supposed to be hidden
         QString iconpath = QString("physfs://Themes/%1/icon.png").arg(theme);
 
         if (!QFile::exists(iconpath))
-            continue;
-
-        QMap<int, QVariant> dataset;
+            dataset.insert(IsHiddenRole, true);
 
         // detect if theme is dlc
         QString themeDir = PHYSFS_getRealDir(QString("Themes/%1/icon.png").arg(theme).toLocal8Bit().data());
--- a/QTfrontend/model/ThemeModel.h	Sun Mar 18 14:38:48 2018 +0100
+++ b/QTfrontend/model/ThemeModel.h	Sun Mar 18 15:24:54 2018 +0100
@@ -40,17 +40,21 @@
         Q_OBJECT
 
     public:
-        enum Roles { ActualNameRole = Qt::UserRole, IsDlcRole, IconPathRole };
+        enum Roles { ActualNameRole = Qt::UserRole, IsDlcRole, IconPathRole, IsHiddenRole };
         explicit ThemeModel(QObject *parent = 0);
 
         int rowCount(const QModelIndex &parent = QModelIndex()) const;
         QVariant data(const QModelIndex &index, int role) const;
         ThemeFilterProxyModel * withoutDLC();
+        ThemeFilterProxyModel * withoutHidden();
+        ThemeFilterProxyModel * withoutDLCOrHidden();
 
     private:
         mutable QList<QMap<int, QVariant> > m_data;
         mutable bool m_themesLoaded;
         mutable ThemeFilterProxyModel * m_filteredNoDLC;
+        mutable ThemeFilterProxyModel * m_filteredNoHidden;
+        mutable ThemeFilterProxyModel * m_filteredNoDLCOrHidden;
 
         void loadThemes() const;
 };
--- a/QTfrontend/ui/widget/mapContainer.cpp	Sun Mar 18 14:38:48 2018 +0100
+++ b/QTfrontend/ui/widget/mapContainer.cpp	Sun Mar 18 15:24:54 2018 +0100
@@ -599,9 +599,9 @@
     QAbstractItemModel * tmodel;
 
     if (m_withoutDLC)
-        tmodel = m_themeModel->withoutDLC();
+        tmodel = m_themeModel->withoutDLCOrHidden();
     else
-        tmodel = m_themeModel;
+        tmodel = m_themeModel->withoutHidden();
 
     if(!tmodel->rowCount()) return;
     quint32 themeNum = rand() % tmodel->rowCount();
--- a/QTfrontend/ui/widget/themeprompt.cpp	Sun Mar 18 14:38:48 2018 +0100
+++ b/QTfrontend/ui/widget/themeprompt.cpp	Sun Mar 18 15:24:54 2018 +0100
@@ -68,10 +68,10 @@
 
     setStyleSheet("QPushButton { padding: 5px; margin-top: 10px; }");
 
-    // Theme model, and a model for setting a filter
+    // Theme model
     ThemeModel * themeModel = DataManager::instance().themeModel();
-    filterModel = new QSortFilterProxyModel();
-    filterModel->setSourceModel(themeModel);
+    filterModel = themeModel->withoutHidden();
+    // Custom filter extension
     filterModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
 
     // Grid