|
| 1 | +import QtQuick 2.15 |
| 2 | +import QtQuick.Templates 2.15 as T |
| 3 | + |
| 4 | +import "qrc:/../common" |
| 5 | +import "qrc:/../DelPopup" |
| 6 | +import "qrc:/../DelScrollBar" |
| 7 | +import "qrc:/../DelToolTip" |
| 8 | + |
| 9 | +T.ComboBox { |
| 10 | + id: control |
| 11 | + |
| 12 | + property bool animationEnabled: true |
| 13 | + property int hoverCursorShape: Qt.PointingHandCursor |
| 14 | + property bool tooltipVisible: false |
| 15 | + property bool loading: false |
| 16 | + property int defaulPopupMaxHeight: 240 |
| 17 | + property color colorText: enabled ? "#000" : Qt.rgba(0,0,0,0.45) |
| 18 | + property color colorBorder: enabled ? hovered ? "#1677ff" : Qt.rgba(0,0,0,0.25) : "transparent" |
| 19 | + property color colorBg: enabled ? "#fff" : Qt.rgba(0,0,0,0.1) |
| 20 | + |
| 21 | + property int radiusBg: 6 |
| 22 | + property int radiusPopupBg: 6 |
| 23 | + property string contentDescription: "" |
| 24 | + |
| 25 | + property Component indicatorDelegate: DelIconText { |
| 26 | + iconSize: 12 |
| 27 | + iconSource: control.loading ? DelIcon.LoadingOutlined : DelIcon.DownOutlined |
| 28 | + |
| 29 | + NumberAnimation on rotation { |
| 30 | + running: control.loading |
| 31 | + from: 0 |
| 32 | + to: 360 |
| 33 | + loops: Animation.Infinite |
| 34 | + duration: 1000 |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + Behavior on colorText { enabled: control.animationEnabled; ColorAnimation { duration: 100 } } |
| 39 | + Behavior on colorBorder { enabled: control.animationEnabled; ColorAnimation { duration: 100 } } |
| 40 | + Behavior on colorBg { enabled: control.animationEnabled; ColorAnimation { duration: 100 } } |
| 41 | + |
| 42 | + rightPadding: 8 |
| 43 | + topPadding: 5 |
| 44 | + bottomPadding: 5 |
| 45 | + implicitWidth: implicitContentWidth + implicitIndicatorWidth + leftPadding + rightPadding |
| 46 | + implicitHeight: implicitContentHeight + topPadding + bottomPadding |
| 47 | + textRole: "label" |
| 48 | + valueRole: "value" |
| 49 | + objectName: "__DelSelect__" |
| 50 | + font { |
| 51 | + family: "微软雅黑" |
| 52 | + pixelSize: 14 |
| 53 | + } |
| 54 | + delegate: T.ItemDelegate { } |
| 55 | + indicator: Loader { |
| 56 | + x: control.width - width - control.rightPadding |
| 57 | + y: control.topPadding + (control.availableHeight - height) / 2 |
| 58 | + sourceComponent: indicatorDelegate |
| 59 | + } |
| 60 | + contentItem: Text { |
| 61 | + leftPadding: 8 |
| 62 | + rightPadding: control.indicator.width + control.spacing |
| 63 | + text: control.displayText |
| 64 | + font: control.font |
| 65 | + color: control.colorText |
| 66 | + verticalAlignment: Text.AlignVCenter |
| 67 | + elide: Text.ElideRight |
| 68 | + } |
| 69 | + background: Rectangle { |
| 70 | + color: control.colorBg |
| 71 | + border.color: control.colorBorder |
| 72 | + border.width: control.visualFocus ? 2 : 1 |
| 73 | + radius: control.radiusBg |
| 74 | + } |
| 75 | + popup: DelPopup { |
| 76 | + y: control.height + 2 |
| 77 | + implicitWidth: control.width |
| 78 | + implicitHeight: implicitContentHeight + topPadding + bottomPadding |
| 79 | + leftPadding: 4 |
| 80 | + rightPadding: 4 |
| 81 | + topPadding: 6 |
| 82 | + bottomPadding: 6 |
| 83 | + enter: Transition { NumberAnimation { property: "opacity"; from: 0.0; to: 1.0; duration: control.animationEnabled ? 200 : 0 } } |
| 84 | + exit: Transition { NumberAnimation { property: "opacity"; from: 1.0; to: 0; duration: control.animationEnabled ? 200 : 0 } } |
| 85 | + contentItem: ListView { |
| 86 | + id: __popupListView |
| 87 | + clip: true |
| 88 | + implicitHeight: Math.min(control.defaulPopupMaxHeight, contentHeight) |
| 89 | + model: control.popup.visible ? control.model : null |
| 90 | + currentIndex: control.highlightedIndex |
| 91 | + boundsBehavior: Flickable.StopAtBounds |
| 92 | + delegate: T.ItemDelegate { |
| 93 | + id: __popupDelegate |
| 94 | + |
| 95 | + required property var modelData |
| 96 | + required property int index |
| 97 | + property alias model: __popupDelegate.modelData |
| 98 | + |
| 99 | + width: __popupListView.width |
| 100 | + height: implicitContentHeight + topPadding + bottomPadding |
| 101 | + leftPadding: 8 |
| 102 | + rightPadding: 8 |
| 103 | + topPadding: 4 |
| 104 | + bottomPadding: 4 |
| 105 | + enabled: model.enabled ?? true |
| 106 | + contentItem: Text { |
| 107 | + text: __popupDelegate.model[control.textRole] |
| 108 | + color: __popupDelegate.enabled ? "#000" : Qt.rgba(0,0,0,0.45) |
| 109 | + font { |
| 110 | + family: "微软雅黑" |
| 111 | + pixelSize: 14 |
| 112 | + weight: highlighted ? Font.DemiBold : Font.Normal |
| 113 | + } |
| 114 | + elide: Text.ElideRight |
| 115 | + verticalAlignment: Text.AlignVCenter |
| 116 | + } |
| 117 | + background: Rectangle { |
| 118 | + radius: 2 |
| 119 | + color: { |
| 120 | + if (__popupDelegate.enabled) |
| 121 | + return highlighted ? "#e6f4ff" : hovered ? Qt.rgba(0,0,0,0.1) : "transparent"; |
| 122 | + else |
| 123 | + return "transparent"; |
| 124 | + } |
| 125 | + |
| 126 | + Behavior on color { enabled: control.animationEnabled; ColorAnimation { duration: 100 } } |
| 127 | + } |
| 128 | + highlighted: control.highlightedIndex === index |
| 129 | + onClicked: { |
| 130 | + control.currentIndex = index; |
| 131 | + control.activated(index); |
| 132 | + control.popup.close(); |
| 133 | + } |
| 134 | + |
| 135 | + HoverHandler { |
| 136 | + cursorShape: control.hoverCursorShape |
| 137 | + } |
| 138 | + |
| 139 | + Loader { |
| 140 | + y: __popupDelegate.height |
| 141 | + anchors.horizontalCenter: parent.horizontalCenter |
| 142 | + active: control.tooltipVisible |
| 143 | + sourceComponent: DelToolTip { |
| 144 | + arrowVisible: false |
| 145 | + visible: __popupDelegate.hovered |
| 146 | + text: __popupDelegate.model[control.textRole] |
| 147 | + position: DelToolTip.Position_Bottom |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + T.ScrollBar.vertical: DelScrollBar { } |
| 152 | + |
| 153 | + Behavior on height { enabled: control.animationEnabled; NumberAnimation { duration: 100 } } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + HoverHandler { |
| 158 | + cursorShape: control.hoverCursorShape |
| 159 | + } |
| 160 | + |
| 161 | + Accessible.role: Accessible.ComboBox |
| 162 | + Accessible.name: control.displayText |
| 163 | + Accessible.description: control.contentDescription |
| 164 | +} |
0 commit comments