# HG changeset patch # User Wuzzy # Date 1521380328 -3600 # Node ID 4df9d8cedf7f35b828410011faf176906406a2bc # Parent 0c98d3b249f7807089f3999db2af39d0eeb604ad ThemeModel: Filter themes based on IsDlcRole rather than string-based diff -r 0c98d3b249f7 -r 4df9d8cedf7f QTfrontend/model/ThemeFilterProxyModel.cpp --- /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 + * + * 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(); +} diff -r 0c98d3b249f7 -r 4df9d8cedf7f QTfrontend/model/ThemeFilterProxyModel.h --- /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 + * + * 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 + +/** + * @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 diff -r 0c98d3b249f7 -r 4df9d8cedf7f QTfrontend/model/ThemeModel.cpp --- 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; } diff -r 0c98d3b249f7 -r 4df9d8cedf7f QTfrontend/model/ThemeModel.h --- 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 -#include #include #include #include +#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 > m_data; mutable bool m_themesLoaded; - mutable QSortFilterProxyModel * m_filteredNoDLC; + mutable ThemeFilterProxyModel * m_filteredNoDLC; void loadThemes() const; };