src/settingseditor/librarieseditor.cpp

changeset 7
68443f5be176
child 8
44679e468ba9
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/settingseditor/librarieseditor.cpp	Sat Oct 05 23:47:03 2019 +0300
@@ -0,0 +1,68 @@
+#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