ComboBox.qml 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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.Controls.impl
  7. import QtQuick.Templates as T
  8. T.ComboBox {
  9. id: control
  10. implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset,
  11. implicitContentWidth + leftPadding + rightPadding)
  12. implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
  13. implicitContentHeight + topPadding + bottomPadding,
  14. implicitIndicatorHeight + topPadding + bottomPadding)
  15. leftPadding: padding + (!control.mirrored || !indicator || !indicator.visible ? 0 : indicator.width + spacing)
  16. rightPadding: padding + (control.mirrored || !indicator || !indicator.visible ? 0 : indicator.width + spacing)
  17. delegate: ItemDelegate {
  18. required property var model
  19. required property int index
  20. width: ListView.view.width
  21. text: model[control.textRole]
  22. palette.text: control.palette.text
  23. palette.highlightedText: control.palette.highlightedText
  24. font.weight: control.currentIndex === index ? Font.DemiBold : Font.Normal
  25. highlighted: control.highlightedIndex === index
  26. hoverEnabled: control.hoverEnabled
  27. }
  28. indicator: ColorImage {
  29. x: control.mirrored ? control.padding : control.width - width - control.padding
  30. y: control.topPadding + (control.availableHeight - height) / 2
  31. color: control.palette.dark
  32. defaultColor: "#353637"
  33. source: "qrc:/qt-project.org/imports/QtQuick/Controls/Basic/images/double-arrow.png"
  34. opacity: enabled ? 1 : 0.3
  35. }
  36. contentItem: T.TextField {
  37. leftPadding: !control.mirrored ? 12 : control.editable && activeFocus ? 3 : 1
  38. rightPadding: control.mirrored ? 12 : control.editable && activeFocus ? 3 : 1
  39. topPadding: 6 - control.padding
  40. bottomPadding: 6 - control.padding
  41. text: control.editable ? control.editText : control.displayText
  42. enabled: control.editable
  43. autoScroll: control.editable
  44. readOnly: control.down
  45. inputMethodHints: control.inputMethodHints
  46. validator: control.validator
  47. selectByMouse: control.selectTextByMouse
  48. color: control.editable ? control.palette.text : control.palette.buttonText
  49. selectionColor: control.palette.highlight
  50. selectedTextColor: control.palette.highlightedText
  51. verticalAlignment: Text.AlignVCenter
  52. background: Rectangle {
  53. visible: control.enabled && control.editable && !control.flat
  54. border.width: parent && parent.activeFocus ? 2 : 1
  55. border.color: parent && parent.activeFocus ? control.palette.highlight :
  56. Qt.styleHints.accessibility.contrastPreference === Qt.HighContrast ?
  57. control.palette.buttonText : control.palette.button
  58. color: control.palette.base
  59. }
  60. }
  61. background: Rectangle {
  62. implicitWidth: 140
  63. implicitHeight: 40
  64. color: control.down ? control.palette.mid : control.palette.button
  65. border.color: !control.editable && control.visualFocus ? control.palette.highlight : control.palette.buttonText
  66. border.width: (!control.editable && control.visualFocus) ? 2 :
  67. Qt.styleHints.accessibility.contrastPreference === Qt.HighContrast ? 1 : 0
  68. visible: !control.flat || control.down
  69. }
  70. popup: T.Popup {
  71. y: control.height
  72. width: control.width
  73. height: Math.min(contentItem.implicitHeight, control.Window.height - topMargin - bottomMargin)
  74. topMargin: 6
  75. bottomMargin: 6
  76. palette: control.palette
  77. contentItem: ListView {
  78. clip: true
  79. implicitHeight: contentHeight
  80. model: control.delegateModel
  81. currentIndex: control.highlightedIndex
  82. highlightMoveDuration: 0
  83. Rectangle {
  84. z: 10
  85. width: parent.width
  86. height: parent.height
  87. color: "transparent"
  88. border.color: control.palette.mid
  89. }
  90. // Show a contour around the highlighted item in high contrast mode
  91. Rectangle {
  92. property Item highlightedItem: parent ? parent.itemAtIndex(control.highlightedIndex) : null
  93. visible: Qt.styleHints.accessibility.contrastPreference === Qt.HighContrast && highlightedItem
  94. z: 11
  95. x: highlightedItem ? highlightedItem.x : 0
  96. y: highlightedItem ? highlightedItem.y : 0
  97. width: highlightedItem ? highlightedItem.width : 0
  98. height: highlightedItem ? highlightedItem.height : 0
  99. color: "transparent"
  100. border.color: control.palette.dark
  101. }
  102. T.ScrollIndicator.vertical: ScrollIndicator { }
  103. }
  104. background: Rectangle {
  105. color: control.palette.window
  106. }
  107. }
  108. }