TreeViewDelegate.qml 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.Controls.impl
  6. import QtQuick.Templates as T
  7. T.TreeViewDelegate {
  8. id: control
  9. implicitWidth: leftMargin + __contentIndent + implicitContentWidth + rightPadding + rightMargin
  10. implicitHeight: Math.max(indicator ? indicator.height : 0, implicitContentHeight) * 1.25
  11. indentation: indicator ? indicator.width : 12
  12. leftMargin: 4
  13. rightMargin: 4
  14. spacing: 4
  15. topPadding: contentItem ? (height - contentItem.implicitHeight) / 2 : 0
  16. leftPadding: !mirrored ? leftMargin + __contentIndent : width - leftMargin - __contentIndent - implicitContentWidth
  17. highlighted: control.selected || control.current
  18. || ((control.treeView.selectionBehavior === TableView.SelectRows
  19. || control.treeView.selectionBehavior === TableView.SelectionDisabled)
  20. && control.row === control.treeView.currentRow)
  21. required property int row
  22. required property var model
  23. readonly property real __contentIndent: !isTreeNode ? 0 : (depth * indentation) + (indicator ? indicator.width + spacing : 0)
  24. indicator: Item {
  25. // Create an area that is big enough for the user to
  26. // click on, since the image is a bit small.
  27. readonly property real __indicatorIndent: control.leftMargin + (control.depth * control.indentation)
  28. x: !control.mirrored ? __indicatorIndent : control.width - __indicatorIndent - width
  29. y: (control.height - height) / 2
  30. implicitWidth: 20
  31. implicitHeight: 40 // same as Button.qml
  32. ColorImage {
  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/Basic/images/arrow-indicator.png"
  37. color: control.palette.windowText
  38. defaultColor: "#353637"
  39. }
  40. }
  41. background: Rectangle {
  42. implicitHeight: 40 // same as Button.qml
  43. border.color: control.current ? control.palette.highlight : control.palette.windowText
  44. border.width: Qt.styleHints.accessibility.contrastPreference !== Qt.HighContrast ? 0 :
  45. control.current ? 2 : 1
  46. color: control.highlighted
  47. ? control.palette.highlight
  48. : (control.treeView.alternatingRows && control.row % 2 !== 0
  49. ? control.palette.alternateBase : control.palette.base)
  50. }
  51. contentItem: Label {
  52. clip: false
  53. text: control.model.display
  54. elide: Text.ElideRight
  55. color: control.highlighted ? control.palette.highlightedText : control.palette.buttonText
  56. visible: !control.editing
  57. }
  58. // The edit delegate is a separate component, and doesn't need
  59. // to follow the same strict rules that are applied to a control.
  60. // qmllint disable attached-property-reuse
  61. // qmllint disable controls-attached-property-reuse
  62. // qmllint disable controls-sanity
  63. TableView.editDelegate: FocusScope {
  64. width: parent.width
  65. height: parent.height
  66. readonly property int __role: {
  67. let model = control.treeView.model
  68. let index = control.treeView.index(row, column)
  69. let editText = model.data(index, Qt.EditRole)
  70. return editText !== undefined ? Qt.EditRole : Qt.DisplayRole
  71. }
  72. TextField {
  73. id: textField
  74. x: control.contentItem.x
  75. y: (parent.height - height) / 2
  76. width: control.contentItem.width
  77. text: control.treeView.model.data(control.treeView.index(row, column), __role)
  78. focus: true
  79. }
  80. TableView.onCommit: {
  81. let index = TableView.view.index(row, column)
  82. TableView.view.model.setData(index, textField.text, __role)
  83. }
  84. Component.onCompleted: textField.selectAll()
  85. }
  86. // qmllint enable attached-property-reuse
  87. // qmllint enable controls-attached-property-reuse
  88. // qmllint enable controls-sanity
  89. }