MauiKit Controls

TextField.qml
1/*
2 * Copyright 2018 Camilo Higuita <milo.h@aol.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library General Public License as
6 * published by the Free Software Foundation; either version 2, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20import QtQuick
21import QtQuick.Controls as QC
22import QtQuick.Templates as T
23
24import org.mauikit.controls as Maui
25import QtQuick.Layouts
26
27
28/**
29 * TextField
30 * A customizable text field for MauiKit applications.
31 *
32 *
33 *
34 *
35 *
36 *
37 */
38T.TextField
39{
40 id: control
41 Maui.Theme.colorSet: Maui.Theme.Button
42 Maui.Theme.inherit: false
43
44 implicitHeight: _layout.implicitHeight + topPadding + bottomPadding
45 implicitWidth: 200
46 property int spacing: Maui.Style.space.small
47
48 /**
49 * menu : Menu
50 */
51 readonly property alias menu : entryMenu
52
53 /**
54 * actions : RowLayout
55 */
56 property list<QtObject> actions
57
58 property alias icon : _icon
59
60 property alias rightContent : _rightLayout.data
61
62 /**
63 * cleared
64 */
65 signal cleared()
66
67 /**
68 * contentDropped :
69 */
70 signal contentDropped(var drop)
71
72 function setTextColor(control)
73 {
74 if(control.Maui.Controls.status)
75 {
76 switch(control.Maui.Controls.status)
77 {
78 case Maui.Controls.Positive: return control.Maui.Theme.positiveBackgroundColor
79 case Maui.Controls.Negative: return control.Maui.Theme.negativeBackgroundColor
80 case Maui.Controls.Neutral: return control.Maui.Theme.neutralBackgroundColor
81 case Maui.Controls.Normal: return control.Maui.Theme.textColor
82 }
83 }
84
85 return control.Maui.Theme.textColor
86 }
87
88 hoverEnabled: !Maui.Handy.isMobile
89
90 opacity: control.enabled ? 1 : 0.5
91
92 color: Maui.Theme.textColor
93 selectionColor: Maui.Theme.highlightColor
94 selectedTextColor: Maui.Theme.highlightedTextColor
95 focus: true
96
97 verticalAlignment: Text.AlignVCenter
98 horizontalAlignment: Text.AlignLeft
99
100 padding: 0
101
102 leftPadding: Maui.Style.space.medium
103 rightPadding: _rightLayout.implicitWidth + Maui.Style.space.medium
104 topPadding: _titleLoader.implicitHeight
105 topInset: _titleLoader.implicitHeight
106
107 bottomPadding: _subtitleLoader.implicitHeight
108 bottomInset: _subtitleLoader.implicitHeight
109
110 selectByMouse: !Maui.Handy.isMobile
111 renderType: Screen.devicePixelRatio % 1 !== 0 ? Text.QtRendering : Text.NativeRendering
112
113 persistentSelection: true
114 font: Maui.Style.defaultFont
115
116 wrapMode: TextInput.NoWrap
117
118 onPressAndHold: (event) =>
119 {
120 if(Maui.Handy.isMobile)
121 {
122 entryMenu.show()
123 event.accepted = true
124 return
125 }
126
127 event.accepted = false
128 return
129 }
130
131 onPressed: (event) =>
132 {
133 if(!Maui.Handy.isMobile && event.button === Qt.RightButton)
134 {
135 entryMenu.show()
136 event.accepted = true
137 return
138 }
139
140 event.accepted = true
141 return
142 }
143
144 Keys.enabled: true
145
147 {
148 sequence: StandardKey.Escape
149 onActivated:
150 {
151 control.clear()
152 control.cleared()
153 }
154 }
155
156 Behavior on leftPadding
157 {
158 NumberAnimation
159 {
160 duration: Maui.Style.units.longDuration
161 easing.type: Easing.InOutQuad
162 }
163 }
164
165 Behavior on rightPadding
166 {
167 NumberAnimation
168 {
169 duration: Maui.Style.units.longDuration
170 easing.type: Easing.InOutQuad
171 }
172 }
173
174 background: Rectangle
175 {
176 implicitHeight: Maui.Style.iconSize
177 color: control.enabled ? (control.hovered ? Maui.Theme.hoverColor : Maui.Theme.backgroundColor) : "transparent"
178
179 radius: Maui.Style.radiusV
180
181 Behavior on color
182 {
183 Maui.ColorTransition{}
184 }
185
186 Behavior on border.color
187 {
188 Maui.ColorTransition{}
189 }
190
191 border.color: statusColor(control)
192
193 function statusColor(control)
194 {
195 if(control.Maui.Controls.status)
196 {
197 switch(control.Maui.Controls.status)
198 {
199 case Maui.Controls.Positive: return control.Maui.Theme.positiveBackgroundColor
200 case Maui.Controls.Negative: return control.Maui.Theme.negativeBackgroundColor
201 case Maui.Controls.Neutral: return control.Maui.Theme.neutralBackgroundColor
202 case Maui.Controls.Normal:
203 default:
204 return "transparent"
205 }
206 }
207
208 return "transparent"
209 }
210 }
211
212 Loader
213 {
214 id: _titleLoader
215 active: control.Maui.Controls.title && control.Maui.Controls.title.length > 0
216 visible: active
217
218 anchors.bottom: _layout.top
219
220 sourceComponent: QC.Label
221 {
222 text: control.Maui.Controls.title
223 color: setTextColor(control)
224 bottomPadding: Maui.Style.defaultSpacing
225 elide:Text.ElideRight
226 wrapMode: Text.NoWrap
227 }
228 }
229
230 Loader
231 {
232 id: _subtitleLoader
233 active: control.Maui.Controls.subtitle && control.Maui.Controls.subtitle.length > 0
234 visible: active
235
236 anchors.top: _layout.bottom
237
238 sourceComponent: QC.Label
239 {
240 text: control.Maui.Controls.subtitle
241 font.pointSize: Maui.Style.fontSizes.small
242 opacity: 0.6
243 color: setTextColor(control)
244 topPadding: Maui.Style.defaultSpacing
245 elide:Text.ElideRight
246 wrapMode: Text.WordWrap
247 }
248 }
249
250
251 RowLayout
252 {
253 id: _layout
254 clip: true
255
256 anchors.fill: parent
257
258 anchors.topMargin: _titleLoader.implicitHeight
259 anchors.bottomMargin: _subtitleLoader.implicitHeight
260
261 anchors.leftMargin: Maui.Style.space.medium
262 anchors.rightMargin: _badgeLoader.visible ? 8 : 0
263
264 spacing: control.spacing
265
266 Maui.Icon
267 {
268 id: _icon
269 visible: source ? true : false
270 implicitHeight: visible ? 16 : 0
271 implicitWidth: height
272 color: control.color
273 opacity: placeholder.opacity
274 }
275
276 Item
277 {
278 Layout.preferredHeight: Maui.Style.iconSize + (Maui.Style.defaultPadding * 2) //simulate the implicitHeight of common button controls
279 }
280
281 Item
282 {
283 Layout.fillWidth: true
284 visible: !placeholder.visible
285 }
286
287 QC.Label
288 {
289 id: placeholder
290 Layout.fillWidth: true
291 text: control.placeholderText
292 font: control.font
293 color: control.color
294 verticalAlignment: control.verticalAlignment
295 elide: Text.ElideRight
296 wrapMode: Text.NoWrap
297
298 visible: opacity > 0
299 opacity: !control.length && !control.preeditText && !control.activeFocus ? 0.5 : 0
300
301 Behavior on opacity
302 {
303 NumberAnimation
304 {
305 duration: Maui.Style.units.longDuration
306 easing.type: Easing.InOutQuad
307 }
308 }
309 }
310
311 Row
312 {
313 id: _rightLayout
314
315 z: parent.z + 1
316 spacing: control.spacing
317
318 QC.ToolButton
319 {
320 flat: !checkable
321 focusPolicy: Qt.NoFocus
322 topInset: 2
323 rightInset: 2
324 bottomInset: 2
325 visible: control.text.length || control.activeFocus
326 icon.name: "edit-clear"
327
328 onClicked:
329 {
330 control.clear()
331 cleared()
332 }
333 }
334
335 Repeater
336 {
337 model: control.actions
338
339 QC.ToolButton
340 {
341 flat: !checkable
342 focusPolicy: Qt.NoFocus
343 action: modelData
344 checkable: action.checkable
345 topInset: 2
346 rightInset: 2
347 bottomInset: 2
348 }
349 }
350 }
351 }
352
353 Loader
354 {
355 id: _badgeLoader
356 asynchronous: true
357 active: control.Maui.Controls.badgeText && control.Maui.Controls.badgeText.length > 0 && control.visible
358 visible: active
359
360 anchors.horizontalCenter: parent.right
361 anchors.verticalCenter: parent.top
362 anchors.verticalCenterOffset: 10
363 anchors.horizontalCenterOffset: -5
364
365 sourceComponent: Maui.Badge
366 {
367 text: control.Maui.Controls.badgeText
368
369 padding: 2
370 font.pointSize: Maui.Style.fontSizes.tiny
371
372 Maui.Theme.colorSet: Maui.Theme.View
373 Maui.Theme.backgroundColor: Maui.Theme.negativeBackgroundColor
374 Maui.Theme.textColor: Maui.Theme.negativeTextColor
375
376 OpacityAnimator on opacity
377 {
378 from: 0
379 to: 1
380 duration: Maui.Style.units.longDuration
381 running: parent.visible
382 }
383
384 ScaleAnimator on scale
385 {
386 from: 0.5
387 to: 1
388 duration: Maui.Style.units.longDuration
389 running: parent.visible
390 easing.type: Easing.OutInQuad
391 }
392 }
393 }
394
395 Maui.ContextualMenu
396 {
397 id: entryMenu
398
399 QC.MenuItem
400 {
401 text: i18nd("mauikit", "Copy")
402 onTriggered: control.copy()
403 enabled: control.selectedText.length
404 }
405
406 QC.MenuItem
407 {
408 text: i18nd("mauikit", "Cut")
409 onTriggered: control.cut()
410 enabled: control.selectedText.length
411 }
412
413 QC.MenuItem
414 {
415 text: i18nd("mauikit", "Paste")
416 onTriggered:
417 {
418 var text = control.paste()
419 control.insert(control.cursorPosition, text)
420 }
421 }
422
423 QC.MenuItem
424 {
425 text: i18nd("mauikit", "Select All")
426 onTriggered: control.selectAll()
427 enabled: control.text.length
428 }
429
430 QC.MenuItem
431 {
432 text: i18nd("mauikit", "Undo")
433 onTriggered: control.undo()
434 enabled: control.canUndo
435 }
436
437 QC.MenuItem
438 {
439 text: i18nd("mauikit", "Redo")
440 onTriggered: control.redo()
441 enabled: control.canRedo
442 }
443 }
444
445 Loader
446 {
447 asynchronous: true
448 anchors.fill: parent
449 sourceComponent: DropArea
450 {
451 onDropped: (drop) =>
452 {
453 console.log(drop.text, drop.html)
454 if (drop.hasText)
455 {
456 control.text += drop.text
457
458 }else if(drop.hasUrls)
459 {
460 control.text = drop.urls
461 }
462
463 control.contentDropped(drop)
464 }
465 }
466 }
467
468 // Loader
469 // {
470 // active: control.Maui.Controls.subtitle && control.Maui.Controls.subtitle.length > 0
471 // visible: active
472 // height: visible ? implicitHeight : -_mainLayout.spacing
473 // Layout.fillWidth: true
474
475 // sourceComponent: QC.Label
476 // {
477 // text: control.Maui.Controls.subtitle
478 // font.pointSize: Maui.Style.fontSizes.small
479 // opacity: 0.6
480 // color: setTextColor(control)
481 // }
482 // }
483}
484
The MauiKit Style preferences singleton object.
Definition style.h:86
list< QtObject > actions
actions : RowLayout
Definition TextField.qml:50
void cleared()
cleared
void contentDropped(var drop)
contentDropped :
QString i18nd(const char *domain, const char *text, const TYPE &arg...)
AKONADI_CALENDAR_EXPORT KCalendarCore::Event::Ptr event(const Akonadi::Item &item)
KIOWIDGETS_EXPORT DropJob * drop(const QDropEvent *dropEvent, const QUrl &destUrl, DropJobFlags dropjobFlags, JobFlags flags=DefaultFlags)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri May 2 2025 11:57:11 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.