TreeViewDelegate.qml 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright (C) 2022 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.Controls.impl
  7. import QtQuick.Controls.Fusion
  8. T.TreeViewDelegate {
  9. id: control
  10. implicitWidth: leftMargin + __contentIndent + implicitContentWidth + rightPadding + rightMargin
  11. implicitHeight: Math.max(implicitBackgroundHeight, implicitContentHeight, implicitIndicatorHeight)
  12. indentation: indicator ? indicator.width : 12
  13. leftMargin: 5
  14. rightMargin: 5
  15. spacing: 5
  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. indicator: Item {
  26. readonly property real __indicatorIndent: control.leftMargin + (control.depth * control.indentation)
  27. x: !control.mirrored ? __indicatorIndent : control.width - __indicatorIndent - width
  28. y: (control.height - height) / 2
  29. implicitWidth: Math.max(arrow.implicitWidth, 20)
  30. implicitHeight: 24 // same as Button.qml
  31. property ColorImage arrow : ColorImage {
  32. parent: control.indicator
  33. x: (parent.width - width) / 2
  34. y: (parent.height - height) / 2
  35. rotation: control.expanded ? 0 : (control.mirrored ? 90 : -90)
  36. source: "qrc:/qt-project.org/imports/QtQuick/Controls/Fusion/images/arrow.png"
  37. color: control.palette.windowText
  38. defaultColor: "#353637"
  39. }
  40. }
  41. background: Rectangle {
  42. implicitHeight: 24 // same as Button.qml
  43. color: control.highlighted
  44. ? control.palette.highlight
  45. : (control.treeView.alternatingRows && control.row % 2 !== 0
  46. ? control.palette.alternateBase : control.palette.base)
  47. }
  48. contentItem: Label {
  49. text: control.model.display
  50. elide: Text.ElideRight
  51. visible: !control.editing
  52. }
  53. // The edit delegate is a separate component, and doesn't need
  54. // to follow the same strict rules that are applied to a control.
  55. // qmllint disable attached-property-reuse
  56. // qmllint disable controls-attached-property-reuse
  57. // qmllint disable controls-sanity
  58. TableView.editDelegate: FocusScope {
  59. width: parent.width
  60. height: parent.height
  61. readonly property int __role: {
  62. let model = control.treeView.model
  63. let index = control.treeView.index(row, column)
  64. let editText = model.data(index, Qt.EditRole)
  65. return editText !== undefined ? Qt.EditRole : Qt.DisplayRole
  66. }
  67. TextField {
  68. id: textField
  69. x: control.contentItem.x
  70. y: (parent.height - height) / 2
  71. width: control.contentItem.width
  72. text: control.treeView.model.data(control.treeView.index(row, column), __role)
  73. focus: true
  74. }
  75. TableView.onCommit: {
  76. let index = TableView.view.index(row, column)
  77. TableView.view.model.setData(index, textField.text, __role)
  78. }
  79. Component.onCompleted: textField.selectAll()
  80. }
  81. // qmllint enable attached-property-reuse
  82. // qmllint enable controls-attached-property-reuse
  83. // qmllint enable controls-sanity
  84. }