Commit 14c0e2d0 authored by Alexander Volkov's avatar Alexander Volkov

Display exclusive actions correctly in systray menus

Summary:
Add exclusive actions to their own action groups so that they
could be diplayed as radio buttons by styles. It's only to
display them correctly, the switching logic is done by Qt
outside this plugin.

Reviewers: #plasma, davidedmundson

Reviewed By: #plasma, davidedmundson

Subscribers: davidedmundson, plasma-devel

Tags: #plasma

Differential Revision: https://phabricator.kde.org/D9733
parent f2275d44
......@@ -247,6 +247,21 @@ void SystemTrayMenuItem::setIconSize(int size)
Q_UNUSED(size);
}
void SystemTrayMenuItem::setHasExclusiveGroup(bool hasExclusiveGroup)
{
if (hasExclusiveGroup) {
if (!m_action->actionGroup()) {
m_action->setActionGroup(new QActionGroup(m_action));
}
} else {
QActionGroup *actionGroup = m_action->actionGroup();
if (actionGroup) {
m_action->setActionGroup(nullptr);
delete actionGroup;
}
}
}
quintptr SystemTrayMenuItem::tag() const
{
return m_tag;
......
......@@ -77,6 +77,7 @@ public:
void setVisible(bool isVisible) override;
quintptr tag() const override;
void setIconSize(int size) override;
void setHasExclusiveGroup(bool hasExclusiveGroup) override;
QAction *action() const;
......
......@@ -3,3 +3,6 @@ target_link_libraries(qfiledialogtest Qt5::Core Qt5::Widgets)
add_executable(windowdecotest windowdecotest.cpp)
target_link_libraries(windowdecotest Qt5::Core Qt5::Widgets KF5::ConfigWidgets)
add_executable(systraymenuexclusiveactionstest systraymenuexclusiveactionstest.cpp)
target_link_libraries(systraymenuexclusiveactionstest Qt5::Core Qt5::Widgets)
#include <QApplication>
#include <QSystemTrayIcon>
#include <QMenu>
#include <QAction>
#include <QActionGroup>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QSystemTrayIcon trayIcon(QIcon::fromTheme(QStringLiteral("application-exit")));
QMenu contextMenu;
QAction *action1 = contextMenu.addAction(QStringLiteral("Exclusive Item 1"));
QAction *action2 = contextMenu.addAction(QStringLiteral("Exclusive Item 2"));
action1->setCheckable(true);
action1->setChecked(true);
action2->setCheckable(true);
QActionGroup *actionGroup = new QActionGroup(&contextMenu);
actionGroup->addAction(action1);
actionGroup->addAction(action2);
QAction quitAction(QIcon::fromTheme(QStringLiteral("application-exit")), QStringLiteral("Quit"));
QObject::connect(&quitAction, &QAction::triggered, &app, &QApplication::quit);
contextMenu.addAction(&quitAction);
trayIcon.setContextMenu(&contextMenu);
trayIcon.show();
return app.exec();
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment