ThemeModel: Filter themes based on IsDlcRole rather than string-based
authorWuzzy <Wuzzy2@mail.ru>
Sun, 18 Mar 2018 14:38:48 +0100
changeset 13252 4df9d8cedf7f
parent 13251 0c98d3b249f7
child 13253 b0022324fb4e
ThemeModel: Filter themes based on IsDlcRole rather than string-based
QTfrontend/model/ThemeFilterProxyModel.cpp
QTfrontend/model/ThemeFilterProxyModel.h
QTfrontend/model/ThemeModel.cpp
QTfrontend/model/ThemeModel.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/QTfrontend/model/ThemeFilterProxyModel.cpp	Sun Mar 18 14:38:48 2018 +0100
@@ -0,0 +1,51 @@
+/*
+ * Hedgewars, a free turn based strategy game
+ * Copyright (c) 2004-2018 Andrey Korotaev <unC0Rr@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+/**
+ * @file
+ * @brief ThemeFilterProxyModel class implementation
+ */
+
+#include "ThemeModel.h"
+#include "ThemeFilterProxyModel.h"
+
+ThemeFilterProxyModel::ThemeFilterProxyModel(QObject *parent)
+    : QSortFilterProxyModel(parent)
+{
+    isFilteringDLC = false;
+}
+
+bool ThemeFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex & sourceParent) const
+{
+    if(isFilteringDLC)
+    {
+        QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent);
+        bool isDLC = index.data(ThemeModel::IsDlcRole).toBool();
+        return !isDLC;
+    }
+    else
+    {
+        return true;
+    }
+}
+
+void ThemeFilterProxyModel::setFilterDLC(bool enable)
+{
+    isFilteringDLC = enable;
+    invalidateFilter();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/QTfrontend/model/ThemeFilterProxyModel.h	Sun Mar 18 14:38:48 2018 +0100
@@ -0,0 +1,47 @@
+/*
+ * Hedgewars, a free turn based strategy game
+ * Copyright (c) 2004-2018 Andrey Korotaev <unC0Rr@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+/**
+ * @file
+ * @brief Class definition of ThemeFilterProxyModel
+ */
+
+#ifndef HEDGEWARS_THEMEFILTERPROXYMODEL_H
+#define HEDGEWARS_THEMEFILTERPROXYMODEL_H
+
+#include <QSortFilterProxyModel>
+
+/**
+ * @brief A filter model for filtering DLC themes
+ */
+class ThemeFilterProxyModel : public QSortFilterProxyModel
+{
+        Q_OBJECT
+
+    public:
+        ThemeFilterProxyModel(QObject *parent = 0);
+        void setFilterDLC(bool enabled);
+
+    protected:
+        bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
+
+    private:
+        bool isFilteringDLC;
+};
+
+#endif // HEDGEWARS_THEMEFILTERPROXYMODEL_H
--- a/QTfrontend/model/ThemeModel.cpp	Sun Mar 18 03:24:49 2018 +0100
+++ b/QTfrontend/model/ThemeModel.cpp	Sun Mar 18 14:38:48 2018 +0100
@@ -35,15 +35,13 @@
     m_filteredNoDLC = NULL;
 }
 
-QSortFilterProxyModel * ThemeModel::withoutDLC()
+ThemeFilterProxyModel * ThemeModel::withoutDLC()
 {
     if (m_filteredNoDLC == NULL)
     {
-        m_filteredNoDLC = new QSortFilterProxyModel(this);
+        m_filteredNoDLC = new ThemeFilterProxyModel(this);
         m_filteredNoDLC->setSourceModel(this);
-        // filtering based on IsDlcRole would be nicer
-        // but seems this model can only do string-based filtering :|
-        m_filteredNoDLC->setFilterRegExp(QRegExp("^[^*]"));
+        m_filteredNoDLC->setFilterDLC(true);
     }
     return m_filteredNoDLC;
 }
--- a/QTfrontend/model/ThemeModel.h	Sun Mar 18 03:24:49 2018 +0100
+++ b/QTfrontend/model/ThemeModel.h	Sun Mar 18 14:38:48 2018 +0100
@@ -25,11 +25,11 @@
 #define HEDGEWARS_THEMEMODEL_H
 
 #include <QAbstractListModel>
-#include <QSortFilterProxyModel>
 #include <QStringList>
 #include <QMap>
 #include <QIcon>
 
+#include "ThemeFilterProxyModel.h"
 #include "DataManager.h"
 
 /**
@@ -45,12 +45,12 @@
 
         int rowCount(const QModelIndex &parent = QModelIndex()) const;
         QVariant data(const QModelIndex &index, int role) const;
-        QSortFilterProxyModel * withoutDLC();
+        ThemeFilterProxyModel * withoutDLC();
 
     private:
         mutable QList<QMap<int, QVariant> > m_data;
         mutable bool m_themesLoaded;
-        mutable QSortFilterProxyModel * m_filteredNoDLC;
+        mutable ThemeFilterProxyModel * m_filteredNoDLC;
 
         void loadThemes() const;
 };