src/settingseditor/librarieseditor.cpp

changeset 8
44679e468ba9
parent 7
68443f5be176
child 41
0abada2a9802
--- a/src/settingseditor/librarieseditor.cpp	Sat Oct 05 23:47:03 2019 +0300
+++ b/src/settingseditor/librarieseditor.cpp	Sun Nov 03 12:17:41 2019 +0200
@@ -1,4 +1,5 @@
 #include <QFileDialog>
+#include <QMenu>
 #include <QMessageBox>
 #include "librarieseditor.h"
 #include "ui_librarieseditor.h"
@@ -20,6 +21,12 @@
 		this,
 		&LibrariesEditor::addNewLibrary);
 	this->ui.librariesTable->setModel(&this->libraries);
+	this->ui.librariesTable->setContextMenuPolicy(Qt::CustomContextMenu);
+	connect(
+		this->ui.librariesTable,
+		&QWidget::customContextMenuRequested,
+		this,
+		&LibrariesEditor::showContextMenu);
 }
 
 LibrariesEditor::~LibrariesEditor()
@@ -43,9 +50,9 @@
 	{
 		QMessageBox::critical(this,
 			tr("Library does not exist"),
-			format(
+			utility::format(
 				tr("The directory %1 does not exist."),
-				quoted(dir.path())));
+				utility::quoted(dir.path())));
 	}
 	else
 	{
@@ -53,15 +60,91 @@
 		{
 			QMessageBox::warning(this,
 				tr("Unreadable library"),
-				format(
+				utility::format(
 					tr("The directory %1 cannot be read."),
-					quoted(dir.path())));
+					utility::quoted(dir.path())));
 		}
 		this->libraries.addLibrary({Library::OfficialLibrary, dir});
 		this->ui.newLibraryPath->clear();
 	}
 }
 
+void LibrariesEditor::showContextMenu(const QPoint position)
+{
+	const int libraryIndex = this->currentLibraryIndex();
+	if (this->libraries.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()
+{
+	const int libraryIndex = currentLibraryIndex();
+	QObject* senderObject = sender();
+	QAction* senderAction = qobject_cast<QAction*>(senderObject);
+	const Library::Role role = senderAction->data().value<Library::Role>();
+	this->libraries.setLibraryRole(libraryIndex, role);
+}
+
+void LibrariesEditor::removeCurrentLibrary()
+{
+	this->libraries.removeLibrary(currentLibraryIndex());
+}
+
+void LibrariesEditor::moveCurrentLibraryUp()
+{
+	const int libraryIndex = this->currentLibraryIndex();
+	this->libraries.moveLibrary(libraryIndex, libraryIndex - 1);
+}
+
+void LibrariesEditor::moveCurrentLibraryDown()
+{
+	const int libraryIndex = this->currentLibraryIndex();
+	this->libraries.moveLibrary(libraryIndex + 1, libraryIndex);
+}
+
+int LibrariesEditor::currentLibraryIndex() const
+{
+	return this->ui.librariesTable->selectionModel()->currentIndex().row();
+}
+
 void LibrariesEditor::saveSettings(QSettings* settings)
 {
 	this->libraries.storeToSettings(settings);

mercurial