summaryrefslogtreecommitdiffstats
path: root/src/webenginequick/ui/ColorDialog.qml
blob: 56216f49be384656c33cba4db01363d183de51d9 (plain)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtWebEngine module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

import QtQuick
import QtQuick.Controls
import QtQuick.Layouts

Dialog {
    id: colorDialog
    title: qsTr("Color Picker Dialog")
    modal: false
    anchors.centerIn: parent
    objectName: "colorDialog"

    property bool handled: false
    property color color

    signal selectedColor(var color)
    signal rejected()

    function accept() {
        handled = true;
        selectedColor(colorDialog.color)
        close();
    }

    function reject() {
        handled = true;
        rejected();
        close();
    }

    // Handle the case where users simply closes or clicks out of the dialog.
    onVisibleChanged: {
        if (!visible && !handled) {
            handled = true;
            rejected();
        } else {
            handled = false;
        }
    }

    function selectColorFromPalette(paletteColor) {
        colorDialog.color = paletteColor;
    }

    function zeroPadding(text, length = 2) {
        var textLength = text.length;

        if (textLength >= length) {
            return text;
        }

        for (var i = 0; i < length - textLength; i++) {
            text = "0" + text;
        }

        return text;
    }

    function calculateRGBA() {

        var rgbArray = [colorDialog.color.r, colorDialog.color.g, colorDialog.color.b]
        if (colorDialog.color.a != 1) {
            rgbArray.push(colorDialog.color.a);
        }

        for (var i = 0; i < rgbArray.length; i++) {
            rgbArray[i] = Number(Math.round(rgbArray[i] * 255)).toString(16);
            rgbArray[i] = zeroPadding(rgbArray[i]);
        }

        return "#" + rgbArray.join("");
    }


    function isNaNOrUndefined(value) {
        return value == null || value == undefined || Number.isNaN(value);
    }

    function parseColorText(colorText) {
        if (colorText[0] == '#') {
            colorText = colorText.substring(1);
        }

        if (!(colorText.length == 6 || colorText.length == 8)) {
            return undefined;
        }

        var rgbaValues = [parseInt("0x" + colorText.substring(0,2)),
                          parseInt("0x" + colorText.substring(2,4)),
                          parseInt("0x" + colorText.substring(4,6)),
                          parseInt("0x" + (colorText.length > 6 ? colorText.substring(6,8) : "FF"))]

        for (var i = 0; i < rgbaValues.length; i++) {
            if (isNaNOrUndefined(rgbaValues[i])) {
                return undefined;
            }
            rgbaValues[i] = rgbaValues[i] / 255;
        }

        return Qt.rgba(rgbaValues[0], rgbaValues[1], rgbaValues[2], rgbaValues[3]);
    }

    ListModel {
        id: colorList
        ListElement { rectangleColor: "red" }
        ListElement { rectangleColor: "orangered" }
        ListElement { rectangleColor: "orange" }
        ListElement { rectangleColor: "gold" }
        ListElement { rectangleColor: "yellow" }
        ListElement { rectangleColor: "yellowgreen" }
        ListElement { rectangleColor: "green" }
        ListElement { rectangleColor: "lightskyblue" }
        ListElement { rectangleColor: "blue" }
        ListElement { rectangleColor: "blueviolet" }
        ListElement { rectangleColor: "violet" }
        ListElement { rectangleColor: "mediumvioletred" }
        ListElement { rectangleColor: "black" }
        ListElement { rectangleColor: "white" }
    }

    contentItem: GridLayout {
        id: grid
        columns: 7
        rows: 5

        Repeater {
            model: colorList
            delegate: Rectangle {
                width: 50
                height: 50
                color: rectangleColor
                border.color: "black"
                border.width: 1

                MouseArea {
                    anchors.fill: parent
                    onClicked: selectColorFromPalette(parent.color)
                }
            }
        }
        ColumnLayout {
            id: colorTools
            Layout.columnSpan: 4
            Layout.rowSpan: 2
            Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter

            Rectangle {
                height: 100
                width: 200
                border.color: "black"
                border.width: 1

                Binding on color {
                    when: colorDialog.color
                    value: colorDialog.color
                }
            }
            TextField {
                id: colorTextField
                width: 100
                selectByMouse: true
                Layout.alignment: Qt.AlignHCenter

                Binding on text {
                    when: colorDialog.color
                    value: calculateRGBA()
                    delayed: true
                }

                onTextEdited: {
                    var parsedColor = parseColorText(colorTextField.text);
                    if (parsedColor != undefined) {
                        colorDialog.color = parsedColor;
                    }
                }

                MouseArea {
                    id: colorTextFieldMouseArea
                    anchors.fill: parent
                    acceptedButtons: Qt.RightButton
                    onClicked: colorTextFieldContextMenu.open()
                }

                Menu {
                    id: colorTextFieldContextMenu
                    x: colorTextFieldMouseArea.mouseX
                    y: colorTextFieldMouseArea.mouseY
                    MenuItem {
                        text: qsTr("Copy color")
                        onTriggered: {
                            colorTextField.selectAll();
                            colorTextField.copy();
                            colorTextField.deselect();
                        }
                    }
                    MenuSeparator {}
                    MenuItem {
                        text: qsTr("Paste")
                        onTriggered: {
                            colorTextField.selectAll();
                            colorTextField.paste();
                        }
                        enabled: colorTextField.canPaste
                    }
                }
            }
        }
        ListModel {
            id: sliderBoxElements
            ListElement { labelText: "Red value"; colorChannel: 0 }
            ListElement { labelText: "Green value"; colorChannel: 1 }
            ListElement { labelText: "Blue value"; colorChannel: 2 }
            ListElement { labelText: "Alpha value"; colorChannel: 3 }
        }
        ColumnLayout {
            id: sliderBox
            Layout.columnSpan: 3
            Layout.rowSpan: 2
            Layout.fillWidth: true
            Layout.fillHeight: true

            Repeater {
                model: sliderBoxElements
                delegate: ColumnLayout {
                    Label {
                        text: labelText
                    }
                    Slider {
                        id: colorSlider
                        property int channel: colorChannel
                        from: 0
                        to: 255
                        stepSize: 1
                        value: {
                            if (colorSlider.channel == 0)
                                return colorDialog.color.r * 255;
                            else if (colorSlider.channel == 1)
                                return colorDialog.color.g * 255;
                            else if (colorSlider.channel == 2)
                                return colorDialog.color.b * 255;
                            else if (colorSlider.channel == 3)
                                return colorDialog.color.a * 255;
                        }

                        Connections {
                            function onMoved() {
                                var redChannelValue = colorDialog.color.r;
                                var greenChannelValue = colorDialog.color.g;
                                var blueChannelValue = colorDialog.color.b;
                                var alphaChannelValue = colorDialog.color.a;

                                if (colorSlider.channel == 0)
                                    redChannelValue = colorSlider.value / 255;
                                else if (colorSlider.channel == 1)
                                    greenChannelValue = colorSlider.value / 255;
                                else if (colorSlider.channel == 2)
                                    blueChannelValue = colorSlider.value / 255;
                                else if (colorSlider.channel == 3)
                                    alphaChannelValue = colorSlider.value / 255;

                                colorDialog.color = Qt.rgba(redChannelValue, greenChannelValue, blueChannelValue, alphaChannelValue);
                            }
                        }
                    }
                }
            }
        }
        DialogButtonBox {
            id: dialogButtonBox
            Layout.columnSpan: 7
            Layout.alignment: Qt.AlignRight

            Button {
                text: qsTr("Apply")
                onClicked: accept()
            }
            Button {
                text: qsTr("Cancel")
                onClicked: reject()
            }
        }
    }
}