Fix syncing of visibility and enabled for menus on Cocoa

Fixed menu handling on Cocoa so if a menu is enabled/disabled or made
visible or not then it will keep this in sync with the appropriate
native menu entry.

Change-Id: If269185fcf065fb1b2f60d6ef8c27c107eb4509f
Reviewed-by: Pasi Matilainen <pasi.matilainen@digia.com>
Reviewed-by: James Turner <james.turner@kdab.com>
Reviewed-by: Andy Shaw <andy.shaw@digia.com>
This commit is contained in:
Andy Shaw 2012-09-28 15:58:58 +02:00 committed by The Qt Project
parent a3941c2f6e
commit 356f3c89b8
6 changed files with 78 additions and 8 deletions

View File

@ -102,6 +102,7 @@ public:
virtual void setText(const QString &text) = 0;
virtual void setEnabled(bool enabled) = 0;
virtual void setVisible(bool visible) = 0;
virtual QPlatformMenuItem *menuItemAt(int position) const = 0;
virtual QPlatformMenuItem *menuItemForTag(quintptr tag) const = 0;

View File

@ -70,6 +70,7 @@ public:
void removeMenuItem(QPlatformMenuItem *menuItem);
void syncMenuItem(QPlatformMenuItem *menuItem);
void setEnabled(bool enabled);
void setVisible(bool visible);
void syncSeparatorsCollapsible(bool enable);
void syncModalState(bool modal);

View File

@ -268,6 +268,12 @@ void QCocoaMenu::setParentItem(QCocoaMenuItem *item)
void QCocoaMenu::setEnabled(bool enabled)
{
m_enabled = enabled;
syncModalState(!m_enabled);
}
void QCocoaMenu::setVisible(bool visible)
{
[m_nativeItem setSubmenu:(visible ? m_nativeMenu : nil)];
}
QPlatformMenuItem *QCocoaMenu::menuItemAt(int position) const

View File

@ -263,7 +263,7 @@ NSMenuItem *QCocoaMenuItem::sync()
// [m_native setHidden:YES];
// [m_native setHidden:NO];
[m_native setHidden: !m_isVisible];
[m_native setEnabled: m_enabled];
QString text = m_text;
QKeySequence accel = m_shortcut;

View File

@ -1285,6 +1285,8 @@ void QMenuBar::actionEvent(QActionEvent *e)
}
} else if (menu) {
menu->setText(e->action()->text());
menu->setVisible(e->action()->isVisible());
menu->setEnabled(e->action()->isEnabled());
d->platformMenuBar->syncMenu(menu);
}
}

View File

@ -39,9 +39,12 @@
**
****************************************************************************/
#include <QtGui>
#include <QtWidgets>
#include <QMainWindow>
#include <QMenu>
#include <QApplication>
#include <QMenuBar>
#include <QMessageBox>
#include <QDebug>
class Responder : public QObject
{
@ -49,13 +52,34 @@ class Responder : public QObject
public:
Responder(QObject *pr) :
QObject(pr)
QObject(pr), visibleMenu(0), visibleSubMenu(0), enabledMenu(0), enabledSubMenu(0), visibleAction(0), enabledAction(0)
{
}
void setVisibleObjects(QMenu *vm, QMenu *vsm, QAction *va)
{
visibleMenu = vm;
visibleSubMenu = vsm;
visibleAction = va;
}
void setEnabledObjects(QMenu *em, QMenu *esm, QAction *ea)
{
enabledMenu = em;
enabledSubMenu = esm;
enabledAction = ea;
}
public slots:
void toggleVisiblity()
{
visibleMenu->menuAction()->setVisible(!visibleMenu->menuAction()->isVisible());
visibleSubMenu->menuAction()->setVisible(!visibleSubMenu->menuAction()->isVisible());
visibleAction->setVisible(!visibleAction->isVisible());
}
void toggleEnabled()
{
enabledMenu->menuAction()->setEnabled(!enabledMenu->menuAction()->isEnabled());
enabledSubMenu->menuAction()->setEnabled(!enabledSubMenu->menuAction()->isEnabled());
enabledAction->setEnabled(!enabledAction->isEnabled());
}
void toggleChecked(bool b)
{
QAction *a = qobject_cast<QAction *>(sender());
@ -82,6 +106,9 @@ public slots:
m->addAction(QString("Recent File %1").arg(i + 1));
}
}
private:
QMenu *visibleMenu, *visibleSubMenu, *enabledMenu, *enabledSubMenu;
QAction *visibleAction, *enabledAction;
};
void createWindow1()
@ -156,6 +183,39 @@ void createWindow1()
menu2->addAction(checkableAction);
QMenu *menu4 = new QMenu("Toggle menu", window);
QAction *toggleVisiblity = new QAction("Toggle visibility", window);
QAction *toggleEnabled = new QAction("Toggle enabled", window);
QObject::connect(toggleVisiblity, SIGNAL(triggered()), r, SLOT(toggleVisiblity()));
QObject::connect(toggleEnabled, SIGNAL(triggered()), r, SLOT(toggleEnabled()));
menu4->addAction(toggleVisiblity);
menu4->addAction(toggleEnabled);
window->menuBar()->addMenu(menu4);
QMenu *menu5 = new QMenu("Visible Menu", window);
menu5->addAction("Dummy action");
window->menuBar()->addMenu(menu5);
QMenu *menu6 = new QMenu("Menu with visible action and submenu", window);
QAction *visibleAction = new QAction("Visible action", window);
menu6->addAction(visibleAction);
QMenu *subMenu6 = new QMenu("Submenu");
subMenu6->addAction("Dummy action");
menu6->addMenu(subMenu6);
window->menuBar()->addMenu(menu6);
QMenu *menu7 = new QMenu("Enabled Menu", window);
menu7->addAction("Dummy action");
window->menuBar()->addMenu(menu7);
QMenu *menu8 = new QMenu("Menu with enabled action and submenu", window);
QAction *enabledAction = new QAction("Enabled action", window);
menu8->addAction(enabledAction);
QMenu *subMenu8 = new QMenu("Submenu");
subMenu8->addAction("Dummy action");
menu8->addMenu(subMenu8);
window->menuBar()->addMenu(menu8);
r->setVisibleObjects(menu5, subMenu6, visibleAction);
r->setEnabledObjects(menu7, subMenu8, enabledAction);
window->show();
}