ComboBox.qml 6.8 KB

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