diff --git a/tools/cabana/messageswidget.cc b/tools/cabana/messageswidget.cc index e74b852617..f7b25bde51 100644 --- a/tools/cabana/messageswidget.cc +++ b/tools/cabana/messageswidget.cc @@ -39,6 +39,10 @@ MessagesWidget::MessagesWidget(QWidget *parent) : QWidget(parent) { view->header()->setSectionsMovable(true); + // Header context menu + view->header()->setContextMenuPolicy(Qt::CustomContextMenu); + QObject::connect(view->header(), &QHeaderView::customContextMenuRequested, view, &MessageView::headerContextMenuEvent); + main_layout->addWidget(view); // suppress @@ -319,3 +323,34 @@ void MessageView::updateBytesSectionSize() { header()->resizeSection(5, width); } } + +void MessageView::headerContextMenuEvent(const QPoint &pos) { + QMenu *menu = new QMenu(this); + int cur_index = header()->logicalIndexAt(pos); + + QString column_name; + QAction *action; + for (int visual_index = 0; visual_index < header()->count(); visual_index++) { + int logical_index = header()->logicalIndex(visual_index); + column_name = model()->headerData(logical_index, Qt::Horizontal).toString(); + + // Hide show action + if (header()->isSectionHidden(logical_index)) { + action = menu->addAction(tr("  %1").arg(column_name), [=]() { header()->showSection(logical_index); }); + } else { + action = menu->addAction(tr("✓ %1").arg(column_name), [=]() { header()->hideSection(logical_index); }); + } + + // Can't hide the name column + action->setEnabled(logical_index > 0); + + // Make current column bold + if (logical_index == cur_index) { + QFont font = action->font(); + font.setBold(true); + action->setFont(font); + } + } + + menu->popup(header()->mapToGlobal(pos)); +} diff --git a/tools/cabana/messageswidget.h b/tools/cabana/messageswidget.h index 2dc0d1d316..ef5467b053 100644 --- a/tools/cabana/messageswidget.h +++ b/tools/cabana/messageswidget.h @@ -2,8 +2,10 @@ #include #include +#include #include #include +#include #include #include @@ -43,6 +45,7 @@ public: void drawBranches(QPainter *painter, const QRect &rect, const QModelIndex &index) const override {} void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector &roles = QVector()) override; void updateBytesSectionSize(); + void headerContextMenuEvent(const QPoint &pos); }; class MessagesWidget : public QWidget {