-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTagTextField.qml
130 lines (116 loc) · 4.57 KB
/
TagTextField.qml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import QtQuick 2.15
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.5
import AppStyle 1.0
import "./"
ColumnLayout {
property int allowMaxTags: 5
property var tagList: []
spacing: 10
Flow{
spacing: 5
Layout.maximumWidth: control.width
Repeater {
id:rep
width: parent.width + 20
model: tagList
function removeTag(tag) {
var index = tagList.indexOf(tag);
if (index !== -1) {
tagList.splice(index, 1);
}
}
function removeAndRefresh(tag){
removeTag(tag)
rep.model = []
rep.model = tagList
control.placeholderText = "Enter tag"
}
Rectangle{
id: tagBackground
property color tagColor: generateRandomColor()
implicitHeight: control.implicitHeight - 25
implicitWidth: tags.implicitWidth + 20 + cross.width
radius: control.radius - 2
border.width: tagHovered.hovered ? 1 : 0
border.color: tagHovered.hovered ? AppStyle.appStyle : AppStyle.borderColor
color: Qt.lighter(tagBackground.tagColor)
function generateRandomColor() {
var red = Math.random()
var green = Math.random()
var blue = Math.random()
var alpha = 1.0 // Set the alpha component to 1.0 for full opacity
return Qt.rgba(red, green, blue, alpha)
}
function lighterColor(color) {
var lighterFactor = 0.2; // Adjust this value to control the lightness
var red = Math.min(color.r + lighterFactor, 1.0);
var green = Math.min(color.g + lighterFactor, 1.0);
var blue = Math.min(color.b + lighterFactor, 1.0);
return Qt.rgba(red, green, blue, color.a);
}
HoverHandler{
id: tagHovered
}
RowLayout {
anchors.centerIn: parent
spacing: 5
Label {
id:tags
text: modelData
color:Qt.darker(tagBackground.tagColor,2.5)
Layout.leftMargin: 5
Layout.alignment: Qt.AlignVCenter
}
Rectangle{
id:cross
Layout.alignment: Qt.AlignVCenter
Layout.rightMargin: 5
width: 18
height: 18
color: tagBackground.tagColor
radius: width / 2
Label {
text: "🗙"
font.pixelSize: 12
color: Qt.lighter(tagBackground.tagColor)
anchors.centerIn: parent
bottomPadding: 2
scale: clickArea.pressed ? 0.9 : 1.0
}
MouseArea{
id:clickArea
anchors.fill: parent
onClicked: {
rep.removeAndRefresh(modelData)
}
}
}
}
}
}
}
TextField {
id: control
width: 200
placeholderText: "Enter tag"
onAccepted: {
var enteredTag = control.text.toLowerCase(); // Convert entered tag to lowercase
if (enteredTag !== "" && !isTagInList(enteredTag) && tagList.length < allowMaxTags) {
tagList.push(control.text)
rep.model = []
rep.model = tagList
control.text = ""
}else if(isTagInList(enteredTag)){
control.text = ""
}else if(tagList.length >= allowMaxTags){
control.text = ""
control.placeholderText = "Maximum tag limit reached!"
}
}
function isTagInList(tag) {
var lowercaseTags = tagList.map(function(item) { return item.toLowerCase() });
return lowercaseTags.includes(tag.toLowerCase());
}
}
}