engineconfigurationbasebox.cpp
1 //------------------------------------------------------------------------------
2 // engineconfigurationbasebox.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) 2010 "Blzut3" <admin@maniacsvault.net>
22 //------------------------------------------------------------------------------
23 
24 #include "engineconfigurationbasebox.h"
25 #include "ui_engineconfigurationbasebox.h"
26 #include "ini/ini.h"
27 #include "plugins/engineplugin.h"
28 
29 #include <QFileDialog>
30 
31 DClass<EngineConfigurationBaseBox> : public Ui::EngineConfigurationBaseBox
32 {
33  public:
34  IniSection *config;
35  const EnginePlugin *plugin;
36  bool clientOnly;
37 
38  QStringList readStoredCustomParameters() const
39  {
40  return config->value("StoredCustomParameters").toStringList();
41  }
42 
43  void saveStoredCustomParameters(const QStringList &params)
44  {
45  config->setValue("StoredCustomParameters", params);
46  }
47 
48  bool existsInStoredCustomParameters(const QString &text) const
49  {
50  return readStoredCustomParameters().contains(text);
51  }
52 };
53 
55 
57 : ConfigurationBaseBox(parent)
58 {
59  d->plugin = plugin;
60  d->config = &cfg;
61  d->setupUi(this);
62 
63  // Prevent combo box stretching.
64  d->cboCustomParameters->setMinimumContentsLength(25);
65  d->cboCustomParameters->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLengthWithIcon);
66 
67  if(plugin->data()->clientOnly)
68  makeClientOnly();
69 
70  if(!plugin->data()->hasMasterServer)
71  d->masterAddressBox->hide();
72 
73  this->connect(d->btnBrowseClientBinary, SIGNAL( clicked() ), SLOT( browseForClientBinary() ));
74  this->connect(d->btnBrowseServerBinary, SIGNAL( clicked() ), SLOT( browseForServerBinary() ));
75 }
76 
77 EngineConfigurationBaseBox::~EngineConfigurationBaseBox()
78 {
79 }
80 
82 {
83  layout()->removeItem(d->verticalSpacer);
84  layout()->addWidget(widget);
85  layout()->addItem(d->verticalSpacer);
86 }
87 
88 void EngineConfigurationBaseBox::browseForBinary(QLineEdit *input, const QString &type)
89 {
90  QString filter;
91 #if defined(Q_OS_WIN32)
92  filter = tr("Binary files (*.exe);;Any files (*)");
93 #else
94  // Other platforms do not have an extension for their binary files.
95  filter = tr("Any files(*)");
96 #endif
97  QString filepath = QFileDialog::getOpenFileName(this,
98  tr("Doomseeker - choose %1 %2").arg(d->plugin->data()->name).arg(type),
99  QString(), filter);
100  if (!filepath.isEmpty())
101  input->setText(filepath);
102 }
103 
104 void EngineConfigurationBaseBox::browseForClientBinary()
105 {
106  browseForBinary(d->leClientBinaryPath, tr("client binary"));
107 
108 }
109 
110 void EngineConfigurationBaseBox::browseForServerBinary()
111 {
112  browseForBinary(d->leServerBinaryPath, tr("server binary"));
113 }
114 
115 QString EngineConfigurationBaseBox::currentCustomParameters() const
116 {
117  return d->cboCustomParameters->currentText().trimmed();
118 }
119 
121 {
122  return d->plugin->icon();
123 }
124 
125 void EngineConfigurationBaseBox::makeClientOnly()
126 {
127  d->clientOnly = true;
128 
129  d->lblClientBinary->setText(tr("Path to executable:"));
130  d->serverBinaryBox->hide();
131 }
132 
134 {
135  return d->plugin->data()->name;
136 }
137 
139 {
140  return d->plugin;
141 }
142 
144 {
145  d->leClientBinaryPath->setText(d->config->value("BinaryPath").toString());
146  d->cboCustomParameters->clear();
147  d->cboCustomParameters->addItems(d->readStoredCustomParameters());
148  d->cboCustomParameters->setEditText(d->config->value("CustomParameters").toString());
149  if(d->plugin->data()->hasMasterServer)
150  d->leMasterserverAddress->setText(d->config->value("Masterserver").toString());
151  d->leServerBinaryPath->setText(d->config->value("ServerBinaryPath").toString());
152 
153  updateCustomParametersSaveState();
154 }
155 
156 void EngineConfigurationBaseBox::removeCurrentCustomParametersFromStorage()
157 {
158  QString currentParams = currentCustomParameters();
159  removeStoredCustomParametersFromConfig(currentParams);
160  removeStoredCustomParametersFromWidget(currentParams);
161  updateCustomParametersSaveState();
162 }
163 
164 void EngineConfigurationBaseBox::removeStoredCustomParametersFromConfig(const QString &parameters)
165 {
166  QStringList storedParameters = d->readStoredCustomParameters();
167  storedParameters.removeAll(parameters);
168  d->saveStoredCustomParameters(storedParameters);
169 }
170 
171 void EngineConfigurationBaseBox::removeStoredCustomParametersFromWidget(const QString &parameters)
172 {
173  int indexInWidget = d->cboCustomParameters->findText(parameters);
174  if (indexInWidget >= 0)
175  {
176  d->cboCustomParameters->removeItem(indexInWidget);
177  d->cboCustomParameters->setEditText(parameters);
178  }
179 }
180 
181 void EngineConfigurationBaseBox::saveCustomParameters()
182 {
183  if (!d->existsInStoredCustomParameters(currentCustomParameters()))
184  {
185  QStringList parameters = d->readStoredCustomParameters();
186  parameters << currentCustomParameters();
187  d->saveStoredCustomParameters(parameters);
188  d->cboCustomParameters->addItem(currentCustomParameters());
189  }
190  updateCustomParametersSaveState();
191 }
192 
194 {
195  QString executable;
196 
197  executable = d->leClientBinaryPath->text();
198  d->config->setValue("BinaryPath", executable);
199  if (!d->clientOnly)
200  {
201  executable = d->leServerBinaryPath->text();
202  }
203  d->config->setValue("ServerBinaryPath", executable);
204  d->config->setValue("CustomParameters", currentCustomParameters());
205  if (d->plugin->data()->hasMasterServer)
206  {
207  d->config->setValue("Masterserver", d->leMasterserverAddress->text());
208  }
209 }
210 
212 {
213  return tr("Game - %1").arg(name());
214 }
215 
216 void EngineConfigurationBaseBox::updateCustomParametersSaveState()
217 {
218  bool paramExists = d->existsInStoredCustomParameters(currentCustomParameters());
219  bool isEmpty = currentCustomParameters().isEmpty();
220  d->btnSaveCustomParameters->setVisible(!isEmpty && !paramExists);
221  d->btnDeleteCustomParameters->setVisible(!isEmpty && paramExists);
222 }
QIcon icon() const
Reimplement this to return a displayable icon for the ConfigurationBaseBox.
const EnginePlugin * plugin() const
Parent plugin handled by this page.
void browseForBinary(QLineEdit *input, const QString &type)
Open file browse dialog and update "input" when successful.
QVariant value(const QString &key) const
Retrieves a variable directly; omits the IniVariable system.
Definition: inisection.cpp:164
void readSettings()
Reimplement this to read settings from config into widgets.
void saveSettings()
Reimplement this to write settings to config from widgets.
QString title() const
Page title, by default returns the same string as name().
QString name() const
Reimplement this to return a list-displayable name for this ConfigurationBaseBox. ...
void addWidget(QWidget *widget)
Add a new, custom widget below the standard ones.
Base for configuration pages for plugins; provides some default behavior.
void setValue(const QString &key, const QVariant &value)
Sets a variable directly. Omits the IniVariable system.
Definition: inisection.cpp:154
INI section representation.
Definition: inisection.h:40
Base class for configuration pages.