1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the Qt Quick Templates 2 module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL3$
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 http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://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.LGPLv3 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.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 later as published by the Free
28** Software Foundation and appearing in the file LICENSE.GPL included in
29** the packaging of this file. Please review the following information to
30** ensure the GNU General Public License version 2.0 requirements will be
31** met: http://www.gnu.org/licenses/gpl-2.0.html.
32**
33** $QT_END_LICENSE$
34**
35****************************************************************************/
36
37#include "qquickmenubaritem_p.h"
38#include "qquickmenubaritem_p_p.h"
39#include "qquickmenubar_p.h"
40#include "qquickmenu_p.h"
41
42QT_BEGIN_NAMESPACE
43
44/*!
45 \qmltype MenuBarItem
46 \inherits AbstractButton
47//! \instantiates QQuickMenuBarItem
48 \inqmlmodule QtQuick.Controls
49 \since 5.10
50 \ingroup qtquickcontrols2-menus
51 \brief Presents a drop-down menu within a MenuBar.
52
53 MenuBarItem presents a Menu within a MenuBar. The respective drop-down menu
54 is shown when a MenuBarItem is \l triggered via keyboard, mouse, or touch.
55
56 \image qtquickcontrols2-menubar.png
57
58 MenuBarItem is used as a default \l {MenuBar::}{delegate} type for MenuBar.
59 Notice that it is not necessary to declare MenuBarItem instances by hand when
60 using MenuBar. It is sufficient to declare Menu instances as children of the
61 MenuBar and the respective items are created automatically.
62
63 \sa {Customizing MenuBar}, MenuBar, {Menu Controls}
64*/
65
66/*!
67 \qmlsignal void QtQuick.Controls::MenuBarItem::triggered()
68
69 This signal is emitted when the menu bar item is triggered by the user.
70*/
71
72void QQuickMenuBarItemPrivate::setMenuBar(QQuickMenuBar *newMenuBar)
73{
74 Q_Q(QQuickMenuBarItem);
75 if (menuBar == newMenuBar)
76 return;
77
78 menuBar = newMenuBar;
79 emit q->menuBarChanged();
80}
81
82QQuickMenuBarItem::QQuickMenuBarItem(QQuickItem *parent)
83 : QQuickAbstractButton(*(new QQuickMenuBarItemPrivate), parent)
84{
85 setFocusPolicy(Qt::NoFocus);
86 connect(sender: this, signal: &QQuickAbstractButton::clicked, receiver: this, slot: &QQuickMenuBarItem::triggered);
87}
88
89/*!
90 \qmlproperty Menu QtQuick.Controls::MenuBarItem::menuBar
91 \readonly
92
93 This property holds the menu bar that contains this item,
94 or \c null if the item is not in a menu bar.
95*/
96QQuickMenuBar *QQuickMenuBarItem::menuBar() const
97{
98 Q_D(const QQuickMenuBarItem);
99 return d->menuBar;
100}
101
102/*!
103 \qmlproperty Menu QtQuick.Controls::MenuBarItem::menu
104
105 This property holds the menu that this item presents in a
106 menu bar, or \c null if this item does not have a menu.
107*/
108QQuickMenu *QQuickMenuBarItem::menu() const
109{
110 Q_D(const QQuickMenuBarItem);
111 return d->menu;
112}
113
114void QQuickMenuBarItem::setMenu(QQuickMenu *menu)
115{
116 Q_D(QQuickMenuBarItem);
117 if (d->menu == menu)
118 return;
119
120 if (d->menu)
121 disconnect(sender: d->menu, signal: &QQuickMenu::titleChanged, receiver: this, slot: &QQuickAbstractButton::setText);
122
123 if (menu) {
124 setText(menu->title());
125 menu->setY(height());
126 menu->setParentItem(this);
127 menu->setClosePolicy(QQuickPopup::CloseOnEscape | QQuickPopup::CloseOnPressOutsideParent | QQuickPopup::CloseOnReleaseOutsideParent);
128 connect(sender: menu, signal: &QQuickMenu::titleChanged, receiver: this, slot: &QQuickAbstractButton::setText);
129 }
130
131 d->menu = menu;
132 emit menuChanged();
133}
134
135/*!
136 \qmlproperty bool QtQuick.Controls::MenuBarItem::highlighted
137
138 This property holds whether the menu bar item is highlighted by the user.
139
140 A menu bar item can be highlighted by mouse hover or keyboard navigation.
141
142 The default value is \c false.
143*/
144bool QQuickMenuBarItem::isHighlighted() const
145{
146 Q_D(const QQuickMenuBarItem);
147 return d->highlighted;
148}
149
150void QQuickMenuBarItem::setHighlighted(bool highlighted)
151{
152 Q_D(QQuickMenuBarItem);
153 if (highlighted == d->highlighted)
154 return;
155
156 d->highlighted = highlighted;
157 emit highlightedChanged();
158}
159
160void QQuickMenuBarItem::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
161{
162 Q_D(QQuickMenuBarItem);
163 QQuickAbstractButton::geometryChanged(newGeometry, oldGeometry);
164 if (d->menu)
165 d->menu->setY(newGeometry.height());
166}
167
168QFont QQuickMenuBarItem::defaultFont() const
169{
170 return QQuickTheme::font(scope: QQuickTheme::MenuBar);
171}
172
173QPalette QQuickMenuBarItem::defaultPalette() const
174{
175 return QQuickTheme::palette(scope: QQuickTheme::MenuBar);
176}
177
178#if QT_CONFIG(accessibility)
179QAccessible::Role QQuickMenuBarItem::accessibleRole() const
180{
181 return QAccessible::MenuBar;
182}
183#endif
184
185QT_END_NAMESPACE
186

source code of qtquickcontrols2/src/quicktemplates2/qquickmenubaritem.cpp