ComboBox.qml 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
  51. contentHeight + topPadding + bottomPadding)
  52. topPadding: control.background ? control.background.topPadding : 0
  53. leftPadding: control.background ? control.background.leftPadding : 0
  54. rightPadding: control.background ? control.background.rightPadding : 0
  55. bottomPadding: control.background ? control.background.bottomPadding : 0
  56. topInset: background ? -background.topInset || 0 : 0
  57. bottomInset: background ? -background.bottomInset || 0 : 0
  58. text: control.editable ? control.editText : control.displayText
  59. enabled: control.editable
  60. autoScroll: control.editable
  61. readOnly: control.down
  62. inputMethodHints: control.inputMethodHints
  63. validator: control.validator
  64. selectByMouse: control.selectTextByMouse
  65. color: control.flat ? control.palette.windowText : control.editable ? control.palette.text : control.palette.buttonText
  66. selectionColor: control.palette.highlight
  67. selectedTextColor: control.palette.highlightedText
  68. verticalAlignment: Text.AlignVCenter
  69. ContextMenu.menu: TextEditingContextMenu {
  70. editor: parent
  71. }
  72. }
  73. background: NinePatchImage {
  74. source: Imagine.url + "combobox-background"
  75. NinePatchImageSelector on source {
  76. states: [
  77. {"disabled": !control.enabled},
  78. {"pressed": control.pressed},
  79. {"editable": control.editable},
  80. {"open": control.down},
  81. {"focused": control.visualFocus || (control.editable && control.activeFocus)},
  82. {"mirrored": control.mirrored},
  83. {"hovered": control.enabled && control.hovered},
  84. {"flat": control.flat}
  85. ]
  86. }
  87. }
  88. popup: T.Popup {
  89. width: control.width
  90. height: Math.min(contentItem.implicitHeight + topPadding + bottomPadding, control.Window.height - topMargin - bottomMargin)
  91. topMargin: background.topInset
  92. bottomMargin: background.bottomInset
  93. topPadding: background.topPadding
  94. leftPadding: background.leftPadding
  95. rightPadding: background.rightPadding
  96. bottomPadding: background.bottomPadding
  97. topInset: background ? -background.topInset || 0 : 0
  98. leftInset: background ? -background.leftInset || 0 : 0
  99. rightInset: background ? -background.rightInset || 0 : 0
  100. bottomInset: background ? -background.bottomInset || 0 : 0
  101. palette.text: control.palette.text
  102. palette.highlight: control.palette.highlight
  103. palette.highlightedText: control.palette.highlightedText
  104. palette.windowText: control.palette.windowText
  105. palette.buttonText: control.palette.buttonText
  106. contentItem: ListView {
  107. clip: true
  108. implicitHeight: contentHeight
  109. model: control.delegateModel
  110. currentIndex: control.highlightedIndex
  111. highlightMoveDuration: 0
  112. T.ScrollIndicator.vertical: ScrollIndicator { }
  113. }
  114. background: NinePatchImage {
  115. source: Imagine.url + "combobox-popup"
  116. NinePatchImageSelector on source {
  117. states: [
  118. {"disabled": !control.enabled},
  119. {"pressed": control.pressed},
  120. {"editable": control.editable},
  121. {"focused": control.visualFocus || (control.editable && control.activeFocus)},
  122. {"mirrored": control.mirrored},
  123. {"hovered": control.enabled && control.hovered},
  124. {"flat": control.flat}
  125. ]
  126. }
  127. }
  128. }
  129. }