1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 BogDan Vatra <bogdan@kde.org>
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the plugins of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #include "qandroidplatformmenu.h"
41 #include "qandroidplatformmenuitem.h"
42 #include "androidjnimenu.h"
43 #include <QtCore/private/qjni_p.h>
44 
45 QT_BEGIN_NAMESPACE
46 
QAndroidPlatformMenu()47 QAndroidPlatformMenu::QAndroidPlatformMenu()
48 {
49     m_enabled = true;
50     m_isVisible = true;
51 }
52 
~QAndroidPlatformMenu()53 QAndroidPlatformMenu::~QAndroidPlatformMenu()
54 {
55     QtAndroidMenu::androidPlatformMenuDestroyed(this);
56 }
57 
insertMenuItem(QPlatformMenuItem * menuItem,QPlatformMenuItem * before)58 void QAndroidPlatformMenu::insertMenuItem(QPlatformMenuItem *menuItem, QPlatformMenuItem *before)
59 {
60     QMutexLocker lock(&m_menuItemsMutex);
61     m_menuItems.insert(std::find(m_menuItems.begin(),
62                                  m_menuItems.end(),
63                                  static_cast<QAndroidPlatformMenuItem *>(before)),
64                        static_cast<QAndroidPlatformMenuItem *>(menuItem));
65     m_menuHash.insert(m_nextMenuId++, menuItem);
66 }
67 
removeMenuItem(QPlatformMenuItem * menuItem)68 void QAndroidPlatformMenu::removeMenuItem(QPlatformMenuItem *menuItem)
69 {
70     QMutexLocker lock(&m_menuItemsMutex);
71     PlatformMenuItemsType::iterator it = std::find(m_menuItems.begin(),
72                                                    m_menuItems.end(),
73                                                    static_cast<QAndroidPlatformMenuItem *>(menuItem));
74     if (it != m_menuItems.end())
75         m_menuItems.erase(it);
76 
77     {
78         int maxId = -1;
79         QHash<int, QPlatformMenuItem *>::iterator it = m_menuHash.begin();
80         while (it != m_menuHash.end()) {
81             if (it.value() == menuItem) {
82                 it = m_menuHash.erase(it);
83             } else {
84                 maxId = qMax(maxId, it.key());
85                 ++it;
86             }
87         }
88 
89         m_nextMenuId = maxId + 1;
90     }
91 }
92 
syncMenuItem(QPlatformMenuItem * menuItem)93 void QAndroidPlatformMenu::syncMenuItem(QPlatformMenuItem *menuItem)
94 {
95     PlatformMenuItemsType::iterator it;
96     for (it = m_menuItems.begin(); it != m_menuItems.end(); ++it) {
97         if ((*it)->tag() == menuItem->tag())
98             break;
99     }
100 
101     if (it != m_menuItems.end())
102         QtAndroidMenu::syncMenu(this);
103 }
104 
syncSeparatorsCollapsible(bool enable)105 void QAndroidPlatformMenu::syncSeparatorsCollapsible(bool enable)
106 {
107     Q_UNUSED(enable)
108 }
109 
setText(const QString & text)110 void QAndroidPlatformMenu::setText(const QString &text)
111 {
112     m_text = text;
113 }
114 
text() const115 QString QAndroidPlatformMenu::text() const
116 {
117     return m_text;
118 }
119 
setIcon(const QIcon & icon)120 void QAndroidPlatformMenu::setIcon(const QIcon &icon)
121 {
122     m_icon = icon;
123 }
124 
icon() const125 QIcon QAndroidPlatformMenu::icon() const
126 {
127     return m_icon;
128 }
129 
setEnabled(bool enabled)130 void QAndroidPlatformMenu::setEnabled(bool enabled)
131 {
132     m_enabled = enabled;
133 }
134 
isEnabled() const135 bool QAndroidPlatformMenu::isEnabled() const
136 {
137     return m_enabled;
138 }
139 
setVisible(bool visible)140 void QAndroidPlatformMenu::setVisible(bool visible)
141 {
142     m_isVisible = visible;
143 }
144 
isVisible() const145 bool QAndroidPlatformMenu::isVisible() const
146 {
147     return m_isVisible;
148 }
149 
showPopup(const QWindow * parentWindow,const QRect & targetRect,const QPlatformMenuItem * item)150 void QAndroidPlatformMenu::showPopup(const QWindow *parentWindow, const QRect &targetRect, const QPlatformMenuItem *item)
151 {
152     Q_UNUSED(parentWindow);
153     Q_UNUSED(item);
154     setVisible(true);
155     QtAndroidMenu::showContextMenu(this, targetRect, QJNIEnvironmentPrivate());
156 }
157 
menuItemForTag(quintptr tag) const158 QPlatformMenuItem *QAndroidPlatformMenu::menuItemForTag(quintptr tag) const
159 {
160     for (QAndroidPlatformMenuItem *menuItem : m_menuItems) {
161         if (menuItem->tag() == tag)
162             return menuItem;
163     }
164 
165     return nullptr;
166 }
167 
menuItemAt(int position) const168 QPlatformMenuItem *QAndroidPlatformMenu::menuItemAt(int position) const
169 {
170     if (position < m_menuItems.size())
171         return m_menuItems[position];
172     return 0;
173 }
174 
menuId(QPlatformMenuItem * menu) const175 int QAndroidPlatformMenu::menuId(QPlatformMenuItem *menu) const
176 {
177     QHash<int, QPlatformMenuItem *>::const_iterator it;
178     for (it = m_menuHash.constBegin(); it != m_menuHash.constEnd(); ++it) {
179         if (it.value() == menu)
180             return it.key();
181     }
182 
183     return -1;
184 }
185 
menuItemForId(int menuId) const186 QPlatformMenuItem *QAndroidPlatformMenu::menuItemForId(int menuId) const
187 {
188     return m_menuHash.value(menuId);
189 }
190 
menuItems() const191 QAndroidPlatformMenu::PlatformMenuItemsType QAndroidPlatformMenu::menuItems() const
192 {
193     return m_menuItems;
194 }
195 
menuItemsMutex()196 QMutex *QAndroidPlatformMenu::menuItemsMutex()
197 {
198     return &m_menuItemsMutex;
199 }
200 
201 QT_END_NAMESPACE
202