Minor improvements and cleanup on VFS panel (#17459)

This commit is contained in:
Antonino Di Guardo 2025-09-01 20:44:00 +02:00 committed by GitHub
parent 4800aa9bfc
commit 6d1a85b947
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -12,21 +12,14 @@ vfs_dialog_path_widget::vfs_dialog_path_widget(const QString& name, const QStrin
const QStringList all_dirs = m_gui_settings->GetValue(m_list_location).toStringList(); const QStringList all_dirs = m_gui_settings->GetValue(m_list_location).toStringList();
QListWidgetItem* selected_item = nullptr;
for (const QString& dir : all_dirs) for (const QString& dir : all_dirs)
{ {
QListWidgetItem* item = add_directory(dir); add_directory(dir);
if (dir == current_path)
selected_item = item;
} }
// We must show the currently selected config. // Add current path if missing; That should never happen if the list is managed only by the use of the GUI.
if (!selected_item) // Code made robust even in case the path was manually removed from the list stored in "CurrentSettings.ini" file
selected_item = add_directory(current_path); add_directory(current_path);
selected_item->setSelected(true);
m_dir_list->setMinimumWidth(m_dir_list->sizeHintForColumn(0)); m_dir_list->setMinimumWidth(m_dir_list->sizeHintForColumn(0));
@ -55,8 +48,6 @@ vfs_dialog_path_widget::vfs_dialog_path_widget(const QString& name, const QStrin
setLayout(vbox); setLayout(vbox);
update_selection();
connect(m_dir_list->model(), &QAbstractItemModel::dataChanged, this, [this](const QModelIndex&, const QModelIndex&, const QList<int>& roles) connect(m_dir_list->model(), &QAbstractItemModel::dataChanged, this, [this](const QModelIndex&, const QModelIndex&, const QList<int>& roles)
{ {
if (m_is_changing_data || (!roles.empty() && !roles.contains(Qt::ItemDataRole::CheckStateRole))) if (m_is_changing_data || (!roles.empty() && !roles.contains(Qt::ItemDataRole::CheckStateRole)))
@ -91,8 +82,10 @@ vfs_dialog_path_widget::vfs_dialog_path_widget(const QString& name, const QStrin
connect(m_dir_list, &QListWidget::currentRowChanged, this, [this, button_remove_dir](int row) connect(m_dir_list, &QListWidget::currentRowChanged, this, [this, button_remove_dir](int row)
{ {
button_remove_dir->setEnabled(m_dir_list->item(row) && row > 0); button_remove_dir->setEnabled(row > 0);
}); });
update_selection(); // As last (just to make also use of the defined connections), update the widget
} }
void vfs_dialog_path_widget::reset() void vfs_dialog_path_widget::reset()
@ -129,7 +122,11 @@ void vfs_dialog_path_widget::add_new_directory()
if (!dir.endsWith("/")) if (!dir.endsWith("/"))
dir += '/'; dir += '/';
m_dir_list->setCurrentItem(add_directory(dir)); if (QListWidgetItem* item = add_directory(dir))
m_dir_list->setCurrentItem(item); // Set both the new current item and new current row
// Not really necessary an update here due to the added item is not also set as the new checked row.
// Keeping just in case of further changes in the current logic
update_selection(); update_selection();
} }
@ -140,6 +137,7 @@ void vfs_dialog_path_widget::remove_directory()
QListWidgetItem* item = m_dir_list->takeItem(row); QListWidgetItem* item = m_dir_list->takeItem(row);
delete item; delete item;
m_dir_list->setCurrentRow(std::min(row, m_dir_list->count() - 1)); // Set current row, if needed
update_selection(); update_selection();
} }
} }
@ -163,18 +161,19 @@ void vfs_dialog_path_widget::update_selection()
if (is_selected) if (is_selected)
{ {
m_dir_list->setCurrentItem(item); if (m_dir_list->currentRow() < 0) // If no current row is set (e.g. at startup)
m_dir_list->setCurrentRow(i); // Set both the new current item and new current row
found_path = true; found_path = true;
} }
} }
} }
if (!found_path) if (!found_path) // If no path is selected (no row is checked)
{ {
QListWidgetItem* item = m_dir_list->item(0); QListWidgetItem* item = m_dir_list->item(0);
m_selected_config_label->setText(item->text().isEmpty() ? EmptyPath : item->text()); m_selected_config_label->setText(item->text().isEmpty() ? EmptyPath : item->text());
item->setCheckState(Qt::CheckState::Checked); item->setCheckState(Qt::CheckState::Checked);
m_dir_list->setCurrentItem(item);
} }
m_is_changing_data = false; m_is_changing_data = false;