From 6ad4017fd8770a7a4deec7bceff6e96b0b6f51a8 Mon Sep 17 00:00:00 2001 From: Dean Lee Date: Tue, 18 Apr 2023 14:00:05 +0800 Subject: [PATCH] cabana: fix unable to scroll to the right edge of the message list. (#27947) * fix scroll issue * resize bytes section after model reset --- tools/cabana/messageswidget.cc | 21 ++++++++++++++++++--- tools/cabana/messageswidget.h | 1 + tools/cabana/util.cc | 10 +++++++--- tools/cabana/util.h | 2 ++ 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/tools/cabana/messageswidget.cc b/tools/cabana/messageswidget.cc index c11d9d7d78..2a21cd908d 100644 --- a/tools/cabana/messageswidget.cc +++ b/tools/cabana/messageswidget.cc @@ -62,6 +62,7 @@ MessagesWidget::MessagesWidget(QWidget *parent) : QWidget(parent) { if (current_msg_id) { selectMessage(*current_msg_id); } + view->updateBytesSectionSize(); }); QObject::connect(view->selectionModel(), &QItemSelectionModel::currentChanged, [=](const QModelIndex ¤t, const QModelIndex &previous) { if (current.isValid() && current.row() < model->msgs.size()) { @@ -296,7 +297,21 @@ void MessageView::drawRow(QPainter *painter, const QStyleOptionViewItem &option, } void MessageView::dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector &roles) { - // Bypass the slow call to QTreeView::dataChanged. - // QTreeView::dataChanged will invalidate the height cache and that's what we don't need in MessageView. - QAbstractItemView::dataChanged(topLeft, bottomRight, roles); + // Bypass the slow call to QTreeView::dataChanged. + // QTreeView::dataChanged will invalidate the height cache and that's what we don't need in MessageView. + QAbstractItemView::dataChanged(topLeft, bottomRight, roles); +} + +void MessageView::updateBytesSectionSize() { + auto delegate = ((MessageBytesDelegate *)itemDelegate()); + int max_bytes = 8; + if (!delegate->multipleLines()) { + for (auto it = can->last_msgs.constBegin(); it != can->last_msgs.constEnd(); ++it) { + max_bytes = std::max(max_bytes, it.value().dat.size()); + } + } + int width = delegate->widthForBytes(max_bytes); + if (header()->sectionSize(5) != width) { + header()->resizeSection(5, width); + } } diff --git a/tools/cabana/messageswidget.h b/tools/cabana/messageswidget.h index a30adc5dcd..2dc0d1d316 100644 --- a/tools/cabana/messageswidget.h +++ b/tools/cabana/messageswidget.h @@ -42,6 +42,7 @@ public: void drawRow(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override; void drawBranches(QPainter *painter, const QRect &rect, const QModelIndex &index) const override {} void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector &roles = QVector()) override; + void updateBytesSectionSize(); }; class MessagesWidget : public QWidget { diff --git a/tools/cabana/util.cc b/tools/cabana/util.cc index f89219f9c2..4fe04a5160 100644 --- a/tools/cabana/util.cc +++ b/tools/cabana/util.cc @@ -53,6 +53,11 @@ void MessageBytesDelegate::setMultipleLines(bool v) { } } +int MessageBytesDelegate::widthForBytes(int n) const { + int h_margin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1; + return n * byte_size.width() + h_margin * 2; +} + QSize MessageBytesDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const { int v_margin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameVMargin) + 1; auto data = index.data(BytesRole); @@ -64,12 +69,11 @@ QSize MessageBytesDelegate::sizeHint(const QStyleOptionViewItem &option, const Q QSize size = size_cache[n - 1]; if (size.isEmpty()) { - int h_margin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1; if (!multiple_lines) { - size.setWidth(h_margin * 2 + n * byte_size.width()); + size.setWidth(widthForBytes(n)); size.setHeight(byte_size.height() + 2 * v_margin); } else { - size.setWidth(h_margin * 2 + 8 * byte_size.width()); + size.setWidth(widthForBytes(8)); size.setHeight(byte_size.height() * std::max(1, n / 8) + 2 * v_margin); } size_cache[n - 1] = size; diff --git a/tools/cabana/util.h b/tools/cabana/util.h index ff7cab9a48..cd18b77448 100644 --- a/tools/cabana/util.h +++ b/tools/cabana/util.h @@ -69,6 +69,8 @@ public: QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override; bool helpEvent(QHelpEvent *event, QAbstractItemView *view, const QStyleOptionViewItem &option, const QModelIndex &index) override; void setMultipleLines(bool v); + int widthForBytes(int n) const; + bool multipleLines() const { return multiple_lines; } private: QFont fixed_font;