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

KDE's Doxygen guidelines are available online.