StyledComboBox.qml 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import QtQuick 2.15
  2. import QtQuick.Controls 2.15
  3. import StandardOfIron 1.0
  4. ComboBox {
  5. id: root
  6. property int textPointSize: Theme.fontSizeMedium
  7. property int textPixelSize: -1
  8. property color textColor: Theme.textMain
  9. property color backgroundColor: Theme.cardBase
  10. property color borderColor: activeFocus ? Theme.accentBr : Theme.cardBorder
  11. property color popupBackground: Theme.panelBase
  12. property color popupBorder: Theme.cardBorder
  13. property color highlightBackground: Theme.hoverBg
  14. property color highlightBorder: Theme.selectedBr
  15. property color itemBackground: Theme.cardBase
  16. property color itemBorder: Theme.cardBorder
  17. property var delegateText: function(data) {
  18. return data;
  19. }
  20. function resolveDelegateText(data) {
  21. return (typeof delegateText === "function") ? delegateText(data) : data;
  22. }
  23. contentItem: Text {
  24. text: root.displayText
  25. color: root.textColor
  26. font.pointSize: root.textPixelSize > 0 ? -1 : root.textPointSize
  27. font.pixelSize: root.textPixelSize > 0 ? root.textPixelSize : -1
  28. elide: Text.ElideRight
  29. verticalAlignment: Text.AlignVCenter
  30. anchors.fill: parent
  31. leftPadding: Theme.spacingSmall
  32. rightPadding: Theme.spacingLarge + root.indicator.width
  33. }
  34. background: Rectangle {
  35. radius: Theme.radiusSmall
  36. color: root.backgroundColor
  37. border.color: root.borderColor
  38. border.width: 1
  39. }
  40. indicator: Canvas {
  41. id: rootIndicator
  42. width: 12
  43. height: 8
  44. anchors.right: parent.right
  45. anchors.rightMargin: Theme.spacingSmall
  46. anchors.verticalCenter: parent.verticalCenter
  47. onPaint: {
  48. var ctx = getContext("2d");
  49. ctx.clearRect(0, 0, width, height);
  50. ctx.fillStyle = root.textColor;
  51. ctx.beginPath();
  52. ctx.moveTo(0, 0);
  53. ctx.lineTo(width, 0);
  54. ctx.lineTo(width / 2, height);
  55. ctx.closePath();
  56. ctx.fill();
  57. }
  58. Component.onCompleted: requestPaint()
  59. onWidthChanged: requestPaint()
  60. onHeightChanged: requestPaint()
  61. Connections {
  62. function onTextColorChanged() {
  63. rootIndicator.requestPaint();
  64. }
  65. target: root
  66. }
  67. }
  68. popup: Popup {
  69. y: root.height
  70. width: root.width
  71. implicitHeight: contentItem.implicitHeight
  72. padding: 0
  73. contentItem: ListView {
  74. clip: true
  75. implicitHeight: contentHeight
  76. model: root.popup.visible ? root.delegateModel : null
  77. currentIndex: root.highlightedIndex
  78. }
  79. background: Rectangle {
  80. radius: Theme.radiusSmall
  81. color: root.popupBackground
  82. border.color: root.popupBorder
  83. border.width: 1
  84. }
  85. }
  86. delegate: ItemDelegate {
  87. width: root.width
  88. highlighted: root.highlightedIndex === index
  89. background: Rectangle {
  90. color: highlighted ? root.highlightBackground : root.itemBackground
  91. border.color: highlighted ? root.highlightBorder : root.itemBorder
  92. border.width: 1
  93. }
  94. contentItem: Text {
  95. text: root.resolveDelegateText(modelData)
  96. color: root.textColor
  97. font.pointSize: root.textPixelSize > 0 ? -1 : root.textPointSize
  98. font.pixelSize: root.textPixelSize > 0 ? root.textPixelSize : -1
  99. elide: Text.ElideRight
  100. verticalAlignment: Text.AlignVCenter
  101. }
  102. }
  103. }