ComboBox.qml 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright (C) 2017 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. pragma ComponentBehavior: Bound
  5. import QtQuick
  6. import QtQuick.Window
  7. import QtQuick.Templates as T
  8. import QtQuick.Controls.Imagine
  9. import QtQuick.Controls.Imagine.impl
  10. T.ComboBox {
  11. id: control
  12. implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset,
  13. implicitContentWidth + (background ? background.leftPadding + background.rightPadding : 0))
  14. implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
  15. Math.max(implicitContentHeight,
  16. implicitIndicatorHeight) + (background ? background.topPadding + background.bottomPadding : 0))
  17. leftPadding: padding + (!control.mirrored || !indicator || !indicator.visible ? 0 : indicator.width + spacing)
  18. rightPadding: padding + (control.mirrored || !indicator || !indicator.visible ? 0 : indicator.width + spacing)
  19. topInset: background ? -background.topInset || 0 : 0
  20. leftInset: background ? -background.leftInset || 0 : 0
  21. rightInset: background ? -background.rightInset || 0 : 0
  22. bottomInset: background ? -background.bottomInset || 0 : 0
  23. delegate: ItemDelegate {
  24. required property var model
  25. required property int index
  26. width: ListView.view.width
  27. text: model[control.textRole]
  28. font.weight: control.currentIndex === index ? Font.DemiBold : Font.Normal
  29. highlighted: control.highlightedIndex === index
  30. hoverEnabled: control.hoverEnabled
  31. }
  32. indicator: Image {
  33. x: control.mirrored ? control.padding : control.width - width - control.padding
  34. y: control.topPadding + (control.availableHeight - height) / 2
  35. source: Imagine.url + "combobox-indicator"
  36. ImageSelector on source {
  37. states: [
  38. {"disabled": !control.enabled},
  39. {"pressed": control.pressed},
  40. {"editable": control.editable},
  41. {"open": control.down},
  42. {"focused": control.visualFocus},
  43. {"mirrored": control.mirrored},
  44. {"hovered": control.enabled && control.hovered},
  45. {"flat": control.flat}
  46. ]
  47. }
  48. }
  49. contentItem: T.TextField {
  50. topPadding: control.background ? control.background.topPadding : 0
  51. leftPadding: control.background ? control.background.leftPadding : 0
  52. rightPadding: control.background ? control.background.rightPadding : 0
  53. bottomPadding: control.background ? control.background.bottomPadding : 0
  54. text: control.editable ? control.editText : control.displayText
  55. enabled: control.editable
  56. autoScroll: control.editable
  57. readOnly: control.down
  58. inputMethodHints: control.inputMethodHints
  59. validator: control.validator
  60. selectByMouse: control.selectTextByMouse
  61. color: control.flat ? control.palette.windowText : control.editable ? control.palette.text : control.palette.buttonText
  62. selectionColor: control.palette.highlight
  63. selectedTextColor: control.palette.highlightedText
  64. verticalAlignment: Text.AlignVCenter
  65. }
  66. background: NinePatchImage {
  67. source: Imagine.url + "combobox-background"
  68. NinePatchImageSelector on source {
  69. states: [
  70. {"disabled": !control.enabled},
  71. {"pressed": control.pressed},
  72. {"editable": control.editable},
  73. {"open": control.down},
  74. {"focused": control.visualFocus || (control.editable && control.activeFocus)},
  75. {"mirrored": control.mirrored},
  76. {"hovered": control.enabled && control.hovered},
  77. {"flat": control.flat}
  78. ]
  79. }
  80. }
  81. popup: T.Popup {
  82. width: control.width
  83. height: Math.min(contentItem.implicitHeight + topPadding + bottomPadding, control.Window.height - topMargin - bottomMargin)
  84. topMargin: background.topInset
  85. bottomMargin: background.bottomInset
  86. topPadding: background.topPadding
  87. leftPadding: background.leftPadding
  88. rightPadding: background.rightPadding
  89. bottomPadding: background.bottomPadding
  90. topInset: background ? -background.topInset || 0 : 0
  91. leftInset: background ? -background.leftInset || 0 : 0
  92. rightInset: background ? -background.rightInset || 0 : 0
  93. bottomInset: background ? -background.bottomInset || 0 : 0
  94. palette.text: control.palette.text
  95. palette.highlight: control.palette.highlight
  96. palette.highlightedText: control.palette.highlightedText
  97. palette.windowText: control.palette.windowText
  98. palette.buttonText: control.palette.buttonText
  99. contentItem: ListView {
  100. clip: true
  101. implicitHeight: contentHeight
  102. model: control.delegateModel
  103. currentIndex: control.highlightedIndex
  104. highlightMoveDuration: 0
  105. T.ScrollIndicator.vertical: ScrollIndicator { }
  106. }
  107. background: NinePatchImage {
  108. source: Imagine.url + "combobox-popup"
  109. NinePatchImageSelector on source {
  110. states: [
  111. {"disabled": !control.enabled},
  112. {"pressed": control.pressed},
  113. {"editable": control.editable},
  114. {"focused": control.visualFocus || (control.editable && control.activeFocus)},
  115. {"mirrored": control.mirrored},
  116. {"hovered": control.enabled && control.hovered},
  117. {"flat": control.flat}
  118. ]
  119. }
  120. }
  121. }
  122. }