config/configwindow.cpp

changeset 46
07578e081ae8
parent 44
d0bf58f3560f
child 50
5dc8b48f80b3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/config/configwindow.cpp	Sat Jun 06 23:12:59 2015 +0300
@@ -0,0 +1,303 @@
+/*
+ *  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 <commonlib/config.h>
+#include <commonlib/version.h>
+#include "addversionprompt.h"
+#include "configwindow.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;
+}
+
+//
+// -------------------------------------------------------------------------------------------------
+//
+
+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());
+
+	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.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)
+{
+	QTableWidgetItem* labelItem = new QTableWidgetItem (version.name);
+	QTableWidgetItem* pathItem = new QTableWidgetItem (version.binaryPath);
+	int rownum = ui.exePaths->rowCount();
+	ui.exePaths->setSortingEnabled (false);
+	ui.exePaths->insertRow (rownum);
+	ui.exePaths->setItem (rownum, LabelColumn, labelItem);
+	ui.exePaths->setItem (rownum, PathColumn, pathItem);
+	ui.exePaths->setSortingEnabled (true);
+
+	VersionGuiEntry* entry = new VersionGuiEntry;
+	entry->isRelease = version.isRelease;
+	entry->name = version.name;
+	entry->labelItem = labelItem;
+	entry->pathItem = 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 (name, isRelease, pathItem->text());
+}
+
+//
+// -------------------------------------------------------------------------------------------------
+//
+
+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 ("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::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();
+	}
+	else if (btn == ui.buttonBox->button (QDialogButtonBox::Apply))
+	{
+		saveSettings();
+	}
+}
\ No newline at end of file

mercurial