ColumnMenuContent.qml 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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.Private 1.0
  43. Item {
  44. id: content
  45. property Component menuItemDelegate
  46. property Component scrollIndicatorStyle
  47. property Component scrollerStyle
  48. property var itemsModel
  49. property int minWidth: 100
  50. property real maxHeight: 800
  51. readonly property bool mousePressed: hoverArea.pressed
  52. signal triggered(var item)
  53. function menuItemAt(index) {
  54. list.currentIndex = index
  55. return list.currentItem
  56. }
  57. width: Math.max(list.contentWidth, minWidth)
  58. height: Math.min(list.contentHeight, fittedMaxHeight)
  59. readonly property int currentIndex: __menu.__currentIndex
  60. property Item currentItem: null
  61. property int itemHeight: 23
  62. Component.onCompleted: {
  63. var children = list.contentItem.children
  64. for (var i = 0; i < list.count; i++) {
  65. var child = children[i]
  66. if (child.visible && child.styleData.type === MenuItemType.Item) {
  67. itemHeight = children[i].height
  68. break
  69. }
  70. }
  71. }
  72. readonly property int fittingItems: Math.floor((maxHeight - downScroller.height) / itemHeight)
  73. readonly property real fittedMaxHeight: itemHeight * fittingItems + downScroller.height
  74. readonly property bool shouldUseScrollers: scrollView.style === emptyScrollerStyle && itemsModel.length > fittingItems
  75. readonly property real upScrollerHeight: upScroller.visible ? upScroller.height : 0
  76. readonly property real downScrollerHeight: downScroller.visible ? downScroller.height : 0
  77. property var oldMousePos: undefined
  78. property var openedSubmenu: null
  79. function updateCurrentItem(mouse) {
  80. var pos = mapToItem(list.contentItem, mouse.x, mouse.y)
  81. var dx = 0
  82. var dy = 0
  83. var dist = 0
  84. if (openedSubmenu && oldMousePos !== undefined) {
  85. dx = mouse.x - oldMousePos.x
  86. dy = mouse.y - oldMousePos.y
  87. dist = Math.sqrt(dx * dx + dy * dy)
  88. }
  89. oldMousePos = mouse
  90. if (openedSubmenu && dist > 5) {
  91. var menuRect = __menu.__popupGeometry
  92. var submenuRect = openedSubmenu.__popupGeometry
  93. var angle = Math.atan2(dy, dx)
  94. var ds = 0
  95. if (submenuRect.x > menuRect.x) {
  96. ds = menuRect.width - oldMousePos.x
  97. } else {
  98. angle = Math.PI - angle
  99. ds = oldMousePos.x
  100. }
  101. var above = submenuRect.y - menuRect.y - oldMousePos.y
  102. var below = submenuRect.height - above
  103. var minAngle = Math.atan2(above, ds)
  104. var maxAngle = Math.atan2(below, ds)
  105. // This tests that the current mouse position is in
  106. // the triangle defined by the previous mouse position
  107. // and the submenu's top-left and bottom-left corners.
  108. if (minAngle < angle && angle < maxAngle) {
  109. sloppyTimer.start()
  110. return
  111. }
  112. }
  113. if (!currentItem || !currentItem.contains(Qt.point(pos.x - currentItem.x, pos.y - currentItem.y))) {
  114. if (currentItem && !hoverArea.pressed
  115. && currentItem.styleData.type === MenuItemType.Menu) {
  116. currentItem.__closeSubMenu()
  117. openedSubmenu = null
  118. }
  119. currentItem = list.itemAt(pos.x, pos.y)
  120. if (currentItem) {
  121. __menu.__currentIndex = currentItem.__menuItemIndex
  122. if (currentItem.styleData.type === MenuItemType.Menu) {
  123. showCurrentItemSubMenu(false)
  124. }
  125. } else {
  126. __menu.__currentIndex = -1
  127. }
  128. }
  129. }
  130. function showCurrentItemSubMenu(immediately) {
  131. if (!currentItem.__menuItem.__popupVisible) {
  132. currentItem.__showSubMenu(immediately)
  133. openedSubmenu = currentItem.__menuItem
  134. }
  135. }
  136. Timer {
  137. id: sloppyTimer
  138. interval: 1000
  139. // Stop timer as soon as we hover one of the submenu items
  140. property int currentIndex: openedSubmenu ? openedSubmenu.__currentIndex : -1
  141. onCurrentIndexChanged: if (currentIndex !== -1) stop()
  142. onTriggered: {
  143. if (openedSubmenu && openedSubmenu.__currentIndex === -1)
  144. updateCurrentItem(oldMousePos)
  145. }
  146. }
  147. Component {
  148. id: emptyScrollerStyle
  149. Style {
  150. padding { left: 0; right: 0; top: 0; bottom: 0 }
  151. property bool scrollToClickedPosition: false
  152. property Component frame: Item { visible: false }
  153. property Component corner: Item { visible: false }
  154. property Component __scrollbar: Item { visible: false }
  155. }
  156. }
  157. ScrollView {
  158. id: scrollView
  159. anchors {
  160. fill: parent
  161. topMargin: upScrollerHeight
  162. bottomMargin: downScrollerHeight
  163. }
  164. style: scrollerStyle || emptyScrollerStyle
  165. __wheelAreaScrollSpeed: itemHeight
  166. ListView {
  167. id: list
  168. model: itemsModel
  169. delegate: menuItemDelegate
  170. snapMode: ListView.SnapToItem
  171. boundsBehavior: Flickable.StopAtBounds
  172. highlightFollowsCurrentItem: true
  173. highlightMoveDuration: 0
  174. }
  175. }
  176. MouseArea {
  177. id: hoverArea
  178. anchors.left: scrollView.left
  179. width: scrollView.width - scrollView.__verticalScrollBar.width
  180. height: parent.height
  181. hoverEnabled: Settings.hoverEnabled
  182. acceptedButtons: Qt.AllButtons
  183. onPositionChanged: updateCurrentItem({ "x": mouse.x, "y": mouse.y })
  184. onPressed: updateCurrentItem({ "x": mouse.x, "y": mouse.y })
  185. onReleased: {
  186. if (currentItem && currentItem.__menuItem.enabled) {
  187. if (currentItem.styleData.type === MenuItemType.Menu) {
  188. showCurrentItemSubMenu(true)
  189. } else {
  190. content.triggered(currentItem)
  191. }
  192. }
  193. }
  194. onExited: {
  195. if (currentItem && !currentItem.__menuItem.__popupVisible) {
  196. currentItem = null
  197. __menu.__currentIndex = -1
  198. }
  199. }
  200. MenuContentScroller {
  201. id: upScroller
  202. direction: Qt.UpArrow
  203. visible: shouldUseScrollers && !list.atYBeginning
  204. function scrollABit() { list.contentY -= itemHeight }
  205. }
  206. MenuContentScroller {
  207. id: downScroller
  208. direction: Qt.DownArrow
  209. visible: shouldUseScrollers && !list.atYEnd
  210. function scrollABit() { list.contentY += itemHeight }
  211. }
  212. }
  213. Timer {
  214. interval: 1
  215. running: true
  216. repeat: false
  217. onTriggered: list.positionViewAtIndex(currentIndex, !scrollView.__style
  218. ? ListView.Center : ListView.Beginning)
  219. }
  220. Qml.Binding {
  221. target: scrollView.__verticalScrollBar
  222. property: "singleStep"
  223. value: itemHeight
  224. restoreMode: Binding.RestoreBinding
  225. }
  226. }