From d94509703616b8df5cfff40afc42422f86dbfb2f Mon Sep 17 00:00:00 2001 From: Simon Kagstrom Date: Mon, 8 Jul 2024 09:37:18 +0200 Subject: [PATCH] qt: Sort symbols, and allow resorting by address/size/symbol name --- qt/emilpro/mainwindow.cc | 51 +++++++++++++++++++++++++++++++++++----- qt/emilpro/mainwindow.hh | 4 ++++ 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/qt/emilpro/mainwindow.cc b/qt/emilpro/mainwindow.cc index 016d0fd..b085bcc 100644 --- a/qt/emilpro/mainwindow.cc +++ b/qt/emilpro/mainwindow.cc @@ -203,8 +203,10 @@ MainWindow::LoadFile(const std::string& filename, std::optionalsymbolTableView->setCurrentIndex(m_symbol_view_model->index(0, 0)); + m_ui->symbolTableView->setCurrentIndex(m_symbol_proxy_model->index(0, 0)); } + + m_symbol_proxy_model->sort(0, Qt::AscendingOrder); m_visible_symbols = m_database.Symbols(); return std::nullopt; @@ -497,6 +499,10 @@ MainWindow::on_locationLineEdit_textChanged(const QString& text) // Hide all symbols which does not match the text / address for (auto i = 0u; i < m_symbol_view_model->rowCount(); i++) { + // Lookup the index in the proxy (which is shown in the view) + auto model_index = m_symbol_view_model->index(i, 0); + auto proxy_index = m_symbol_proxy_model->mapFromSource(model_index); + QString to_compare; const auto& sym = m_visible_symbols[i].get(); @@ -522,18 +528,18 @@ MainWindow::on_locationLineEdit_textChanged(const QString& text) if (to_compare.contains(text, Qt::CaseInsensitive) || m_current_symbol == &sym) { - m_ui->symbolTableView->showRow(i); + m_ui->symbolTableView->showRow(proxy_index.row()); lowest_visible = std::min(lowest_visible, i); } else { - m_ui->symbolTableView->hideRow(i); + m_ui->symbolTableView->hideRow(proxy_index.row()); } } // ... and focus the first visible line - m_ui->symbolTableView->setCurrentIndex(m_symbol_view_model->index(lowest_visible, 0)); + m_ui->symbolTableView->setCurrentIndex(m_symbol_proxy_model->index(lowest_visible, 0)); } @@ -899,12 +905,17 @@ void MainWindow::SetupSymbolView() { m_symbol_view_model = new QStandardItemModel(0, 4, this); + m_symbol_proxy_model = std::make_unique(this); + m_symbol_proxy_model->setSourceModel(m_symbol_view_model); + m_symbol_proxy_model->setSortCaseSensitivity(Qt::CaseInsensitive); + + m_symbol_view_model->setHorizontalHeaderItem(0, new QStandardItem(QString("Address"))); m_symbol_view_model->setHorizontalHeaderItem(1, new QStandardItem(QString("Size"))); m_symbol_view_model->setHorizontalHeaderItem(2, new QStandardItem(QString("Flags"))); m_symbol_view_model->setHorizontalHeaderItem(3, new QStandardItem(QString("Section"))); m_symbol_view_model->setHorizontalHeaderItem(4, new QStandardItem(QString("Symbol name"))); - m_ui->symbolTableView->setModel(m_symbol_view_model); + m_ui->symbolTableView->horizontalHeader()->setStretchLastSection(true); m_ui->symbolTableView->resizeColumnsToContents(); m_ui->symbolTableView->setColumnWidth(0, 120); @@ -914,7 +925,35 @@ MainWindow::SetupSymbolView() // Install an event filter to have the Enter key behave like activate m_ui->symbolTableView->installEventFilter(this); - + m_ui->symbolTableView->setModel(m_symbol_proxy_model.get()); + + connect(m_ui->symbolTableView->horizontalHeader(), + &QHeaderView::sectionClicked, + [this](int column) { + Qt::SortOrder currentOrder = m_symbol_proxy_model->sortOrder(); + int currentSortColumn = m_symbol_proxy_model->sortColumn(); + + if (column == 2) + { + // Don't allow sorting by flags + return; + } + + if (column == currentSortColumn) + { + // Toggle the sort order if the same column is clicked + currentOrder = (currentOrder == Qt::AscendingOrder) ? Qt::DescendingOrder + : Qt::AscendingOrder; + } + else + { + // Default to ascending order if a different column is clicked + currentOrder = Qt::AscendingOrder; + } + + // Apply the sorting + m_symbol_proxy_model->sort(column, currentOrder); + }); connect(m_ui->symbolTableView->selectionModel(), SIGNAL(currentChanged(QModelIndex, QModelIndex)), diff --git a/qt/emilpro/mainwindow.hh b/qt/emilpro/mainwindow.hh index f12a97c..0d54605 100644 --- a/qt/emilpro/mainwindow.hh +++ b/qt/emilpro/mainwindow.hh @@ -10,6 +10,7 @@ #include #include +#include #include namespace Ui @@ -130,6 +131,9 @@ private: QStandardItemModel* m_referred_by_view_model {nullptr}; QStandardItemModel* m_address_history_view_model {nullptr}; + std::unique_ptr m_section_proxy_model; + std::unique_ptr m_symbol_proxy_model; + const emilpro::ISymbol* m_current_symbol {nullptr}; JumpLaneDelegate m_forward_item_delegate;