TreeView.qml 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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.4
  40. import QtQuick.Controls 1.4
  41. import QtQuick.Controls.Private 1.0
  42. import QtQuick.Controls.Styles 1.2
  43. import QtQml.Models 2.2
  44. BasicTableView {
  45. id: root
  46. property var model: null
  47. property alias rootIndex: modelAdaptor.rootIndex
  48. readonly property var currentIndex: modelAdaptor.updateCount, modelAdaptor.mapRowToModelIndex(__currentRow)
  49. property ItemSelectionModel selection: null
  50. signal activated(var index)
  51. signal clicked(var index)
  52. signal doubleClicked(var index)
  53. signal pressAndHold(var index)
  54. signal expanded(var index)
  55. signal collapsed(var index)
  56. function isExpanded(index) {
  57. if (index.valid && index.model !== model) {
  58. console.warn("TreeView.isExpanded: model and index mismatch")
  59. return false
  60. }
  61. return modelAdaptor.isExpanded(index)
  62. }
  63. function collapse(index) {
  64. if (index.valid && index.model !== model)
  65. console.warn("TreeView.collapse: model and index mismatch")
  66. else
  67. modelAdaptor.collapse(index)
  68. }
  69. function expand(index) {
  70. if (index.valid && index.model !== model)
  71. console.warn("TreeView.expand: model and index mismatch")
  72. else
  73. modelAdaptor.expand(index)
  74. }
  75. function indexAt(x, y) {
  76. var obj = root.mapToItem(__listView.contentItem, x, y)
  77. return modelAdaptor.mapRowToModelIndex(__listView.indexAt(obj.x, obj.y))
  78. }
  79. style: Settings.styleComponent(Settings.style, "TreeViewStyle.qml", root)
  80. // Internal stuff. Do not look
  81. __viewTypeName: "TreeView"
  82. __model: TreeModelAdaptor {
  83. id: modelAdaptor
  84. model: root.model
  85. // Hack to force re-evaluation of the currentIndex binding
  86. property int updateCount: 0
  87. onModelReset: updateCount++
  88. onRowsInserted: updateCount++
  89. onRowsRemoved: updateCount++
  90. onExpanded: root.expanded(index)
  91. onCollapsed: root.collapsed(index)
  92. }
  93. __itemDelegateLoader: TreeViewItemDelegateLoader {
  94. __style: root.__style
  95. __itemDelegate: root.itemDelegate
  96. __mouseArea: mouseArea
  97. __treeModel: modelAdaptor
  98. }
  99. onSelectionModeChanged: if (!!selection) selection.clear()
  100. __mouseArea: MouseArea {
  101. id: mouseArea
  102. parent: __listView
  103. width: __listView.width
  104. height: __listView.height
  105. z: -1
  106. propagateComposedEvents: true
  107. focus: true
  108. // If there is not a touchscreen, keep the flickable from eating our mouse drags.
  109. // If there is a touchscreen, flicking is possible, but selection can be done only by tapping, not by dragging.
  110. preventStealing: !Settings.hasTouchScreen
  111. property var clickedIndex: undefined
  112. property var pressedIndex: undefined
  113. property bool selectOnRelease: false
  114. property int pressedColumn: -1
  115. readonly property alias currentRow: root.__currentRow
  116. readonly property alias currentIndex: root.currentIndex
  117. // Handle vertical scrolling whem dragging mouse outside boundaries
  118. property int autoScroll: 0 // 0 -> do nothing; 1 -> increment; 2 -> decrement
  119. property bool shiftPressed: false // forward shift key state to the autoscroll timer
  120. Timer {
  121. running: mouseArea.autoScroll !== 0 && __verticalScrollBar.visible
  122. interval: 20
  123. repeat: true
  124. onTriggered: {
  125. var oldPressedIndex = mouseArea.pressedIndex
  126. var row
  127. if (mouseArea.autoScroll === 1) {
  128. __listView.incrementCurrentIndexBlocking();
  129. row = __listView.indexAt(0, __listView.height + __listView.contentY)
  130. if (row === -1)
  131. row = __listView.count - 1
  132. } else {
  133. __listView.decrementCurrentIndexBlocking();
  134. row = __listView.indexAt(0, __listView.contentY)
  135. }
  136. var index = modelAdaptor.mapRowToModelIndex(row)
  137. if (index !== oldPressedIndex) {
  138. mouseArea.pressedIndex = index
  139. var modifiers = mouseArea.shiftPressed ? Qt.ShiftModifier : Qt.NoModifier
  140. mouseArea.mouseSelect(index, modifiers, true /* drag */)
  141. }
  142. }
  143. }
  144. function mouseSelect(modelIndex, modifiers, drag) {
  145. if (!selection) {
  146. maybeWarnAboutSelectionMode()
  147. return
  148. }
  149. if (selectionMode) {
  150. selection.setCurrentIndex(modelIndex, ItemSelectionModel.NoUpdate)
  151. if (selectionMode === SelectionMode.SingleSelection) {
  152. selection.select(modelIndex, ItemSelectionModel.ClearAndSelect)
  153. } else {
  154. var selectRowRange = (drag && (selectionMode === SelectionMode.MultiSelection
  155. || (selectionMode === SelectionMode.ExtendedSelection
  156. && modifiers & Qt.ControlModifier)))
  157. || modifiers & Qt.ShiftModifier
  158. var itemSelection = !selectRowRange || clickedIndex === modelIndex ? modelIndex
  159. : modelAdaptor.selectionForRowRange(clickedIndex, modelIndex)
  160. if (selectionMode === SelectionMode.MultiSelection
  161. || selectionMode === SelectionMode.ExtendedSelection && modifiers & Qt.ControlModifier) {
  162. if (drag)
  163. selection.select(itemSelection, ItemSelectionModel.ToggleCurrent)
  164. else
  165. selection.select(modelIndex, ItemSelectionModel.Toggle)
  166. } else if (modifiers & Qt.ShiftModifier) {
  167. selection.select(itemSelection, ItemSelectionModel.SelectCurrent)
  168. } else {
  169. clickedIndex = modelIndex // Needed only when drag is true
  170. selection.select(modelIndex, ItemSelectionModel.ClearAndSelect)
  171. }
  172. }
  173. }
  174. }
  175. function keySelect(keyModifiers) {
  176. if (selectionMode) {
  177. if (!keyModifiers)
  178. clickedIndex = currentIndex
  179. if (!(keyModifiers & Qt.ControlModifier))
  180. mouseSelect(currentIndex, keyModifiers, keyModifiers & Qt.ShiftModifier)
  181. }
  182. }
  183. function selected(row) {
  184. if (selectionMode === SelectionMode.NoSelection)
  185. return false
  186. var modelIndex = null
  187. if (!!selection) {
  188. modelIndex = modelAdaptor.mapRowToModelIndex(row)
  189. if (modelIndex.valid) {
  190. if (selectionMode === SelectionMode.SingleSelection)
  191. return selection.currentIndex === modelIndex
  192. return selection.hasSelection && selection.isSelected(modelIndex)
  193. } else {
  194. return false
  195. }
  196. }
  197. return row === currentRow
  198. && (selectionMode === SelectionMode.SingleSelection
  199. || (selectionMode > SelectionMode.SingleSelection && !selection))
  200. }
  201. function branchDecorationContains(x, y) {
  202. var clickedItem = __listView.itemAt(0, y + __listView.contentY)
  203. if (!(clickedItem && clickedItem.rowItem))
  204. return false
  205. var branchDecoration = clickedItem.rowItem.branchDecoration
  206. if (!branchDecoration)
  207. return false
  208. var pos = mapToItem(branchDecoration, x, y)
  209. return branchDecoration.contains(Qt.point(pos.x, pos.y))
  210. }
  211. function maybeWarnAboutSelectionMode() {
  212. if (selectionMode > SelectionMode.SingleSelection)
  213. console.warn("TreeView: Non-single selection is not supported without an ItemSelectionModel.")
  214. }
  215. onPressed: {
  216. var pressedRow = __listView.indexAt(0, mouseY + __listView.contentY)
  217. pressedIndex = modelAdaptor.mapRowToModelIndex(pressedRow)
  218. pressedColumn = __listView.columnAt(mouseX)
  219. selectOnRelease = false
  220. __listView.forceActiveFocus()
  221. if (pressedRow === -1
  222. || Settings.hasTouchScreen
  223. || branchDecorationContains(mouse.x, mouse.y)) {
  224. return
  225. }
  226. if (selectionMode === SelectionMode.ExtendedSelection
  227. && selection.isSelected(pressedIndex)) {
  228. selectOnRelease = true
  229. return
  230. }
  231. __listView.currentIndex = pressedRow
  232. if (!clickedIndex)
  233. clickedIndex = pressedIndex
  234. mouseSelect(pressedIndex, mouse.modifiers, false)
  235. if (!mouse.modifiers)
  236. clickedIndex = pressedIndex
  237. }
  238. onReleased: {
  239. if (selectOnRelease) {
  240. var releasedRow = __listView.indexAt(0, mouseY + __listView.contentY)
  241. var releasedIndex = modelAdaptor.mapRowToModelIndex(releasedRow)
  242. if (releasedRow >= 0 && releasedIndex === pressedIndex)
  243. mouseSelect(pressedIndex, mouse.modifiers, false)
  244. }
  245. pressedIndex = undefined
  246. pressedColumn = -1
  247. autoScroll = 0
  248. selectOnRelease = false
  249. }
  250. onPositionChanged: {
  251. // NOTE: Testing for pressed is not technically needed, at least
  252. // until we decide to support tooltips or some other hover feature
  253. if (mouseY > __listView.height && pressed) {
  254. if (autoScroll === 1) return;
  255. autoScroll = 1
  256. } else if (mouseY < 0 && pressed) {
  257. if (autoScroll === 2) return;
  258. autoScroll = 2
  259. } else {
  260. autoScroll = 0
  261. }
  262. if (pressed && containsMouse) {
  263. var oldPressedIndex = pressedIndex
  264. var pressedRow = __listView.indexAt(0, mouseY + __listView.contentY)
  265. pressedIndex = modelAdaptor.mapRowToModelIndex(pressedRow)
  266. pressedColumn = __listView.columnAt(mouseX)
  267. if (pressedRow > -1 && oldPressedIndex !== pressedIndex) {
  268. __listView.currentIndex = pressedRow
  269. mouseSelect(pressedIndex, mouse.modifiers, true /* drag */)
  270. }
  271. }
  272. }
  273. onExited: {
  274. pressedIndex = undefined
  275. pressedColumn = -1
  276. selectOnRelease = false
  277. }
  278. onCanceled: {
  279. pressedIndex = undefined
  280. pressedColumn = -1
  281. autoScroll = 0
  282. selectOnRelease = false
  283. }
  284. onClicked: {
  285. var clickIndex = __listView.indexAt(0, mouseY + __listView.contentY)
  286. if (clickIndex > -1) {
  287. var modelIndex = modelAdaptor.mapRowToModelIndex(clickIndex)
  288. if (branchDecorationContains(mouse.x, mouse.y)) {
  289. if (modelAdaptor.isExpanded(modelIndex))
  290. modelAdaptor.collapse(modelIndex)
  291. else
  292. modelAdaptor.expand(modelIndex)
  293. } else {
  294. if (Settings.hasTouchScreen) {
  295. // compensate for the fact that onPressed didn't select on press: do it here instead
  296. pressedIndex = modelAdaptor.mapRowToModelIndex(clickIndex)
  297. pressedColumn = __listView.columnAt(mouseX)
  298. selectOnRelease = false
  299. __listView.forceActiveFocus()
  300. __listView.currentIndex = clickIndex
  301. if (!clickedIndex)
  302. clickedIndex = pressedIndex
  303. mouseSelect(pressedIndex, mouse.modifiers, false)
  304. if (!mouse.modifiers)
  305. clickedIndex = pressedIndex
  306. }
  307. if (root.__activateItemOnSingleClick && !mouse.modifiers)
  308. root.activated(modelIndex)
  309. }
  310. root.clicked(modelIndex)
  311. }
  312. }
  313. onDoubleClicked: {
  314. var clickIndex = __listView.indexAt(0, mouseY + __listView.contentY)
  315. if (clickIndex > -1) {
  316. var modelIndex = modelAdaptor.mapRowToModelIndex(clickIndex)
  317. if (!root.__activateItemOnSingleClick)
  318. root.activated(modelIndex)
  319. root.doubleClicked(modelIndex)
  320. }
  321. }
  322. onPressAndHold: {
  323. var pressIndex = __listView.indexAt(0, mouseY + __listView.contentY)
  324. if (pressIndex > -1) {
  325. var modelIndex = modelAdaptor.mapRowToModelIndex(pressIndex)
  326. root.pressAndHold(modelIndex)
  327. }
  328. }
  329. Keys.forwardTo: [root]
  330. Keys.onUpPressed: {
  331. event.accepted = __listView.decrementCurrentIndexBlocking()
  332. keySelect(event.modifiers)
  333. }
  334. Keys.onDownPressed: {
  335. event.accepted = __listView.incrementCurrentIndexBlocking()
  336. keySelect(event.modifiers)
  337. }
  338. Keys.onRightPressed: {
  339. if (root.currentIndex.valid)
  340. root.expand(currentIndex)
  341. else
  342. event.accepted = false
  343. }
  344. Keys.onLeftPressed: {
  345. if (root.currentIndex.valid)
  346. root.collapse(currentIndex)
  347. else
  348. event.accepted = false
  349. }
  350. Keys.onReturnPressed: {
  351. if (root.currentIndex.valid)
  352. root.activated(currentIndex)
  353. else
  354. event.accepted = false
  355. }
  356. Keys.onPressed: {
  357. __listView.scrollIfNeeded(event.key)
  358. if (event.key === Qt.Key_A && event.modifiers & Qt.ControlModifier
  359. && !!selection && selectionMode > SelectionMode.SingleSelection) {
  360. var sel = modelAdaptor.selectionForRowRange(0, __listView.count - 1)
  361. selection.select(sel, ItemSelectionModel.SelectCurrent)
  362. } else if (event.key === Qt.Key_Shift) {
  363. shiftPressed = true
  364. }
  365. }
  366. Keys.onReleased: {
  367. if (event.key === Qt.Key_Shift)
  368. shiftPressed = false
  369. }
  370. }
  371. }