ComboBox.qml 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.Controls.impl
  38. import QtQuick.Templates as T
  39. T.ComboBox {
  40. id: control
  41. implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset,
  42. implicitContentWidth + leftPadding + rightPadding)
  43. implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
  44. implicitContentHeight + topPadding + bottomPadding,
  45. implicitIndicatorHeight + topPadding + bottomPadding)
  46. leftPadding: padding + (!control.mirrored || !indicator || !indicator.visible ? 0 : indicator.width + spacing)
  47. rightPadding: padding + (control.mirrored || !indicator || !indicator.visible ? 0 : indicator.width + spacing)
  48. delegate: ItemDelegate {
  49. width: ListView.view.width
  50. text: control.textRole ? (Array.isArray(control.model) ? modelData[control.textRole] : model[control.textRole]) : modelData
  51. palette.text: control.palette.text
  52. palette.highlightedText: control.palette.highlightedText
  53. font.weight: control.currentIndex === index ? Font.DemiBold : Font.Normal
  54. highlighted: control.highlightedIndex === index
  55. hoverEnabled: control.hoverEnabled
  56. }
  57. indicator: ColorImage {
  58. x: control.mirrored ? control.padding : control.width - width - control.padding
  59. y: control.topPadding + (control.availableHeight - height) / 2
  60. color: control.palette.dark
  61. defaultColor: "#353637"
  62. source: "qrc:/qt-project.org/imports/QtQuick/Controls/Basic/images/double-arrow.png"
  63. opacity: enabled ? 1 : 0.3
  64. }
  65. contentItem: T.TextField {
  66. leftPadding: !control.mirrored ? 12 : control.editable && activeFocus ? 3 : 1
  67. rightPadding: control.mirrored ? 12 : control.editable && activeFocus ? 3 : 1
  68. topPadding: 6 - control.padding
  69. bottomPadding: 6 - control.padding
  70. text: control.editable ? control.editText : control.displayText
  71. enabled: control.editable
  72. autoScroll: control.editable
  73. readOnly: control.down
  74. inputMethodHints: control.inputMethodHints
  75. validator: control.validator
  76. selectByMouse: control.selectTextByMouse
  77. font: control.font
  78. color: control.editable ? control.palette.text : control.palette.buttonText
  79. selectionColor: control.palette.highlight
  80. selectedTextColor: control.palette.highlightedText
  81. verticalAlignment: Text.AlignVCenter
  82. background: Rectangle {
  83. visible: control.enabled && control.editable && !control.flat
  84. border.width: parent && parent.activeFocus ? 2 : 1
  85. border.color: parent && parent.activeFocus ? control.palette.highlight : control.palette.button
  86. color: control.palette.base
  87. }
  88. }
  89. background: Rectangle {
  90. implicitWidth: 140
  91. implicitHeight: 40
  92. color: control.down ? control.palette.mid : control.palette.button
  93. border.color: control.palette.highlight
  94. border.width: !control.editable && control.visualFocus ? 2 : 0
  95. visible: !control.flat || control.down
  96. }
  97. popup: T.Popup {
  98. y: control.height
  99. width: control.width
  100. height: Math.min(contentItem.implicitHeight, control.Window.height - topMargin - bottomMargin)
  101. topMargin: 6
  102. bottomMargin: 6
  103. contentItem: ListView {
  104. clip: true
  105. implicitHeight: contentHeight
  106. model: control.delegateModel
  107. currentIndex: control.highlightedIndex
  108. highlightMoveDuration: 0
  109. Rectangle {
  110. z: 10
  111. width: parent.width
  112. height: parent.height
  113. color: "transparent"
  114. border.color: control.palette.mid
  115. }
  116. T.ScrollIndicator.vertical: ScrollIndicator { }
  117. }
  118. background: Rectangle {
  119. color: control.palette.window
  120. }
  121. }
  122. }