src/settingseditor/librarieseditor.cpp

changeset 259
c27612f0eac0
parent 218
63125c36de73
child 264
76a025db4948
--- a/src/settingseditor/librarieseditor.cpp	Wed Jun 22 23:51:06 2022 +0300
+++ b/src/settingseditor/librarieseditor.cpp	Sun Jun 26 19:44:45 2022 +0300
@@ -6,7 +6,6 @@
 
 LibrariesEditor::LibrariesEditor(QWidget* parent) :
 	QWidget{parent},
-	libraries{this},
 	ui{*new Ui_LibrariesEditor}
 {
 	this->ui.setupUi(this);
@@ -20,13 +19,13 @@
 		&QPushButton::clicked,
 		this,
 		&LibrariesEditor::addNewLibrary);
-	this->ui.librariesTable->setModel(&this->libraries);
 	this->ui.librariesTable->setContextMenuPolicy(Qt::CustomContextMenu);
 	connect(
 		this->ui.librariesTable,
 		&QWidget::customContextMenuRequested,
 		this,
 		&LibrariesEditor::showContextMenu);
+	this->setEnabled(false);
 }
 
 LibrariesEditor::~LibrariesEditor()
@@ -45,33 +44,36 @@
 
 void LibrariesEditor::addNewLibrary()
 {
-	const QDir dir{this->ui.newLibraryPath->text()};
-	if (not dir.exists())
-	{
-		QMessageBox::critical(
+	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())
+			);
+		}
+		else
 		{
-			QMessageBox::warning(this,
+			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();
 		}
-		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))
+	const std::size_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")};
@@ -115,36 +117,58 @@
 
 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);
+	if (LibrariesModel* model = this->currentModel()) {
+		const std::size_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()
 {
-	this->libraries.removeLibrary(currentLibraryIndex());
+	if (LibrariesModel* model = this->currentModel()) {
+		model->removeLibrary(currentLibraryIndex());
+	}
 }
 
 void LibrariesEditor::moveCurrentLibraryUp()
 {
-	const int libraryIndex = this->currentLibraryIndex();
-	this->libraries.moveLibrary(libraryIndex, libraryIndex - 1);
+	if (LibrariesModel* model = this->currentModel()) {
+		const std::size_t libraryIndex = this->currentLibraryIndex();
+		model->moveLibrary(libraryIndex, libraryIndex - 1);
+	}
 }
 
 void LibrariesEditor::moveCurrentLibraryDown()
 {
-	const int libraryIndex = this->currentLibraryIndex();
-	this->libraries.moveLibrary(libraryIndex + 1, libraryIndex);
+	if (LibrariesModel* model = this->currentModel()) {
+		const std::size_t libraryIndex = this->currentLibraryIndex();
+		model->moveLibrary(libraryIndex + 1, libraryIndex);
+	}
 }
 
-int LibrariesEditor::currentLibraryIndex() const
+LibrariesModel *LibrariesEditor::currentModel()
 {
-	return this->ui.librariesTable->selectionModel()->currentIndex().row();
+	return qobject_cast<LibrariesModel*>(this->ui.librariesTable->model());
+}
+
+std::size_t LibrariesEditor::currentLibraryIndex() const
+{
+	const int row = this->ui.librariesTable->selectionModel()->currentIndex().row();
+	return static_cast<std::size_t>(row);
 }
 
 void LibrariesEditor::saveSettings()
 {
-	this->libraries.storeToSettings();
+	if (LibrariesModel* model = this->currentModel()) {
+		model->storeToSettings();
+	}
 }
+
+void LibrariesEditor::setModel(LibrariesModel *model)
+{
+	this->ui.librariesTable->setModel(model);
+	this->setEnabled(model != nullptr);
+}

mercurial