bool isEnabled() const { return menu ? menu->isEnabled() : menuBar->isEnabled(); }
void HeaderViewWidget::contextMenuEvent(QContextMenuEvent *event) { ItemViewWidget *view = qobject_cast<ItemViewWidget*>(parent()); if (!view) { return; } const int sortColumn = view->getSortColumn(); const int sortOrder = view->getSortOrder(); QMenu menu(this); QMenu *sortMenu = menu.addMenu(tr("Sorting")); QAction *sortAscendingAction = sortMenu->addAction(tr("Sort Ascending")); sortAscendingAction->setData(-2); sortAscendingAction->setCheckable(true); sortAscendingAction->setChecked(sortOrder == Qt::AscendingOrder); QAction *sortDescendingAction = sortMenu->addAction(tr("Sort Descending")); sortDescendingAction->setData(-3); sortDescendingAction->setCheckable(true); sortDescendingAction->setChecked(sortOrder == Qt::DescendingOrder); sortMenu->addSeparator(); QAction *noSortAction = sortMenu->addAction(tr("No Sorting")); noSortAction->setData(-1); noSortAction->setCheckable(true); noSortAction->setChecked(sortColumn < 0); sortMenu->addSeparator(); QMenu *visibilityMenu = menu.addMenu(tr("Visible Columns")); visibilityMenu->setEnabled(model()->columnCount() > 1); QAction *showAllColumnsAction = NULL; bool allColumnsVisible = true; if (visibilityMenu->isEnabled()) { showAllColumnsAction = visibilityMenu->addAction(tr("Show All")); showAllColumnsAction->setData(-1); showAllColumnsAction->setCheckable(true); visibilityMenu->addSeparator(); } for (int i = 0; i < model()->columnCount(); ++i) { const QString title(model()->headerData(i, orientation()).toString()); QAction *action = sortMenu->addAction(title); action->setData(i); action->setCheckable(true); action->setChecked(i == sortColumn); if (visibilityMenu->isEnabled()) { QAction *action = visibilityMenu->addAction(title); action->setData(i); action->setCheckable(true); action->setChecked(!view->isColumnHidden(i)); if (!action->isChecked()) { allColumnsVisible = false; } } } if (showAllColumnsAction) { showAllColumnsAction->setChecked(allColumnsVisible); showAllColumnsAction->setEnabled(!allColumnsVisible); } connect(sortMenu, SIGNAL(triggered(QAction*)), this, SLOT(toggleSort(QAction*))); connect(visibilityMenu, SIGNAL(triggered(QAction*)), this, SLOT(toggleColumnVisibility(QAction*))); menu.exec(event->globalPos()); }