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