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
47 connect(d->btnAdd, SIGNAL( clicked() ),
this, SLOT( add() ));
48 connect(d->btnRemove, SIGNAL( clicked() ),
this, SLOT(
remove() ));
49 connect(d->btnSetEngine, SIGNAL( clicked() ),
this, SLOT( setEngine() ));
51 prepareEnginesComboBox();
54 CFGCustomServers::~CFGCustomServers()
58 void CFGCustomServers::add()
60 int pluginIndex = d->cboEngines->itemData(d->cboEngines->currentIndex()).toInt();
61 const EnginePlugin* plugin = gPlugins->info(pluginIndex);
63 QString engineName = d->cboEngines->itemText(d->cboEngines->currentIndex());
65 add(engineName,
"", plugin->data()->defaultServerPort);
68 void CFGCustomServers::add(
const QString& engineName,
const QString& host,
unsigned short port)
70 QList<QStandardItem* > record;
72 QStandardItem* engineItem =
new QStandardItem();
73 setEngineOnItem(engineItem, engineName);
75 QString portString = QString::number(port);
77 record.append(engineItem);
78 record.append(
new QStandardItem(host));
79 record.append(
new QStandardItem(portString));
81 model->appendRow(record);
82 d->tvServers->resizeRowsToContents();
87 CheckAndFixPorts returnValue = AllOk;
88 for (
int rowIndex = firstRow; rowIndex <= lastRow; ++rowIndex)
90 if (!isPortCorrect(rowIndex))
92 returnValue = AtLeastOneFixed;
94 setPortToDefault(rowIndex);
101 void CFGCustomServers::dataChanged(
const QModelIndex& topLeft,
const QModelIndex& bottomRight)
103 const QString MESSAGE_TITLE = tr(
"Doomseeker - custom servers");
105 int leftmostColumn = topLeft.column();
106 int rightmostColumn = bottomRight.column();
108 if ( isPortColumnWithingRange(leftmostColumn, rightmostColumn) )
116 case AtLeastOneFixed:
117 QMessageBox::warning(
this, MESSAGE_TITLE, tr(
"Port must be within range 1 - 65535"));
121 QMessageBox::warning(
this, MESSAGE_TITLE, tr(
"Unimplemented behavior!"));
127 const EnginePlugin* CFGCustomServers::getPluginInfoForRow(
int rowIndex)
129 QStandardItem* itemEngine = model->item(rowIndex, EngineColumnIndex);
130 QString engineName = itemEngine->data().toString();
131 int pluginIndex = gPlugins->pluginIndexFromName(engineName);
132 return gPlugins->info(pluginIndex);
135 bool CFGCustomServers::isPortColumnWithingRange(
int leftmostColumnIndex,
int rightmostColumnIndex)
137 return leftmostColumnIndex <= PortColumnIndex && rightmostColumnIndex >= PortColumnIndex;
140 bool CFGCustomServers::isPortCorrect(
int rowIndex)
142 const int MIN_PORT = 1;
143 const int MAX_PORT = 65535;
145 QStandardItem* item = model->item(rowIndex, PortColumnIndex);
147 int port = item->text().toInt(&ok);
149 return ok && port >= MIN_PORT && port <= MAX_PORT;
152 void CFGCustomServers::prepareEnginesComboBox()
154 d->cboEngines->clear();
156 for (
unsigned i = 0; i < gPlugins->numPlugins(); ++i)
159 d->cboEngines->addItem(plugin->icon(), plugin->data()->name, i);
162 if (d->cboEngines->count() > 0)
164 d->cboEngines->setCurrentIndex(0);
168 void CFGCustomServers::prepareTable()
170 model =
new QStandardItemModel(
this);
172 connect(model, SIGNAL( dataChanged(
const QModelIndex&,
const QModelIndex&) ),
this, SLOT( dataChanged(
const QModelIndex&,
const QModelIndex&) ) );
175 labels <<
"" << tr(
"Host") << tr(
"Port");
176 model->setHorizontalHeaderLabels(labels);
178 d->tvServers->setModel(model);
180 d->tvServers->setColumnWidth(0, 23);
181 d->tvServers->setColumnWidth(1, 180);
182 d->tvServers->setColumnWidth(2, 60);
184 d->tvServers->horizontalHeader()->setHighlightSections(
false);
185 d->tvServers->horizontalHeader()->setResizeMode(0, QHeaderView::Fixed);
187 d->tvServers->verticalHeader()->hide();
194 QList<CustomServerInfo> customServersList = gConfig.doomseeker.customServers.toList();
195 QList<CustomServerInfo>::iterator it;
196 for (it = customServersList.begin(); it != customServersList.end(); ++it)
198 add(it->engine, it->host, it->port);
202 void CFGCustomServers::remove()
204 QItemSelectionModel* selModel = d->tvServers->selectionModel();
205 QModelIndexList indexList = selModel->selectedRows();
208 QList<QStandardItem*> itemList;
209 for (
int i = 0; i < indexList.count(); ++i)
211 itemList << model->item(indexList[i].row(), 0);
214 for (
int i = 0; i < itemList.count(); ++i)
216 QModelIndex index = model->indexFromItem(itemList[i]);
217 model->removeRow(index.row());
223 gConfig.doomseeker.customServers = this->tableGetServers();
226 void CFGCustomServers::setEngine()
228 QItemSelectionModel* sel = d->tvServers->selectionModel();
229 QModelIndexList indexList = sel->selectedRows();
231 QModelIndexList::iterator it;
232 for (it = indexList.begin(); it != indexList.end(); ++it)
234 QStandardItem* item = model->itemFromIndex(*it);
235 QString engineName = d->cboEngines->itemText(d->cboEngines->currentIndex());
236 setEngineOnItem(item, engineName);
240 void CFGCustomServers::setEngineOnItem(QStandardItem* item,
const QString& engineName)
242 int engineId = gPlugins->pluginIndexFromName(engineName);
244 item->setData(engineName);
245 item->setToolTip(engineName);
249 item->setIcon(plugin->icon());
253 item->setIcon(QPixmap(unknownengine_xpm));
256 item->setEditable(
false);
260 void CFGCustomServers::setPortToDefault(
int rowIndex)
262 const EnginePlugin* pluginInfo = getPluginInfoForRow(rowIndex);
263 QString defaultPort = QString::number(pluginInfo->data()->defaultServerPort);
265 QStandardItem* itemPort = model->item(rowIndex, PortColumnIndex);
266 itemPort->setText(defaultPort);
269 QVector<CustomServerInfo> CFGCustomServers::tableGetServers()
271 QVector<CustomServerInfo> servers;
272 for (
int i = 0; i < model->rowCount(); ++i)
276 QStandardItem* item = model->item(i, EngineColumnIndex);
277 customServer.engine = item->data().toString();
279 customServer.engineIndex = gPlugins->pluginIndexFromName(customServer.engine);
281 item = model->item(i, AddressColumnIndex);
282 customServer.host = item->text();
284 item = model->item(i, PortColumnIndex);
285 customServer.port = item->text().toUShort();
287 servers << customServer;
void readSettings()
Reimplement this to read settings from config into widgets.
CheckAndFixPorts checkAndFixPorts(int firstRow, int lastRow)
Moves through rows and checks if network port information is correct.
void saveSettings()
Reimplement this to write settings to config from widgets.
Base class for configuration pages.