src/config.cpp

Fri, 05 Jun 2015 19:13:44 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Fri, 05 Jun 2015 19:13:44 +0300
changeset 38
db677d321cf4
parent 37
c82a86ea87be
child 39
2c368cf5cc19
permissions
-rw-r--r--

Fix demo launching

/*
 *  ZCinema: Zandronum demo launcher
 *  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
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <QLabel>
#include <QFileDialog>
#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"

class FindPathButton : public QPushButton
{
public:
	explicit FindPathButton (QWidget* parent = NULL) :
		QPushButton (parent)
	{
		setText ("...");
	}

	QLineEdit* editWidget() const
	{
		return m_editWidget;
	}

	void setEditWidget (QLineEdit* edit)
	{
		m_editWidget = edit;
	}

private:
	QLineEdit* m_editWidget;
};

ConfigWindow::ConfigWindow (QWidget* parent, Qt::WindowFlags f) :
	QDialog (parent, f),
	ui (new Ui_ConfigBox),
	m_releaseLayout (NULL),
	m_testLayout (NULL)
{
	ui->setupUi (this);
	
	initVersions();
	initFromSettings();
	
	connect (ui->wad_add, SIGNAL (clicked()), this, SLOT (addPath()));
	connect (ui->wad_pathEntry, SIGNAL (returnPressed()), this, SLOT (addPath()));
	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*)));
	setWindowTitle (versionSignature());
}

//
// -----------------------------------------------------------------------------
//

ConfigWindow::~ConfigWindow()
{
	delete ui;
}

//
// -----------------------------------------------------------------------------
//

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)
			w->deleteLater();
		
		m_binaryLayoutWidgets.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);
	
	m_pathInputFields[version.name] = pathInput;
	
	if (version.isRelease)
		m_releaseLayout->addLayout (pathInputLayout);
	else
		m_testLayout->addLayout (pathInputLayout);
	
	m_binaryLayoutWidgets << pathInput << pathFindButton << label;
}

// =============================================================================
// -----------------------------------------------------------------------------
void ConfigWindow::initFromSettings()
{
	ui->wad_pathsList->clear();
	
	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);
	}
	
	ui->noDemoPrompt->setChecked (Config::get ("noprompt").toBool());
}

//
// -----------------------------------------------------------------------------
//

void ConfigWindow::saveSettings()
{
	QList<QVariant> wadPathList;
	
	for (int i = 0; i < ui->wad_pathsList->count(); ++i)
		wadPathList << ui->wad_pathsList->item (i)->text();
	
	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 ConfigWindow::addPath()
{
	addPath (ui->wad_pathEntry->text());
	ui->wad_pathEntry->clear();
}

//
// -----------------------------------------------------------------------------
//

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 ConfigWindow::findPath()
{
	QString path = QFileDialog::getExistingDirectory (this);

	if (path.isEmpty())
		return;

	ui->wad_pathEntry->setText (path);
}

//
// -----------------------------------------------------------------------------
//

void ConfigWindow::delPath()
{
	delete ui->wad_pathsList->currentItem();
}

//
// -----------------------------------------------------------------------------
//

void ConfigWindow::findZanBinary()
{
	FindPathButton* button = static_cast<FindPathButton*> (sender());
	QString path = getBinaryPath (this);

	if (path.isEmpty())
		return;

	button->editWidget()->setText (path);
}

//
// -----------------------------------------------------------------------------
//

QString ConfigWindow::getBinaryPath (QWidget* parent)
{	
#ifdef _WIN32
# define ZAN_EXE_NAME "zandronum.exe"
#else
# define ZAN_EXE_NAME "zandronum"
#endif

	return QFileDialog::getOpenFileName (parent, "", "",
		"Zandronum Binaries (" ZAN_EXE_NAME ")(" ZAN_EXE_NAME ");;All files (*.*)(*.*)");
}

//
// -----------------------------------------------------------------------------
//

void ConfigWindow::buttonPressed (QAbstractButton* btn) {
	if (btn == ui->buttonBox->button (QDialogButtonBox::Ok))
	{
		saveSettings();
		accept();
	}
	else if (btn == ui->buttonBox->button (QDialogButtonBox::Cancel))
		reject();
	else if (btn == ui->buttonBox->button (QDialogButtonBox::Apply))
		saveSettings();
}

mercurial