DefaultTreeViewDelegate.qml 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright (C) 2021 The Qt Company Ltd.
  2. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
  3. // Qt-Security score:significant reason:default
  4. import QtQuick
  5. import QtQuick.Templates as T
  6. import QtQuick.NativeStyle as NativeStyle
  7. import QtQuick.Controls
  8. T.TreeViewDelegate {
  9. id: control
  10. implicitWidth: leftMargin + __contentIndent + implicitContentWidth + rightPadding + rightMargin
  11. implicitHeight: Math.max(indicator ? indicator.height : 0, implicitContentHeight) * 1.25
  12. indentation: indicator ? indicator.width : 12
  13. leftMargin: 4
  14. rightMargin: 4
  15. spacing: 4
  16. topPadding: contentItem ? (height - contentItem.implicitHeight) / 2 : 0
  17. leftPadding: !mirrored ? leftMargin + __contentIndent : width - leftMargin - __contentIndent - implicitContentWidth
  18. highlighted: control.selected || control.current
  19. || ((control.treeView.selectionBehavior === TableView.SelectRows
  20. || control.treeView.selectionBehavior === TableView.SelectionDisabled)
  21. && control.row === control.treeView.currentRow)
  22. required property int row
  23. required property var model
  24. readonly property real __contentIndent: !isTreeNode ? 0 : (depth * indentation) + (indicator ? indicator.width + spacing : 0)
  25. readonly property bool __notCustomizable: true
  26. indicator: Item {
  27. // Create an area that is big enough for the user to
  28. // click on, since the image is a bit small.
  29. readonly property real __indicatorIndent: control.leftMargin + (control.depth * control.indentation)
  30. x: !control.mirrored ? __indicatorIndent : control.width - __indicatorIndent - width
  31. y: (control.height - height) / 2
  32. width: 16
  33. height: 16
  34. NativeStyle.TreeIndicator {
  35. x: (parent.width - width) / 2
  36. y: (parent.height - height) / 2
  37. control: control
  38. useNinePatchImage: false
  39. }
  40. readonly property bool __ignoreNotCustomizable: true
  41. }
  42. background: Rectangle {
  43. color: control.highlighted ? control.palette.highlight
  44. : (control.treeView.alternatingRows && control.row % 2 !== 0
  45. ? control.palette.alternateBase : control.palette.base)
  46. readonly property bool __ignoreNotCustomizable: true
  47. }
  48. contentItem: Label {
  49. clip: false
  50. text: control.model.display
  51. elide: Text.ElideRight
  52. color: control.highlighted ? control.palette.highlightedText : control.palette.buttonText
  53. visible: !control.editing
  54. readonly property bool __ignoreNotCustomizable: true
  55. }
  56. // The edit delegate is a separate component, and doesn't need
  57. // to follow the same strict rules that are applied to a control.
  58. // qmllint disable attached-property-reuse
  59. // qmllint disable controls-attached-property-reuse
  60. // qmllint disable controls-sanity
  61. TableView.editDelegate: FocusScope {
  62. width: parent.width
  63. height: parent.height
  64. readonly property int __role: {
  65. let model = control.treeView.model
  66. let index = control.treeView.index(row, column)
  67. let editText = model.data(index, Qt.EditRole)
  68. return editText !== undefined ? Qt.EditRole : Qt.DisplayRole
  69. }
  70. TextField {
  71. id: textField
  72. x: control.contentItem.x
  73. y: (parent.height - height) / 2
  74. width: control.contentItem.width
  75. text: control.treeView.model.data(control.treeView.index(row, column), __role)
  76. focus: true
  77. }
  78. TableView.onCommit: {
  79. let index = TableView.view.index(row, column)
  80. TableView.view.model.setData(index, textField.text, __role)
  81. }
  82. Component.onCompleted: textField.selectAll()
  83. }
  84. // qmllint enable attached-property-reuse
  85. // qmllint enable controls-attached-property-reuse
  86. // qmllint enable controls-sanity
  87. }