ComboBox.qml 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2017 The Qt Company Ltd.
  4. ** Contact: http://www.qt.io/licensing/
  5. **
  6. ** This file is part of the Qt Quick Controls 2 module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL3$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and The Qt Company. For licensing terms
  14. ** and conditions see http://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at http://www.qt.io/contact-us.
  16. **
  17. ** GNU Lesser General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU Lesser
  19. ** General Public License version 3 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPLv3 included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 3 requirements
  23. ** will be met: https://www.gnu.org/licenses/lgpl.html.
  24. **
  25. ** GNU General Public License Usage
  26. ** Alternatively, this file may be used under the terms of the GNU
  27. ** General Public License version 2.0 or later as published by the Free
  28. ** Software Foundation and appearing in the file LICENSE.GPL included in
  29. ** the packaging of this file. Please review the following information to
  30. ** ensure the GNU General Public License version 2.0 requirements will be
  31. ** met: http://www.gnu.org/licenses/gpl-2.0.html.
  32. **
  33. ** $QT_END_LICENSE$
  34. **
  35. ****************************************************************************/
  36. import QtQuick 2.15
  37. import QtQuick.Window 2.15
  38. import QtQuick.Controls 2.15
  39. import QtQuick.Controls.impl 2.15
  40. import QtQuick.Templates 2.15 as T
  41. T.ComboBox {
  42. id: control
  43. implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset,
  44. implicitContentWidth + leftPadding + rightPadding)
  45. implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
  46. implicitContentHeight + topPadding + bottomPadding,
  47. implicitIndicatorHeight + topPadding + bottomPadding)
  48. leftPadding: padding + (!control.mirrored || !indicator || !indicator.visible ? 0 : indicator.width + spacing)
  49. rightPadding: padding + (control.mirrored || !indicator || !indicator.visible ? 0 : indicator.width + spacing)
  50. delegate: ItemDelegate {
  51. width: ListView.view.width
  52. text: control.textRole ? (Array.isArray(control.model) ? modelData[control.textRole] : model[control.textRole]) : modelData
  53. palette.text: control.palette.text
  54. palette.highlightedText: control.palette.highlightedText
  55. font.weight: control.currentIndex === index ? Font.DemiBold : Font.Normal
  56. highlighted: control.highlightedIndex === index
  57. hoverEnabled: control.hoverEnabled
  58. }
  59. indicator: ColorImage {
  60. x: control.mirrored ? control.padding : control.width - width - control.padding
  61. y: control.topPadding + (control.availableHeight - height) / 2
  62. color: control.palette.dark
  63. defaultColor: "#353637"
  64. source: "qrc:/qt-project.org/imports/QtQuick/Controls.2/images/double-arrow.png"
  65. opacity: enabled ? 1 : 0.3
  66. }
  67. contentItem: T.TextField {
  68. leftPadding: !control.mirrored ? 12 : control.editable && activeFocus ? 3 : 1
  69. rightPadding: control.mirrored ? 12 : control.editable && activeFocus ? 3 : 1
  70. topPadding: 6 - control.padding
  71. bottomPadding: 6 - control.padding
  72. text: control.editable ? control.editText : control.displayText
  73. enabled: control.editable
  74. autoScroll: control.editable
  75. readOnly: control.down
  76. inputMethodHints: control.inputMethodHints
  77. validator: control.validator
  78. selectByMouse: control.selectTextByMouse
  79. font: control.font
  80. color: control.editable ? control.palette.text : control.palette.buttonText
  81. selectionColor: control.palette.highlight
  82. selectedTextColor: control.palette.highlightedText
  83. verticalAlignment: Text.AlignVCenter
  84. background: Rectangle {
  85. visible: control.enabled && control.editable && !control.flat
  86. border.width: parent && parent.activeFocus ? 2 : 1
  87. border.color: parent && parent.activeFocus ? control.palette.highlight : control.palette.button
  88. color: control.palette.base
  89. }
  90. }
  91. background: Rectangle {
  92. implicitWidth: 140
  93. implicitHeight: 40
  94. color: control.down ? control.palette.mid : control.palette.button
  95. border.color: control.palette.highlight
  96. border.width: !control.editable && control.visualFocus ? 2 : 0
  97. visible: !control.flat || control.down
  98. }
  99. popup: T.Popup {
  100. y: control.height
  101. width: control.width
  102. height: Math.min(contentItem.implicitHeight, control.Window.height - topMargin - bottomMargin)
  103. topMargin: 6
  104. bottomMargin: 6
  105. contentItem: ListView {
  106. clip: true
  107. implicitHeight: contentHeight
  108. model: control.delegateModel
  109. currentIndex: control.highlightedIndex
  110. highlightMoveDuration: 0
  111. Rectangle {
  112. z: 10
  113. width: parent.width
  114. height: parent.height
  115. color: "transparent"
  116. border.color: control.palette.mid
  117. }
  118. T.ScrollIndicator.vertical: ScrollIndicator { }
  119. }
  120. background: Rectangle {
  121. color: control.palette.window
  122. }
  123. }
  124. }