| 1 | // Copyright (C) 2017 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qwidgetplatformmenu_p.h" |
| 5 | #include "qwidgetplatformmenuitem_p.h" |
| 6 | |
| 7 | #include <QtGui/qaction.h> |
| 8 | #include <QtGui/qwindow.h> |
| 9 | #include <QtGui/private/qhighdpiscaling_p.h> |
| 10 | #include <QtWidgets/qmenu.h> |
| 11 | |
| 12 | QT_BEGIN_NAMESPACE |
| 13 | |
| 14 | QWidgetPlatformMenu::(QObject *parent) |
| 15 | : m_menu(new QMenu) |
| 16 | { |
| 17 | setParent(parent); |
| 18 | |
| 19 | connect(sender: m_menu.data(), signal: &QMenu::aboutToShow, context: this, slot: &QPlatformMenu::aboutToShow); |
| 20 | connect(sender: m_menu.data(), signal: &QMenu::aboutToHide, context: this, slot: &QPlatformMenu::aboutToHide); |
| 21 | } |
| 22 | |
| 23 | QWidgetPlatformMenu::() |
| 24 | { |
| 25 | } |
| 26 | |
| 27 | QMenu *QWidgetPlatformMenu::() const |
| 28 | { |
| 29 | return m_menu.data(); |
| 30 | } |
| 31 | |
| 32 | void QWidgetPlatformMenu::(QPlatformMenuItem *item, QPlatformMenuItem *before) |
| 33 | { |
| 34 | QWidgetPlatformMenuItem *widgetItem = qobject_cast<QWidgetPlatformMenuItem *>(object: item); |
| 35 | if (!widgetItem) |
| 36 | return; |
| 37 | |
| 38 | QWidgetPlatformMenuItem *widgetBefore = qobject_cast<QWidgetPlatformMenuItem *>(object: before); |
| 39 | m_menu->insertAction(before: widgetBefore ? widgetBefore->action() : nullptr, action: widgetItem->action()); |
| 40 | int index = m_items.indexOf(t: widgetBefore); |
| 41 | if (index < 0) |
| 42 | index = m_items.size(); |
| 43 | m_items.insert(i: index, t: widgetItem); |
| 44 | } |
| 45 | |
| 46 | void QWidgetPlatformMenu::(QPlatformMenuItem *item) |
| 47 | { |
| 48 | QWidgetPlatformMenuItem *widgetItem = qobject_cast<QWidgetPlatformMenuItem *>(object: item); |
| 49 | if (!widgetItem) |
| 50 | return; |
| 51 | |
| 52 | m_items.removeOne(t: widgetItem); |
| 53 | m_menu->removeAction(action: widgetItem->action()); |
| 54 | } |
| 55 | |
| 56 | void QWidgetPlatformMenu::(QPlatformMenuItem *item) |
| 57 | { |
| 58 | Q_UNUSED(item); |
| 59 | } |
| 60 | |
| 61 | void QWidgetPlatformMenu::(bool enable) |
| 62 | { |
| 63 | m_menu->setSeparatorsCollapsible(enable); |
| 64 | } |
| 65 | |
| 66 | void QWidgetPlatformMenu::(const QString &text) |
| 67 | { |
| 68 | m_menu->setTitle(text); |
| 69 | } |
| 70 | |
| 71 | void QWidgetPlatformMenu::(const QIcon &icon) |
| 72 | { |
| 73 | m_menu->setIcon(icon); |
| 74 | } |
| 75 | |
| 76 | void QWidgetPlatformMenu::(bool enabled) |
| 77 | { |
| 78 | m_menu->menuAction()->setEnabled(enabled); |
| 79 | } |
| 80 | |
| 81 | bool QWidgetPlatformMenu::() const |
| 82 | { |
| 83 | return m_menu->menuAction()->isEnabled(); |
| 84 | } |
| 85 | |
| 86 | void QWidgetPlatformMenu::(bool visible) |
| 87 | { |
| 88 | m_menu->menuAction()->setVisible(visible); |
| 89 | } |
| 90 | |
| 91 | void QWidgetPlatformMenu::(int width) |
| 92 | { |
| 93 | if (width > 0) |
| 94 | m_menu->setMinimumWidth(width); |
| 95 | } |
| 96 | |
| 97 | void QWidgetPlatformMenu::(const QFont &font) |
| 98 | { |
| 99 | m_menu->setFont(font); |
| 100 | } |
| 101 | |
| 102 | void QWidgetPlatformMenu::(MenuType type) |
| 103 | { |
| 104 | Q_UNUSED(type); |
| 105 | } |
| 106 | |
| 107 | void QWidgetPlatformMenu::(const QWindow *window, const QRect &targetRect, const QPlatformMenuItem *item) |
| 108 | { |
| 109 | m_menu->createWinId(); |
| 110 | QWindow *handle = m_menu->windowHandle(); |
| 111 | Q_ASSERT(handle); |
| 112 | handle->setTransientParent(const_cast<QWindow *>(window)); |
| 113 | |
| 114 | QPoint targetPos = targetRect.bottomLeft(); |
| 115 | if (window) |
| 116 | targetPos = window->mapToGlobal(pos: QHighDpi::fromNativeLocalPosition(value: targetPos, context: window)); |
| 117 | |
| 118 | const QWidgetPlatformMenuItem *widgetItem = qobject_cast<const QWidgetPlatformMenuItem *>(object: item); |
| 119 | m_menu->popup(pos: targetPos, at: widgetItem ? widgetItem->action() : nullptr); |
| 120 | } |
| 121 | |
| 122 | void QWidgetPlatformMenu::() |
| 123 | { |
| 124 | m_menu->close(); |
| 125 | } |
| 126 | |
| 127 | QPlatformMenuItem *QWidgetPlatformMenu::(int position) const |
| 128 | { |
| 129 | return m_items.value(i: position); |
| 130 | } |
| 131 | |
| 132 | QPlatformMenuItem *QWidgetPlatformMenu::(quintptr tag) const |
| 133 | { |
| 134 | for (QWidgetPlatformMenuItem *item : m_items) { |
| 135 | if (item->tag() == tag) |
| 136 | return item; |
| 137 | } |
| 138 | return nullptr; |
| 139 | } |
| 140 | |
| 141 | QPlatformMenuItem *QWidgetPlatformMenu::() const |
| 142 | { |
| 143 | return new QWidgetPlatformMenuItem; |
| 144 | } |
| 145 | |
| 146 | QPlatformMenu *QWidgetPlatformMenu::() const |
| 147 | { |
| 148 | return new QWidgetPlatformMenu; |
| 149 | } |
| 150 | |
| 151 | QT_END_NAMESPACE |
| 152 | |
| 153 | #include "moc_qwidgetplatformmenu_p.cpp" |
| 154 | |