diff --git a/apps/browser/CMakeLists.txt b/apps/browser/CMakeLists.txt index dc8b2b9ed..95f09bfec 100644 --- a/apps/browser/CMakeLists.txt +++ b/apps/browser/CMakeLists.txt @@ -11,6 +11,7 @@ set(BROWSER ServerModel.cpp NetController.cpp ServerInfoDialog.cpp + MySortFilterProxyModel.cpp netutils/HTTPNetwork.cpp netutils/Utils.cpp ${CMAKE_SOURCE_DIR}/files/tes3mp/browser.rc @@ -20,6 +21,7 @@ set(BROWSER_HEADER_MOC MainWindow.hpp ServerModel.hpp ServerInfoDialog.hpp + MySortFilterProxyModel.hpp ) set(BROWSER_HEADER diff --git a/apps/browser/MySortFilterProxyModel.cpp b/apps/browser/MySortFilterProxyModel.cpp new file mode 100644 index 000000000..3d50174a1 --- /dev/null +++ b/apps/browser/MySortFilterProxyModel.cpp @@ -0,0 +1,52 @@ +// +// Created by koncord on 30.01.17. +// + +#include "MySortFilterProxyModel.hpp" +#include "ServerModel.hpp" + +#include + +bool MySortFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const +{ + + QModelIndex pingIndex = sourceModel()->index(sourceRow, ServerData::PING, sourceParent); + QModelIndex plIndex = sourceModel()->index(sourceRow, ServerData::PLAYERS, sourceParent); + QModelIndex maxPlIndex = sourceModel()->index(sourceRow, ServerData::MAX_PLAYERS, sourceParent); + + int ping = sourceModel()->data(pingIndex).toInt(); + int players = sourceModel()->data(plIndex).toInt(); + int maxPlayers = sourceModel()->data(maxPlIndex).toInt(); + + if(maxPing > 0 && (ping == -1 || ping > maxPing)) + return false; + if(filterEmpty && players == 0) + return false; + if(filterFull && players >= maxPlayers) + return false; + + return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent); +} + +MySortFilterProxyModel::MySortFilterProxyModel(QObject *parent) : QSortFilterProxyModel(parent) +{ + +} + +void MySortFilterProxyModel::filterEmptyServers(bool state) +{ + filterEmpty = state; + invalidateFilter(); +} + +void MySortFilterProxyModel::filterFullServer(bool state) +{ + filterFull = state; + invalidateFilter(); +} + +void MySortFilterProxyModel::pingLessThan(int maxPing) +{ + this->maxPing = maxPing; + invalidateFilter(); +} diff --git a/apps/browser/MySortFilterProxyModel.hpp b/apps/browser/MySortFilterProxyModel.hpp new file mode 100644 index 000000000..c53de3a90 --- /dev/null +++ b/apps/browser/MySortFilterProxyModel.hpp @@ -0,0 +1,27 @@ +// +// Created by koncord on 30.01.17. +// + +#ifndef OPENMW_MYSORTFILTERPROXYMODEL_HPP +#define OPENMW_MYSORTFILTERPROXYMODEL_HPP + + +#include + +class MySortFilterProxyModel : public QSortFilterProxyModel +{ + Q_OBJECT +protected: + bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const Q_DECL_FINAL; +public: + MySortFilterProxyModel(QObject *parent); + void filterFullServer(bool state); + void filterEmptyServers(bool state); + void pingLessThan(int maxPing); +private: + bool filterEmpty, filterFull; + int maxPing; +}; + + +#endif //OPENMW_MYSORTFILTERPROXYMODEL_HPP