src/config.cpp

changeset 39
2c368cf5cc19
parent 37
c82a86ea87be
--- a/src/config.cpp	Fri Jun 05 19:13:44 2015 +0300
+++ b/src/config.cpp	Sat Jun 06 22:03:00 2015 +0300
@@ -16,106 +16,160 @@
  *  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 ("...");
-	}
+#include "prompts.h"
 
-	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()
+	ui (*new Ui_ConfigBox)
 {
-	delete ui;
-}
+	ui.setupUi (this);
 
-//
-// -----------------------------------------------------------------------------
-//
+	QStringList wadpaths = Config::get ("wadpaths").toStringList();
+	QList<QVariant> versions = Config::get ("versions").toList();
 
-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();
-	}
+	for (int i = 0; i < wadpaths.size(); ++i)
+		addWadPath (wadpaths[i]);
 
-	QList<QVariant> versions = getVersions();
+	ui.noDemoPrompt->setChecked (Config::get ("noprompt").toBool());
 
 	for (int i = 0; i < versions.size(); ++i)
 	{
 		if (not versions[i].canConvert<ZandronumVersion>())
 			continue;
 
-		ZandronumVersion version = versions[i].value<ZandronumVersion>();
-		addVersion (version);
+		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();
 	}
 }
 
@@ -123,166 +177,116 @@
 // -------------------------------------------------------------------------------------------------
 //
 
-void ConfigWindow::addVersion (const ZandronumVersion& version)
+ZandronumVersion VersionGuiEntry::toNonGuiVersion() const
 {
-	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());
+	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;
-	
-	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;
 
-	QList<QVariant> versions = getVersions();
+	for (int i = 0; i < ui.wad_pathsList->count(); ++i)
+		wadPathList.append (ui.wad_pathsList->item (i)->text());
 
-	for (int i = 0; i < versions.size(); ++i)
+	for (int i = 0; i < m_versionEntries.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);
+		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::addPath()
+void ConfigWindow::addWadPath()
 {
-	addPath (ui->wad_pathEntry->text());
-	ui->wad_pathEntry->clear();
+	addWadPath (ui.wad_pathEntry->text());
+	ui.wad_pathEntry->clear();
 }
 
 //
-// -----------------------------------------------------------------------------
+// -------------------------------------------------------------------------------------------------
 //
 
-void ConfigWindow::addPath (QString path)
+void ConfigWindow::addWadPath (QString path)
 {
-	ui->wad_pathsList->addItem (path);
-	QListWidgetItem* item = ui->wad_pathsList->item (ui->wad_pathsList->count() - 1);
+	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()
+void ConfigWindow::findWadPath()
 {
 	QString path = QFileDialog::getExistingDirectory (this);
 
 	if (path.isEmpty())
 		return;
 
-	ui->wad_pathEntry->setText (path);
-}
-
-//
-// -----------------------------------------------------------------------------
-//
-
-void ConfigWindow::delPath()
-{
-	delete ui->wad_pathsList->currentItem();
+	ui.wad_pathEntry->setText (path);
 }
 
 //
-// -----------------------------------------------------------------------------
+// -------------------------------------------------------------------------------------------------
 //
 
-void ConfigWindow::findZanBinary()
+void ConfigWindow::removeCurrentWadPath()
 {
-	FindPathButton* button = static_cast<FindPathButton*> (sender());
-	QString path = getBinaryPath (this);
-
-	if (path.isEmpty())
-		return;
-
-	button->editWidget()->setText (path);
+	delete ui.wad_pathsList->currentItem();
 }
 
 //
-// -----------------------------------------------------------------------------
+// -------------------------------------------------------------------------------------------------
 //
 
-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))
+void ConfigWindow::buttonPressed (QAbstractButton* btn)
+{
+	if (btn == ui.buttonBox->button (QDialogButtonBox::Ok))
 	{
 		saveSettings();
 		accept();
 	}
-	else if (btn == ui->buttonBox->button (QDialogButtonBox::Cancel))
+	else if (btn == ui.buttonBox->button (QDialogButtonBox::Cancel))
+	{
 		reject();
-	else if (btn == ui->buttonBox->button (QDialogButtonBox::Apply))
+	}
+	else if (btn == ui.buttonBox->button (QDialogButtonBox::Apply))
+	{
 		saveSettings();
+	}
 }
\ No newline at end of file

mercurial