1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
//
// Created by Alexander Khosrowshahi on 8/22/25.
//
#pragma once
#include <QtWidgets/QTableView>
#include <QtWidgets/QHeaderView>
#include <QtCore/QTimer>
#include <QMenu>
/// Base class for table views in Binary Ninja views
/// - Moveable, resizeable columns with saved state
/// - QSettings save to Tables/<viewName>/<Suffix>
/// - Reset columns context menu action
class BINARYNINJAUIAPI TableViewBase: public QTableView {
Q_OBJECT
public:
explicit TableViewBase(QWidget* parent = nullptr, const QString& viewName = {}): QTableView(parent), m_viewName(viewName) {
auto* hh = horizontalHeader();
hh->setStretchLastSection(true);
hh->setSectionResizeMode(QHeaderView::Interactive);
hh->setSectionsMovable(true);
hh->setSectionsClickable(true);
hh->setSortIndicatorShown(true);
hh->setSortIndicator(0, Qt::AscendingOrder);
hh->setContextMenuPolicy(Qt::CustomContextMenu);
connect(hh, &QHeaderView::customContextMenuRequested, this,
[this, hh](const QPoint& p) {
QMenu menu(hh);
QAction* reset = menu.addAction(tr("Reset Column Layout"));
connect(reset, &QAction::triggered, this, &TableViewBase::resetColumnLayout);
populateHeaderContextMenu(&menu, p);
menu.exec(hh->viewport()->mapToGlobal(p));
});
m_headerSaveDebounce.setSingleShot(true);
m_headerSaveDebounce.setInterval(150);
connect(&m_headerSaveDebounce, &QTimer::timeout, this, &TableViewBase::saveHeaderState);
connect(hh, &QHeaderView::sectionResized, this, &TableViewBase::scheduleSaveHeaderState);
connect(hh, &QHeaderView::sectionMoved, this, &TableViewBase::scheduleSaveHeaderState);
setShowGrid(false);
setSortingEnabled(true);
QMetaObject::invokeMethod(this, "restoreHeaderState", Qt::QueuedConnection);
}
void setModel(QAbstractItemModel* m) override
{
QTableView::setModel(m);
if (!m) return;
connect(m, &QAbstractItemModel::modelReset, this, &TableViewBase::restoreHeaderState);
connect(m, &QAbstractItemModel::columnsInserted, this, &TableViewBase::restoreHeaderState);
connect(m, &QAbstractItemModel::columnsRemoved, this, &TableViewBase::restoreHeaderState);
// Grab default header state
QTimer::singleShot(0, this, [this]{
captureDefaultHeaderState();
});
QMetaObject::invokeMethod(this, "restoreHeaderState", Qt::QueuedConnection);
}
// Save after debounce for repeated move/drag
void scheduleSaveHeaderState() { m_headerSaveDebounce.start(); }
Q_SIGNALS:
// For owners/derived classes to add their own menu items
void populateHeaderContextMenu(QMenu*, const QPoint&);
protected:
QString viewName() const {
if (!m_viewName.isEmpty()) return m_viewName;
if (!objectName().isEmpty()) return objectName();
return metaObject()->className();
}
QString settingsKey(const QString& suffix) const {
return QStringLiteral("tables/%1/%2").arg(viewName(), suffix);
}
void saveHeaderState() const {
auto* hh = horizontalHeader();
if (!hh) return;
QSettings s;
s.setValue(settingsKey("horizontalHeaderState"), hh->saveState());
}
void restoreHeaderState() const
{
auto* hh = horizontalHeader();
if (!hh) return;
QSettings s;
const QByteArray st = s.value(settingsKey("horizontalHeaderState")).toByteArray();
if (!st.isEmpty()) hh->restoreState(st);
const QByteArray def = s.value(settingsKey("horizontalHeaderDefaultState")).toByteArray();
if (!def.isEmpty()) hh->restoreState(def);
}
virtual int defaultSectionWidth(const int logicalIndex, const int charWidth) const
{
QString headerText;
if (model()) {
headerText = model()->headerData(logicalIndex, Qt::Horizontal, Qt::DisplayRole).toString();
}
constexpr int minChars = 8;
const int headerChars = qMax(minChars, headerText.size() + 2);
return headerChars * charWidth;
}
/// Grabs default header states on startup to save
/// Kind of a hacky fix, but many of our tables have manually set widths,
/// so compensating for them is a hassle.
void captureDefaultHeaderState() const
{
auto* hh = horizontalHeader();
if (!hh) return;
QSettings s;
const auto key = settingsKey("horizontalHeaderDefaultState");
if (!s.contains(key)) {
s.setValue(key, hh->saveState());
s.sync();
}
}
void resetColumnLayout() const
{
auto* hh = horizontalHeader();
if (!hh || !model()) return;
{
QSettings s;
s.remove(settingsKey("horizontalHeaderState"));
}
QSettings s;
const QByteArray def = s.value(settingsKey("horizontalHeaderDefaultState")).
toByteArray();
if (!def.isEmpty() && hh->restoreState(def))
{
return;
}
// If no default or set user layout, use size hints
for (int c = 0; c < model()->columnCount(); ++c)
{
constexpr int extra = 12;
int w = sizeHintForColumn(c);
QVariant head = model()->headerData(c, Qt::Horizontal, Qt::SizeHintRole);
int headerW = hh->sectionSizeHint(c);
if (head.canConvert<QSize>())
headerW = std::max(headerW, head.toSize().width());
hh->resizeSection(c, std::max(w, headerW) + extra);
}
}
private:
QString m_viewName;
QTimer m_headerSaveDebounce;
};
|