UI: Tray Menu: Use as context menu

This commit is contained in:
Nodir Temirkhodjaev 2024-01-05 16:09:18 +03:00
parent b19080d970
commit b5aaee82ba

View File

@ -142,9 +142,11 @@ void setActionCheckable(QAction *action, bool checked = false, const QObject *re
} }
QAction *addAction(QWidget *widget, const QString &iconPath, const QObject *receiver = nullptr, QAction *addAction(QWidget *widget, const QString &iconPath, const QObject *receiver = nullptr,
const char *member = nullptr, bool checkable = false, bool checked = false) const char *member = nullptr, TrayIcon::ActionType actionType = TrayIcon::ActionNone,
bool checkable = false, bool checked = false)
{ {
auto action = new QAction(widget); auto action = new QAction(widget);
action->setData(actionType);
if (!iconPath.isEmpty()) { if (!iconPath.isEmpty()) {
action->setIcon(IconCache::icon(iconPath)); action->setIcon(IconCache::icon(iconPath));
@ -351,46 +353,53 @@ void TrayIcon::retranslateFilterModeActions()
void TrayIcon::setupUi() void TrayIcon::setupUi()
{ {
this->setToolTip(QApplication::applicationDisplayName());
setupTrayMenu(); setupTrayMenu();
updateTrayMenu(); updateTrayMenu();
this->setContextMenu(m_menu);
this->setToolTip(QApplication::applicationDisplayName());
} }
void TrayIcon::setupTrayMenu() void TrayIcon::setupTrayMenu()
{ {
m_menu = ControlUtil::createMenu(windowManager()->mainWindow()); m_menu = ControlUtil::createMenu(windowManager()->mainWindow());
m_homeAction = addAction(m_menu, ":/icons/fort.png", windowManager(), SLOT(showHomeWindow())); m_homeAction = addAction(
m_menu, ":/icons/fort.png", windowManager(), SLOT(showHomeWindow()), ActionShowHome);
addHotKey(m_homeAction, iniUser()->hotKeyHome()); addHotKey(m_homeAction, iniUser()->hotKeyHome());
m_programsAction = addAction( m_programsAction = addAction(m_menu, ":/icons/application.png", windowManager(),
m_menu, ":/icons/application.png", windowManager(), SLOT(showProgramsWindow())); SLOT(showProgramsWindow()), ActionShowPrograms);
addHotKey(m_programsAction, iniUser()->hotKeyPrograms()); addHotKey(m_programsAction, iniUser()->hotKeyPrograms());
setupTrayMenuOptions(); setupTrayMenuOptions();
m_menu->addMenu(m_optionsMenu); m_menu->addMenu(m_optionsMenu);
m_statisticsAction = addAction( m_statisticsAction = addAction(m_menu, ":/icons/chart_bar.png", windowManager(),
m_menu, ":/icons/chart_bar.png", windowManager(), SLOT(showStatisticsWindow())); SLOT(showStatisticsWindow()), ActionShowStatistics);
addHotKey(m_statisticsAction, iniUser()->hotKeyStatistics()); addHotKey(m_statisticsAction, iniUser()->hotKeyStatistics());
m_graphAction = addAction(m_menu, ":/icons/action_log.png", windowManager(), m_graphAction = addAction(m_menu, ":/icons/action_log.png", windowManager(),
SLOT(switchGraphWindow()), true, windowManager()->isWindowOpen(WindowGraph)); SLOT(switchGraphWindow()), ActionShowTrafficGraph, /*checkable=*/true,
windowManager()->isWindowOpen(WindowGraph));
addHotKey(m_graphAction, iniUser()->hotKeyGraph()); addHotKey(m_graphAction, iniUser()->hotKeyGraph());
m_menu->addSeparator(); m_menu->addSeparator();
m_filterEnabledAction = addAction(m_menu, QString(), this, SLOT(switchTrayFlag(bool)), true); m_filterEnabledAction = addAction(m_menu, QString(), this, SLOT(switchTrayFlag(bool)),
ActionSwitchFilterEnabled, /*checkable=*/true);
addHotKey(m_filterEnabledAction, iniUser()->hotKeyFilter()); addHotKey(m_filterEnabledAction, iniUser()->hotKeyFilter());
m_blockTrafficAction = addAction(m_menu, QString(), this, SLOT(switchTrayFlag(bool)), true); m_blockTrafficAction = addAction(m_menu, QString(), this, SLOT(switchTrayFlag(bool)),
ActionSwitchBlockTraffic, /*checkable=*/true);
addHotKey(m_blockTrafficAction, iniUser()->hotKeyBlockTraffic()); addHotKey(m_blockTrafficAction, iniUser()->hotKeyBlockTraffic());
m_blockInetTrafficAction = addAction(m_menu, QString(), this, SLOT(switchTrayFlag(bool)), true); m_blockInetTrafficAction = addAction(m_menu, QString(), this, SLOT(switchTrayFlag(bool)),
ActionSwitchBlockInetTraffic, /*checkable=*/true);
addHotKey(m_blockInetTrafficAction, iniUser()->hotKeyBlockInetTraffic()); addHotKey(m_blockInetTrafficAction, iniUser()->hotKeyBlockInetTraffic());
m_filterModeMenuAction = addAction(m_menu, QString(), this, SLOT(switchFilterModeMenu(bool))); m_filterModeMenuAction = addAction(
m_menu, QString(), this, SLOT(switchFilterModeMenu(bool)), ActionShowFilterModeMenu);
m_filterModeMenuAction->setVisible(false); m_filterModeMenuAction->setVisible(false);
setupTrayMenuFilterMode(); setupTrayMenuFilterMode();
@ -399,8 +408,8 @@ void TrayIcon::setupTrayMenu()
m_menu->addSeparator(); m_menu->addSeparator();
for (int i = 0; i < MAX_APP_GROUP_COUNT; ++i) { for (int i = 0; i < MAX_APP_GROUP_COUNT; ++i) {
QAction *a = QAction *a = addAction(m_menu, QString(), this, SLOT(switchTrayFlag(bool)), ActionNone,
addAction(m_menu, QString(), this, SLOT(switchTrayFlag(bool)), /*checkable=*/true); /*checkable=*/true);
constexpr int maxFKeyCount = 12; constexpr int maxFKeyCount = 12;
if (i < maxFKeyCount) { if (i < maxFKeyCount) {
@ -418,7 +427,8 @@ void TrayIcon::setupTrayMenu()
m_quitAction = addAction(m_menu, ":/icons/standby.png", this, SLOT(quitProgram())); m_quitAction = addAction(m_menu, ":/icons/standby.png", this, SLOT(quitProgram()));
addHotKey(m_quitAction, iniUser()->hotKeyQuit()); addHotKey(m_quitAction, iniUser()->hotKeyQuit());
m_trayMenuAction = addAction(m_menu, QString(), this, SLOT(switchTrayMenu(bool))); m_trayMenuAction =
addAction(m_menu, QString(), this, SLOT(switchTrayMenu(bool)), ActionShowTrayMenu);
m_trayMenuAction->setVisible(false); m_trayMenuAction->setVisible(false);
} }
@ -459,7 +469,7 @@ void TrayIcon::setupTrayMenuFilterMode()
const QString hotKey = hotKeys.at(index); const QString hotKey = hotKeys.at(index);
QAction *a = addAction(m_filterModeMenu, iconPath, /*receiver=*/nullptr, /*member=*/nullptr, QAction *a = addAction(m_filterModeMenu, iconPath, /*receiver=*/nullptr, /*member=*/nullptr,
/*checkable=*/true); ActionNone, /*checkable=*/true);
a->setText(name); a->setText(name);
addHotKey(a, iniUser()->hotKeyValue(hotKey)); addHotKey(a, iniUser()->hotKeyValue(hotKey));
@ -766,9 +776,17 @@ QAction *TrayIcon::clickActionByType(TrayIcon::ActionType actionType) const
void TrayIcon::onMouseClicked(TrayIcon::ClickType clickType) void TrayIcon::onMouseClicked(TrayIcon::ClickType clickType)
{ {
QAction *action = clickAction(clickType); QAction *action = clickAction(clickType);
if (action) { if (!action)
action->trigger(); return;
if (clickType == TrayIcon::RightClick) {
if (action->data().toInt() == TrayIcon::ActionShowTrayMenu)
return; // already handled by context-menu logic
m_menu->hide(); // revert the default action
} }
action->trigger();
} }
void TrayIcon::onTrayActivatedByTrigger() void TrayIcon::onTrayActivatedByTrigger()