Skip to content

Commit

Permalink
Make DebugRegistersWidget inherit from QTableView rather than having …
Browse files Browse the repository at this point in the history
…a QTableView as its member. Fix Vector35#426
  • Loading branch information
xusheng6 committed Mar 8, 2023
1 parent 9ac515b commit 6481f13
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 41 deletions.
71 changes: 32 additions & 39 deletions ui/registerswidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,48 +391,41 @@ void DebugRegistersItemDelegate::setEditorData(QWidget* editor, const QModelInde
}


static void updateColumnWidths(QTableView* table)
void DebugRegistersWidget::updateColumnWidths()
{
table->resizeColumnToContents(DebugRegistersListModel::NameColumn);
table->resizeColumnToContents(DebugRegistersListModel::ValueColumn);
table->resizeColumnToContents(DebugRegistersListModel::HintColumn);
resizeColumnToContents(DebugRegistersListModel::NameColumn);
resizeColumnToContents(DebugRegistersListModel::ValueColumn);
resizeColumnToContents(DebugRegistersListModel::HintColumn);
}


DebugRegistersWidget::DebugRegistersWidget(ViewFrame* view, BinaryViewRef data, Menu* menu) : m_view(view)
{
m_controller = DebuggerController::GetController(data);

m_table = new QTableView(this);
m_model = new DebugRegistersListModel(m_table, m_controller, view);
m_model = new DebugRegistersListModel(this, m_controller, view);
m_filter = new DebugRegisterFilterProxyModel(this);
m_filter->setSourceModel(m_model);
m_table->setModel(m_filter);
m_table->setEditTriggers(QAbstractItemView::EditKeyPressed);
m_table->setShowGrid(false);
setModel(m_filter);
setEditTriggers(QAbstractItemView::EditKeyPressed);
setShowGrid(false);

m_delegate = new DebugRegistersItemDelegate(this);
m_table->setItemDelegate(m_delegate);
setItemDelegate(m_delegate);

m_table->setSelectionBehavior(QAbstractItemView::SelectItems);
m_table->setSelectionMode(QAbstractItemView::SingleSelection);
setSelectionBehavior(QAbstractItemView::SelectItems);
setSelectionMode(QAbstractItemView::SingleSelection);

m_table->verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
m_table->verticalHeader()->setVisible(false);
verticalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
verticalHeader()->setVisible(false);

m_table->horizontalHeader()->setStretchLastSection(true);
m_table->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
m_table->setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
m_table->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
horizontalHeader()->setStretchLastSection(true);
setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
setHorizontalScrollMode(QAbstractItemView::ScrollPerPixel);
setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);

m_table->resizeColumnsToContents();
m_table->resizeRowsToContents();

QVBoxLayout* layout = new QVBoxLayout;
layout->setContentsMargins(0, 0, 0, 0);
layout->setSpacing(0);
layout->addWidget(m_table);
setLayout(layout);
resizeColumnsToContents();
resizeRowsToContents();

