| 1 | /* |
| 2 | * Copyright (C) 2016 Canonical, Ltd. |
| 3 | * |
| 4 | * This program is free software: you can redistribute it and/or modify it under |
| 5 | * the terms of the GNU Lesser General Public License version 3, as published by |
| 6 | * the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY, |
| 10 | * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * Lesser General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU Lesser General Public License |
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | */ |
| 16 | |
| 17 | #ifndef EXPORTEDPLATFORMMENUBAR_H |
| 18 | #define EXPORTEDPLATFORMMENUBAR_H |
| 19 | |
| 20 | #include <qpa/qplatformmenu.h> |
| 21 | |
| 22 | // Local |
| 23 | class LomiriGMenuModelExporter; |
| 24 | class LomiriMenuRegistrar; |
| 25 | class QWindow; |
| 26 | |
| 27 | class LomiriPlatformMenuBar : public QPlatformMenuBar |
| 28 | { |
| 29 | Q_OBJECT |
| 30 | public: |
| 31 | LomiriPlatformMenuBar(); |
| 32 | ~LomiriPlatformMenuBar(); |
| 33 | |
| 34 | QString exportedPath() const; |
| 35 | |
| 36 | virtual void insertMenu(QPlatformMenu *menu, QPlatformMenu* before) override; |
| 37 | virtual void removeMenu(QPlatformMenu *menu) override; |
| 38 | virtual void syncMenu(QPlatformMenu *menu) override; |
| 39 | virtual void handleReparent(QWindow *newParentWindow) override; |
| 40 | virtual QPlatformMenu *menuForTag(quintptr tag) const override; |
| 41 | |
| 42 | const QList<QPlatformMenu*> menus() const; |
| 43 | |
| 44 | QDebug operator<<(QDebug stream); |
| 45 | |
| 46 | #if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0) |
| 47 | virtual QPlatformMenu *createMenu() const override; |
| 48 | #endif |
| 49 | |
| 50 | Q_SIGNALS: |
| 51 | void menuInserted(QPlatformMenu *menu); |
| 52 | void menuRemoved(QPlatformMenu *menu); |
| 53 | |
| 54 | void structureChanged(); |
| 55 | void ready(); |
| 56 | |
| 57 | private: |
| 58 | void setReady(bool); |
| 59 | |
| 60 | QList<QPlatformMenu*> m_menus; |
| 61 | QScopedPointer<LomiriGMenuModelExporter> m_exporter; |
| 62 | QScopedPointer<LomiriMenuRegistrar> m_registrar; |
| 63 | bool m_ready; |
| 64 | }; |
| 65 | |
| 66 | #define MENU_PROPERTY(class, name, type, defaultValue) \ |
| 67 | static type get_##name(const class *menuItem) { return menuItem->m_##name; } \ |
| 68 | type m_##name = defaultValue; |
| 69 | |
| 70 | class Q_DECL_EXPORT LomiriPlatformMenu : public QPlatformMenu |
| 71 | { |
| 72 | Q_OBJECT |
| 73 | public: |
| 74 | LomiriPlatformMenu(); |
| 75 | ~LomiriPlatformMenu(); |
| 76 | |
| 77 | virtual void insertMenuItem(QPlatformMenuItem *menuItem, QPlatformMenuItem *before) override; |
| 78 | virtual void removeMenuItem(QPlatformMenuItem *menuItem) override; |
| 79 | virtual void syncMenuItem(QPlatformMenuItem *menuItem) override; |
| 80 | virtual void syncSeparatorsCollapsible(bool enable) override; |
| 81 | |
| 82 | virtual void setTag(quintptr tag) override; |
| 83 | virtual quintptr tag() const override; |
| 84 | |
| 85 | virtual void setText(const QString &text) override; |
| 86 | virtual void setIcon(const QIcon &icon) override; |
| 87 | virtual void setEnabled(bool isEnabled) override; |
| 88 | virtual void setVisible(bool isVisible) override; |
| 89 | virtual void setMinimumWidth(int width) override; |
| 90 | virtual void setFont(const QFont &font) override; |
| 91 | |
| 92 | virtual void showPopup(const QWindow *parentWindow, const QRect &targetRect, const QPlatformMenuItem *item) override; |
| 93 | |
| 94 | virtual void dismiss() override; // Closes this and all its related menu popups |
| 95 | |
| 96 | virtual QPlatformMenuItem *menuItemAt(int position) const override; |
| 97 | virtual QPlatformMenuItem *menuItemForTag(quintptr tag) const override; |
| 98 | |
| 99 | virtual QPlatformMenuItem *createMenuItem() const override; |
| 100 | #if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0) |
| 101 | virtual QPlatformMenu *createSubMenu() const override; |
| 102 | #endif |
| 103 | |
| 104 | int id() const; |
| 105 | |
| 106 | const QList<QPlatformMenuItem*> menuItems() const; |
| 107 | |
| 108 | QDebug operator<<(QDebug stream); |
| 109 | |
| 110 | Q_SIGNALS: |
| 111 | void menuItemInserted(QPlatformMenuItem *menuItem); |
| 112 | void menuItemRemoved(QPlatformMenuItem *menuItem); |
| 113 | void structureChanged(); |
| 114 | void enabledChanged(bool); |
| 115 | |
| 116 | private: |
| 117 | MENU_PROPERTY(LomiriPlatformMenu, visible, bool, true) |
| 118 | MENU_PROPERTY(LomiriPlatformMenu, text, QString, QString()) |
| 119 | MENU_PROPERTY(LomiriPlatformMenu, enabled, bool, true) |
| 120 | MENU_PROPERTY(LomiriPlatformMenu, icon, QIcon, QIcon()) |
| 121 | |
| 122 | quintptr m_tag; |
| 123 | QList<QPlatformMenuItem*> m_menuItems; |
| 124 | const QWindow* m_parentWindow; |
| 125 | QScopedPointer<LomiriGMenuModelExporter> m_exporter; |
| 126 | QScopedPointer<LomiriMenuRegistrar> m_registrar; |
| 127 | |
| 128 | friend class LomiriGMenuModelExporter; |
| 129 | }; |
| 130 | |
| 131 | |
| 132 | class Q_DECL_EXPORT LomiriPlatformMenuItem : public QPlatformMenuItem |
| 133 | { |
| 134 | Q_OBJECT |
| 135 | public: |
| 136 | LomiriPlatformMenuItem(); |
| 137 | ~LomiriPlatformMenuItem(); |
| 138 | |
| 139 | virtual void setTag(quintptr tag) override; |
| 140 | virtual quintptr tag() const override; |
| 141 | |
| 142 | virtual void setText(const QString &text) override; |
| 143 | virtual void setIcon(const QIcon &icon) override; |
| 144 | virtual void setMenu(QPlatformMenu *menu) override; |
| 145 | virtual void setVisible(bool isVisible) override; |
| 146 | virtual void setIsSeparator(bool isSeparator) override; |
| 147 | virtual void setFont(const QFont &font) override; |
| 148 | virtual void setRole(MenuRole role) override; |
| 149 | virtual void setCheckable(bool checkable) override; |
| 150 | virtual void setChecked(bool isChecked) override; |
| 151 | virtual void setShortcut(const QKeySequence& shortcut) override; |
| 152 | virtual void setEnabled(bool enabled) override; |
| 153 | virtual void setIconSize(int size) override; |
| 154 | |
| 155 | QPlatformMenu* menu() const; |
| 156 | |
| 157 | QDebug operator<<(QDebug stream); |
| 158 | |
| 159 | Q_SIGNALS: |
| 160 | void checkedChanged(bool); |
| 161 | void enabledChanged(bool); |
| 162 | void visibleChanged(bool); |
| 163 | |
| 164 | private: |
| 165 | MENU_PROPERTY(LomiriPlatformMenuItem, separator, bool, false) |
| 166 | MENU_PROPERTY(LomiriPlatformMenuItem, visible, bool, true) |
| 167 | MENU_PROPERTY(LomiriPlatformMenuItem, text, QString, QString()) |
| 168 | MENU_PROPERTY(LomiriPlatformMenuItem, enabled, bool, true) |
| 169 | MENU_PROPERTY(LomiriPlatformMenuItem, checkable, bool, false) |
| 170 | MENU_PROPERTY(LomiriPlatformMenuItem, checked, bool, false) |
| 171 | MENU_PROPERTY(LomiriPlatformMenuItem, shortcut, QKeySequence, QKeySequence()) |
| 172 | MENU_PROPERTY(LomiriPlatformMenuItem, icon, QIcon, QIcon()) |
| 173 | MENU_PROPERTY(LomiriPlatformMenuItem, iconSize, int, 16) |
| 174 | MENU_PROPERTY(LomiriPlatformMenuItem, menu, QPlatformMenu*, nullptr) |
| 175 | |
| 176 | |
| 177 | quintptr m_tag; |
| 178 | friend class LomiriGMenuModelExporter; |
| 179 | }; |
| 180 | |
| 181 | #endif // EXPORTEDPLATFORMMENUBAR_H |