cabana: fix unable to scroll to the right edge of the message list. (#27947)

* fix scroll issue

* resize bytes section after model reset
pull/27948/head
Dean Lee 2 years ago committed by GitHub
parent 13c75dc138
commit 6ad4017fd8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 21
      tools/cabana/messageswidget.cc
  2. 1
      tools/cabana/messageswidget.h
  3. 10
      tools/cabana/util.cc
  4. 2
      tools/cabana/util.h

@ -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 &current, 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<int> &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);
}
}

@ -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<int> &roles = QVector<int>()) override;
void updateBytesSectionSize();
};
class MessagesWidget : public QWidget {

@ -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;

@ -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;

Loading…
Cancel
Save