ComboBox.qml 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. import QtQuick.Controls.Universal 2.15
  42. T.ComboBox {
  43. id: control
  44. implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset,
  45. implicitContentWidth + leftPadding + rightPadding)
  46. implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
  47. implicitContentHeight + topPadding + bottomPadding,
  48. implicitIndicatorHeight + topPadding + bottomPadding)
  49. leftPadding: padding + (!control.mirrored || !indicator || !indicator.visible ? 0 : indicator.width + spacing)
  50. rightPadding: padding + (control.mirrored || !indicator || !indicator.visible ? 0 : indicator.width + spacing)
  51. Universal.theme: editable && activeFocus ? Universal.Light : undefined
  52. delegate: ItemDelegate {
  53. width: ListView.view.width
  54. text: control.textRole ? (Array.isArray(control.model) ? modelData[control.textRole] : model[control.textRole]) : modelData
  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.enabled ? control.Universal.baseLowColor : control.Universal.baseMediumHighColor
  63. source: "qrc:/qt-project.org/imports/QtQuick/Controls.2/Universal/images/downarrow.png"
  64. Rectangle {
  65. z: -1
  66. width: parent.width
  67. height: parent.height
  68. color: control.activeFocus ? control.Universal.accent :
  69. control.pressed ? control.Universal.baseMediumLowColor :
  70. control.hovered ? control.Universal.baseLowColor : "transparent"
  71. visible: control.editable && !contentItem.hovered && (control.pressed || control.hovered)
  72. opacity: control.activeFocus && !control.pressed ? 0.4 : 1.0
  73. }
  74. }
  75. contentItem: T.TextField {
  76. leftPadding: control.mirrored ? 1 : 12
  77. rightPadding: control.mirrored ? 10 : 1
  78. topPadding: 5 - control.topPadding
  79. bottomPadding: 7 - control.bottomPadding
  80. text: control.editable ? control.editText : control.displayText
  81. enabled: control.editable
  82. autoScroll: control.editable
  83. readOnly: control.down
  84. inputMethodHints: control.inputMethodHints
  85. validator: control.validator
  86. selectByMouse: control.selectTextByMouse
  87. font: control.font
  88. color: !control.enabled ? control.Universal.chromeDisabledLowColor :
  89. control.editable && control.activeFocus ? control.Universal.chromeBlackHighColor : control.Universal.foreground
  90. selectionColor: control.Universal.accent
  91. selectedTextColor: control.Universal.chromeWhiteColor
  92. verticalAlignment: Text.AlignVCenter
  93. }
  94. background: Rectangle {
  95. implicitWidth: 120
  96. implicitHeight: 32
  97. border.width: control.flat ? 0 : 2 // ComboBoxBorderThemeThickness
  98. border.color: !control.enabled ? control.Universal.baseLowColor :
  99. control.editable && control.activeFocus ? control.Universal.accent :
  100. control.down ? control.Universal.baseMediumLowColor :
  101. control.hovered ? control.Universal.baseMediumColor : control.Universal.baseMediumLowColor
  102. color: !control.enabled ? control.Universal.baseLowColor :
  103. control.down ? control.Universal.listMediumColor :
  104. control.flat && control.hovered ? control.Universal.listLowColor :
  105. control.editable && control.activeFocus ? control.Universal.background : control.Universal.altMediumLowColor
  106. visible: !control.flat || control.pressed || control.hovered || control.visualFocus
  107. Rectangle {
  108. x: 2
  109. y: 2
  110. width: parent.width - 4
  111. height: parent.height - 4
  112. visible: control.visualFocus && !control.editable
  113. color: control.Universal.accent
  114. opacity: control.Universal.theme === Universal.Light ? 0.4 : 0.6
  115. }
  116. }
  117. popup: T.Popup {
  118. width: control.width
  119. height: Math.min(contentItem.implicitHeight, control.Window.height - topMargin - bottomMargin)
  120. topMargin: 8
  121. bottomMargin: 8
  122. Universal.theme: control.Universal.theme
  123. Universal.accent: control.Universal.accent
  124. contentItem: ListView {
  125. clip: true
  126. implicitHeight: contentHeight
  127. model: control.delegateModel
  128. currentIndex: control.highlightedIndex
  129. highlightMoveDuration: 0
  130. T.ScrollIndicator.vertical: ScrollIndicator { }
  131. }
  132. background: Rectangle {
  133. color: control.Universal.chromeMediumLowColor
  134. border.color: control.Universal.chromeHighColor
  135. border.width: 1 // FlyoutBorderThemeThickness
  136. }
  137. }
  138. }