m_actionHandler.setupActionHandler(this);
m_contextMenuManager = new ContextMenuManager(this);
Expand All @@ -458,7 +451,7 @@ DebugRegistersWidget::DebugRegistersWidget(ViewFrame* view, BinaryViewRef data,
m_menu->addAction("Copy", "Options", MENU_ORDER_NORMAL);
m_actionHandler.bindAction("Copy", UIAction([&]() { copy(); }, [&]() { return selectionNotEmpty(); }));
m_actionHandler.setActionDisplayName("Copy", [&]() {
QModelIndexList sel = m_table->selectionModel()->selectedIndexes();
QModelIndexList sel = selectionModel()->selectedIndexes();
if (sel.empty())
return "Copy";

Expand All @@ -485,11 +478,11 @@ DebugRegistersWidget::DebugRegistersWidget(ViewFrame* view, BinaryViewRef data,
m_menu->setGroupOrdering("Display", MENU_ORDER_LAST);
m_actionHandler.bindAction(actionName, UIAction([=]() {
m_filter->toggleHideUnusedRegisters();
updateColumnWidths(m_table);
updateColumnWidths();
}));
m_actionHandler.setChecked(actionName, [this]() { return m_filter->getHideUnusedRegisters(); });

connect(m_table, &QTableView::doubleClicked, this, &DebugRegistersWidget::onDoubleClicked);
connect(this, &QTableView::doubleClicked, this, &DebugRegistersWidget::onDoubleClicked);

updateContent();
}
Expand All @@ -498,7 +491,7 @@ DebugRegistersWidget::DebugRegistersWidget(ViewFrame* view, BinaryViewRef data,
void DebugRegistersWidget::notifyRegistersChanged(std::vector<DebugRegister> regs)
{
m_model->updateRows(regs);
updateColumnWidths(m_table);
updateColumnWidths();
}


Expand Down Expand Up @@ -532,7 +525,7 @@ void DebugRegistersWidget::updateFonts()

void DebugRegistersWidget::setToZero()
{
QModelIndexList sel = m_table->selectionModel()->selectedIndexes();
QModelIndexList sel = selectionModel()->selectedIndexes();
if (sel.empty())
return;

Expand All @@ -547,7 +540,7 @@ void DebugRegistersWidget::setToZero()

void DebugRegistersWidget::jump()
{
QModelIndexList sel = m_table->selectionModel()->selectedIndexes();
QModelIndexList sel = selectionModel()->selectedIndexes();
if (sel.empty())
return;

Expand Down Expand Up @@ -575,7 +568,7 @@ void DebugRegistersWidget::jump()

void DebugRegistersWidget::copy()
{
QModelIndexList sel = m_table->selectionModel()->selectedIndexes();
QModelIndexList sel = selectionModel()->selectedIndexes();
if (sel.empty())
return;

Expand Down Expand Up @@ -611,7 +604,7 @@ void DebugRegistersWidget::copy()

void DebugRegistersWidget::paste()
{
QModelIndexList sel = m_table->selectionModel()->selectedIndexes();
QModelIndexList sel = selectionModel()->selectedIndexes();
if (sel.empty())
return;

Expand Down Expand Up @@ -643,14 +636,14 @@ void DebugRegistersWidget::paste()

bool DebugRegistersWidget::selectionNotEmpty()
{
QModelIndexList sel = m_table->selectionModel()->selectedIndexes();
QModelIndexList sel = selectionModel()->selectedIndexes();
return (!sel.empty()) && sel[0].isValid();
}


bool DebugRegistersWidget::canPaste()
{
QModelIndexList sel = m_table->selectionModel()->selectedIndexes();
QModelIndexList sel = selectionModel()->selectedIndexes();
if (sel.empty())
return false;

Expand All @@ -666,7 +659,7 @@ void DebugRegistersWidget::onDoubleClicked()

void DebugRegistersWidget::editValue()
{
QModelIndexList sel = m_table->selectionModel()->selectedIndexes();
QModelIndexList sel = selectionModel()->selectedIndexes();
if (sel.empty())
return;

Expand All @@ -677,14 +670,14 @@ void DebugRegistersWidget::editValue()
if (!cell.isValid())
return;

m_table->edit(cell);
edit(cell);
}


void DebugRegistersWidget::setFilter(const string& filter)
{
m_filter->setFilterRegularExpression(QString::fromStdString(filter));
updateColumnWidths(m_table);
updateColumnWidths();
}


Expand Down
5 changes: 3 additions & 2 deletions ui/registerswidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,13 @@ class DebugRegisterFilterProxyModel : public QSortFilterProxyModel
};


class DebugRegistersWidget : public QWidget, public FilterTarget
class DebugRegistersWidget : public QTableView, public FilterTarget
{
Q_OBJECT;

ViewFrame* m_view;
DbgRef<DebuggerController> m_controller;

QTableView* m_table;
DebugRegistersListModel* m_model;
DebugRegistersItemDelegate* m_delegate;
DebugRegisterFilterProxyModel* m_filter;
Expand All @@ -182,6 +181,8 @@ class DebugRegistersWidget : public QWidget, public FilterTarget
virtual void selectFirstItem() override;
virtual void activateFirstItem() override;

void updateColumnWidths();

public:
DebugRegistersWidget(ViewFrame* view, BinaryViewRef data, Menu* menu);
void notifyRegistersChanged(std::vector<DebugRegister> regs);
Expand Down

0 comments on commit 6481f13

Please sign in to comment.