src/config.cpp

changeset 37
c82a86ea87be
parent 36
b8fa9171be6e
child 39
2c368cf5cc19
equal deleted inserted replaced
36:b8fa9171be6e 37:c82a86ea87be
1 /* 1 /*
2 * ZCinema: Zandronum demo launcher 2 * ZCinema: Zandronum demo launcher
3 * Copyright (C) 2013 Santeri Piippo 3 * Copyright (C) 2013-2015 Teemu Piippo
4 * 4 *
5 * This program is free software: you can redistribute it and/or modify 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 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 7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version. 8 * (at your option) any later version.
19 #include <QLabel> 19 #include <QLabel>
20 #include <QFileDialog> 20 #include <QFileDialog>
21 #include <QFormLayout> 21 #include <QFormLayout>
22 #include <QProgressBar> 22 #include <QProgressBar>
23 #include <QMessageBox> 23 #include <QMessageBox>
24 #include <QLineEdit>
25 #include <QListWidget>
26 #include <QPushButton>
27 #include <QCheckBox>
28 #include <QDialogButtonBox>
29 #include <QVBoxLayout>
24 #include "config.h" 30 #include "config.h"
25 #include "ui_configbox.h" 31 #include "ui_configbox.h"
26 #include "misc.h" 32 #include "misc.h"
27 #include "demo.h" 33 #include "demo.h"
28 #include "versionEditor.h" 34
29 35 class FindPathButton : public QPushButton
30 CONFIG (Bool, noprompt, false) 36 {
31 CONFIG (List, devBuildNames, cfg::List())
32 CONFIG (List, releaseNames, cfg::List())
33 CONFIG (List, wadpaths, cfg::List())
34 CONFIG (Map, binaryPaths, cfg::Map())
35
36 // =============================================================================
37 // -----------------------------------------------------------------------------
38 class FindPathButton : public QPushButton {
39 public: 37 public:
40 explicit FindPathButton (QWidget* parent = null) : QPushButton (parent) { 38 explicit FindPathButton (QWidget* parent = NULL) :
39 QPushButton (parent)
40 {
41 setText ("..."); 41 setText ("...");
42 } 42 }
43 43
44 QLineEdit* editWidget() const { 44 QLineEdit* editWidget() const
45 {
45 return m_editWidget; 46 return m_editWidget;
46 } 47 }
47 void setEditWidget (QLineEdit* edit) { 48
49 void setEditWidget (QLineEdit* edit)
50 {
48 m_editWidget = edit; 51 m_editWidget = edit;
49 } 52 }
50 53
51 private: 54 private:
52 QLineEdit* m_editWidget; 55 QLineEdit* m_editWidget;
53 }; 56 };
54 57
55 // ============================================================================= 58 ConfigWindow::ConfigWindow (QWidget* parent, Qt::WindowFlags f) :
56 // -----------------------------------------------------------------------------
57 ConfigBox::ConfigBox (QWidget* parent, Qt::WindowFlags f) :
58 QDialog (parent, f), 59 QDialog (parent, f),
59 ui (new Ui_ConfigBox), 60 ui (new Ui_ConfigBox),
60 m_releaseLayout (null), 61 m_releaseLayout (NULL),
61 m_testLayout (null) 62 m_testLayout (NULL)
62 { 63 {
63 ui->setupUi (this); 64 ui->setupUi (this);
64 65
65 initVersions(); 66 initVersions();
66 initFromSettings(); 67 initFromSettings();
68 connect (ui->wad_add, SIGNAL (clicked()), this, SLOT (addPath())); 69 connect (ui->wad_add, SIGNAL (clicked()), this, SLOT (addPath()));
69 connect (ui->wad_pathEntry, SIGNAL (returnPressed()), this, SLOT (addPath())); 70 connect (ui->wad_pathEntry, SIGNAL (returnPressed()), this, SLOT (addPath()));
70 connect (ui->wad_findPath, SIGNAL (clicked()), this, SLOT (findPath())); 71 connect (ui->wad_findPath, SIGNAL (clicked()), this, SLOT (findPath()));
71 connect (ui->wad_del, SIGNAL (clicked()), this, SLOT (delPath())); 72 connect (ui->wad_del, SIGNAL (clicked()), this, SLOT (delPath()));
72 connect (ui->buttonBox, SIGNAL (clicked (QAbstractButton*)), this, 73 connect (ui->buttonBox, SIGNAL (clicked (QAbstractButton*)), this,
73 SLOT (buttonPressed (QAbstractButton*))); 74 SLOT (buttonPressed (QAbstractButton*)));
74 connect (ui->m_editVersions, SIGNAL (clicked()), this, SLOT (editBinaries()));
75 connect (ui->m_editVersions_2, SIGNAL (clicked()), this, SLOT (editBinaries()));
76 setWindowTitle (versionSignature()); 75 setWindowTitle (versionSignature());
77 } 76 }
78 77
79 // ============================================================================= 78 //
80 // ----------------------------------------------------------------------------- 79 // -----------------------------------------------------------------------------
81 ConfigBox::~ConfigBox() { 80 //
81
82 ConfigWindow::~ConfigWindow()
83 {
82 delete ui; 84 delete ui;
83 } 85 }
84 86
85 // ============================================================================= 87 //
86 // ----------------------------------------------------------------------------- 88 // -----------------------------------------------------------------------------
87 void ConfigBox::initVersions() { 89 //
88 if (m_releaseLayout == null) { 90
89 m_releaseLayout = new QFormLayout; 91 void ConfigWindow::initVersions()
90 m_testLayout = new QFormLayout; 92 {
91 ui->zandronumVersions->setLayout (m_releaseLayout); 93 if (m_releaseLayout == NULL)
92 ui->betaVersions->setLayout (m_testLayout); 94 {
93 } else { 95 m_releaseLayout = new QVBoxLayout;
94 // re-init, clear the layouts 96 m_testLayout = new QVBoxLayout;
97 ui->releaseVersions->setLayout (m_releaseLayout);
98 ui->testingVersions->setLayout (m_testLayout);
99 }
100 else
101 {
102 // Versions are being re-initialized, clear everything
95 for (QWidget* w : m_binaryLayoutWidgets) 103 for (QWidget* w : m_binaryLayoutWidgets)
96 delete w; 104 w->deleteLater();
97 105
98 m_binaryLayoutWidgets.clear(); 106 m_binaryLayoutWidgets.clear();
99 m_zanBinaries.clear(); 107 m_pathInputFields.clear();
100 } 108 }
101 109
102 for (const QVariant& ver : cfg::devBuildNames) 110 QList<QVariant> versions = getVersions();
103 addVersion (ver.toString(), false); 111
104 112 for (int i = 0; i < versions.size(); ++i)
105 for (const QVariant& rel : cfg::releaseNames) 113 {
106 addVersion (rel.toString(), true); 114 if (not versions[i].canConvert<ZandronumVersion>())
115 continue;
116
117 ZandronumVersion version = versions[i].value<ZandronumVersion>();
118 addVersion (version);
119 }
120 }
121
122 //
123 // -------------------------------------------------------------------------------------------------
124 //
125
126 void ConfigWindow::addVersion (const ZandronumVersion& version)
127 {
128 QLabel* label = new QLabel (version.name + ":");
129 QLineEdit* pathInput = new QLineEdit;
130 FindPathButton* pathFindButton = new FindPathButton;
131 pathFindButton->setEditWidget (pathInput);
132 connect (pathFindButton, SIGNAL (clicked()), this, SLOT (findZanBinary()));
133
134 QHBoxLayout* pathInputLayout = new QHBoxLayout;
135 pathInputLayout->addWidget (label);
136 pathInputLayout->addWidget (pathInput);
137 pathInputLayout->addWidget (pathFindButton);
138 pathInput->setText (version.binaryPath);
139
140 m_pathInputFields[version.name] = pathInput;
141
142 if (version.isRelease)
143 m_releaseLayout->addLayout (pathInputLayout);
144 else
145 m_testLayout->addLayout (pathInputLayout);
146
147 m_binaryLayoutWidgets << pathInput << pathFindButton << label;
107 } 148 }
108 149
109 // ============================================================================= 150 // =============================================================================
110 // ----------------------------------------------------------------------------- 151 // -----------------------------------------------------------------------------
111 void ConfigBox::addVersion (const str& name, bool isRelease) { 152 void ConfigWindow::initFromSettings()
112 QLabel* lb = new QLabel (name + ":"); 153 {
113 QLineEdit* ledit = new QLineEdit;
114 FindPathButton* btn = new FindPathButton;
115 btn->setEditWidget (ledit);
116
117 QWidget* wdg = new QWidget;
118 QHBoxLayout* leditLayout = new QHBoxLayout (wdg);
119 leditLayout->addWidget (ledit);
120 leditLayout->addWidget (btn);
121
122 m_zanBinaries << ledit;
123 connect (btn, SIGNAL (clicked()), this, SLOT (findZanBinary()));
124
125 if (isRelease)
126 m_releaseLayout->addRow (lb, wdg);
127 else
128 m_testLayout->addRow (lb, wdg);
129
130 m_binaryLayoutWidgets << ledit << btn << wdg << lb;
131 }
132
133 // =============================================================================
134 // -----------------------------------------------------------------------------
135 void ConfigBox::initFromSettings() {
136 ui->wad_pathsList->clear(); 154 ui->wad_pathsList->clear();
137 155
138 for (const QVariant& it : cfg::wadpaths) 156 for (const QVariant& it : Config::get ("wadpaths").toList())
139 addPath (it.toString()); 157 addPath (it.toString());
140 158
141 int i = 0; 159 QList<QVariant> versions = Config::get ("versions").toList();
142 160
143 for (const QVariant& ver : getVersions()) 161 for (int i = 0; i < versions.size(); ++i)
144 m_zanBinaries[i++]->setText (cfg::binaryPaths[ver.toString()].toString()); 162 {
145 163 ZandronumVersion version = versions[i].value<ZandronumVersion>();
146 ui->noDemoPrompt->setChecked (cfg::noprompt); 164 m_pathInputFields[version.name]->setText (version.binaryPath);
147 } 165 }
148 166
149 // ============================================================================= 167 ui->noDemoPrompt->setChecked (Config::get ("noprompt").toBool());
150 // ----------------------------------------------------------------------------- 168 }
151 void ConfigBox::saveSettings() { 169
170 //
171 // -----------------------------------------------------------------------------
172 //
173
174 void ConfigWindow::saveSettings()
175 {
152 QList<QVariant> wadPathList; 176 QList<QVariant> wadPathList;
153 177
154 for (int i = 0; i < ui->wad_pathsList->count(); ++i) 178 for (int i = 0; i < ui->wad_pathsList->count(); ++i)
155 wadPathList << ui->wad_pathsList->item (i)->text(); 179 wadPathList << ui->wad_pathsList->item (i)->text();
156 180
157 cfg::wadpaths = wadPathList; 181 Config::set ("wadpaths", wadPathList);
158 cfg::noprompt = ui->noDemoPrompt->isChecked(); 182 Config::set ("noprompt", ui->noDemoPrompt->isChecked());
159 183
160 int i = 0; 184 QList<QVariant> versions = getVersions();
161 for (const QVariant& ver : getVersions()) 185
162 cfg::binaryPaths[ver.toString()] = m_zanBinaries[i++]->text(); 186 for (int i = 0; i < versions.size(); ++i)
163 187 {
164 cfg::save(); 188 if (not versions[i].canConvert<ZandronumVersion>())
165 } 189 continue;
166 190
167 // ============================================================================= 191 ZandronumVersion version = versions[i].value<ZandronumVersion>();
168 // ----------------------------------------------------------------------------- 192 version.binaryPath = m_pathInputFields[version.name]->text();
169 void ConfigBox::addPath() { 193 versions[i].setValue (version);
194 }
195
196 Config::set ("versions", versions);
197 }
198
199 //
200 // -----------------------------------------------------------------------------
201 //
202
203 void ConfigWindow::addPath()
204 {
170 addPath (ui->wad_pathEntry->text()); 205 addPath (ui->wad_pathEntry->text());
171 ui->wad_pathEntry->clear(); 206 ui->wad_pathEntry->clear();
172 } 207 }
173 208
174 // ============================================================================= 209 //
175 // ----------------------------------------------------------------------------- 210 // -----------------------------------------------------------------------------
176 void ConfigBox::addPath (str path) { 211 //
212
213 void ConfigWindow::addPath (QString path)
214 {
177 ui->wad_pathsList->addItem (path); 215 ui->wad_pathsList->addItem (path);
178 QListWidgetItem* item = ui->wad_pathsList->item (ui->wad_pathsList->count() - 1); 216 QListWidgetItem* item = ui->wad_pathsList->item (ui->wad_pathsList->count() - 1);
179 item->setFlags (item->flags() | Qt::ItemIsEditable); 217 item->setFlags (item->flags() | Qt::ItemIsEditable);
180 } 218 }
181 219
182 // ============================================================================= 220 //
183 // ----------------------------------------------------------------------------- 221 // -----------------------------------------------------------------------------
184 void ConfigBox::findPath() { 222 //
185 str path = QFileDialog::getExistingDirectory (this); 223
224 void ConfigWindow::findPath()
225 {
226 QString path = QFileDialog::getExistingDirectory (this);
186 227
187 if (path.isEmpty()) 228 if (path.isEmpty())
188 return; 229 return;
189 230
190 ui->wad_pathEntry->setText (path); 231 ui->wad_pathEntry->setText (path);
191 } 232 }
192 233
193 // ============================================================================= 234 //
194 // ----------------------------------------------------------------------------- 235 // -----------------------------------------------------------------------------
195 void ConfigBox::delPath() { 236 //
237
238 void ConfigWindow::delPath()
239 {
196 delete ui->wad_pathsList->currentItem(); 240 delete ui->wad_pathsList->currentItem();
197 } 241 }
198 242
199 // ============================================================================= 243 //
200 // ----------------------------------------------------------------------------- 244 // -----------------------------------------------------------------------------
201 void ConfigBox::findZanBinary() { 245 //
202 FindPathButton* btn = dynamic_cast<FindPathButton*> (sender()); 246
203 247 void ConfigWindow::findZanBinary()
204 if (!btn) 248 {
205 return; 249 FindPathButton* button = static_cast<FindPathButton*> (sender());
206 250 QString path = getBinaryPath (this);
207 str path = getBinaryPath (this); 251
208 if (path.isEmpty()) 252 if (path.isEmpty())
209 return; 253 return;
210 254
211 btn->editWidget()->setText (path); 255 button->editWidget()->setText (path);
212 } 256 }
213 257
214 // ============================================================================= 258 //
215 // ----------------------------------------------------------------------------- 259 // -----------------------------------------------------------------------------
216 str ConfigBox::getBinaryPath (QWidget* parent) { 260 //
217 str path; 261
218 const str filter = 262 QString ConfigWindow::getBinaryPath (QWidget* parent)
263 {
219 #ifdef _WIN32 264 #ifdef _WIN32
220 "Zandronum Binaries (zandronum.exe)(zandronum.exe);;All files (*.*)(*.*)"; 265 # define ZAN_EXE_NAME "zandronum.exe"
221 #else 266 #else
222 "Zandronum Binaries (zandronum)(zandronum);;All files (*.*)(*.*)"; 267 # define ZAN_EXE_NAME "zandronum"
223 #endif 268 #endif
224 269
225 return QFileDialog::getOpenFileName (parent, QString(), QString(), filter); 270 return QFileDialog::getOpenFileName (parent, "", "",
226 } 271 "Zandronum Binaries (" ZAN_EXE_NAME ")(" ZAN_EXE_NAME ");;All files (*.*)(*.*)");
227 272 }
228 // ============================================================================= 273
229 // ----------------------------------------------------------------------------- 274 //
230 void ConfigBox::buttonPressed (QAbstractButton* btn) { 275 // -----------------------------------------------------------------------------
231 if (btn == ui->buttonBox->button (QDialogButtonBox::Ok)) { 276 //
277
278 void ConfigWindow::buttonPressed (QAbstractButton* btn) {
279 if (btn == ui->buttonBox->button (QDialogButtonBox::Ok))
280 {
232 saveSettings(); 281 saveSettings();
233 accept(); 282 accept();
234 } elif (btn == ui->buttonBox->button (QDialogButtonBox::Cancel)) 283 }
284 else if (btn == ui->buttonBox->button (QDialogButtonBox::Cancel))
235 reject(); 285 reject();
236 elif (btn == ui->buttonBox->button (QDialogButtonBox::Apply)) 286 else if (btn == ui->buttonBox->button (QDialogButtonBox::Apply))
237 saveSettings(); 287 saveSettings();
238 } 288 }
239
240 // =============================================================================
241 // -----------------------------------------------------------------------------
242 void ConfigBox::editBinaries() {
243 VersionEditor* dlg = new VersionEditor (this);
244
245 if (dlg->exec()) {
246 dlg->saveChanges();
247 initVersions();
248 }
249 }

mercurial