src/settingseditor/librarieseditor.cpp

Sat, 05 Oct 2019 23:47:03 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 05 Oct 2019 23:47:03 +0300
changeset 7
68443f5be176
child 8
44679e468ba9
permissions
-rw-r--r--

added the settings editor

#include <QFileDialog>
#include <QMessageBox>
#include "librarieseditor.h"
#include "ui_librarieseditor.h"

LibrariesEditor::LibrariesEditor(QSettings* settings, QWidget* parent) :
	QWidget{parent},
	libraries{settings, this},
	ui{*new Ui_LibrariesEditor}
{
	this->ui.setupUi(this);
	connect(
		this->ui.newLibrarySearch,
		&QPushButton::clicked,
		this,
		&LibrariesEditor::searchPathForNewLibrary);
	connect(
		this->ui.newLibraryAdd,
		&QPushButton::clicked,
		this,
		&LibrariesEditor::addNewLibrary);
	this->ui.librariesTable->setModel(&this->libraries);
}

LibrariesEditor::~LibrariesEditor()
{
	delete &this->ui;
}

void LibrariesEditor::searchPathForNewLibrary()
{
	const QString path = QFileDialog::getExistingDirectory(this, tr("Browse LDraw library"));
	if (not path.isEmpty())
	{
		this->ui.newLibraryPath->setText(path);
	}
}

void LibrariesEditor::addNewLibrary()
{
	const QDir dir{this->ui.newLibraryPath->text()};
	if (not dir.exists())
	{
		QMessageBox::critical(this,
			tr("Library does not exist"),
			format(
				tr("The directory %1 does not exist."),
				quoted(dir.path())));
	}
	else
	{
		if (not dir.isReadable())
		{
			QMessageBox::warning(this,
				tr("Unreadable library"),
				format(
					tr("The directory %1 cannot be read."),
					quoted(dir.path())));
		}
		this->libraries.addLibrary({Library::OfficialLibrary, dir});
		this->ui.newLibraryPath->clear();
	}
}

void LibrariesEditor::saveSettings(QSettings* settings)
{
	this->libraries.storeToSettings(settings);
}

mercurial