Libplasma

ComboBox.qml
1/*
2 SPDX-FileCopyrightText: 2016 Marco Martin <mart@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7import QtQuick
8import QtQuick.Templates as T
9import QtQuick.Controls as Controls
10import org.kde.ksvg as KSvg
11//NOTE: importing PlasmaCore is necessary in order to make KSvg load the current Plasma Theme
12import org.kde.plasma.core as PlasmaCore
13import org.kde.kirigami as Kirigami
14import "private" as Private
15import "mobiletextselection" as MobileTextSelection
17T.ComboBox {
18 id: control
19
20 property real __indicatorMargin: control.indicator && control.indicator.visible && control.indicator.width > 0 ? control.spacing + indicator.width : 0
21
22 implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset,
23 implicitContentWidth + leftPadding + rightPadding)
24 implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
25 implicitContentHeight + topPadding + bottomPadding,
26 implicitIndicatorHeight + topPadding + bottomPadding)
27
28 baselineOffset: contentItem.y + contentItem.baselineOffset
29
30 hoverEnabled: true
31
32 topPadding: surfaceNormal.margins.top
33 leftPadding: surfaceNormal.margins.left + (!control.mirrored ? 0 : __indicatorMargin)
34 rightPadding: surfaceNormal.margins.right + (control.mirrored ? 0 : __indicatorMargin)
35 bottomPadding: surfaceNormal.margins.bottom
36 spacing: Kirigami.Units.smallSpacing
37
38 delegate: ItemDelegate {
39 width: control.popup.width
40 text: control.textRole ? (Array.isArray(control.model) ? modelData[control.textRole] : model[control.textRole]) : modelData
41 highlighted: control.highlightedIndex == index
42 property bool separatorVisible: false
43 }
44
45 indicator: KSvg.SvgItem {
46 implicitWidth: Kirigami.Units.iconSizes.small
47 implicitHeight: implicitWidth
48 anchors {
49 right: parent.right
50 rightMargin: surfaceNormal.margins.right
51 verticalCenter: parent.verticalCenter
52 }
53 svg: KSvg.Svg {
54 imagePath: "widgets/arrows"
55 colorSet: KSvg.Svg.Button
56 }
57 elementId: "down-arrow"
58 }
59
60 contentItem: T.TextField {
61 id: textField
62 implicitWidth: Math.ceil(contentWidth) + leftPadding + rightPadding
63 implicitHeight: Math.ceil(contentHeight) + topPadding + bottomPadding
64 padding: 0
65 text: control.editable ? control.editText : control.displayText
66
67 enabled: control.editable
68 autoScroll: control.editable
69
70 readOnly: control.down || !control.editable
71 inputMethodHints: control.inputMethodHints
72 validator: control.validator
73 color: Kirigami.Theme.textColor
74 selectionColor: Kirigami.Theme.highlightColor
75 selectedTextColor: Kirigami.Theme.highlightedTextColor
76
77 selectByMouse: !Kirigami.Settings.tabletMode
78 cursorDelegate: Kirigami.Settings.tabletMode ? mobileCursor : null
79
80 font: control.font
81 horizontalAlignment: Text.AlignLeft
82 verticalAlignment: Text.AlignVCenter
83 opacity: control.enabled ? 1 : 0.3
84 onFocusChanged: {
85 if (focus) {
86 MobileTextSelection.MobileTextActionsToolBar.controlRoot = textField;
87 }
88 }
89
90 onTextChanged: MobileTextSelection.MobileTextActionsToolBar.shouldBeVisible = false;
91 onPressed: event => MobileTextSelection.MobileTextActionsToolBar.shouldBeVisible = true;
92
93 onPressAndHold: event => {
94 if (!Kirigami.Settings.tabletMode) {
95 return;
96 }
97 forceActiveFocus();
98 cursorPosition = positionAt(event.x, event.y);
99 selectWord();
100 }
101 }
102
103 Component {
104 id: mobileCursor
105 MobileTextSelection.MobileCursor {
106 target: textField
107 }
108 }
109
110 MobileTextSelection.MobileCursor {
111 target: textField
112 selectionStartHandle: true
113 property var rect: textField.positionToRectangle(textField.selectionStart)
114 //FIXME: this magic values seem to be always valid, for every font,every dpi, every scaling
115 x: rect.x + 5
116 y: rect.y + 6
117 }
118
119 background: KSvg.FrameSvgItem {
120 id: surfaceNormal
121
122 anchors.fill: parent
123
124 imagePath: control.editable ? "widgets/lineedit" : "widgets/button"
125 prefix: control.editable
126 ? "base"
127 : (control.down ? "pressed" : "normal")
128
129 Private.ButtonShadow {
130 anchors.fill: parent
131 showShadow: !control.editable && !control.down
132 }
133
134 Private.TextFieldFocus {
135 visible: control.editable
136 z: -1
137 state: control.activeFocus ? "focus" : (control.hovered ? "hover" : "hidden")
138 anchors.fill: parent
139 }
140
141 Private.ButtonFocus {
142 anchors.fill: parent
143 showFocus: control.activeFocus && !control.down
144 }
145
146 Private.ButtonHover {
147 anchors.fill: parent
148 showHover: control.hovered && !control.down
149 }
150
151 MouseArea {
152 anchors {
153 fill: parent
154 leftMargin: control.leftPadding
155 rightMargin: control.rightPadding
156 }
157 acceptedButtons: Qt.NoButton
158 onWheel: wheel => {
159 if (wheel.pixelDelta.y < 0 || wheel.angleDelta.y < 0) {
160 control.currentIndex = Math.min(control.currentIndex + 1, delegateModel.count -1);
161 } else {
162 control.currentIndex = Math.max(control.currentIndex - 1, 0);
163 }
164 control.activated(control.currentIndex);
165 }
166 }
167 }
168
169 popup: T.Popup {
170 x: control.mirrored ? control.width - width : 0
171 y: control.height
172 width: Math.max(control.width, 150)
173 implicitHeight: contentItem.implicitHeight
174 topMargin: 6
175 bottomMargin: 6
176
177 contentItem: ListView {
178 id: listView
179 clip: true
180 implicitHeight: contentHeight
181 model: control.popup.visible ? control.delegateModel : null
182 currentIndex: control.highlightedIndex
183 highlightRangeMode: ListView.ApplyRange
184 highlightMoveDuration: 0
185 // HACK: When the ComboBox is not inside a top-level Window, it's Popup does not inherit
186 // the LayoutMirroring options. This is a workaround to fix this by enforcing
187 // the LayoutMirroring options properly.
188 // QTBUG: https://bugreports.qt.io/browse/QTBUG-66446
189 LayoutMirroring.enabled: Qt.application.layoutDirection === Qt.RightToLeft
190 LayoutMirroring.childrenInherit: true
191 T.ScrollBar.vertical: Controls.ScrollBar { }
192 }
193 background: Kirigami.ShadowedRectangle {
194 anchors {
195 fill: parent
196 margins: -1
197 }
198 radius: 2
199 Kirigami.Theme.colorSet: Kirigami.Theme.View
200 Kirigami.Theme.inherit: false
201 color: Kirigami.Theme.backgroundColor
202 border {
203 color: Qt.rgba(Kirigami.Theme.textColor.r, Kirigami.Theme.textColor.g, Kirigami.Theme.textColor.b, 0.3)
204 width: 1
205 }
206 shadow {
207 size: 4
208 xOffset: 2
209 yOffset: 2
210 color: Qt.rgba(0, 0, 0, 0.3)
211 }
212 }
213 }
214}
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:06:24 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.