Skip to content

Commit 6c4d739

Browse files
deanleepull[bot]
authored andcommitted
Cabana: increase cell height of BinaryView and cleanup code (commaai#26754)
* setDefaultSectionSize * cleanup
1 parent 1ba4758 commit 6c4d739

File tree

2 files changed

+12
-30
lines changed

2 files changed

+12
-30
lines changed

tools/cabana/binaryview.cc

+11-23
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
// BinaryView
1212

13-
const int CELL_HEIGHT = 26;
13+
const int CELL_HEIGHT = 36;
1414

1515
inline int get_bit_index(const QModelIndex &index, bool little_endian) {
1616
return index.row() * 8 + (little_endian ? 7 - index.column() : index.column());
@@ -24,14 +24,13 @@ BinaryView::BinaryView(QWidget *parent) : QTableView(parent) {
2424
horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
2525
verticalHeader()->setSectionsClickable(false);
2626
verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
27+
verticalHeader()->setDefaultSectionSize(CELL_HEIGHT);
2728
horizontalHeader()->hide();
28-
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
29-
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
30-
setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
3129
setFrameShape(QFrame::NoFrame);
3230
setShowGrid(false);
33-
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Minimum);
3431
setMouseTracking(true);
32+
setSizeAdjustPolicy(QAbstractScrollArea::AdjustToContents);
33+
setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
3534
}
3635

3736
void BinaryView::highlight(const Signal *sig) {
@@ -57,7 +56,7 @@ void BinaryView::setSelection(const QRect &rect, QItemSelectionModel::SelectionF
5756
}
5857

5958
void BinaryView::mousePressEvent(QMouseEvent *event) {
60-
delegate->setSelectionColor(style()->standardPalette().color(QPalette::Active, QPalette::Highlight));
59+
delegate->selection_color = (palette().color(QPalette::Active, QPalette::Highlight));
6160
if (auto index = indexAt(event->pos()); index.isValid() && index.column() != 8) {
6261
anchor_index = index;
6362
auto item = (const BinaryViewModel::Item *)anchor_index.internalPointer();
@@ -66,7 +65,7 @@ void BinaryView::mousePressEvent(QMouseEvent *event) {
6665
if (bit_idx == s->lsb || bit_idx == s->msb) {
6766
anchor_index = model->bitIndex(bit_idx == s->lsb ? s->msb : s->lsb, true);
6867
resize_sig = s;
69-
delegate->setSelectionColor(item->bg_color);
68+
delegate->selection_color = item->bg_color;
7069
break;
7170
}
7271
}
@@ -79,8 +78,7 @@ void BinaryView::highlightPosition(const QPoint &pos) {
7978
auto item = (BinaryViewModel::Item *)index.internalPointer();
8079
const Signal *sig = item->sigs.isEmpty() ? nullptr : item->sigs.back();
8180
highlight(sig);
82-
sig ? QToolTip::showText(pos, sig->name.c_str(), this, rect())
83-
: QToolTip::hideText();
81+
QToolTip::showText(pos, sig ? sig->name.c_str() : "", this, rect());
8482
}
8583
}
8684

@@ -214,7 +212,7 @@ QVariant BinaryViewModel::headerData(int section, Qt::Orientation orientation, i
214212
if (orientation == Qt::Vertical) {
215213
switch (role) {
216214
case Qt::DisplayRole: return section;
217-
case Qt::SizeHintRole: return QSize(30, CELL_HEIGHT);
215+
case Qt::SizeHintRole: return QSize(30, 0);
218216
case Qt::TextAlignmentRole: return Qt::AlignCenter;
219217
}
220218
}
@@ -224,16 +222,9 @@ QVariant BinaryViewModel::headerData(int section, Qt::Orientation orientation, i
224222
// BinaryItemDelegate
225223

226224
BinaryItemDelegate::BinaryItemDelegate(QObject *parent) : QStyledItemDelegate(parent) {
227-
// cache fonts and color
228-
small_font.setPointSize(6);
225+
small_font.setPixelSize(8);
229226
hex_font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
230227
hex_font.setBold(true);
231-
selection_color = QApplication::style()->standardPalette().color(QPalette::Active, QPalette::Highlight);
232-
}
233-
234-
QSize BinaryItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const {
235-
QSize sz = QStyledItemDelegate::sizeHint(option, index);
236-
return {sz.width(), CELL_HEIGHT};
237228
}
238229

239230
void BinaryItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
@@ -245,19 +236,16 @@ void BinaryItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &op
245236
painter->setFont(hex_font);
246237
} else if (option.state & QStyle::State_Selected) {
247238
painter->fillRect(option.rect, selection_color);
248-
painter->setPen(QApplication::style()->standardPalette().color(QPalette::BrightText));
239+
painter->setPen(option.palette.color(QPalette::BrightText));
249240
} else if (!item->sigs.isEmpty() && (!bin_view->selectionModel()->hasSelection() || !item->sigs.contains(bin_view->resize_sig))) {
250241
painter->fillRect(option.rect, item->bg_color);
251-
painter->setPen(item->sigs.contains(bin_view->hovered_sig)
252-
? QApplication::style()->standardPalette().color(QPalette::BrightText)
253-
: Qt::black);
242+
painter->setPen(item->sigs.contains(bin_view->hovered_sig) ? option.palette.color(QPalette::BrightText) : Qt::black);
254243
}
255244

256245
painter->drawText(option.rect, Qt::AlignCenter, item->val);
257246
if (item->is_msb || item->is_lsb) {
258247
painter->setFont(small_font);
259248
painter->drawText(option.rect, Qt::AlignHCenter | Qt::AlignBottom, item->is_msb ? "MSB" : "LSB");
260249
}
261-
262250
painter->restore();
263251
}

tools/cabana/binaryview.h

+1-7
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,16 @@
99
#include "tools/cabana/dbcmanager.h"
1010

1111
class BinaryItemDelegate : public QStyledItemDelegate {
12-
Q_OBJECT
13-
1412
public:
1513
BinaryItemDelegate(QObject *parent);
1614
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override;
17-
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override;
1815
void setSelectionColor(const QColor &color) { selection_color = color; }
1916

20-
private:
2117
QFont small_font, hex_font;
2218
QColor selection_color;
2319
};
2420

2521
class BinaryViewModel : public QAbstractTableModel {
26-
Q_OBJECT
27-
2822
public:
2923
BinaryViewModel(QObject *parent) : QAbstractTableModel(parent) {}
3024
void setMessage(const QString &message_id);
@@ -52,7 +46,7 @@ class BinaryViewModel : public QAbstractTableModel {
5246

5347
private:
5448
QString msg_id;
55-
const DBCMsg *dbc_msg;
49+
const DBCMsg *dbc_msg = nullptr;
5650
int row_count = 0;
5751
const int column_count = 9;
5852
};

0 commit comments

Comments
 (0)