config/configwindow.cpp

Mon, 06 Dec 2021 00:29:47 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Mon, 06 Dec 2021 00:29:47 +0200
changeset 67
c34057d3c94e
parent 66
c68545f1aecb
permissions
-rw-r--r--

Fix build on Windows

/*
 *  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 <QFileDialog>
#include <QMessageBox>
#include <QLabel>
#include <commonlib/config.h>
#include <commonlib/version.h>
#include <commonlib/misc.h>
#include "addversionprompt.h"
#include "configwindow.h"
#include "versionguientry.h"
#include "ui_configbox.h"

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

bool confirm (const QString& text)
{
	return QMessageBox::question (NULL, QObject::tr ("Confirm"), text,
		QMessageBox::Yes | QMessageBox::No, QMessageBox::No) != QMessageBox::No;
}

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

void VersionGuiEntry::setName (const QString& a)
{
	m_name = a;
	m_label->setText (a);
}

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

ConfigWindow::ConfigWindow (QWidget* parent, Qt::WindowFlags f) :
	QDialog (parent, f),
	ui (*new Ui_ConfigBox)
{
	ui.setupUi (this);

	QStringList wadpaths = Config::get ("wadpaths").toStringList();
	QList<QVariant> versions = Config::get ("versions").toList();

	for (int i = 0; i < wadpaths.size(); ++i)
		addWadPath (wadpaths[i]);

	ui.noDemoPrompt->setChecked (Config::get ("noprompt").toBool());
	ui.autoAssimilate->setChecked (Config::get ("autoassimilate").toBool());

	for (int i = 0; i < versions.size(); ++i)
	{
		if (not versions[i].canConvert<ZandronumVersion>())
			continue;

		addVersion (versions[i].value<ZandronumVersion>());
	}

	connect (ui.wad_add, SIGNAL (clicked()), this, SLOT (addWadPath()));
	connect (ui.wad_pathEntry, SIGNAL (returnPressed()), this, SLOT (addWadPath()));
	connect (ui.wad_findPath, SIGNAL (clicked()), this, SLOT (findWadPath()));
	connect (ui.wad_del, SIGNAL (clicked()), this, SLOT (removeCurrentWadPath()));
	connect (ui.wad_autofind, SIGNAL (clicked()), this, SLOT (autoFindWadPaths()));
	connect (ui.buttonBox, SIGNAL (clicked (QAbstractButton*)), this,
		SLOT (buttonPressed (QAbstractButton*)));
	connect (ui.addExePath, SIGNAL (clicked()), this, SLOT (newVersion()));
	connect (ui.editExePath, SIGNAL (clicked()), this, SLOT (editExePressed()));
	connect (ui.removeExePath, SIGNAL (clicked()), this, SLOT (removeCurrentVersion()));
	connect (ui.clearExePaths, SIGNAL (clicked()), this, SLOT (clearExePathsClicked()));
	setWindowTitle (versionSignature());
}

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

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

	for (int i = 0; i < m_versionEntries.size(); ++i)
		delete m_versionEntries[i];
}

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

VersionGuiEntry* ConfigWindow::addVersion (const ZandronumVersion& version)
{
	QLabel* label = new QLabel (version.name);

	QTableWidgetItem* pathItem = new QTableWidgetItem (version.binaryPath);
	int rownum = ui.exePaths->rowCount();
	ui.exePaths->setSortingEnabled (false);
	ui.exePaths->insertRow (rownum);
	ui.exePaths->setCellWidget (rownum, LabelColumn, label);
	ui.exePaths->setItem (rownum, PathColumn, pathItem);
	ui.exePaths->setSortingEnabled (true);

	VersionGuiEntry* entry = new VersionGuiEntry (version.name, version.isRelease, label, pathItem);
	m_versionEntries.append (entry);
	m_versionEntryMap[pathItem] = entry;
	return entry;
}

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

void ConfigWindow::removeVersion (VersionGuiEntry* entry)
{
	for (int i = 0; i < m_versionEntries.size(); ++i)
	{
		if (m_versionEntries[i] == entry)
		{
			m_versionEntries.removeAt (i);
			break;
		}
	}

	m_versionEntryMap.remove (entry->pathItem());
	ui.exePaths->removeRow (entry->pathItem()->row());
	delete entry;
}

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

void ConfigWindow::removeCurrentVersion()
{
	VersionGuiEntry* entry = currentVersionEntry();

	if (entry)
		removeVersion (entry);
}

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

void ConfigWindow::newVersion()
{
	VersionGuiEntry* entry = addVersion (ZandronumVersion());
	AddVersionPrompt* prompt = new AddVersionPrompt (entry, this);

	if (not prompt->exec())
		removeVersion (entry);
}

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

void ConfigWindow::editExePressed()
{
	VersionGuiEntry* entry = currentVersionEntry();
	(new AddVersionPrompt (entry, this))->exec();
}

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

void ConfigWindow::clearExePathsClicked()
{
	if (confirm ("Are you sure you want to clear all Zandronum versions?"))
	{
		ui.exePaths->clearContents();
		ui.exePaths->setRowCount (0);

		for (int i = 0; i < m_versionEntries.size(); ++i)
			delete m_versionEntries[i];

		m_versionEntries.clear();
		m_versionEntryMap.clear();
	}
}

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

ZandronumVersion VersionGuiEntry::toNonGuiVersion() const
{
	return ZandronumVersion{m_name, m_pathItem->text(), m_isRelease};
}

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

VersionGuiEntry* ConfigWindow::currentVersionEntry()
{
	int row = ui.exePaths->currentRow();

	if (row != -1)
	{
		VersionEntryMap::iterator it = m_versionEntryMap.find (ui.exePaths->item (row, PathColumn));

		if (it != m_versionEntryMap.end())
			return it.value();
	}

	return NULL;
}

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

void ConfigWindow::saveSettings()
{
	QList<QVariant> wadPathList;
	QList<QVariant> versions;

	for (int i = 0; i < ui.wad_pathsList->count(); ++i)
		wadPathList.append (ui.wad_pathsList->item (i)->text());

	for (int i = 0; i < m_versionEntries.size(); ++i)
	{
		QVariant var;
		var.setValue (m_versionEntries[i]->toNonGuiVersion());
		versions.append (var);
	}

	Config::set ("wadpaths", wadPathList);
	Config::set ("noprompt", ui.noDemoPrompt->isChecked());
	Config::set ("autoassimilate", ui.autoAssimilate->isChecked());
	Config::set ("versions", versions);
	Config::sync();
}

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

void ConfigWindow::addWadPath()
{
	addWadPath (ui.wad_pathEntry->text());
	ui.wad_pathEntry->clear();
}

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

void ConfigWindow::addWadPath (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::autoFindWadPaths()
{
	QStringList paths;

	for (int i = 0; i < ui.wad_pathsList->count(); ++i)
		paths.append (ui.wad_pathsList->item (i)->text());

	assimilateWadPaths (paths);
	ui.wad_pathsList->clear();

	for (int i = 0; i < paths.size(); ++i)
		addWadPath (paths[i]);
}

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

void ConfigWindow::findWadPath()
{
	QString path = QFileDialog::getExistingDirectory (this);

	if (path.isEmpty())
		return;

	ui.wad_pathEntry->setText (path);
}

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

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

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

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

mercurial