diff options
| author | Gabriel de Dietrich <gabriel.dedietrich@digia.com> | 2013-02-25 14:40:24 +0100 |
|---|---|---|
| committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-02-25 15:43:37 +0100 |
| commit | 7cf9b32258c0c1b11cc12693f6feef7dc9d13ef7 (patch) | |
| tree | ba7334c3f36c0a0de1320356836f1b3bfda14544 /src/controls/qtmenu.cpp | |
| parent | ea4a6d3c095b62e1afb4ccf5125f1280f172e30e (diff) | |
Menu: Fix 'selectedIndex' update on item trigger
This demanded some code refactoring, and the introduction
of the parent menu property. Updated auto-tests too.
Change-Id: I3d9070b0a1a76d6c174ccf13965d30413f23aadd
Reviewed-by: J-P Nurmi <jpnurmi@digia.com>
Reviewed-by: Caroline Chao <caroline.chao@digia.com>
Diffstat (limited to 'src/controls/qtmenu.cpp')
| -rw-r--r-- | src/controls/qtmenu.cpp | 55 |
1 files changed, 18 insertions, 37 deletions
diff --git a/src/controls/qtmenu.cpp b/src/controls/qtmenu.cpp index 5c1587c8e..766379049 100644 --- a/src/controls/qtmenu.cpp +++ b/src/controls/qtmenu.cpp @@ -157,15 +157,17 @@ void QtMenu::setSelectedIndex(int index) return; m_selectedIndex = index; - - if (m_selectedIndex >= 0 && m_selectedIndex < m_menuItems.size()) - if (QtMenuItem *item = qobject_cast<QtMenuItem *>(m_menuItems[m_selectedIndex])) - if (item->checkable()) - item->setChecked(true); - emit selectedIndexChanged(); } +void QtMenu::updateSelectedIndex() +{ + if (QtMenuBase *menuItem = qobject_cast<QtMenuItem*>(sender())) { + int index = m_menuItems.indexOf(menuItem); + setSelectedIndex(index); + } +} + QQmlListProperty<QtMenuBase> QtMenu::menuItems() { return QQmlListProperty<QtMenuBase>(this, 0, &QtMenu::append_menuItems, &QtMenu::count_menuItems, &QtMenu::at_menuItems, 0); @@ -268,40 +270,24 @@ void QtMenu::clearMenuItems() m_menuItems.clear(); } -QtMenuItem *QtMenu::addMenuItem(const QString &text) +void QtMenu::addMenuItem(QtMenuBase *menuItem) { - QtMenuItem *menuItem = new QtMenuItem(this); - menuItem->setText(text); + menuItem->setParentMenu(this); m_menuItems.append(menuItem); if (QPlatformMenuItem *platformItem = menuItem->platformItem()) { if (m_platformMenu) m_platformMenu->insertMenuItem(platformItem, 0 /* append */); - - connect(platformItem, SIGNAL(activated()), this, SLOT(emitSelected())); } - - if (m_menuItems.size() == 1) - // Inform QML that the selected action (0) now has changed contents: - emit selectedIndexChanged(); - - emit menuItemsChanged(); - return menuItem; } -void QtMenu::emitSelected() +QtMenuItem *QtMenu::addMenuItem(const QString &text) { - QPlatformMenuItem *platformItem = qobject_cast<QPlatformMenuItem *>(sender()); - if (!platformItem) - return; - - int index = -1; - foreach (QtMenuBase *item, m_menuItems) { - ++index; - if (item->platformItem() == platformItem) - break; - } + QtMenuItem *menuItem = new QtMenuItem(this); + menuItem->setText(text); + addMenuItem(menuItem); - setSelectedIndex(index); + emit menuItemsChanged(); + return menuItem; } QString QtMenu::itemTextAt(int index) const @@ -336,13 +322,8 @@ int QtMenu::modelCount() const void QtMenu::append_menuItems(QQmlListProperty<QtMenuBase> *list, QtMenuBase *menuItem) { - QtMenu *menu = qobject_cast<QtMenu *>(list->object); - if (menu) { - menuItem->setParent(menu); - menu->m_menuItems.append(menuItem); - if (menu->m_platformMenu) - menu->m_platformMenu->insertMenuItem(menuItem->platformItem(), 0 /* append */); - } + if (QtMenu *menu = qobject_cast<QtMenu *>(list->object)) + menu->addMenuItem(menuItem); } int QtMenu::count_menuItems(QQmlListProperty<QtMenuBase> *list) |
