-
Notifications
You must be signed in to change notification settings - Fork 3
/
ColorField.qml
159 lines (143 loc) · 4.07 KB
/
ColorField.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Version 8
import QtQuick
import QtQuick.Controls as QQC2
import QtQuick.Dialogs as QtDialogs
import QtQuick.Window
import org.kde.kirigami as Kirigami
// https://doc.qt.io/qt-6/qtgraphicaleffects5-index.html
import Qt5Compat.GraphicalEffects as QtGraphicalEffects // TODO Deprecated in Qt6
QQC2.TextField {
id: colorField
font.family: "monospace"
readonly property string defaultText: "#AARRGGBB"
placeholderText: defaultColor ? defaultColor : defaultText
onTextChanged: {
// Make sure the text is:
// Empty (use default)
// or #123 or #112233 or #11223344 before applying the color.
if (text.length === 0
|| (text.indexOf('#') === 0 && (text.length == 4 || text.length == 7 || text.length == 9))
) {
colorField.value = text
}
}
property bool showAlphaChannel: true
property bool showPreviewBg: true
property string configKey: ''
property string defaultColor: ''
property string value: {
if (configKey) {
return plasmoid.configuration[configKey]
} else {
return "#000"
}
}
readonly property color defaultColorValue: defaultColor
readonly property color valueColor: {
if (value == '' && defaultColor) {
return defaultColor
} else {
return value
}
}
onValueChanged: {
if (!activeFocus) {
text = colorField.value
}
if (configKey) {
if (value == defaultColorValue) {
plasmoid.configuration[configKey] = ""
} else {
plasmoid.configuration[configKey] = value
}
}
}
leftPadding: rightPadding + mouseArea.height + rightPadding
FontMetrics {
id: fontMetrics
font.family: colorField.font.family
font.italic: colorField.font.italic
font.pointSize: colorField.font.pointSize
font.pixelSize: colorField.font.pixelSize
font.weight: colorField.font.weight
}
readonly property int defaultWidth: Math.ceil(fontMetrics.advanceWidth(defaultText))
implicitWidth: rightPadding + Math.max(defaultWidth, contentWidth) + leftPadding
MouseArea {
id: mouseArea
anchors.leftMargin: parent.rightPadding
anchors.topMargin: parent.topPadding
anchors.bottomMargin: parent.bottomPadding
anchors.left: parent.left
anchors.top: parent.top
anchors.bottom: parent.bottom
width: height
hoverEnabled: true
cursorShape: Qt.PointingHandCursor
onClicked: dialogLoader.active = true
// Color Preview Circle
Rectangle {
id: previewBgMask
visible: false
anchors.fill: parent
border.width: 1 * Screen.devicePixelRatio
border.color: "transparent"
radius: width / 2
}
QtGraphicalEffects.ConicalGradient {
id: previewBgGradient
visible: colorField.showPreviewBg
anchors.fill: parent
angle: 0.0
gradient: Gradient {
GradientStop { position: 0.00; color: "white" }
GradientStop { position: 0.24; color: "white" }
GradientStop { position: 0.25; color: "#cccccc" }
GradientStop { position: 0.49; color: "#cccccc" }
GradientStop { position: 0.50; color: "white" }
GradientStop { position: 0.74; color: "white" }
GradientStop { position: 0.75; color: "#cccccc" }
GradientStop { position: 1.00; color: "#cccccc" }
}
source: previewBgMask
}
Rectangle {
id: previewFill
anchors.fill: parent
color: colorField.valueColor
border.width: 1 * Screen.devicePixelRatio
border.color: Kirigami.ColorUtils.linearInterpolation(color, Kirigami.Theme.textColor, 0.5)
radius: width / 2
}
}
Loader {
id: dialogLoader
active: false
sourceComponent: QtDialogs.ColorDialog {
id: dialog
visible: true
modality: Qt.WindowModal
options: colorField.showAlphaChannel ? QtDialogs.ColorDialog.ShowAlphaChannel : 0
selectedColor: colorField.valueColor
onSelectedColorChanged: {
if (visible) {
colorField.text = selectedColor
}
}
onAccepted: {
colorField.text = selectedColor
dialogLoader.active = false
}
onRejected: {
// This event is also triggered when the user clicks outside the popup modal.
// TODO Find a way to only trigger when Cancel is clicked.
colorField.text = initColor
dialogLoader.active = false
}
property color initColor
Component.onCompleted: {
initColor = colorField.valueColor
}
}
}
}