ComboBox.qml 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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.Material 2.15
  42. import QtQuick.Controls.Material.impl 2.15
  43. T.ComboBox {
  44. id: control
  45. implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset,
  46. implicitContentWidth + leftPadding + rightPadding)
  47. implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
  48. implicitContentHeight + topPadding + bottomPadding,
  49. implicitIndicatorHeight + topPadding + bottomPadding)
  50. topInset: 6
  51. bottomInset: 6
  52. leftPadding: padding + (!control.mirrored || !indicator || !indicator.visible ? 0 : indicator.width + spacing)
  53. rightPadding: padding + (control.mirrored || !indicator || !indicator.visible ? 0 : indicator.width + spacing)
  54. Material.elevation: flat ? control.pressed || control.hovered ? 2 : 0
  55. : control.pressed ? 8 : 2
  56. Material.background: flat ? "transparent" : undefined
  57. Material.foreground: flat ? undefined : Material.primaryTextColor
  58. delegate: MenuItem {
  59. width: ListView.view.width
  60. text: control.textRole ? (Array.isArray(control.model) ? modelData[control.textRole] : model[control.textRole]) : modelData
  61. Material.foreground: control.currentIndex === index ? ListView.view.contentItem.Material.accent : ListView.view.contentItem.Material.foreground
  62. highlighted: control.highlightedIndex === index
  63. hoverEnabled: control.hoverEnabled
  64. }
  65. indicator: ColorImage {
  66. x: control.mirrored ? control.padding : control.width - width - control.padding
  67. y: control.topPadding + (control.availableHeight - height) / 2
  68. color: control.enabled ? control.Material.foreground : control.Material.hintTextColor
  69. source: "qrc:/qt-project.org/imports/QtQuick/Controls.2/Material/images/drop-indicator.png"
  70. }
  71. contentItem: T.TextField {
  72. padding: 6
  73. leftPadding: control.editable ? 2 : control.mirrored ? 0 : 12
  74. rightPadding: control.editable ? 2 : control.mirrored ? 12 : 0
  75. text: control.editable ? control.editText : control.displayText
  76. enabled: control.editable
  77. autoScroll: control.editable
  78. readOnly: control.down
  79. inputMethodHints: control.inputMethodHints
  80. validator: control.validator
  81. selectByMouse: control.selectTextByMouse
  82. font: control.font
  83. color: control.enabled ? control.Material.foreground : control.Material.hintTextColor
  84. selectionColor: control.Material.accentColor
  85. selectedTextColor: control.Material.primaryHighlightedTextColor
  86. verticalAlignment: Text.AlignVCenter
  87. cursorDelegate: CursorDelegate { }
  88. }
  89. background: Rectangle {
  90. implicitWidth: 120
  91. implicitHeight: control.Material.buttonHeight
  92. radius: control.flat ? 0 : 2
  93. color: !control.editable ? control.Material.dialogColor : "transparent"
  94. layer.enabled: control.enabled && !control.editable && control.Material.background.a > 0
  95. layer.effect: ElevationEffect {
  96. elevation: control.Material.elevation
  97. }
  98. Rectangle {
  99. visible: control.editable
  100. y: parent.y + control.baselineOffset
  101. width: parent.width
  102. height: control.activeFocus ? 2 : 1
  103. color: control.editable && control.activeFocus ? control.Material.accentColor : control.Material.hintTextColor
  104. }
  105. Ripple {
  106. clip: control.flat
  107. clipRadius: control.flat ? 0 : 2
  108. x: control.editable && control.indicator ? control.indicator.x : 0
  109. width: control.editable && control.indicator ? control.indicator.width : parent.width
  110. height: parent.height
  111. pressed: control.pressed
  112. anchor: control.editable && control.indicator ? control.indicator : control
  113. active: control.pressed || control.visualFocus || control.hovered
  114. color: control.Material.rippleColor
  115. }
  116. }
  117. popup: T.Popup {
  118. y: control.editable ? control.height - 5 : 0
  119. width: control.width
  120. height: Math.min(contentItem.implicitHeight, control.Window.height - topMargin - bottomMargin)
  121. transformOrigin: Item.Top
  122. topMargin: 12
  123. bottomMargin: 12
  124. Material.theme: control.Material.theme
  125. Material.accent: control.Material.accent
  126. Material.primary: control.Material.primary
  127. enter: Transition {
  128. // grow_fade_in
  129. NumberAnimation { property: "scale"; from: 0.9; to: 1.0; easing.type: Easing.OutQuint; duration: 220 }
  130. NumberAnimation { property: "opacity"; from: 0.0; to: 1.0; easing.type: Easing.OutCubic; duration: 150 }
  131. }
  132. exit: Transition {
  133. // shrink_fade_out
  134. NumberAnimation { property: "scale"; from: 1.0; to: 0.9; easing.type: Easing.OutQuint; duration: 220 }
  135. NumberAnimation { property: "opacity"; from: 1.0; to: 0.0; easing.type: Easing.OutCubic; duration: 150 }
  136. }
  137. contentItem: ListView {
  138. clip: true
  139. implicitHeight: contentHeight
  140. model: control.delegateModel
  141. currentIndex: control.highlightedIndex
  142. highlightMoveDuration: 0
  143. T.ScrollIndicator.vertical: ScrollIndicator { }
  144. }
  145. background: Rectangle {
  146. radius: 2
  147. color: parent.Material.dialogColor
  148. layer.enabled: control.enabled
  149. layer.effect: ElevationEffect {
  150. elevation: 8
  151. }
  152. }
  153. }
  154. }