serverlistproxymodel.h
1 //------------------------------------------------------------------------------
2 // serverlistproxymodel.h
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2011 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #ifndef __SERVERLISTPROXYMODEL_H__
24 #define __SERVERLISTPROXYMODEL_H__
25 
26 #include "dptr.h"
27 #include "serverapi/serverptr.h"
28 #include <QSortFilterProxyModel>
29 
31 class ServerList;
32 class Server;
33 
35 {
36 public:
37  static ColumnSort deserializeQVariant(const QVariant &v);
38 
39  ColumnSort();
40  ColumnSort(int columnId, Qt::SortOrder order);
41 
42  int columnId() const;
43  bool isValid() const;
44  Qt::SortOrder order() const;
45 
46  bool operator==(const ColumnSort &other) const;
47 
48  QVariant serializeQVariant() const;
49 
50 private:
51  int columnId_;
52  Qt::SortOrder order_;
53 };
54 
55 
56 class ServerListProxyModel : public QSortFilterProxyModel
57 {
58  Q_OBJECT
59 
60 public:
61  ServerListProxyModel(ServerList *serverListHandler);
62  ~ServerListProxyModel() override;
63 
64  void addAdditionalColumnSorting(int column, Qt::SortOrder order);
65  const QList<ColumnSort> &additionalSortColumns() const;
66  void clearAdditionalSorting();
67  const ServerListFilterInfo &filterInfo() const;
68 
69  bool isAnyColumnSortedAdditionally() const;
70  bool isSortingAdditionallyByColumn(int column) const;
71 
72  void removeAdditionalColumnSorting(int column);
73  void setAdditionalSortColumns(const QList<ColumnSort> &columns);
77  void setFilterInfo(const ServerListFilterInfo &filterInfo);
78  void setGroupServersWithPlayersAtTop(bool b);
79 
80  void sortServers(int column, Qt::SortOrder order = Qt::AscendingOrder);
81 
82 signals:
83  void additionalSortColumnsChanged();
84 
85 protected:
89  bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
90 
91 private:
93 
94  int compareColumnSortData(QVariant &var1, QVariant &var2, int column) const;
95  bool lessThan(const QModelIndex &left, const QModelIndex &right) const override;
96 
97  ServerPtr serverFromList(const QModelIndex &index) const;
98  ServerPtr serverFromList(int row) const;
99 };
100 
101 #endif