Sat, 05 Mar 2022 12:47:10 +0200
fix crash when loading a file
`object` is moved so we cannot access it after moving
#include <QFileDialog> #include <QMenu> #include <QMessageBox> #include "librarieseditor.h" #include "ui_librarieseditor.h" LibrariesEditor::LibrariesEditor(Configuration* 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); this->ui.librariesTable->setContextMenuPolicy(Qt::CustomContextMenu); connect( this->ui.librariesTable, &QWidget::customContextMenuRequested, this, &LibrariesEditor::showContextMenu); } 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"), utility::format( tr("The directory %1 does not exist."), utility::quoted(dir.path()))); } else { if (not dir.isReadable()) { QMessageBox::warning(this, tr("Unreadable library"), utility::format( tr("The directory %1 cannot be read."), 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(Configuration* settings) { this->libraries.storeToSettings(settings); }