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