ComboBox.qml 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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.Templates 2.15 as T
  39. import QtQuick.Controls 2.15
  40. import QtQuick.Controls.Imagine 2.15
  41. import QtQuick.Controls.Imagine.impl 2.15
  42. T.ComboBox {
  43. id: control
  44. implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset,
  45. contentItem.implicitWidth + background ? (background.leftPadding + background.rightPadding) : 0)
  46. implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
  47. Math.max(implicitContentHeight,
  48. implicitIndicatorHeight) + background ? (background.topPadding + background.bottomPadding) : 0)
  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. topInset: background ? -background.topInset || 0 : 0
  52. leftInset: background ? -background.leftInset || 0 : 0
  53. rightInset: background ? -background.rightInset || 0 : 0
  54. bottomInset: background ? -background.bottomInset || 0 : 0
  55. delegate: ItemDelegate {
  56. width: ListView.view.width
  57. text: control.textRole ? (Array.isArray(control.model) ? modelData[control.textRole] : model[control.textRole]) : modelData
  58. font.weight: control.currentIndex === index ? Font.DemiBold : Font.Normal
  59. highlighted: control.highlightedIndex === index
  60. hoverEnabled: control.hoverEnabled
  61. }
  62. indicator: Image {
  63. x: control.mirrored ? control.padding : control.width - width - control.padding
  64. y: control.topPadding + (control.availableHeight - height) / 2
  65. source: Imagine.url + "combobox-indicator"
  66. ImageSelector on source {
  67. states: [
  68. {"disabled": !control.enabled},
  69. {"pressed": control.pressed},
  70. {"editable": control.editable},
  71. {"open": control.down},
  72. {"focused": control.visualFocus},
  73. {"mirrored": control.mirrored},
  74. {"hovered": control.hovered},
  75. {"flat": control.flat}
  76. ]
  77. }
  78. }
  79. contentItem: T.TextField {
  80. topPadding: control.background ? control.background.topPadding : 0
  81. leftPadding: control.background ? control.background.leftPadding : 0
  82. rightPadding: control.background ? control.background.rightPadding : 0
  83. bottomPadding: control.background ? control.background.bottomPadding : 0
  84. text: control.editable ? control.editText : control.displayText
  85. enabled: control.editable
  86. autoScroll: control.editable
  87. readOnly: control.down
  88. inputMethodHints: control.inputMethodHints
  89. validator: control.validator
  90. selectByMouse: control.selectTextByMouse
  91. font: control.font
  92. color: control.flat ? control.palette.windowText : control.editable ? control.palette.text : control.palette.buttonText
  93. selectionColor: control.palette.highlight
  94. selectedTextColor: control.palette.highlightedText
  95. verticalAlignment: Text.AlignVCenter
  96. }
  97. background: NinePatchImage {
  98. source: Imagine.url + "combobox-background"
  99. NinePatchImageSelector on source {
  100. states: [
  101. {"disabled": !control.enabled},
  102. {"pressed": control.pressed},
  103. {"editable": control.editable},
  104. {"open": control.down},
  105. {"focused": control.visualFocus || (control.editable && control.activeFocus)},
  106. {"mirrored": control.mirrored},
  107. {"hovered": control.hovered},
  108. {"flat": control.flat}
  109. ]
  110. }
  111. }
  112. popup: T.Popup {
  113. width: control.width
  114. height: Math.min(contentItem.implicitHeight + topPadding + bottomPadding, control.Window.height - topMargin - bottomMargin)
  115. topMargin: background.topInset
  116. bottomMargin: background.bottomInset
  117. topPadding: background.topPadding
  118. leftPadding: background.leftPadding
  119. rightPadding: background.rightPadding
  120. bottomPadding: background.bottomPadding
  121. topInset: background ? -background.topInset || 0 : 0
  122. leftInset: background ? -background.leftInset || 0 : 0
  123. rightInset: background ? -background.rightInset || 0 : 0
  124. bottomInset: background ? -background.bottomInset || 0 : 0
  125. palette.text: control.palette.text
  126. palette.highlight: control.palette.highlight
  127. palette.highlightedText: control.palette.highlightedText
  128. palette.windowText: control.palette.windowText
  129. palette.buttonText: control.palette.buttonText
  130. contentItem: ListView {
  131. clip: true
  132. implicitHeight: contentHeight
  133. model: control.delegateModel
  134. currentIndex: control.highlightedIndex
  135. highlightMoveDuration: 0
  136. T.ScrollIndicator.vertical: ScrollIndicator { }
  137. }
  138. background: NinePatchImage {
  139. source: Imagine.url + "combobox-popup"
  140. NinePatchImageSelector on source {
  141. states: [
  142. {"disabled": !control.enabled},
  143. {"pressed": control.pressed},
  144. {"editable": control.editable},
  145. {"focused": control.visualFocus || (control.editable && control.activeFocus)},
  146. {"mirrored": control.mirrored},
  147. {"hovered": control.hovered},
  148. {"flat": control.flat}
  149. ]
  150. }
  151. }
  152. }
  153. }