dbusmenu: Use proper toggle-type for items that are part of group

To achieve that, add a new virtual setHasExclusiveGroup() method to
QPlatformMenuItem class (optional because we don't want to break existing
platform themes), call it when converting QActions into platform menu items,
and use it when exporting the menu items over D-Bus.

Also, send ActionChanged event for actions when their group is changed, so
that the platform menus are updated too.

Change-Id: I8d951ace8c4097decec2a0154163e3672214effb
Reviewed-by: Shawn Rutledge <shawn.rutledge@theqtcompany.com>
This commit is contained in:
Dmitry Shachnev 2016-01-22 10:46:05 +03:00
parent b30edc5153
commit 488cf78e44
7 changed files with 16 additions and 4 deletions

View File

@ -87,6 +87,7 @@ public:
virtual void setEnabled(bool enabled) = 0;
virtual void setIconSize(int size) = 0;
virtual void setNativeContents(WId item) { Q_UNUSED(item); }
virtual void setHasExclusiveGroup(bool hasExclusiveGroup) { Q_UNUSED(hasExclusiveGroup); }
Q_SIGNALS:
void activated();

View File

@ -184,9 +184,8 @@ QDBusMenuItem::QDBusMenuItem(const QDBusPlatformMenuItem *item)
m_properties.insert(QLatin1String("children-display"), QLatin1String("submenu"));
m_properties.insert(QLatin1String("enabled"), item->isEnabled());
if (item->isCheckable()) {
// dbusmenu supports "radio" too, but QPlatformMenuItem doesn't seem to
// (QAction would have an exclusive actionGroup)
m_properties.insert(QLatin1String("toggle-type"), QLatin1String("checkmark"));
QString toggleType = item->hasExclusiveGroup() ? QLatin1String("radio") : QLatin1String("checkmark");
m_properties.insert(QLatin1String("toggle-type"), toggleType);
m_properties.insert(QLatin1String("toggle-state"), item->isChecked() ? 1 : 0);
}
/* TODO support shortcuts

View File

@ -61,6 +61,7 @@ QDBusPlatformMenuItem::QDBusPlatformMenuItem(quintptr tag)
, m_isCheckable(false)
, m_isChecked(false)
, m_dbusID(nextDBusID++)
, m_hasExclusiveGroup(false)
{
menuItemsByID.insert(m_dbusID, this);
}
@ -124,6 +125,11 @@ void QDBusPlatformMenuItem::setChecked(bool isChecked)
m_isChecked = isChecked;
}
void QDBusPlatformMenuItem::setHasExclusiveGroup(bool hasExclusiveGroup)
{
m_hasExclusiveGroup = hasExclusiveGroup;
}
void QDBusPlatformMenuItem::setShortcut(const QKeySequence &shortcut)
{
m_shortcut = shortcut;

View File

@ -97,6 +97,8 @@ public:
void setCheckable(bool checkable) Q_DECL_OVERRIDE;
bool isChecked() const { return m_isChecked; }
void setChecked(bool isChecked) Q_DECL_OVERRIDE;
bool hasExclusiveGroup() const { return m_hasExclusiveGroup; }
void setHasExclusiveGroup(bool hasExclusiveGroup) Q_DECL_OVERRIDE;
QKeySequence shortcut() const { return m_shortcut; }
void setShortcut(const QKeySequence& shortcut) Q_DECL_OVERRIDE;
void setIconSize(int size) Q_DECL_OVERRIDE { Q_UNUSED(size); }
@ -123,7 +125,8 @@ private:
bool m_isCheckable : 1;
bool m_isChecked : 1;
int m_dbusID : 16;
int m_reserved : 7;
bool m_hasExclusiveGroup : 1;
int m_reserved : 6;
QKeySequence m_shortcut;
};

View File

@ -612,6 +612,7 @@ void QAction::setActionGroup(QActionGroup *group)
d->group = group;
if(group)
group->addAction(this);
d->sendDataChanged();
}
/*!

View File

@ -196,6 +196,7 @@ QAction *QActionGroup::addAction(QAction* a)
if (oldGroup)
oldGroup->removeAction(a);
a->d_func()->group = this;
a->d_func()->sendDataChanged();
}
return a;
}

View File

@ -3231,6 +3231,7 @@ static void copyActionToPlatformItem(const QAction *action, QPlatformMenuItem *i
item->setShortcut(action->shortcut());
item->setCheckable(action->isCheckable());
item->setChecked(action->isChecked());
item->setHasExclusiveGroup(action->actionGroup() && action->actionGroup()->isExclusive());
item->setFont(action->font());
item->setRole((QPlatformMenuItem::MenuRole) action->menuRole());
item->setEnabled(action->isEnabled());