Menu.qml 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the Qt Quick Controls module 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. import QtQuick 2.2
  40. import QtQuick.Controls 1.2
  41. import QtQuick.Controls.Styles 1.1
  42. import QtQuick.Controls.Private 1.0
  43. /*!
  44. \qmltype Menu
  45. \inqmlmodule QtQuick.Controls
  46. \since 5.1
  47. \ingroup menus
  48. \ingroup controls
  49. \brief Provides a menu component for use as a context menu, popup menu, or
  50. as part of a menu bar.
  51. \image menu.png
  52. \code
  53. Menu {
  54. title: "Edit"
  55. MenuItem {
  56. text: "Cut"
  57. shortcut: "Ctrl+X"
  58. onTriggered: ...
  59. }
  60. MenuItem {
  61. text: "Copy"
  62. shortcut: "Ctrl+C"
  63. onTriggered: ...
  64. }
  65. MenuItem {
  66. text: "Paste"
  67. shortcut: "Ctrl+V"
  68. onTriggered: ...
  69. }
  70. MenuSeparator { }
  71. Menu {
  72. title: "More Stuff"
  73. MenuItem {
  74. text: "Do Nothing"
  75. }
  76. }
  77. }
  78. \endcode
  79. The main uses for menus:
  80. \list
  81. \li
  82. as a \e top-level menu in a \l MenuBar
  83. \li
  84. as a \e submenu inside another menu
  85. \li
  86. as a standalone or \e context menu
  87. \endlist
  88. Note that some properties, such as \c enabled, \c text, or \c iconSource,
  89. only make sense in a particular use case of the menu.
  90. \sa MenuBar, MenuItem, MenuSeparator
  91. */
  92. MenuPrivate {
  93. id: root
  94. /*! \internal
  95. \omit
  96. Documented in qqquickmenu.cpp.
  97. \endomit
  98. */
  99. function addMenu(title) {
  100. return root.insertMenu(items.length, title)
  101. }
  102. /*! \internal
  103. \omit
  104. Documented in qquickmenu.cpp.
  105. \endomit
  106. */
  107. function insertMenu(index, title) {
  108. if (!__selfComponent)
  109. __selfComponent = Qt.createComponent("Menu.qml", root)
  110. var submenu = __selfComponent.createObject(__selfComponent, { "title": title })
  111. root.insertItem(index, submenu)
  112. return submenu
  113. }
  114. /*! \internal */
  115. property Component __selfComponent: null
  116. /*! \qmlproperty Component Menu::style
  117. \since QtQuick.Controls.Styles 1.2
  118. The style Component for this control.
  119. \sa {MenuStyle}
  120. */
  121. property Component style
  122. Component.onCompleted: {
  123. if (!style) {
  124. __usingDefaultStyle = true
  125. style = Qt.binding(function() { return Settings.styleComponent(Settings.style, "MenuStyle.qml", root) })
  126. }
  127. }
  128. /*! \internal */
  129. property bool __usingDefaultStyle: false
  130. /*! \internal */
  131. property var __parentContentItem: __parentMenu ? __parentMenu.__contentItem : null
  132. /*! \internal */
  133. property int __currentIndex: -1
  134. /*! \internal */
  135. onAboutToHide: __currentIndex = -1
  136. on__MenuPopupDestroyed: contentLoader.active = false
  137. onPopupVisibleChanged: {
  138. if (__popupVisible)
  139. contentLoader.active = true
  140. }
  141. /*! \internal */
  142. __contentItem: Loader {
  143. id: contentLoader
  144. Component {
  145. id: menuContent
  146. MenuContentItem {
  147. __menu: root
  148. }
  149. }
  150. sourceComponent: root.__isNative ? null : menuContent
  151. active: false
  152. focus: true
  153. Keys.forwardTo: item ? [item, root.__parentContentItem] : []
  154. property bool altPressed: root.__parentContentItem ? root.__parentContentItem.altPressed : false
  155. }
  156. }