mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-07-10 03:42:09 +08:00
Cabana: sort signals by address in logs view (#26639)
cleanup old-commit-hash: 2b916044f5e7e6466709bd45301765d0b7599eec
This commit is contained in:
+21
-27
@@ -5,20 +5,15 @@
|
||||
|
||||
// HistoryLogModel
|
||||
|
||||
inline const Signal &get_signal(const DBCMsg *m, int index) {
|
||||
return std::next(m->sigs.begin(), index)->second;
|
||||
}
|
||||
|
||||
QVariant HistoryLogModel::data(const QModelIndex &index, int role) const {
|
||||
bool has_signal = dbc_msg && !dbc_msg->sigs.empty();
|
||||
if (role == Qt::DisplayRole) {
|
||||
const auto &m = messages[index.row()];
|
||||
if (index.column() == 0) {
|
||||
return QString::number(m.ts, 'f', 2);
|
||||
}
|
||||
return has_signal ? QString::number(get_raw_value((uint8_t *)m.dat.begin(), m.dat.size(), get_signal(dbc_msg, index.column() - 1)))
|
||||
: toHex(m.dat);
|
||||
} else if (role == Qt::FontRole && index.column() == 1 && !has_signal) {
|
||||
return !sigs.empty() ? QString::number(get_raw_value((uint8_t *)m.dat.data(), m.dat.size(), *sigs[index.column() - 1]))
|
||||
: toHex(m.dat);
|
||||
} else if (role == Qt::FontRole && index.column() == 1 && sigs.empty()) {
|
||||
return QFontDatabase::systemFont(QFontDatabase::FixedFont);
|
||||
}
|
||||
return {};
|
||||
@@ -27,25 +22,25 @@ QVariant HistoryLogModel::data(const QModelIndex &index, int role) const {
|
||||
void HistoryLogModel::setMessage(const QString &message_id) {
|
||||
beginResetModel();
|
||||
msg_id = message_id;
|
||||
dbc_msg = dbc()->msg(message_id);
|
||||
column_count = (dbc_msg && !dbc_msg->sigs.empty() ? dbc_msg->sigs.size() : 1) + 1;
|
||||
row_count = 0;
|
||||
sigs.clear();
|
||||
messages.clear();
|
||||
if (auto dbc_msg = dbc()->msg(message_id)) {
|
||||
sigs = dbc_msg->getSignals();
|
||||
}
|
||||
endResetModel();
|
||||
|
||||
updateState();
|
||||
}
|
||||
|
||||
QVariant HistoryLogModel::headerData(int section, Qt::Orientation orientation, int role) const {
|
||||
if (orientation == Qt::Horizontal) {
|
||||
bool has_signal = dbc_msg && !dbc_msg->sigs.empty();
|
||||
if (role == Qt::DisplayRole || role == Qt::ToolTipRole) {
|
||||
if (section == 0) {
|
||||
return "Time";
|
||||
}
|
||||
return has_signal ? QString::fromStdString(get_signal(dbc_msg, section - 1).name).replace('_', ' ') : "Data";
|
||||
} else if (role == Qt::BackgroundRole && section > 0 && has_signal) {
|
||||
return !sigs.empty() ? QString::fromStdString(sigs[section - 1]->name).replace('_', ' ') : "Data";
|
||||
} else if (role == Qt::BackgroundRole && section > 0 && !sigs.empty()) {
|
||||
return QBrush(QColor(getColor(section - 1)));
|
||||
} else if (role == Qt::ForegroundRole && section > 0 && has_signal) {
|
||||
} else if (role == Qt::ForegroundRole && section > 0 && !sigs.empty()) {
|
||||
return QBrush(Qt::black);
|
||||
}
|
||||
}
|
||||
@@ -53,21 +48,20 @@ QVariant HistoryLogModel::headerData(int section, Qt::Orientation orientation, i
|
||||
}
|
||||
|
||||
void HistoryLogModel::updateState() {
|
||||
if (msg_id.isEmpty()) return;
|
||||
|
||||
int prev_row_count = row_count;
|
||||
messages = can->messages(msg_id);
|
||||
row_count = messages.size();
|
||||
int delta = row_count - prev_row_count;
|
||||
int prev_row_count = messages.size();
|
||||
if (!msg_id.isEmpty()) {
|
||||
messages = can->messages(msg_id);
|
||||
}
|
||||
int delta = messages.size() - prev_row_count;
|
||||
if (delta > 0) {
|
||||
beginInsertRows({}, prev_row_count, row_count - 1);
|
||||
beginInsertRows({}, prev_row_count, messages.size() - 1);
|
||||
endInsertRows();
|
||||
} else if (delta < 0) {
|
||||
beginRemoveRows({}, row_count, prev_row_count - 1);
|
||||
beginRemoveRows({}, messages.size(), prev_row_count - 1);
|
||||
endRemoveRows();
|
||||
}
|
||||
if (row_count > 0) {
|
||||
emit dataChanged(index(0, 0), index(row_count - 1, column_count - 1), {Qt::DisplayRole});
|
||||
if (!messages.empty()) {
|
||||
emit dataChanged(index(0, 0), index(rowCount() - 1, columnCount() - 1), {Qt::DisplayRole});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,5 +100,5 @@ HistoryLog::HistoryLog(QWidget *parent) : QTableView(parent) {
|
||||
|
||||
int HistoryLog::sizeHintForColumn(int column) const {
|
||||
// sizeHintForColumn is only called for column 0 (ResizeToContents)
|
||||
return itemDelegate()->sizeHint(viewOptions(), model->index(0, 0)).width() + 1; // +1 for grid
|
||||
return itemDelegate()->sizeHint(viewOptions(), model->index(0, 0)).width() + 5;
|
||||
}
|
||||
|
||||
@@ -14,32 +14,27 @@ public:
|
||||
};
|
||||
|
||||
class HistoryLogModel : public QAbstractTableModel {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
HistoryLogModel(QObject *parent) : QAbstractTableModel(parent) {}
|
||||
void setMessage(const QString &message_id);
|
||||
void updateState();
|
||||
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
|
||||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override { return row_count; }
|
||||
int columnCount(const QModelIndex &parent = QModelIndex()) const override { return column_count; }
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override { return messages.size(); }
|
||||
int columnCount(const QModelIndex &parent = QModelIndex()) const override { return std::max(1ul, sigs.size()) + 1; }
|
||||
|
||||
private:
|
||||
QString msg_id;
|
||||
int row_count = 0;
|
||||
int column_count = 2;
|
||||
const DBCMsg *dbc_msg = nullptr;
|
||||
std::deque<CanData> messages;
|
||||
std::vector<const Signal*> sigs;
|
||||
};
|
||||
|
||||
class HistoryLog : public QTableView {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
HistoryLog(QWidget *parent);
|
||||
void setMessage(const QString &message_id) { model->setMessage(message_id); }
|
||||
void updateState() { model->updateState(); }
|
||||
|
||||
private:
|
||||
int sizeHintForColumn(int column) const override;
|
||||
HistoryLogModel *model;
|
||||
|
||||
Reference in New Issue
Block a user