src/settingseditor/librarieseditor.cpp

Fri, 01 Jul 2022 16:46:43 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Fri, 01 Jul 2022 16:46:43 +0300
changeset 312
2637134bc37c
parent 300
3a4b132b8353
child 381
80bea7a6e84f
permissions
-rw-r--r--

Fix right click to delete not really working properly
Instead of removing the point that had been added, it would remove
the point that is being drawn, which would cause it to overwrite the
previous point using the new point, causing a bit of a delay

#include <QFileDialog>
#include <QMenu>
#include <QMessageBox>
#include <ui_librarieseditor.h>
#include "src/settingseditor/librarieseditor.h"

LibrariesEditor::LibrariesEditor(QWidget* parent) :
	QWidget{parent},
	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->setContextMenuPolicy(Qt::CustomContextMenu);
	connect(
		this->ui.librariesTable,
		&QWidget::customContextMenuRequested,
		this,
		&LibrariesEditor::showContextMenu);
	this->setEnabled(false);
}

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()
{
	if (LibrariesModel* model = this->currentModel()) {
		const QDir dir{this->ui.newLibraryPath->text()};
		if (not dir.exists())
		{
			QMessageBox::critical(
			this,
			tr("Library does not exist"),
			tr("The directory %1 does not exist.").arg(quoted(dir.path()))
			);
		}
		else
		{
			if (not dir.isReadable())
			{
				QMessageBox::warning(this,
				tr("Unreadable library"),
				tr("The directory %1 cannot be read.").arg(quoted(dir.path()))
				);
			}
			model->addLibrary({Library::OfficialLibrary, dir});
			this->ui.newLibraryPath->clear();
		}
	}
}

void LibrariesEditor::showContextMenu(const QPoint position)
{
	const index_t libraryIndex = this->currentLibraryIndex();
	LibrariesModel* model = this->currentModel();
	if (model != nullptr and model->isValidIndex(libraryIndex))
	{
		QMenu* contextMenu = new QMenu{this};
		QAction* removeAction = new QAction{tr("Remove library")};
		connect(
			removeAction,
			&QAction::triggered,
			this,
			&LibrariesEditor::removeCurrentLibrary);
		QMenu* roleMenu = new QMenu{tr("Set role"), contextMenu};
		for (const Library::Role role : Library::allRoles)
		{
			QAction* setRoleAction = new QAction{Library::libraryRoleName(role)};
			setRoleAction->setData(role);
			roleMenu->addAction(setRoleAction);
			connect(
				setRoleAction,
				&QAction::triggered,
				this,
				&LibrariesEditor::setCurrentLibraryRole);
		}
		QAction* moveUpAction = new QAction{tr("Move up")};
		connect(
			moveUpAction,
			&QAction::triggered,
			this,
			&LibrariesEditor::moveCurrentLibraryUp);
		QAction* moveDownAction = new QAction{tr("Move down")};
		connect(
			moveDownAction,
			&QAction::triggered,
			this,
			&LibrariesEditor::moveCurrentLibraryDown);
		contextMenu->addMenu(roleMenu);
		contextMenu->addSeparator();
		contextMenu->addAction(moveDownAction);
		contextMenu->addAction(moveUpAction);
		contextMenu->addAction(removeAction);
		contextMenu->popup(this->ui.librariesTable->mapToGlobal(position));
	}
}

void LibrariesEditor::setCurrentLibraryRole()
{
	if (LibrariesModel* model = this->currentModel()) {
		const index_t libraryIndex = currentLibraryIndex();
		QObject* senderObject = sender();
		QAction* senderAction = qobject_cast<QAction*>(senderObject);
		const Library::Role role = senderAction->data().value<Library::Role>();
		model->setLibraryRole(libraryIndex, role);
	}
}

void LibrariesEditor::removeCurrentLibrary()
{
	if (LibrariesModel* model = this->currentModel()) {
		model->removeLibrary(currentLibraryIndex());
	}
}

void LibrariesEditor::moveCurrentLibraryUp()
{
	if (LibrariesModel* model = this->currentModel()) {
		const index_t libraryIndex = this->currentLibraryIndex();
		model->moveLibrary(libraryIndex, libraryIndex - 1);
	}
}

void LibrariesEditor::moveCurrentLibraryDown()
{
	if (LibrariesModel* model = this->currentModel()) {
		const index_t libraryIndex = this->currentLibraryIndex();
		model->moveLibrary(libraryIndex + 1, libraryIndex);
	}
}

LibrariesModel *LibrariesEditor::currentModel()
{
	return qobject_cast<LibrariesModel*>(this->ui.librariesTable->model());
}

index_t LibrariesEditor::currentLibraryIndex() const
{
	return this->ui.librariesTable->selectionModel()->currentIndex().row();
}

void LibrariesEditor::saveSettings()
{
	if (LibrariesModel* model = this->currentModel()) {
		model->storeToSettings();
	}
}

void LibrariesEditor::setModel(LibrariesModel *model)
{
	this->ui.librariesTable->setModel(model);
	this->setEnabled(model != nullptr);
}

mercurial