src/config.cpp

changeset 39
2c368cf5cc19
parent 37
c82a86ea87be
equal deleted inserted replaced
38:db677d321cf4 39:2c368cf5cc19
14 * 14 *
15 * You should have received a copy of the GNU General Public License 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/>. 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */ 17 */
18 18
19 #include <QLabel>
20 #include <QFileDialog> 19 #include <QFileDialog>
21 #include <QFormLayout>
22 #include <QProgressBar>
23 #include <QMessageBox>
24 #include <QLineEdit>
25 #include <QListWidget>
26 #include <QPushButton>
27 #include <QCheckBox>
28 #include <QDialogButtonBox>
29 #include <QVBoxLayout>
30 #include "config.h" 20 #include "config.h"
31 #include "ui_configbox.h" 21 #include "ui_configbox.h"
32 #include "misc.h" 22 #include "misc.h"
33 #include "demo.h" 23 #include "demo.h"
34 24 #include "prompts.h"
35 class FindPathButton : public QPushButton 25
36 { 26 //
37 public: 27 // -------------------------------------------------------------------------------------------------
38 explicit FindPathButton (QWidget* parent = NULL) : 28 //
39 QPushButton (parent)
40 {
41 setText ("...");
42 }
43
44 QLineEdit* editWidget() const
45 {
46 return m_editWidget;
47 }
48
49 void setEditWidget (QLineEdit* edit)
50 {
51 m_editWidget = edit;
52 }
53
54 private:
55 QLineEdit* m_editWidget;
56 };
57 29
58 ConfigWindow::ConfigWindow (QWidget* parent, Qt::WindowFlags f) : 30 ConfigWindow::ConfigWindow (QWidget* parent, Qt::WindowFlags f) :
59 QDialog (parent, f), 31 QDialog (parent, f),
60 ui (new Ui_ConfigBox), 32 ui (*new Ui_ConfigBox)
61 m_releaseLayout (NULL), 33 {
62 m_testLayout (NULL) 34 ui.setupUi (this);
63 { 35
64 ui->setupUi (this); 36 QStringList wadpaths = Config::get ("wadpaths").toStringList();
65 37 QList<QVariant> versions = Config::get ("versions").toList();
66 initVersions(); 38
67 initFromSettings(); 39 for (int i = 0; i < wadpaths.size(); ++i)
68 40 addWadPath (wadpaths[i]);
69 connect (ui->wad_add, SIGNAL (clicked()), this, SLOT (addPath())); 41
70 connect (ui->wad_pathEntry, SIGNAL (returnPressed()), this, SLOT (addPath())); 42 ui.noDemoPrompt->setChecked (Config::get ("noprompt").toBool());
71 connect (ui->wad_findPath, SIGNAL (clicked()), this, SLOT (findPath()));
72 connect (ui->wad_del, SIGNAL (clicked()), this, SLOT (delPath()));
73 connect (ui->buttonBox, SIGNAL (clicked (QAbstractButton*)), this,
74 SLOT (buttonPressed (QAbstractButton*)));
75 setWindowTitle (versionSignature());
76 }
77
78 //
79 // -----------------------------------------------------------------------------
80 //
81
82 ConfigWindow::~ConfigWindow()
83 {
84 delete ui;
85 }
86
87 //
88 // -----------------------------------------------------------------------------
89 //
90
91 void ConfigWindow::initVersions()
92 {
93 if (m_releaseLayout == NULL)
94 {
95 m_releaseLayout = new QVBoxLayout;
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
103 for (QWidget* w : m_binaryLayoutWidgets)
104 w->deleteLater();
105
106 m_binaryLayoutWidgets.clear();
107 m_pathInputFields.clear();
108 }
109
110 QList<QVariant> versions = getVersions();
111 43
112 for (int i = 0; i < versions.size(); ++i) 44 for (int i = 0; i < versions.size(); ++i)
113 { 45 {
114 if (not versions[i].canConvert<ZandronumVersion>()) 46 if (not versions[i].canConvert<ZandronumVersion>())
115 continue; 47 continue;
116 48
117 ZandronumVersion version = versions[i].value<ZandronumVersion>(); 49 addVersion (versions[i].value<ZandronumVersion>());
118 addVersion (version); 50 }
119 } 51
120 } 52 connect (ui.wad_add, SIGNAL (clicked()), this, SLOT (addWadPath()));
121 53 connect (ui.wad_pathEntry, SIGNAL (returnPressed()), this, SLOT (addWadPath()));
122 // 54 connect (ui.wad_findPath, SIGNAL (clicked()), this, SLOT (findWadPath()));
123 // ------------------------------------------------------------------------------------------------- 55 connect (ui.wad_del, SIGNAL (clicked()), this, SLOT (removeCurrentWadPath()));
124 // 56 connect (ui.buttonBox, SIGNAL (clicked (QAbstractButton*)), this,
125 57 SLOT (buttonPressed (QAbstractButton*)));
126 void ConfigWindow::addVersion (const ZandronumVersion& version) 58 connect (ui.addExePath, SIGNAL (clicked()), this, SLOT (newVersion()));
127 { 59 connect (ui.editExePath, SIGNAL (clicked()), this, SLOT (editExePressed()));
128 QLabel* label = new QLabel (version.name + ":"); 60 connect (ui.removeExePath, SIGNAL (clicked()), this, SLOT (removeCurrentVersion()));
129 QLineEdit* pathInput = new QLineEdit; 61 connect (ui.clearExePaths, SIGNAL (clicked()), this, SLOT (clearExePathsClicked()));
130 FindPathButton* pathFindButton = new FindPathButton; 62 setWindowTitle (versionSignature());
131 pathFindButton->setEditWidget (pathInput); 63 }
132 connect (pathFindButton, SIGNAL (clicked()), this, SLOT (findZanBinary())); 64
133 65 //
134 QHBoxLayout* pathInputLayout = new QHBoxLayout; 66 // -------------------------------------------------------------------------------------------------
135 pathInputLayout->addWidget (label); 67 //
136 pathInputLayout->addWidget (pathInput); 68
137 pathInputLayout->addWidget (pathFindButton); 69 ConfigWindow::~ConfigWindow()
138 pathInput->setText (version.binaryPath); 70 {
139 71 delete &ui;
140 m_pathInputFields[version.name] = pathInput; 72
141 73 for (int i = 0; i < m_versionEntries.size(); ++i)
142 if (version.isRelease) 74 delete m_versionEntries[i];
143 m_releaseLayout->addLayout (pathInputLayout); 75 }
144 else 76
145 m_testLayout->addLayout (pathInputLayout); 77 //
146 78 // -------------------------------------------------------------------------------------------------
147 m_binaryLayoutWidgets << pathInput << pathFindButton << label; 79 //
148 } 80
149 81 VersionGuiEntry* ConfigWindow::addVersion (const ZandronumVersion& version)
150 // ============================================================================= 82 {
151 // ----------------------------------------------------------------------------- 83 QTableWidgetItem* labelItem = new QTableWidgetItem (version.name);
152 void ConfigWindow::initFromSettings() 84 QTableWidgetItem* pathItem = new QTableWidgetItem (version.binaryPath);
153 { 85 int rownum = ui.exePaths->rowCount();
154 ui->wad_pathsList->clear(); 86 ui.exePaths->setSortingEnabled (false);
155 87 ui.exePaths->insertRow (rownum);
156 for (const QVariant& it : Config::get ("wadpaths").toList()) 88 ui.exePaths->setItem (rownum, LabelColumn, labelItem);
157 addPath (it.toString()); 89 ui.exePaths->setItem (rownum, PathColumn, pathItem);
158 90 ui.exePaths->setSortingEnabled (true);
159 QList<QVariant> versions = Config::get ("versions").toList(); 91
160 92 VersionGuiEntry* entry = new VersionGuiEntry;
161 for (int i = 0; i < versions.size(); ++i) 93 entry->isRelease = version.isRelease;
162 { 94 entry->name = version.name;
163 ZandronumVersion version = versions[i].value<ZandronumVersion>(); 95 entry->labelItem = labelItem;
164 m_pathInputFields[version.name]->setText (version.binaryPath); 96 entry->pathItem = pathItem;
165 } 97 m_versionEntries.append (entry);
166 98 m_versionEntryMap[pathItem] = entry;
167 ui->noDemoPrompt->setChecked (Config::get ("noprompt").toBool()); 99 return entry;
168 } 100 }
169 101
170 // 102 //
171 // ----------------------------------------------------------------------------- 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 // -------------------------------------------------------------------------------------------------
172 // 206 //
173 207
174 void ConfigWindow::saveSettings() 208 void ConfigWindow::saveSettings()
175 { 209 {
176 QList<QVariant> wadPathList; 210 QList<QVariant> wadPathList;
177 211 QList<QVariant> versions;
178 for (int i = 0; i < ui->wad_pathsList->count(); ++i) 212
179 wadPathList << ui->wad_pathsList->item (i)->text(); 213 for (int i = 0; i < ui.wad_pathsList->count(); ++i)
180 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
181 Config::set ("wadpaths", wadPathList); 223 Config::set ("wadpaths", wadPathList);
182 Config::set ("noprompt", ui->noDemoPrompt->isChecked()); 224 Config::set ("noprompt", ui.noDemoPrompt->isChecked());
183
184 QList<QVariant> versions = getVersions();
185
186 for (int i = 0; i < versions.size(); ++i)
187 {
188 if (not versions[i].canConvert<ZandronumVersion>())
189 continue;
190
191 ZandronumVersion version = versions[i].value<ZandronumVersion>();
192 version.binaryPath = m_pathInputFields[version.name]->text();
193 versions[i].setValue (version);
194 }
195
196 Config::set ("versions", versions); 225 Config::set ("versions", versions);
197 } 226 Config::sync();
198 227 }
199 // 228
200 // ----------------------------------------------------------------------------- 229 //
201 // 230 // -------------------------------------------------------------------------------------------------
202 231 //
203 void ConfigWindow::addPath() 232
204 { 233 void ConfigWindow::addWadPath()
205 addPath (ui->wad_pathEntry->text()); 234 {
206 ui->wad_pathEntry->clear(); 235 addWadPath (ui.wad_pathEntry->text());
207 } 236 ui.wad_pathEntry->clear();
208 237 }
209 // 238
210 // ----------------------------------------------------------------------------- 239 //
211 // 240 // -------------------------------------------------------------------------------------------------
212 241 //
213 void ConfigWindow::addPath (QString path) 242
214 { 243 void ConfigWindow::addWadPath (QString path)
215 ui->wad_pathsList->addItem (path); 244 {
216 QListWidgetItem* item = ui->wad_pathsList->item (ui->wad_pathsList->count() - 1); 245 ui.wad_pathsList->addItem (path);
246 QListWidgetItem* item = ui.wad_pathsList->item (ui.wad_pathsList->count() - 1);
217 item->setFlags (item->flags() | Qt::ItemIsEditable); 247 item->setFlags (item->flags() | Qt::ItemIsEditable);
218 } 248 }
219 249
220 // 250 //
221 // ----------------------------------------------------------------------------- 251 // -------------------------------------------------------------------------------------------------
222 // 252 //
223 253
224 void ConfigWindow::findPath() 254 void ConfigWindow::findWadPath()
225 { 255 {
226 QString path = QFileDialog::getExistingDirectory (this); 256 QString path = QFileDialog::getExistingDirectory (this);
227 257
228 if (path.isEmpty()) 258 if (path.isEmpty())
229 return; 259 return;
230 260
231 ui->wad_pathEntry->setText (path); 261 ui.wad_pathEntry->setText (path);
232 } 262 }
233 263
234 // 264 //
235 // ----------------------------------------------------------------------------- 265 // -------------------------------------------------------------------------------------------------
236 // 266 //
237 267
238 void ConfigWindow::delPath() 268 void ConfigWindow::removeCurrentWadPath()
239 { 269 {
240 delete ui->wad_pathsList->currentItem(); 270 delete ui.wad_pathsList->currentItem();
241 } 271 }
242 272
243 // 273 //
244 // ----------------------------------------------------------------------------- 274 // -------------------------------------------------------------------------------------------------
245 // 275 //
246 276
247 void ConfigWindow::findZanBinary() 277 void ConfigWindow::buttonPressed (QAbstractButton* btn)
248 { 278 {
249 FindPathButton* button = static_cast<FindPathButton*> (sender()); 279 if (btn == ui.buttonBox->button (QDialogButtonBox::Ok))
250 QString path = getBinaryPath (this);
251
252 if (path.isEmpty())
253 return;
254
255 button->editWidget()->setText (path);
256 }
257
258 //
259 // -----------------------------------------------------------------------------
260 //
261
262 QString ConfigWindow::getBinaryPath (QWidget* parent)
263 {
264 #ifdef _WIN32
265 # define ZAN_EXE_NAME "zandronum.exe"
266 #else
267 # define ZAN_EXE_NAME "zandronum"
268 #endif
269
270 return QFileDialog::getOpenFileName (parent, "", "",
271 "Zandronum Binaries (" ZAN_EXE_NAME ")(" ZAN_EXE_NAME ");;All files (*.*)(*.*)");
272 }
273
274 //
275 // -----------------------------------------------------------------------------
276 //
277
278 void ConfigWindow::buttonPressed (QAbstractButton* btn) {
279 if (btn == ui->buttonBox->button (QDialogButtonBox::Ok))
280 { 280 {
281 saveSettings(); 281 saveSettings();
282 accept(); 282 accept();
283 } 283 }
284 else if (btn == ui->buttonBox->button (QDialogButtonBox::Cancel)) 284 else if (btn == ui.buttonBox->button (QDialogButtonBox::Cancel))
285 {
285 reject(); 286 reject();
286 else if (btn == ui->buttonBox->button (QDialogButtonBox::Apply)) 287 }
288 else if (btn == ui.buttonBox->button (QDialogButtonBox::Apply))
289 {
287 saveSettings(); 290 saveSettings();
288 } 291 }
292 }

mercurial