Skip to content

Commit

Permalink
qt: Sort symbols, and allow resorting by address/size/symbol name
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonKagstrom committed Jul 8, 2024
1 parent b8647b7 commit d945097
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
51 changes: 45 additions & 6 deletions qt/emilpro/mainwindow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,10 @@ MainWindow::LoadFile(const std::string& filename, std::optional<emilpro::Machine
}
}

m_ui->symbolTableView->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;
Expand Down Expand Up @@ -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();

Expand All @@ -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));
}


Expand Down Expand Up @@ -899,12 +905,17 @@ void
MainWindow::SetupSymbolView()
{
m_symbol_view_model = new QStandardItemModel(0, 4, this);
m_symbol_proxy_model = std::make_unique<QSortFilterProxyModel>(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);
Expand All @@ -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)),
Expand Down
4 changes: 4 additions & 0 deletions qt/emilpro/mainwindow.hh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <QMainWindow>
#include <QSettings>
#include <QSortFilterProxyModel>
#include <qstandarditemmodel.h>

namespace Ui
Expand Down Expand Up @@ -130,6 +131,9 @@ private:
QStandardItemModel* m_referred_by_view_model {nullptr};
QStandardItemModel* m_address_history_view_model {nullptr};

std::unique_ptr<QSortFilterProxyModel> m_section_proxy_model;
std::unique_ptr<QSortFilterProxyModel> m_symbol_proxy_model;

const emilpro::ISymbol* m_current_symbol {nullptr};

JumpLaneDelegate m_forward_item_delegate;
Expand Down

0 comments on commit d945097

Please sign in to comment.