mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-07-04 04:52:09 +08:00
cabana: fix unable to scroll to the right edge of the message list. (#27947)
* fix scroll issue * resize bytes section after model reset old-commit-hash: 6ad4017fd8770a7a4deec7bceff6e96b0b6f51a8
This commit is contained in:
@@ -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<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;
|
||||
|
||||
Reference in New Issue
Block a user