serverfilterbuildermenu.cpp
1 //------------------------------------------------------------------------------
2 // serverfilterbuildermenu.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program 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
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; 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) 2013 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "serverfilterbuildermenu.h"
24 
25 #include "gui/entity/serverlistfilterinfo.h"
26 #include "serverapi/server.h"
28 
29 DClass<ServerFilterBuilderMenu>
30 {
31  public:
32  ServerListFilterInfo filter;
33  QString gameMode;
34  unsigned maxPing;
35 
36  static void addIfNotContains(QStringList& target, const QString& candidate)
37  {
38  if (!target.contains(candidate, Qt::CaseInsensitive))
39  {
40  target << candidate;
41  }
42  }
43 };
44 
45 DPointered(ServerFilterBuilderMenu)
46 
48  const ServerListFilterInfo& filter, QWidget* parent)
49 : QMenu(tr("Build server filter ..."), parent)
50 {
51  d->filter = filter;
52  d->gameMode = server.gameMode().name();
53  d->maxPing = server.ping();
54 
55  addAction(this, tr("Show only servers with ping lower than %1").arg(d->maxPing),
56  SLOT(applyPingFilter()));
57  if (!d->filter.gameModes.contains(d->gameMode, Qt::CaseInsensitive))
58  {
59  addAction(this, tr("Filter by game mode \"%1\"").arg(d->gameMode),
60  SLOT(applyGameModeFilter()));
61  }
62  if (!d->filter.gameModesExcluded.contains(d->gameMode, Qt::CaseInsensitive))
63  {
64  addAction(this, tr("Hide game mode \"%1\"").arg(d->gameMode),
65  SLOT(applyGameModeExcludedFilter()));
66  }
67 
68  QMenu* includeWads = new QMenu(tr("Include WAD ..."), this);
69  QMenu* excludeWads = new QMenu(tr("Exclude WAD ..."), this);
70 
71  QStringList wads = server.allWadNames();
72  foreach (const QString& wad, wads)
73  {
74  if (!d->filter.wadsExcluded.contains(wad, Qt::CaseInsensitive))
75  {
76  mkExcludeWadAction(excludeWads, wad);
77  }
78  if (!d->filter.wads.contains(wad, Qt::CaseInsensitive))
79  {
80  mkIncludeWadAction(includeWads, wad);
81  }
82  }
83 
84  if (!excludeWads->isEmpty())
85  {
86  addMenu(excludeWads);
87  }
88  if (!includeWads->isEmpty())
89  {
90  addMenu(includeWads);
91  }
92 }
93 
94 ServerFilterBuilderMenu::~ServerFilterBuilderMenu()
95 {
96 }
97 
98 QAction* ServerFilterBuilderMenu::addAction(QMenu* menu, const QString& text, const char* slot)
99 {
100  QAction* action = new QAction(menu);
101  action->setText(text);
102  this->connect(action, SIGNAL(triggered()), slot);
103  menu->addAction(action);
104  return action;
105 }
106 
107 void ServerFilterBuilderMenu::applyGameModeExcludedFilter()
108 {
109  if (!d->filter.gameModesExcluded.contains(d->gameMode, Qt::CaseInsensitive))
110  {
111  d->filter.gameModesExcluded << d->gameMode;
112  }
113 }
114 
115 void ServerFilterBuilderMenu::applyGameModeFilter()
116 {
117  if (!d->filter.gameModes.contains(d->gameMode, Qt::CaseInsensitive))
118  {
119  d->filter.gameModes << d->gameMode;
120  }
121 }
122 
123 void ServerFilterBuilderMenu::applyPingFilter()
124 {
125  d->filter.maxPing = d->maxPing;
126 }
127 
128 void ServerFilterBuilderMenu::excludeWadFromAction()
129 {
130  QAction* action = static_cast<QAction*>(sender());
131  PrivData<ServerFilterBuilderMenu>::addIfNotContains(d->filter.wadsExcluded, action->text());
132 }
133 
134 const ServerListFilterInfo& ServerFilterBuilderMenu::filter() const
135 {
136  return d->filter;
137 }
138 
139 void ServerFilterBuilderMenu::includeWadFromAction()
140 {
141  QAction* action = static_cast<QAction*>(sender());
142  PrivData<ServerFilterBuilderMenu>::addIfNotContains(d->filter.wads, action->text());
143 }
144 
145 QAction* ServerFilterBuilderMenu::mkExcludeWadAction(QMenu* menu, const QString& wadName)
146 {
147  return addAction(menu, wadName, SLOT(excludeWadFromAction()));
148 }
149 
150 QAction* ServerFilterBuilderMenu::mkIncludeWadAction(QMenu* menu, const QString& wadName)
151 {
152  return addAction(menu, wadName, SLOT(includeWadFromAction()));
153 }
Structure describing server filter.
unsigned maxPing
Maximum allowed ping.
A representation of a server for a given game.
Definition: server.h:93
Definition: dptr.h:31