serverfilterbuildermenu.cpp
1 //------------------------------------------------------------------------------
2 // serverfilterbuildermenu.cpp
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) 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  target << candidate;
40  }
41 };
42 
43 DPointered(ServerFilterBuilderMenu)
44 
46  const ServerListFilterInfo &filter, QWidget *parent)
47  : QMenu(tr("Build server filter ..."), parent)
48 {
49  d->filter = filter;
50  d->gameMode = server.gameMode().name();
51  d->maxPing = server.ping();
52 
53  addAction(this, tr("Show only servers with ping lower than %1").arg(d->maxPing),
54  SLOT(applyPingFilter()));
55  if (!d->filter.gameModes.contains(d->gameMode, Qt::CaseInsensitive))
56  {
57  addAction(this, tr("Filter by game mode \"%1\"").arg(d->gameMode),
58  SLOT(applyGameModeFilter()));
59  }
60  if (!d->filter.gameModesExcluded.contains(d->gameMode, Qt::CaseInsensitive))
61  {
62  addAction(this, tr("Hide game mode \"%1\"").arg(d->gameMode),
63  SLOT(applyGameModeExcludedFilter()));
64  }
65 
66  QMenu *includeWads = new QMenu(tr("Include WAD ..."), this);
67  QMenu *excludeWads = new QMenu(tr("Exclude WAD ..."), this);
68 
69  QStringList wads = server.allWadNames();
70  for (const QString &wad : wads)
71  {
72  if (!d->filter.wadsExcluded.contains(wad, Qt::CaseInsensitive))
73  mkExcludeWadAction(excludeWads, wad);
74  if (!d->filter.wads.contains(wad, Qt::CaseInsensitive))
75  mkIncludeWadAction(includeWads, wad);
76  }
77 
78  if (!excludeWads->isEmpty())
79  addMenu(excludeWads);
80  if (!includeWads->isEmpty())
81  addMenu(includeWads);
82 }
83 
84 ServerFilterBuilderMenu::~ServerFilterBuilderMenu()
85 {
86 }
87 
88 QAction *ServerFilterBuilderMenu::addAction(QMenu *menu, const QString &text, const char *slot)
89 {
90  auto action = new QAction(menu);
91  action->setText(text);
92  this->connect(action, SIGNAL(triggered()), slot);
93  menu->addAction(action);
94  return action;
95 }
96 
97 void ServerFilterBuilderMenu::applyGameModeExcludedFilter()
98 {
99  if (!d->filter.gameModesExcluded.contains(d->gameMode, Qt::CaseInsensitive))
100  d->filter.gameModesExcluded << d->gameMode;
101 }
102 
103 void ServerFilterBuilderMenu::applyGameModeFilter()
104 {
105  if (!d->filter.gameModes.contains(d->gameMode, Qt::CaseInsensitive))
106  d->filter.gameModes << d->gameMode;
107 }
108 
109 void ServerFilterBuilderMenu::applyPingFilter()
110 {
111  d->filter.maxPing = d->maxPing;
112 }
113 
114 void ServerFilterBuilderMenu::excludeWadFromAction()
115 {
116  auto action = static_cast<QAction *>(sender());
117  PrivData<ServerFilterBuilderMenu>::addIfNotContains(d->filter.wadsExcluded, action->text());
118 }
119 
120 const ServerListFilterInfo &ServerFilterBuilderMenu::filter() const
121 {
122  return d->filter;
123 }
124 
125 void ServerFilterBuilderMenu::includeWadFromAction()
126 {
127  auto action = static_cast<QAction *>(sender());
128  PrivData<ServerFilterBuilderMenu>::addIfNotContains(d->filter.wads, action->text());
129 }
130 
131 QAction *ServerFilterBuilderMenu::mkExcludeWadAction(QMenu *menu, const QString &wadName)
132 {
133  return addAction(menu, wadName, SLOT(excludeWadFromAction()));
134 }
135 
136 QAction *ServerFilterBuilderMenu::mkIncludeWadAction(QMenu *menu, const QString &wadName)
137 {
138  return addAction(menu, wadName, SLOT(includeWadFromAction()));
139 }