23 #include "configuration/doomseekerconfig.h" 24 #include "cfgcustomservers.h" 25 #include "ui_cfgcustomservers.h" 26 #include "plugins/engineplugin.h" 27 #include "plugins/pluginloader.h" 28 #include <QHeaderView> 29 #include <QMessageBox> 30 #include <QStandardItemModel> 34 #include "unknownengine.xpm" 36 DClass<CFGCustomServers> :
public Ui::CFGCustomServers
39 QStandardItemModel* model;
49 connect(d->btnAdd, SIGNAL( clicked() ),
this, SLOT( add() ));
50 connect(d->btnDisableSelected, SIGNAL( clicked() ),
this, SLOT( disableSelected() ));
51 connect(d->btnEnableSelected, SIGNAL( clicked() ),
this, SLOT( enableSelected() ));
52 connect(d->btnRemove, SIGNAL( clicked() ),
this, SLOT(
remove() ));
53 connect(d->btnSetEngine, SIGNAL( clicked() ),
this, SLOT( setEngine() ));
55 prepareEnginesComboBox();
58 CFGCustomServers::~CFGCustomServers()
62 void CFGCustomServers::add()
64 int pluginIndex = d->cboEngines->itemData(d->cboEngines->currentIndex()).toInt();
65 const EnginePlugin* plugin = gPlugins->info(pluginIndex);
67 QString engineName = d->cboEngines->itemText(d->cboEngines->currentIndex());
69 add(engineName,
"", plugin->data()->defaultServerPort,
true);
72 void CFGCustomServers::add(
const QString& engineName,
const QString& host,
73 unsigned short port,
bool enabled)
75 QList<QStandardItem* > record;
77 QStandardItem* engineItem =
new QStandardItem();
78 setEngineOnItem(engineItem, engineName);
80 QString portString = QString::number(port);
82 record.append(engineItem);
83 record.append(
new QStandardItem(host));
84 record.append(
new QStandardItem(portString));
86 QStandardItem *enabledItem =
new QStandardItem();
87 enabledItem->setCheckState(enabled ? Qt::Checked : Qt::Unchecked);
88 enabledItem->setCheckable(
true);
89 enabledItem->setToolTip(tr(
"Toggle enabled state"));
90 record.append(enabledItem);
92 d->model->appendRow(record);
93 d->tvServers->resizeRowsToContents();
96 CFGCustomServers::CheckAndFixPorts CFGCustomServers::checkAndFixPorts(
int firstRow,
int lastRow)
98 CheckAndFixPorts returnValue = AllOk;
99 for (
int rowIndex = firstRow; rowIndex <= lastRow; ++rowIndex)
101 if (!isPortCorrect(rowIndex))
103 returnValue = AtLeastOneFixed;
105 setPortToDefault(rowIndex);
112 void CFGCustomServers::dataChanged(
const QModelIndex& topLeft,
const QModelIndex& bottomRight)
114 const QString MESSAGE_TITLE = tr(
"Doomseeker - pinned servers");
116 int leftmostColumn = topLeft.column();
117 int rightmostColumn = bottomRight.column();
119 if ( isPortColumnWithinRange(leftmostColumn, rightmostColumn) )
121 switch ( checkAndFixPorts(topLeft.row(), bottomRight.row()) )
127 case AtLeastOneFixed:
128 QMessageBox::warning(
this, MESSAGE_TITLE, tr(
"Port must be within range 1 - 65535"));
132 QMessageBox::warning(
this, MESSAGE_TITLE, tr(
"Unimplemented behavior!"));
138 void CFGCustomServers::disableSelected()
140 setEnabledOnSelected(
false);
143 void CFGCustomServers::enableSelected()
145 setEnabledOnSelected(
true);
148 void CFGCustomServers::setEnabledOnSelected(
bool enabled)
150 QItemSelectionModel* sel = d->tvServers->selectionModel();
151 QModelIndexList indexList = sel->selectedRows();
153 QModelIndexList::iterator it;
154 for (it = indexList.begin(); it != indexList.end(); ++it)
156 QStandardItem* item = d->model->itemFromIndex(it->sibling(it->row(), EnabledIndex));
157 item->setCheckState(enabled ? Qt::Checked : Qt::Unchecked);
161 const EnginePlugin* CFGCustomServers::getPluginInfoForRow(
int rowIndex)
163 QStandardItem* itemEngine = d->model->item(rowIndex, EngineColumnIndex);
164 QString engineName = itemEngine->data().toString();
165 int pluginIndex = gPlugins->pluginIndexFromName(engineName);
166 return gPlugins->info(pluginIndex);
169 bool CFGCustomServers::isPortColumnWithinRange(
int leftmostColumnIndex,
int rightmostColumnIndex)
171 return leftmostColumnIndex <= PortColumnIndex && rightmostColumnIndex >= PortColumnIndex;
174 bool CFGCustomServers::isPortCorrect(
int rowIndex)
176 const int MIN_PORT = 1;
177 const int MAX_PORT = 65535;
179 QStandardItem* item = d->model->item(rowIndex, PortColumnIndex);
181 int port = item->text().toInt(&ok);
183 return ok && port >= MIN_PORT && port <= MAX_PORT;
186 void CFGCustomServers::prepareEnginesComboBox()
188 d->cboEngines->clear();
190 for (
unsigned i = 0; i < gPlugins->numPlugins(); ++i)
193 d->cboEngines->addItem(plugin->icon(), plugin->data()->name, i);
196 if (d->cboEngines->count() > 0)
198 d->cboEngines->setCurrentIndex(0);
202 void CFGCustomServers::prepareTable()
204 d->model =
new QStandardItemModel(
this);
206 connect(d->model, SIGNAL( dataChanged(
const QModelIndex&,
const QModelIndex&) ),
this, SLOT( dataChanged(
const QModelIndex&,
const QModelIndex&) ) );
209 labels <<
"" << tr(
"Host") << tr(
"Port") <<
"";
210 d->model->setHorizontalHeaderLabels(labels);
212 d->tvServers->setModel(d->model);
214 d->tvServers->setColumnWidth(EngineColumnIndex, 23);
215 d->tvServers->setColumnWidth(AddressColumnIndex, 180);
216 d->tvServers->setColumnWidth(PortColumnIndex, 60);
217 d->tvServers->setColumnWidth(EnabledIndex, 30);
219 QList<ColumnIndices> fixedSizeColumns;
220 fixedSizeColumns << EngineColumnIndex << EnabledIndex;
221 foreach (ColumnIndices columnIdx, fixedSizeColumns)
223 #if QT_VERSION >= 0x050000 224 d->tvServers->horizontalHeader()->setSectionResizeMode(columnIdx, QHeaderView::Fixed);
226 d->tvServers->horizontalHeader()->setResizeMode(columnIdx, QHeaderView::Fixed);
229 d->tvServers->horizontalHeader()->setHighlightSections(
false);
231 d->tvServers->verticalHeader()->hide();
238 QList<CustomServerInfo> customServersList = gConfig.doomseeker.customServers.toList();
239 QList<CustomServerInfo>::iterator it;
240 for (it = customServersList.begin(); it != customServersList.end(); ++it)
242 add(it->engine, it->host, it->port, it->enabled);
246 void CFGCustomServers::remove()
248 QItemSelectionModel* selModel = d->tvServers->selectionModel();
249 QModelIndexList indexList = selModel->selectedRows();
252 QList<QStandardItem*> itemList;
253 for (
int i = 0; i < indexList.count(); ++i)
255 itemList << d->model->item(indexList[i].row(), 0);
258 for (
int i = 0; i < itemList.count(); ++i)
260 QModelIndex index = d->model->indexFromItem(itemList[i]);
261 d->model->removeRow(index.row());
267 gConfig.doomseeker.customServers = this->tableGetServers();
270 void CFGCustomServers::setEngine()
272 QItemSelectionModel* sel = d->tvServers->selectionModel();
273 QModelIndexList indexList = sel->selectedRows();
275 QModelIndexList::iterator it;
276 for (it = indexList.begin(); it != indexList.end(); ++it)
278 QStandardItem* item = d->model->itemFromIndex(*it);
279 QString engineName = d->cboEngines->itemText(d->cboEngines->currentIndex());
280 setEngineOnItem(item, engineName);
284 void CFGCustomServers::setEngineOnItem(QStandardItem* item,
const QString& engineName)
286 int engineId = gPlugins->pluginIndexFromName(engineName);
288 item->setData(engineName);
289 item->setToolTip(engineName);
293 item->setIcon(plugin->icon());
297 item->setIcon(QPixmap(unknownengine_xpm));
300 item->setEditable(
false);
304 void CFGCustomServers::setPortToDefault(
int rowIndex)
306 const EnginePlugin* pluginInfo = getPluginInfoForRow(rowIndex);
307 QString defaultPort = QString::number(pluginInfo->data()->defaultServerPort);
309 QStandardItem* itemPort = d->model->item(rowIndex, PortColumnIndex);
310 itemPort->setText(defaultPort);
313 QVector<CustomServerInfo> CFGCustomServers::tableGetServers()
315 QVector<CustomServerInfo> servers;
316 for (
int i = 0; i < d->model->rowCount(); ++i)
320 QStandardItem* item = d->model->item(i, EngineColumnIndex);
321 customServer.engine = item->data().toString();
323 customServer.engineIndex = gPlugins->pluginIndexFromName(customServer.engine);
325 item = d->model->item(i, AddressColumnIndex);
326 customServer.host = item->text();
328 item = d->model->item(i, PortColumnIndex);
329 customServer.port = item->text().toUShort();
331 item = d->model->item(i, EnabledIndex);
332 customServer.enabled = (item->checkState() == Qt::Checked);
334 servers << customServer;
void readSettings()
Reimplement this to read settings from config into widgets.
void saveSettings()
Reimplement this to write settings to config from widgets.
Base class for configuration pages.