--- a/src/config.cpp Mon Jun 01 17:06:13 2015 +0300 +++ b/src/config.cpp Fri Jun 05 18:33:51 2015 +0300 @@ -1,6 +1,6 @@ /* * ZCinema: Zandronum demo launcher - * Copyright (C) 2013 Santeri Piippo + * Copyright (C) 2013-2015 Teemu Piippo * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -21,30 +21,33 @@ #include <QFormLayout> #include <QProgressBar> #include <QMessageBox> +#include <QLineEdit> +#include <QListWidget> +#include <QPushButton> +#include <QCheckBox> +#include <QDialogButtonBox> +#include <QVBoxLayout> #include "config.h" #include "ui_configbox.h" #include "misc.h" #include "demo.h" -#include "versionEditor.h" -CONFIG (Bool, noprompt, false) -CONFIG (List, devBuildNames, cfg::List()) -CONFIG (List, releaseNames, cfg::List()) -CONFIG (List, wadpaths, cfg::List()) -CONFIG (Map, binaryPaths, cfg::Map()) - -// ============================================================================= -// ----------------------------------------------------------------------------- -class FindPathButton : public QPushButton { +class FindPathButton : public QPushButton +{ public: - explicit FindPathButton (QWidget* parent = null) : QPushButton (parent) { + explicit FindPathButton (QWidget* parent = NULL) : + QPushButton (parent) + { setText ("..."); } - QLineEdit* editWidget() const { + QLineEdit* editWidget() const + { return m_editWidget; } - void setEditWidget (QLineEdit* edit) { + + void setEditWidget (QLineEdit* edit) + { m_editWidget = edit; } @@ -52,13 +55,11 @@ QLineEdit* m_editWidget; }; -// ============================================================================= -// ----------------------------------------------------------------------------- -ConfigBox::ConfigBox (QWidget* parent, Qt::WindowFlags f) : +ConfigWindow::ConfigWindow (QWidget* parent, Qt::WindowFlags f) : QDialog (parent, f), ui (new Ui_ConfigBox), - m_releaseLayout (null), - m_testLayout (null) + m_releaseLayout (NULL), + m_testLayout (NULL) { ui->setupUi (this); @@ -70,119 +71,159 @@ connect (ui->wad_findPath, SIGNAL (clicked()), this, SLOT (findPath())); connect (ui->wad_del, SIGNAL (clicked()), this, SLOT (delPath())); connect (ui->buttonBox, SIGNAL (clicked (QAbstractButton*)), this, - SLOT (buttonPressed (QAbstractButton*))); - connect (ui->m_editVersions, SIGNAL (clicked()), this, SLOT (editBinaries())); - connect (ui->m_editVersions_2, SIGNAL (clicked()), this, SLOT (editBinaries())); + SLOT (buttonPressed (QAbstractButton*))); setWindowTitle (versionSignature()); } -// ============================================================================= +// // ----------------------------------------------------------------------------- -ConfigBox::~ConfigBox() { +// + +ConfigWindow::~ConfigWindow() +{ delete ui; } -// ============================================================================= +// // ----------------------------------------------------------------------------- -void ConfigBox::initVersions() { - if (m_releaseLayout == null) { - m_releaseLayout = new QFormLayout; - m_testLayout = new QFormLayout; - ui->zandronumVersions->setLayout (m_releaseLayout); - ui->betaVersions->setLayout (m_testLayout); - } else { - // re-init, clear the layouts +// + +void ConfigWindow::initVersions() +{ + if (m_releaseLayout == NULL) + { + m_releaseLayout = new QVBoxLayout; + m_testLayout = new QVBoxLayout; + ui->releaseVersions->setLayout (m_releaseLayout); + ui->testingVersions->setLayout (m_testLayout); + } + else + { + // Versions are being re-initialized, clear everything for (QWidget* w : m_binaryLayoutWidgets) - delete w; + w->deleteLater(); m_binaryLayoutWidgets.clear(); - m_zanBinaries.clear(); + m_pathInputFields.clear(); + } + + QList<QVariant> versions = getVersions(); + + for (int i = 0; i < versions.size(); ++i) + { + if (not versions[i].canConvert<ZandronumVersion>()) + continue; + + ZandronumVersion version = versions[i].value<ZandronumVersion>(); + addVersion (version); } +} + +// +// ------------------------------------------------------------------------------------------------- +// + +void ConfigWindow::addVersion (const ZandronumVersion& version) +{ + QLabel* label = new QLabel (version.name + ":"); + QLineEdit* pathInput = new QLineEdit; + FindPathButton* pathFindButton = new FindPathButton; + pathFindButton->setEditWidget (pathInput); + connect (pathFindButton, SIGNAL (clicked()), this, SLOT (findZanBinary())); + + QHBoxLayout* pathInputLayout = new QHBoxLayout; + pathInputLayout->addWidget (label); + pathInputLayout->addWidget (pathInput); + pathInputLayout->addWidget (pathFindButton); + pathInput->setText (version.binaryPath); - for (const QVariant& ver : cfg::devBuildNames) - addVersion (ver.toString(), false); + m_pathInputFields[version.name] = pathInput; - for (const QVariant& rel : cfg::releaseNames) - addVersion (rel.toString(), true); + if (version.isRelease) + m_releaseLayout->addLayout (pathInputLayout); + else + m_testLayout->addLayout (pathInputLayout); + + m_binaryLayoutWidgets << pathInput << pathFindButton << label; } // ============================================================================= // ----------------------------------------------------------------------------- -void ConfigBox::addVersion (const str& name, bool isRelease) { - QLabel* lb = new QLabel (name + ":"); - QLineEdit* ledit = new QLineEdit; - FindPathButton* btn = new FindPathButton; - btn->setEditWidget (ledit); +void ConfigWindow::initFromSettings() +{ + ui->wad_pathsList->clear(); - QWidget* wdg = new QWidget; - QHBoxLayout* leditLayout = new QHBoxLayout (wdg); - leditLayout->addWidget (ledit); - leditLayout->addWidget (btn); + for (const QVariant& it : Config::get ("wadpaths").toList()) + addPath (it.toString()); + + QList<QVariant> versions = Config::get ("versions").toList(); + + for (int i = 0; i < versions.size(); ++i) + { + ZandronumVersion version = versions[i].value<ZandronumVersion>(); + m_pathInputFields[version.name]->setText (version.binaryPath); + } - m_zanBinaries << ledit; - connect (btn, SIGNAL (clicked()), this, SLOT (findZanBinary())); - - if (isRelease) - m_releaseLayout->addRow (lb, wdg); - else - m_testLayout->addRow (lb, wdg); - - m_binaryLayoutWidgets << ledit << btn << wdg << lb; + ui->noDemoPrompt->setChecked (Config::get ("noprompt").toBool()); } -// ============================================================================= +// // ----------------------------------------------------------------------------- -void ConfigBox::initFromSettings() { - ui->wad_pathsList->clear(); - - for (const QVariant& it : cfg::wadpaths) - addPath (it.toString()); - - int i = 0; - - for (const QVariant& ver : getVersions()) - m_zanBinaries[i++]->setText (cfg::binaryPaths[ver.toString()].toString()); - - ui->noDemoPrompt->setChecked (cfg::noprompt); -} +// -// ============================================================================= -// ----------------------------------------------------------------------------- -void ConfigBox::saveSettings() { +void ConfigWindow::saveSettings() +{ QList<QVariant> wadPathList; for (int i = 0; i < ui->wad_pathsList->count(); ++i) wadPathList << ui->wad_pathsList->item (i)->text(); - cfg::wadpaths = wadPathList; - cfg::noprompt = ui->noDemoPrompt->isChecked(); - - int i = 0; - for (const QVariant& ver : getVersions()) - cfg::binaryPaths[ver.toString()] = m_zanBinaries[i++]->text(); - - cfg::save(); + Config::set ("wadpaths", wadPathList); + Config::set ("noprompt", ui->noDemoPrompt->isChecked()); + + QList<QVariant> versions = getVersions(); + + for (int i = 0; i < versions.size(); ++i) + { + if (not versions[i].canConvert<ZandronumVersion>()) + continue; + + ZandronumVersion version = versions[i].value<ZandronumVersion>(); + version.binaryPath = m_pathInputFields[version.name]->text(); + versions[i].setValue (version); + } + + Config::set ("versions", versions); } -// ============================================================================= +// // ----------------------------------------------------------------------------- -void ConfigBox::addPath() { +// + +void ConfigWindow::addPath() +{ addPath (ui->wad_pathEntry->text()); ui->wad_pathEntry->clear(); } -// ============================================================================= +// // ----------------------------------------------------------------------------- -void ConfigBox::addPath (str path) { +// + +void ConfigWindow::addPath (QString path) +{ ui->wad_pathsList->addItem (path); QListWidgetItem* item = ui->wad_pathsList->item (ui->wad_pathsList->count() - 1); item->setFlags (item->flags() | Qt::ItemIsEditable); } -// ============================================================================= +// // ----------------------------------------------------------------------------- -void ConfigBox::findPath() { - str path = QFileDialog::getExistingDirectory (this); +// + +void ConfigWindow::findPath() +{ + QString path = QFileDialog::getExistingDirectory (this); if (path.isEmpty()) return; @@ -190,60 +231,58 @@ ui->wad_pathEntry->setText (path); } -// ============================================================================= +// // ----------------------------------------------------------------------------- -void ConfigBox::delPath() { +// + +void ConfigWindow::delPath() +{ delete ui->wad_pathsList->currentItem(); } -// ============================================================================= +// // ----------------------------------------------------------------------------- -void ConfigBox::findZanBinary() { - FindPathButton* btn = dynamic_cast<FindPathButton*> (sender()); - - if (!btn) - return; - - str path = getBinaryPath (this); +// + +void ConfigWindow::findZanBinary() +{ + FindPathButton* button = static_cast<FindPathButton*> (sender()); + QString path = getBinaryPath (this); + if (path.isEmpty()) return; - - btn->editWidget()->setText (path); + + button->editWidget()->setText (path); } -// ============================================================================= +// // ----------------------------------------------------------------------------- -str ConfigBox::getBinaryPath (QWidget* parent) { - str path; - const str filter = +// + +QString ConfigWindow::getBinaryPath (QWidget* parent) +{ #ifdef _WIN32 - "Zandronum Binaries (zandronum.exe)(zandronum.exe);;All files (*.*)(*.*)"; +# define ZAN_EXE_NAME "zandronum.exe" #else - "Zandronum Binaries (zandronum)(zandronum);;All files (*.*)(*.*)"; +# define ZAN_EXE_NAME "zandronum" #endif - - return QFileDialog::getOpenFileName (parent, QString(), QString(), filter); + + return QFileDialog::getOpenFileName (parent, "", "", + "Zandronum Binaries (" ZAN_EXE_NAME ")(" ZAN_EXE_NAME ");;All files (*.*)(*.*)"); } -// ============================================================================= +// // ----------------------------------------------------------------------------- -void ConfigBox::buttonPressed (QAbstractButton* btn) { - if (btn == ui->buttonBox->button (QDialogButtonBox::Ok)) { +// + +void ConfigWindow::buttonPressed (QAbstractButton* btn) { + if (btn == ui->buttonBox->button (QDialogButtonBox::Ok)) + { saveSettings(); accept(); - } elif (btn == ui->buttonBox->button (QDialogButtonBox::Cancel)) + } + else if (btn == ui->buttonBox->button (QDialogButtonBox::Cancel)) reject(); - elif (btn == ui->buttonBox->button (QDialogButtonBox::Apply)) + else if (btn == ui->buttonBox->button (QDialogButtonBox::Apply)) saveSettings(); -} - -// ============================================================================= -// ----------------------------------------------------------------------------- -void ConfigBox::editBinaries() { - VersionEditor* dlg = new VersionEditor (this); - - if (dlg->exec()) { - dlg->saveChanges(); - initVersions(); - } -} +} \ No newline at end of file