Skip to content

Commit

Permalink
cabana: fix messages not updated correctly after seekto() (commaai#…
Browse files Browse the repository at this point in the history
…30351)

fix messages not updated correctly after seekto
  • Loading branch information
deanlee authored Oct 31, 2023
1 parent c04e019 commit bf5a45e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
31 changes: 16 additions & 15 deletions tools/cabana/messageswidget.cc
Original file line number Diff line number Diff line change
Expand Up @@ -186,20 +186,21 @@ QVariant MessageListModel::data(const QModelIndex &index, int role) const {
};

const auto &item = items_[index.row()];
const auto &data = can->lastMessage(item.id);
if (role == Qt::DisplayRole) {
switch (index.column()) {
case Column::NAME: return item.name;
case Column::SOURCE: return item.id.source != INVALID_SOURCE ? QString::number(item.id.source) : "N/A";
case Column::ADDRESS: return QString::number(item.id.address, 16);
case Column::NODE: return item.node;
case Column::FREQ: return item.id.source != INVALID_SOURCE ? getFreq(*item.data) : "N/A";
case Column::COUNT: return item.id.source != INVALID_SOURCE ? QString::number(item.data->count) : "N/A";
case Column::FREQ: return item.id.source != INVALID_SOURCE ? getFreq(data) : "N/A";
case Column::COUNT: return item.id.source != INVALID_SOURCE ? QString::number(data.count) : "N/A";
case Column::DATA: return item.id.source != INVALID_SOURCE ? "" : "N/A";
}
} else if (role == ColorsRole) {
return QVariant::fromValue((void*)(&item.data->colors));
return QVariant::fromValue((void*)(&data.colors));
} else if (role == BytesRole && index.column() == Column::DATA && item.id.source != INVALID_SOURCE) {
return QVariant::fromValue((void*)(&item.data->dat));
return QVariant::fromValue((void*)(&data.dat));
} else if (role == Qt::ToolTipRole && index.column() == Column::NAME) {
auto msg = dbc()->msg(item.id);
auto tooltip = item.name;
Expand All @@ -219,7 +220,7 @@ void MessageListModel::dbcModified() {
for (const auto &[_, m] : dbc()->getMessages(-1)) {
dbc_messages_.insert(MessageId{.source = INVALID_SOURCE, .address = m.address});
}
filterAndSort(true);
filterAndSort();
}

void MessageListModel::sortItems(std::vector<MessageListModel::Item> &items) {
Expand All @@ -233,8 +234,8 @@ void MessageListModel::sortItems(std::vector<MessageListModel::Item> &items) {
case Column::SOURCE: do_sort(items, [](auto &item) { return std::tie(item.id.source, item.id); }); break;
case Column::ADDRESS: do_sort(items, [](auto &item) { return std::tie(item.id.address, item.id);}); break;
case Column::NODE: do_sort(items, [](auto &item) { return std::tie(item.node, item.id);}); break;
case Column::FREQ: do_sort(items, [](auto &item) { return std::tie(item.data->freq, item.id); }); break;
case Column::COUNT: do_sort(items, [](auto &item) { return std::tie(item.data->count, item.id); }); break;
case Column::FREQ: do_sort(items, [](auto &item) { return std::make_pair(can->lastMessage(item.id).freq, item.id); }); break;
case Column::COUNT: do_sort(items, [](auto &item) { return std::make_pair(can->lastMessage(item.id).count, item.id); }); break;
}
}

Expand All @@ -258,6 +259,7 @@ bool MessageListModel::match(const MessageListModel::Item &item) {
return true;

bool match = true;
const auto &data = can->lastMessage(item.id);
for (auto it = filters_.cbegin(); it != filters_.cend() && match; ++it) {
const QString &txt = it.value();
switch (it.key()) {
Expand All @@ -282,20 +284,20 @@ bool MessageListModel::match(const MessageListModel::Item &item) {
break;
case Column::FREQ:
// TODO: Hide stale messages?
match = parseRange(txt, item.data->freq);
match = parseRange(txt, data.freq);
break;
case Column::COUNT:
match = parseRange(txt, item.data->count);
match = parseRange(txt, data.count);
break;
case Column::DATA:
match = utils::toHex(item.data->dat).contains(txt, Qt::CaseInsensitive);
match = utils::toHex(data.dat).contains(txt, Qt::CaseInsensitive);
break;
}
}
return match;
}

void MessageListModel::filterAndSort(bool force_reset) {
void MessageListModel::filterAndSort() {
// merge CAN and DBC messages
std::vector<MessageId> all_messages;
all_messages.reserve(can->lastMessages().size() + dbc_messages_.size());
Expand All @@ -304,22 +306,21 @@ void MessageListModel::filterAndSort(bool force_reset) {
all_messages.push_back(id);
dbc_msgs.erase(MessageId{.source = INVALID_SOURCE, .address = id.address});
}
std::copy(dbc_msgs.begin(), dbc_msgs.end(), std::back_inserter(all_messages));
all_messages.insert(all_messages.end(), dbc_msgs.begin(), dbc_msgs.end());

// filter and sort
std::vector<Item> items;
for (const auto &id : all_messages) {
auto msg = dbc()->msg(id);
Item item = {.id = id,
.name = msg ? msg->name : UNTITLED,
.node = msg ? msg->transmitter : QString(),
.data = &can->lastMessage(id)};
.node = msg ? msg->transmitter : QString()};
if (match(item))
items.emplace_back(item);
}
sortItems(items);

if (force_reset || items_ != items) {
if (items_ != items) {
beginResetModel();
items_ = std::move(items);
endResetModel();
Expand Down
7 changes: 4 additions & 3 deletions tools/cabana/messageswidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,16 @@ Q_OBJECT
void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override;
void setFilterStrings(const QMap<int, QString> &filters);
void msgsReceived(const std::set<MessageId> *new_msgs, bool has_new_ids);
void filterAndSort(bool force_reset = false);
void filterAndSort();
void dbcModified();

struct Item {
MessageId id;
QString name;
QString node;
const CanData *data;
bool operator==(const Item &other) const { return id == other.id; }
bool operator==(const Item &other) const {
return id == other.id && name == other.name && node == other.node;
}
};
std::vector<Item> items_;

Expand Down

0 comments on commit bf5a45e

Please sign in to comment.