src/config.cpp

changeset 42
9528f8d06962
parent 41
e985ebb67fdd
child 43
1394901b557a
equal deleted inserted replaced
41:e985ebb67fdd 42:9528f8d06962
1 /*
2 * ZCinema: Zandronum demo launcher
3 * Copyright (C) 2013-2015 Teemu Piippo
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (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, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <QFileDialog>
20 #include "config.h"
21 #include "ui_configbox.h"
22 #include "misc.h"
23 #include "demo.h"
24 #include "prompts.h"
25
26 //
27 // -------------------------------------------------------------------------------------------------
28 //
29
30 ConfigWindow::ConfigWindow (QWidget* parent, Qt::WindowFlags f) :
31 QDialog (parent, f),
32 ui (*new Ui_ConfigBox)
33 {
34 ui.setupUi (this);
35
36 QStringList wadpaths = Config::get ("wadpaths").toStringList();
37 QList<QVariant> versions = Config::get ("versions").toList();
38
39 for (int i = 0; i < wadpaths.size(); ++i)
40 addWadPath (wadpaths[i]);
41
42 ui.noDemoPrompt->setChecked (Config::get ("noprompt").toBool());
43
44 for (int i = 0; i < versions.size(); ++i)
45 {
46 if (not versions[i].canConvert<ZandronumVersion>())
47 continue;
48
49 addVersion (versions[i].value<ZandronumVersion>());
50 }
51
52 connect (ui.wad_add, SIGNAL (clicked()), this, SLOT (addWadPath()));
53 connect (ui.wad_pathEntry, SIGNAL (returnPressed()), this, SLOT (addWadPath()));
54 connect (ui.wad_findPath, SIGNAL (clicked()), this, SLOT (findWadPath()));
55 connect (ui.wad_del, SIGNAL (clicked()), this, SLOT (removeCurrentWadPath()));
56 connect (ui.buttonBox, SIGNAL (clicked (QAbstractButton*)), this,
57 SLOT (buttonPressed (QAbstractButton*)));
58 connect (ui.addExePath, SIGNAL (clicked()), this, SLOT (newVersion()));
59 connect (ui.editExePath, SIGNAL (clicked()), this, SLOT (editExePressed()));
60 connect (ui.removeExePath, SIGNAL (clicked()), this, SLOT (removeCurrentVersion()));
61 connect (ui.clearExePaths, SIGNAL (clicked()), this, SLOT (clearExePathsClicked()));
62 setWindowTitle (versionSignature());
63 }
64
65 //
66 // -------------------------------------------------------------------------------------------------
67 //
68
69 ConfigWindow::~ConfigWindow()
70 {
71 delete &ui;
72
73 for (int i = 0; i < m_versionEntries.size(); ++i)
74 delete m_versionEntries[i];
75 }
76
77 //
78 // -------------------------------------------------------------------------------------------------
79 //
80
81 VersionGuiEntry* ConfigWindow::addVersion (const ZandronumVersion& version)
82 {
83 QTableWidgetItem* labelItem = new QTableWidgetItem (version.name);
84 QTableWidgetItem* pathItem = new QTableWidgetItem (version.binaryPath);
85 int rownum = ui.exePaths->rowCount();
86 ui.exePaths->setSortingEnabled (false);
87 ui.exePaths->insertRow (rownum);
88 ui.exePaths->setItem (rownum, LabelColumn, labelItem);
89 ui.exePaths->setItem (rownum, PathColumn, pathItem);
90 ui.exePaths->setSortingEnabled (true);
91
92 VersionGuiEntry* entry = new VersionGuiEntry;
93 entry->isRelease = version.isRelease;
94 entry->name = version.name;
95 entry->labelItem = labelItem;
96 entry->pathItem = pathItem;
97 m_versionEntries.append (entry);
98 m_versionEntryMap[pathItem] = entry;
99 return entry;
100 }
101
102 //
103 // -------------------------------------------------------------------------------------------------
104 //
105
106 void ConfigWindow::removeVersion (VersionGuiEntry* entry)
107 {
108 for (int i = 0; i < m_versionEntries.size(); ++i)
109 {
110 if (m_versionEntries[i] == entry)
111 {
112 m_versionEntries.removeAt (i);
113 break;
114 }
115 }
116
117 m_versionEntryMap.remove (entry->pathItem);
118 ui.exePaths->removeRow (entry->pathItem->row());
119 delete entry;
120 }
121
122 //
123 // -------------------------------------------------------------------------------------------------
124 //
125
126 void ConfigWindow::removeCurrentVersion()
127 {
128 VersionGuiEntry* entry = currentVersionEntry();
129
130 if (entry)
131 removeVersion (entry);
132 }
133
134 //
135 // -------------------------------------------------------------------------------------------------
136 //
137
138 void ConfigWindow::newVersion()
139 {
140 VersionGuiEntry* entry = addVersion (ZandronumVersion());
141 AddVersionPrompt* prompt = new AddVersionPrompt (entry, this);
142
143 if (not prompt->exec())
144 removeVersion (entry);
145 }
146
147 //
148 // -------------------------------------------------------------------------------------------------
149 //
150
151 void ConfigWindow::editExePressed()
152 {
153 VersionGuiEntry* entry = currentVersionEntry();
154 (new AddVersionPrompt (entry, this))->exec();
155 }
156
157 //
158 // -------------------------------------------------------------------------------------------------
159 //
160
161 void ConfigWindow::clearExePathsClicked()
162 {
163 if (confirm ("Are you sure you want to clear all Zandronum versions?"))
164 {
165 ui.exePaths->clearContents();
166 ui.exePaths->setRowCount (0);
167
168 for (int i = 0; i < m_versionEntries.size(); ++i)
169 delete m_versionEntries[i];
170
171 m_versionEntries.clear();
172 m_versionEntryMap.clear();
173 }
174 }
175
176 //
177 // -------------------------------------------------------------------------------------------------
178 //
179
180 ZandronumVersion VersionGuiEntry::toNonGuiVersion() const
181 {
182 return ZandronumVersion (name, isRelease, pathItem->text());
183 }
184
185 //
186 // -------------------------------------------------------------------------------------------------
187 //
188
189 VersionGuiEntry* ConfigWindow::currentVersionEntry()
190 {
191 int row = ui.exePaths->currentRow();
192
193 if (row != -1)
194 {
195 VersionEntryMap::iterator it = m_versionEntryMap.find (ui.exePaths->item (row, PathColumn));
196
197 if (it != m_versionEntryMap.end())
198 return it.value();
199 }
200
201 return NULL;
202 }
203
204 //
205 // -------------------------------------------------------------------------------------------------
206 //
207
208 void ConfigWindow::saveSettings()
209 {
210 QList<QVariant> wadPathList;
211 QList<QVariant> versions;
212
213 for (int i = 0; i < ui.wad_pathsList->count(); ++i)
214 wadPathList.append (ui.wad_pathsList->item (i)->text());
215
216 for (int i = 0; i < m_versionEntries.size(); ++i)
217 {
218 QVariant var;
219 var.setValue (m_versionEntries[i]->toNonGuiVersion());
220 versions.append (var);
221 }
222
223 Config::set ("wadpaths", wadPathList);
224 Config::set ("noprompt", ui.noDemoPrompt->isChecked());
225 Config::set ("versions", versions);
226 Config::sync();
227 }
228
229 //
230 // -------------------------------------------------------------------------------------------------
231 //
232
233 void ConfigWindow::addWadPath()
234 {
235 addWadPath (ui.wad_pathEntry->text());
236 ui.wad_pathEntry->clear();
237 }
238
239 //
240 // -------------------------------------------------------------------------------------------------
241 //
242
243 void ConfigWindow::addWadPath (QString path)
244 {
245 ui.wad_pathsList->addItem (path);
246 QListWidgetItem* item = ui.wad_pathsList->item (ui.wad_pathsList->count() - 1);
247 item->setFlags (item->flags() | Qt::ItemIsEditable);
248 }
249
250 //
251 // -------------------------------------------------------------------------------------------------
252 //
253
254 void ConfigWindow::findWadPath()
255 {
256 QString path = QFileDialog::getExistingDirectory (this);
257
258 if (path.isEmpty())
259 return;
260
261 ui.wad_pathEntry->setText (path);
262 }
263
264 //
265 // -------------------------------------------------------------------------------------------------
266 //
267
268 void ConfigWindow::removeCurrentWadPath()
269 {
270 delete ui.wad_pathsList->currentItem();
271 }
272
273 //
274 // -------------------------------------------------------------------------------------------------
275 //
276
277 void ConfigWindow::buttonPressed (QAbstractButton* btn)
278 {
279 if (btn == ui.buttonBox->button (QDialogButtonBox::Ok))
280 {
281 saveSettings();
282 accept();
283 }
284 else if (btn == ui.buttonBox->button (QDialogButtonBox::Cancel))
285 {
286 reject();
287 }
288 else if (btn == ui.buttonBox->button (QDialogButtonBox::Apply))
289 {
290 saveSettings();
291 }
292 }

mercurial