cabana: hide/show columns using context menu (#28033)

old-commit-hash: 8318d7207b
beeps
Willem Melching 2 years ago committed by GitHub
parent 6817df8eb1
commit 5c74c4d75d
  1. 35
      tools/cabana/messageswidget.cc
  2. 3
      tools/cabana/messageswidget.h

@ -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));
}

@ -2,8 +2,10 @@
#include <QAbstractTableModel>
#include <QCheckBox>
#include <QContextMenuEvent>
#include <QHeaderView>
#include <QLineEdit>
#include <QMenu>
#include <QSet>
#include <QTreeView>
@ -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<int> &roles = QVector<int>()) override;
void updateBytesSectionSize();
void headerContextMenuEvent(const QPoint &pos);
};
class MessagesWidget : public QWidget {

Loading…
Cancel
Save