Cabana: word wrap header (#26341)

* auto wrap header

* custom headerview
old-commit-hash: 70363e2491
taco
Dean Lee 3 years ago committed by GitHub
parent 5d8574eec6
commit d81bd3f8ec
  1. 17
      tools/cabana/historylog.cc
  2. 7
      tools/cabana/historylog.h

@ -1,9 +1,10 @@
#include "tools/cabana/historylog.h"
#include <QFontDatabase>
#include <QHeaderView>
#include <QVBoxLayout>
// HistoryLogModel
QVariant HistoryLogModel::data(const QModelIndex &index, int role) const {
bool has_signal = dbc_msg && !dbc_msg->sigs.empty();
if (role == Qt::DisplayRole) {
@ -37,7 +38,7 @@ QVariant HistoryLogModel::headerData(int section, Qt::Orientation orientation, i
if (section == 0) {
return "Time";
}
return has_signal ? dbc_msg->sigs[section - 1].name.c_str() : "Data";
return has_signal ? QString::fromStdString(dbc_msg->sigs[section - 1].name).replace('_', ' ') : "Data";
} else if (role == Qt::BackgroundRole && section > 0 && has_signal) {
return QBrush(QColor(getColor(section - 1)));
}
@ -64,10 +65,22 @@ void HistoryLogModel::updateState() {
}
}
// HeaderView
QSize HeaderView::sectionSizeFromContents(int logicalIndex) const {
const QString text = model()->headerData(logicalIndex, this->orientation(), Qt::DisplayRole).toString();
const QRect rect = fontMetrics().boundingRect(QRect(0, 0, sectionSize(logicalIndex), 1000), defaultAlignment(), text);
return rect.size() + QSize{10, 5};
}
// HistoryLog
HistoryLog::HistoryLog(QWidget *parent) : QTableView(parent) {
model = new HistoryLogModel(this);
setModel(model);
setHorizontalHeader(new HeaderView(Qt::Horizontal, this));
horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
horizontalHeader()->setDefaultAlignment(Qt::AlignLeft | (Qt::Alignment)Qt::TextWordWrap);
horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
verticalHeader()->setVisible(false);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

@ -1,10 +1,17 @@
#pragma once
#include <QHeaderView>
#include <QTableView>
#include "tools/cabana/canmessages.h"
#include "tools/cabana/dbcmanager.h"
class HeaderView : public QHeaderView {
public:
HeaderView(Qt::Orientation orientation, QWidget *parent = nullptr) : QHeaderView(orientation, parent) {}
QSize sectionSizeFromContents(int logicalIndex) const;
};
class HistoryLogModel : public QAbstractTableModel {
Q_OBJECT

Loading…
Cancel
Save