cabana: faster history log with better stretch mode (#26112)

pull/26124/head
Dean Lee 3 years ago committed by GitHub
parent cd12ef4aa1
commit 409e8f5f89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 57
      tools/cabana/historylog.cc
  2. 11
      tools/cabana/historylog.h

@ -5,30 +5,25 @@
#include <QVBoxLayout> #include <QVBoxLayout>
QVariant HistoryLogModel::data(const QModelIndex &index, int role) const { QVariant HistoryLogModel::data(const QModelIndex &index, int role) const {
auto msg = dbc()->msg(msg_id); bool has_signal = dbc_msg && !dbc_msg->sigs.empty();
if (role == Qt::DisplayRole) { if (role == Qt::DisplayRole) {
const auto &can_msgs = can->messages(msg_id); const auto &m = can->messages(msg_id)[index.row()];
if (index.row() < can_msgs.size()) { if (index.column() == 0) {
const auto &can_data = can_msgs[index.row()]; return QString::number(m.ts, 'f', 2);
if (msg && index.column() < msg->sigs.size()) {
return get_raw_value((uint8_t *)can_data.dat.begin(), can_data.dat.size(), msg->sigs[index.column()]);
} else {
return toHex(can_data.dat);
} }
} return has_signal ? QString::number(get_raw_value((uint8_t *)m.dat.begin(), m.dat.size(), dbc_msg->sigs[index.column() - 1]))
} else if (role == Qt::FontRole) { : toHex(m.dat);
if (index.column() == 0 && !(msg && msg->sigs.size() > 0)) { } else if (role == Qt::FontRole && index.column() == 1 && !has_signal) {
return QFontDatabase::systemFont(QFontDatabase::FixedFont); return QFontDatabase::systemFont(QFontDatabase::FixedFont);
} }
}
return {}; return {};
} }
void HistoryLogModel::setMessage(const QString &message_id) { void HistoryLogModel::setMessage(const QString &message_id) {
beginResetModel(); beginResetModel();
msg_id = message_id; msg_id = message_id;
const auto msg = dbc()->msg(message_id); dbc_msg = dbc()->msg(message_id);
column_count = msg && !msg->sigs.empty() ? msg->sigs.size() : 1; column_count = (dbc_msg && !dbc_msg->sigs.empty() ? dbc_msg->sigs.size() : 1) + 1;
row_count = 0; row_count = 0;
endResetModel(); endResetModel();
@ -37,18 +32,14 @@ void HistoryLogModel::setMessage(const QString &message_id) {
QVariant HistoryLogModel::headerData(int section, Qt::Orientation orientation, int role) const { QVariant HistoryLogModel::headerData(int section, Qt::Orientation orientation, int role) const {
if (orientation == Qt::Horizontal) { if (orientation == Qt::Horizontal) {
auto msg = dbc()->msg(msg_id); bool has_signal = dbc_msg && !dbc_msg->sigs.empty();
if (msg && section < msg->sigs.size()) { if (role == Qt::DisplayRole || role == Qt::ToolTipRole) {
if (role == Qt::BackgroundRole) { if (section == 0) {
return QBrush(QColor(getColor(section))); return "Time";
} else if (role == Qt::DisplayRole || role == Qt::ToolTipRole) {
return QString::fromStdString(msg->sigs[section].name);
} }
} return has_signal ? dbc_msg->sigs[section - 1].name.c_str() : "Data";
} else if (role == Qt::DisplayRole) { } else if (role == Qt::BackgroundRole && section > 0 && has_signal) {
const auto &can_msgs = can->messages(msg_id); return QBrush(QColor(getColor(section - 1)));
if (section < can_msgs.size()) {
return QString::number(can_msgs[section].ts, 'f', 2);
} }
} }
return {}; return {};
@ -69,7 +60,6 @@ void HistoryLogModel::updateState() {
} }
if (row_count > 0) { if (row_count > 0) {
emit dataChanged(index(0, 0), index(row_count - 1, column_count - 1), {Qt::DisplayRole}); emit dataChanged(index(0, 0), index(row_count - 1, column_count - 1), {Qt::DisplayRole});
emit headerDataChanged(Qt::Vertical, 0, row_count - 1);
} }
} }
@ -79,17 +69,10 @@ HistoryLog::HistoryLog(QWidget *parent) : QWidget(parent) {
model = new HistoryLogModel(this); model = new HistoryLogModel(this);
table = new QTableView(this); table = new QTableView(this);
table->setModel(model); table->setModel(model);
table->horizontalHeader()->setStretchLastSection(true); table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
table->setEditTriggers(QAbstractItemView::NoEditTriggers); table->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Fixed);
table->setColumnWidth(0, 60);
table->verticalHeader()->setVisible(false);
table->setStyleSheet("QTableView::item { border:0px; padding-left:5px; padding-right:5px; }"); table->setStyleSheet("QTableView::item { border:0px; padding-left:5px; padding-right:5px; }");
table->verticalHeader()->setStyleSheet("QHeaderView::section {padding-left: 5px; padding-right: 5px;min-width:40px;}");
main_layout->addWidget(table); main_layout->addWidget(table);
} }
void HistoryLog::setMessage(const QString &message_id) {
model->setMessage(message_id);
}
void HistoryLog::updateState() {
model->updateState();
}

@ -13,14 +13,15 @@ public:
void setMessage(const QString &message_id); void setMessage(const QString &message_id);
void updateState(); void updateState();
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override { return column_count; } QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const override { return row_count; } int rowCount(const QModelIndex &parent = QModelIndex()) const override { return row_count; }
int columnCount(const QModelIndex &parent = QModelIndex()) const override { return column_count; }
private: private:
QString msg_id; QString msg_id;
int row_count = 0; int row_count = 0;
int column_count = 0; int column_count = 2;
const Msg *dbc_msg = nullptr;
}; };
class HistoryLog : public QWidget { class HistoryLog : public QWidget {
@ -28,8 +29,8 @@ class HistoryLog : public QWidget {
public: public:
HistoryLog(QWidget *parent); HistoryLog(QWidget *parent);
void setMessage(const QString &message_id); void setMessage(const QString &message_id) { model->setMessage(message_id); }
void updateState(); void updateState() { model->updateState(); }
private: private:
QTableView *table; QTableView *table;

Loading…
Cancel
Save