MenuContentItem.qml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 QtQml 2.14 as Qml
  40. import QtQuick 2.2
  41. import QtQuick.Controls 1.2
  42. import QtQuick.Controls.Styles 1.1
  43. import QtQuick.Controls.Private 1.0
  44. Loader {
  45. id: menuFrameLoader
  46. property var __menu
  47. Accessible.role: Accessible.PopupMenu
  48. visible: status === Loader.Ready
  49. width: content.width + (d.style ? d.style.padding.left + d.style.padding.right : 0)
  50. height: content.height + (d.style ? d.style.padding.top + d.style.padding.bottom : 0)
  51. Loader {
  52. id: styleLoader
  53. active: !__menu.isNative
  54. sourceComponent: __menu.style
  55. property alias __control: menuFrameLoader
  56. onStatusChanged: {
  57. if (status === Loader.Error)
  58. console.error("Failed to load Style for", __menu)
  59. }
  60. }
  61. sourceComponent: d.style ? d.style.frame : undefined
  62. QtObject {
  63. id: d
  64. property var mnemonicsMap: ({})
  65. readonly property Style style: styleLoader.item
  66. readonly property Component menuItemPanel: style ? style.menuItemPanel : null
  67. function canBeHovered(index) {
  68. var item = content.menuItemAt(index)
  69. if (item && item.visible && item.styleData.type !== MenuItemType.Separator && item.styleData.enabled) {
  70. __menu.__currentIndex = index
  71. return true
  72. }
  73. return false
  74. }
  75. function triggerCurrent() {
  76. var item = content.menuItemAt(__menu.__currentIndex)
  77. if (item)
  78. triggerAndDismiss(item)
  79. }
  80. function triggerAndDismiss(item) {
  81. if (!item)
  82. return;
  83. if (item.styleData.type === MenuItemType.Separator)
  84. __menu.__dismissAndDestroy()
  85. else if (item.styleData.type === MenuItemType.Item)
  86. item.__menuItem.trigger()
  87. }
  88. }
  89. focus: true
  90. Keys.onPressed: {
  91. var item = null
  92. if (!(event.modifiers & Qt.AltModifier)
  93. && (item = d.mnemonicsMap[event.text.toUpperCase()])) {
  94. if (item.styleData.type === MenuItemType.Menu) {
  95. __menu.__currentIndex = item.__menuItemIndex
  96. item.__showSubMenu(true)
  97. item.__menuItem.__currentIndex = 0
  98. } else {
  99. d.triggerAndDismiss(item)
  100. }
  101. event.accepted = true
  102. } else {
  103. event.accepted = false
  104. }
  105. }
  106. Keys.onEscapePressed: __menu.__dismissAndDestroy()
  107. Keys.onDownPressed: {
  108. if (__menu.__currentIndex < 0)
  109. __menu.__currentIndex = -1
  110. for (var i = __menu.__currentIndex + 1;
  111. i < __menu.items.length && !d.canBeHovered(i); i++)
  112. ;
  113. event.accepted = true
  114. }
  115. Keys.onUpPressed: {
  116. for (var i = __menu.__currentIndex - 1;
  117. i >= 0 && !d.canBeHovered(i); i--)
  118. ;
  119. event.accepted = true
  120. }
  121. Keys.onLeftPressed: {
  122. if ((event.accepted = __menu.__parentMenu.hasOwnProperty("title")))
  123. __menu.__closeAndDestroy()
  124. }
  125. Keys.onRightPressed: {
  126. var item = content.menuItemAt(__menu.__currentIndex)
  127. if (item && item.styleData.type === MenuItemType.Menu
  128. && !item.__menuItem.__popupVisible) {
  129. item.__showSubMenu(true)
  130. item.__menuItem.__currentIndex = 0
  131. event.accepted = true
  132. } else {
  133. event.accepted = false
  134. }
  135. }
  136. Keys.onSpacePressed: d.triggerCurrent()
  137. Keys.onReturnPressed: d.triggerCurrent()
  138. Keys.onEnterPressed: d.triggerCurrent()
  139. Qml.Binding {
  140. // Make sure the styled frame is in the background
  141. target: item
  142. property: "z"
  143. value: content.z - 1
  144. restoreMode: Binding.RestoreBinding
  145. }
  146. ColumnMenuContent {
  147. id: content
  148. x: d.style ? d.style.padding.left : 0
  149. y: d.style ? d.style.padding.top : 0
  150. menuItemDelegate: menuItemComponent
  151. scrollIndicatorStyle: d.style && d.style.scrollIndicator || null
  152. scrollerStyle: d.style && d.style.__scrollerStyle
  153. itemsModel: __menu.items
  154. minWidth: __menu.__minimumWidth
  155. maxHeight: d.style ? d.style.__maxPopupHeight : 0
  156. onTriggered: d.triggerAndDismiss(item)
  157. }
  158. Component {
  159. id: menuItemComponent
  160. Loader {
  161. id: menuItemLoader
  162. Accessible.role: opts.type === MenuItemType.Item || opts.type === MenuItemType.Menu ?
  163. Accessible.MenuItem : Accessible.NoRole
  164. Accessible.name: StyleHelpers.removeMnemonics(opts.text)
  165. Accessible.checkable: opts.checkable
  166. Accessible.checked: opts.checked
  167. Accessible.onPressAction: {
  168. if (opts.type === MenuItemType.Item) {
  169. d.triggerAndDismiss(menuItemLoader)
  170. } else if (opts.type === MenuItemType.Menu) {
  171. __showSubMenu(true /*immediately*/)
  172. }
  173. }
  174. property QtObject styleData: QtObject {
  175. id: opts
  176. readonly property int index: __menuItemIndex
  177. readonly property int type: __menuItem ? __menuItem.type : -1
  178. readonly property bool selected: type !== MenuItemType.Separator && __menu.__currentIndex === index
  179. readonly property bool pressed: type !== MenuItemType.Separator && __menu.__currentIndex === index
  180. && content.mousePressed // TODO Add key pressed condition once we get delayed menu closing
  181. readonly property string text: type === MenuItemType.Menu ? __menuItem.title :
  182. type !== MenuItemType.Separator ? __menuItem.text : ""
  183. readonly property bool underlineMnemonic: __menu.__contentItem.altPressed
  184. readonly property string shortcut: !!__menuItem && __menuItem["shortcut"] || ""
  185. readonly property var iconSource: !!__menuItem && __menuItem["iconSource"] || undefined
  186. readonly property bool enabled: type !== MenuItemType.Separator && !!__menuItem && __menuItem.enabled
  187. readonly property bool checked: !!__menuItem && !!__menuItem["checked"]
  188. readonly property bool checkable: !!__menuItem && !!__menuItem["checkable"]
  189. readonly property bool exclusive: !!__menuItem && !!__menuItem["exclusiveGroup"]
  190. readonly property int scrollerDirection: Qt.NoArrow
  191. }
  192. readonly property var __menuItem: modelData
  193. readonly property int __menuItemIndex: index
  194. sourceComponent: d.menuItemPanel
  195. enabled: visible && opts.enabled
  196. visible: !!__menuItem && __menuItem.visible
  197. active: visible
  198. function __showSubMenu(immediately) {
  199. if (!__menuItem.enabled)
  200. return;
  201. if (immediately) {
  202. if (__menu.__currentIndex === __menuItemIndex) {
  203. if (__menuItem.__usingDefaultStyle)
  204. __menuItem.style = __menu.style
  205. __menuItem.__popup(Qt.rect(menuFrameLoader.width - (d.style.submenuOverlap + d.style.padding.right), -d.style.padding.top, 0, 0), -1)
  206. }
  207. } else {
  208. openMenuTimer.start()
  209. }
  210. }
  211. Timer {
  212. id: openMenuTimer
  213. interval: d.style.submenuPopupDelay
  214. onTriggered: menuItemLoader.__showSubMenu(true)
  215. }
  216. function __closeSubMenu() {
  217. if (openMenuTimer.running)
  218. openMenuTimer.stop()
  219. else if (__menuItem.__popupVisible)
  220. closeMenuTimer.start()
  221. }
  222. Timer {
  223. id: closeMenuTimer
  224. interval: 1
  225. onTriggered: {
  226. if (__menu.__currentIndex !== __menuItemIndex)
  227. __menuItem.__closeAndDestroy()
  228. }
  229. }
  230. onLoaded: {
  231. __menuItem.__visualItem = menuItemLoader
  232. if (content.width < item.implicitWidth)
  233. content.width = item.implicitWidth
  234. var title = opts.text
  235. var ampersandPos = title.indexOf("&")
  236. if (ampersandPos !== -1)
  237. d.mnemonicsMap[title[ampersandPos + 1].toUpperCase()] = menuItemLoader
  238. }
  239. Qml.Binding {
  240. target: menuItemLoader.item
  241. property: "width"
  242. property alias menuItem: menuItemLoader.item
  243. value: menuItem ? Math.max(__menu.__minimumWidth, content.width) - 2 * menuItem.x : 0
  244. restoreMode: Binding.RestoreBinding
  245. }
  246. }
  247. }
  248. }