TreeViewDelegate.qml 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.Material
  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: 16
  14. rightMargin: 16
  15. spacing: 14
  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: control.Material.buttonHeight
  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 ? 90 : (control.mirrored ? 180 : 0)
  36. source: "qrc:/qt-project.org/imports/QtQuick/Controls/Material/images/arrow-indicator.png"
  37. color: control.enabled ? control.Material.foreground : control.Material.hintTextColor
  38. defaultColor: "#353637"
  39. }
  40. }
  41. background: Rectangle {
  42. implicitHeight: control.Material.buttonHeight
  43. color: control.highlighted
  44. ? control.Material.accentColor
  45. : (control.treeView.alternatingRows && control.row % 2 !== 0
  46. ? control.Material.background
  47. // The Material.shade() is used as the alternate background color for rows
  48. // based on the Material.theme value.
  49. : control.Material.shade(control.Material.background,
  50. control.Material.theme === Material.Dark
  51. ? Material.Shade100 // the lighter background color
  52. : Material.Shade700 // the darker background color
  53. ))
  54. }
  55. contentItem: Label {
  56. text: control.model.display
  57. elide: Text.ElideRight
  58. visible: !control.editing
  59. }
  60. // The edit delegate is a separate component, and doesn't need
  61. // to follow the same strict rules that are applied to a control.
  62. // qmllint disable attached-property-reuse
  63. // qmllint disable controls-attached-property-reuse
  64. // qmllint disable controls-sanity
  65. TableView.editDelegate: FocusScope {
  66. width: parent.width
  67. height: parent.height
  68. readonly property int __role: {
  69. let model = control.treeView.model
  70. let index = control.treeView.index(row, column)
  71. let editText = model.data(index, Qt.EditRole)
  72. return editText !== undefined ? Qt.EditRole : Qt.DisplayRole
  73. }
  74. TextField {
  75. id: textField
  76. x: control.contentItem.x
  77. y: (parent.height - height) / 2
  78. width: control.contentItem.width
  79. text: control.treeView.model.data(control.treeView.index(row, column), __role)
  80. focus: true
  81. }
  82. TableView.onCommit: {
  83. let index = TableView.view.index(row, column)
  84. TableView.view.model.setData(index, textField.text, __role)
  85. }
  86. Component.onCompleted: textField.selectAll()
  87. }
  88. // qmllint enable attached-property-reuse
  89. // qmllint enable controls-attached-property-reuse
  90. // qmllint enable controls-sanity
  91. }