config/configwindow.cpp

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

mercurial