serverfilterdock.cpp
1 //------------------------------------------------------------------------------
2 // serverfilterdock.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) 2011 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "serverfilterdock.h"
24 #include "ui_serverfilterdock.h"
25 
26 #include "configuration/doomseekerconfig.h"
27 #include "gui/entity/serverlistfilterinfo.h"
28 #include "strings.hpp"
29 
30 #include <QAction>
31 
32 DClass<ServerFilterDock> : public Ui::ServerFilterDock
33 {
34 public:
42  QLineEdit *leQuickSearch;
47  bool bDisableUpdate;
48 };
49 
50 DPointered(ServerFilterDock)
51 
52 ServerFilterDock::ServerFilterDock(QWidget *pParent)
53  : QDockWidget(pParent)
54 {
55  d->setupUi(this);
56  d->leQuickSearch = nullptr;
57  d->bDisableUpdate = false;
58  d->cbGroupServersWithPlayersAtTop->setChecked(gConfig.doomseeker.bGroupServersWithPlayersAtTheTopOfTheList);
59 
60  toggleViewAction()->setIcon(QIcon(":/icons/filter.png"));
61 
62  toggleViewAction()->setText(ServerFilterDock::tr("Server &filter"));
63  toggleViewAction()->setShortcut(ServerFilterDock::tr("CTRL+F"));
64 }
65 
66 ServerFilterDock::~ServerFilterDock()
67 {
68 }
69 
70 void ServerFilterDock::addGameModeToComboBox(const QString &gameMode)
71 {
72  addSortedNonDuplicate(d->cboGameMode, gameMode.trimmed());
73  addSortedNonDuplicate(d->cboExcludeGameMode, gameMode.trimmed());
74 }
75 
76 void ServerFilterDock::addSortedNonDuplicate(QComboBox *comboBox, const QString &text)
77 {
78  if (comboBox->findText(text, Qt::MatchFixedString) < 0)
79  {
80  // Make sure combobox contents are sorted.
81  for (int i = 0; i < comboBox->count(); ++i)
82  {
83  if (text < comboBox->itemText(i))
84  {
85  comboBox->insertItem(i, text);
86  return;
87  }
88  }
89 
90  // The above routine didn't return.
91  // This item belongs to the end of the list.
92  comboBox->addItem(text);
93  }
94 }
95 
96 void ServerFilterDock::clear()
97 {
99 }
100 
102 {
103  if (d->leQuickSearch == nullptr)
104  {
105  auto qs = new QLineEdit();
106  qs->setText(d->leServerName->text());
107 
108  connect(d->leServerName, SIGNAL(textEdited(const QString&)), qs, SLOT(setText(const QString&)));
109  connect(qs, SIGNAL(textEdited(const QString&)), d->leServerName, SLOT(setText(const QString&)));
110 
111  d->leQuickSearch = qs;
112  }
113 
114  return d->leQuickSearch;
115 }
116 
117 void ServerFilterDock::emitUpdated()
118 {
119  if (d->bDisableUpdate)
120  return;
121 
122  emit filterUpdated(filterInfo());
123 }
124 
125 void ServerFilterDock::enableFilter()
126 {
127  d->cbFilteringEnabled->setChecked(true);
128  emitUpdated();
129 }
130 
131 ServerListFilterInfo ServerFilterDock::filterInfo() const
132 {
133  ServerListFilterInfo filterInfo;
134 
135  filterInfo.bEnabled = d->cbFilteringEnabled->isChecked();
136  filterInfo.bShowEmpty = d->cbShowEmpty->isChecked();
137  filterInfo.bShowFull = d->cbShowFull->isChecked();
138  filterInfo.bShowOnlyValid = d->cbShowOnlyValid->isChecked();
139  filterInfo.gameModes = d->cboGameMode->selectedItemTexts();
140  filterInfo.gameModesExcluded = d->cboExcludeGameMode->selectedItemTexts();
141  filterInfo.maxPing = d->spinMaxPing->value();
142  filterInfo.testingServers = Doomseeker::checkboxTristateToShowMode(d->cbShowTestingServers->checkState());
143  filterInfo.serverName = d->leServerName->text();
144  filterInfo.wads = d->leWads->text().trimmed().split(",", Qt::SkipEmptyParts);
145  filterInfo.wadsExcluded = d->leExcludeWads->text().trimmed().split(",", Qt::SkipEmptyParts);
146 
147  return filterInfo;
148 }
149 
150 void ServerFilterDock::onServerGroupingChange()
151 {
152  gConfig.doomseeker.bGroupServersWithPlayersAtTheTopOfTheList = d->cbGroupServersWithPlayersAtTop->isChecked();
153  emit nonEmptyServerGroupingAtTopToggled(d->cbGroupServersWithPlayersAtTop->isChecked());
154 }
155 
157 {
158  d->bDisableUpdate = true;
159 
160  d->cbFilteringEnabled->setChecked(filterInfo.bEnabled);
161  d->cbShowEmpty->setChecked(filterInfo.bShowEmpty);
162  d->cbShowFull->setChecked(filterInfo.bShowFull);
163  d->cbShowOnlyValid->setChecked(filterInfo.bShowOnlyValid);
164 
165  for (const QString &gameMode : filterInfo.gameModes)
166  {
167  addGameModeToComboBox(gameMode);
168  }
169  d->cboGameMode->setSelectedTexts(filterInfo.gameModes);
170 
171  for (const QString &gameMode : filterInfo.gameModesExcluded)
172  {
173  addGameModeToComboBox(gameMode);
174  }
175  d->cboExcludeGameMode->setSelectedTexts(filterInfo.gameModesExcluded);
176 
177  d->spinMaxPing->setValue(filterInfo.maxPing);
178  if (d->leQuickSearch != nullptr)
179  d->leQuickSearch->setText(filterInfo.serverName.trimmed());
180 
181  d->leServerName->setText(filterInfo.serverName.trimmed());
182  d->leWads->setText(filterInfo.wads.join(",").trimmed());
183  d->leExcludeWads->setText(filterInfo.wadsExcluded.join(",").trimmed());
184 
185  d->cbShowTestingServers->setCheckState(Doomseeker::showModeToCheckboxState(
186  filterInfo.testingServers));
187 
188  d->bDisableUpdate = false;
189  emitUpdated();
190 }