Update tooltip code, Use an Enum for editMode, setup strings for i18n

This commit is contained in:
Corey Woodworth
2025-11-21 22:39:05 -05:00
parent bd1c9d30d9
commit 42f782abbf

View File

@@ -1,6 +1,7 @@
import QtQuick import QtQuick
import QtQuick.Controls import QtQuick.Controls
import QtQuick.Layouts import QtQuick.Layouts
import QtQml
import qs.Commons import qs.Commons
import qs.Widgets import qs.Widgets
import qs.Services.UI import qs.Services.UI
@@ -10,7 +11,16 @@ Popup {
id: root id: root
property color selectedColor: Color.black property color selectedColor: Color.black
property string editMode: "r" // Options: "r", "g", "b", "h", "s", "v"
enum EditMode {
R,
G,
B,
H,
S,
V
}
property int editMode: NColorPickerDialog.EditMode.R
// Code to deal with Hue when color is achromatic // Code to deal with Hue when color is achromatic
property real stableHue: 0 property real stableHue: 0
@@ -160,7 +170,7 @@ Popup {
text: "R" text: "R"
font.weight: Font.Bold font.weight: Font.Bold
checked: true checked: true
onClicked: root.editMode = "r" onClicked: root.editMode = NColorPickerDialog.EditMode.R
Layout.fillWidth: false Layout.fillWidth: false
} }
@@ -191,7 +201,7 @@ Popup {
ButtonGroup.group: colorValues ButtonGroup.group: colorValues
text: "G" text: "G"
font.weight: Font.Bold font.weight: Font.Bold
onClicked: root.editMode = "g" onClicked: root.editMode = NColorPickerDialog.EditMode.G
Layout.fillWidth: false Layout.fillWidth: false
} }
@@ -222,7 +232,7 @@ Popup {
ButtonGroup.group: colorValues ButtonGroup.group: colorValues
text: "B" text: "B"
font.weight: Font.Bold font.weight: Font.Bold
onClicked: root.editMode = "b" onClicked: root.editMode = NColorPickerDialog.EditMode.B
Layout.fillWidth: false Layout.fillWidth: false
} }
@@ -260,7 +270,7 @@ Popup {
text: "H" text: "H"
font.weight: Font.Bold font.weight: Font.Bold
checked: true checked: true
onClicked: root.editMode = "h" onClicked: root.editMode = NColorPickerDialog.EditMode.H
Layout.fillWidth: false Layout.fillWidth: false
} }
@@ -294,7 +304,7 @@ Popup {
ButtonGroup.group: colorValues ButtonGroup.group: colorValues
text: "S" text: "S"
font.weight: Font.Bold font.weight: Font.Bold
onClicked: root.editMode = "s" onClicked: root.editMode = NColorPickerDialog.EditMode.S
Layout.fillWidth: false Layout.fillWidth: false
} }
@@ -324,7 +334,7 @@ Popup {
ButtonGroup.group: colorValues ButtonGroup.group: colorValues
text: "V" text: "V"
font.weight: Font.Bold font.weight: Font.Bold
onClicked: root.editMode = "v" onClicked: root.editMode = NColorPickerDialog.EditMode.V
Layout.fillWidth: false Layout.fillWidth: false
} }
@@ -359,8 +369,7 @@ Popup {
spacing: Style.marginM spacing: Style.marginM
NLabel { NLabel {
label: "Hex:" // I18n.tr("widgets.color-picker.hex.label") label: I18n.tr("widgets.color-picker.hex")
// description: I18n.tr("widgets.color-picker.hex.description")
Layout.alignment: Qt.AlignVCenter Layout.alignment: Qt.AlignVCenter
} }
@@ -396,11 +405,11 @@ Popup {
if (rainbowMode) if (rainbowMode)
return "transparent"; return "transparent";
switch (root.editMode) { switch (root.editMode) {
case "r": case NColorPickerDialog.EditMode.R:
return "#FF0000"; return "#FF0000";
case "g": case NColorPickerDialog.EditMode.G:
return "#00FF00"; return "#00FF00";
case "b": case NColorPickerDialog.EditMode.B:
return "#0000FF"; return "#0000FF";
default: default:
return "#FFFFFF"; return "#FFFFFF";
@@ -413,17 +422,17 @@ Popup {
when: !selectedSlider.pressed // Only update from model when NOT dragging when: !selectedSlider.pressed // Only update from model when NOT dragging
value: { value: {
switch (root.editMode) { switch (root.editMode) {
case "r": case NColorPickerDialog.EditMode.R:
return root.selectedColor.r; return root.selectedColor.r;
case "g": case NColorPickerDialog.EditMode.G:
return root.selectedColor.g; return root.selectedColor.g;
case "b": case NColorPickerDialog.EditMode.B:
return root.selectedColor.b; return root.selectedColor.b;
case "h": case NColorPickerDialog.EditMode.H:
return root.displayHue; return root.displayHue;
case "s": case NColorPickerDialog.EditMode.S:
return root.selectedColor.hsvSaturation; return root.selectedColor.hsvSaturation;
case "v": case NColorPickerDialog.EditMode.V:
return root.selectedColor.hsvValue; return root.selectedColor.hsvValue;
default: default:
return 0; return 0;
@@ -434,23 +443,23 @@ Popup {
onMoved: { onMoved: {
var v = value; var v = value;
switch (root.editMode) { switch (root.editMode) {
case "r": case NColorPickerDialog.EditMode.R:
root.selectedColor = Qt.rgba(v, root.selectedColor.g, root.selectedColor.b, 1); root.selectedColor = Qt.rgba(v, root.selectedColor.g, root.selectedColor.b, 1);
break; break;
case "g": case NColorPickerDialog.EditMode.G:
root.selectedColor = Qt.rgba(root.selectedColor.r, v, root.selectedColor.b, 1); root.selectedColor = Qt.rgba(root.selectedColor.r, v, root.selectedColor.b, 1);
break; break;
case "b": case NColorPickerDialog.EditMode.B:
root.selectedColor = Qt.rgba(root.selectedColor.r, root.selectedColor.g, v, 1); root.selectedColor = Qt.rgba(root.selectedColor.r, root.selectedColor.g, v, 1);
break; break;
case "h": case NColorPickerDialog.EditMode.H:
root.selectedColor = Qt.hsva(v, root.selectedColor.hsvSaturation, root.selectedColor.hsvValue, 1); root.selectedColor = Qt.hsva(v, root.selectedColor.hsvSaturation, root.selectedColor.hsvValue, 1);
root.stableHue = v; root.stableHue = v;
break; break;
case "s": case NColorPickerDialog.EditMode.S:
root.selectedColor = Qt.hsva(root.selectedColor.hsvHue, v, root.selectedColor.hsvValue, 1); root.selectedColor = Qt.hsva(root.selectedColor.hsvHue, v, root.selectedColor.hsvValue, 1);
break; break;
case "v": case NColorPickerDialog.EditMode.V:
root.selectedColor = Qt.hsva(root.selectedColor.hsvHue, root.selectedColor.hsvSaturation, v, 1); root.selectedColor = Qt.hsva(root.selectedColor.hsvHue, root.selectedColor.hsvSaturation, v, 1);
break; break;
} }
@@ -476,40 +485,20 @@ Popup {
fragmentShader: "../Shaders/qsb/color_picker.frag.qsb" fragmentShader: "../Shaders/qsb/color_picker.frag.qsb"
// Map strings to integers
readonly property int modeInt: {
switch (root.editMode) {
case "r":
return 0;
case "g":
return 1;
case "b":
return 2;
case "h":
return 3;
case "s":
return 4;
case "v":
return 5;
default:
return 0;
}
}
// Pass which radio is selected // Pass which radio is selected
readonly property real fixedVal: { readonly property real fixedVal: {
switch (root.editMode) { switch (root.editMode) {
case "r": case NColorPickerDialog.EditMode.R:
return root.selectedColor.r; return root.selectedColor.r;
case "g": case NColorPickerDialog.EditMode.G:
return root.selectedColor.g; return root.selectedColor.g;
case "b": case NColorPickerDialog.EditMode.B:
return root.selectedColor.b; return root.selectedColor.b;
case "h": case NColorPickerDialog.EditMode.H:
return root.displayHue; return root.displayHue;
case "s": case NColorPickerDialog.EditMode.S:
return root.selectedColor.hsvSaturation; return root.selectedColor.hsvSaturation;
case "v": case NColorPickerDialog.EditMode.V:
return root.selectedColor.hsvValue; return root.selectedColor.hsvValue;
default: default:
return 0; return 0;
@@ -517,7 +506,7 @@ Popup {
} }
// Send as one vector because GPUs are so damn picky // Send as one vector because GPUs are so damn picky
property vector4d params: Qt.vector4d(1.0, fixedVal, modeInt, 0) property vector4d params: Qt.vector4d(1.0, fixedVal, root.editMode, 0)
} }
MouseArea { MouseArea {
@@ -535,31 +524,31 @@ Popup {
var fixed = 0.0; var fixed = 0.0;
switch (root.editMode) { switch (root.editMode) {
case "r": case NColorPickerDialog.EditMode.R:
fixed = root.selectedColor.r; fixed = root.selectedColor.r;
root.selectedColor = Qt.rgba(fixed, xVal, yVal, 1); root.selectedColor = Qt.rgba(fixed, xVal, yVal, 1);
break; break;
case "g": case NColorPickerDialog.EditMode.G:
fixed = root.selectedColor.g; fixed = root.selectedColor.g;
root.selectedColor = Qt.rgba(xVal, fixed, yVal, 1); root.selectedColor = Qt.rgba(xVal, fixed, yVal, 1);
break; break;
case "b": case NColorPickerDialog.EditMode.B:
fixed = root.selectedColor.b; fixed = root.selectedColor.b;
root.selectedColor = Qt.rgba(xVal, yVal, fixed, 1); root.selectedColor = Qt.rgba(xVal, yVal, fixed, 1);
break; break;
case "h": case NColorPickerDialog.EditMode.H:
// Use stableHue to prevent flipping to -1 // Use stableHue to prevent flipping to -1
fixed = root.displayHue; fixed = root.displayHue;
root.selectedColor = Qt.hsva(fixed, xVal, yVal, 1); root.selectedColor = Qt.hsva(fixed, xVal, yVal, 1);
root.stableHue = fixed; root.stableHue = fixed;
break; break;
case "s": case NColorPickerDialog.EditMode.S:
fixed = root.selectedColor.hsvSaturation; fixed = root.selectedColor.hsvSaturation;
root.selectedColor = Qt.hsva(xVal, fixed, yVal, 1); root.selectedColor = Qt.hsva(xVal, fixed, yVal, 1);
// If we dragged Hue (xVal), update stableHue // If we dragged Hue (xVal), update stableHue
root.stableHue = yVal; root.stableHue = yVal;
break; break;
case "v": case NColorPickerDialog.EditMode.V:
fixed = root.selectedColor.hsvValue; fixed = root.selectedColor.hsvValue;
root.selectedColor = Qt.hsva(xVal, yVal, fixed, 1); root.selectedColor = Qt.hsva(xVal, yVal, fixed, 1);
// If we dragged Hue (xVal), update stableHue // If we dragged Hue (xVal), update stableHue
@@ -583,17 +572,17 @@ Popup {
// Find position based on the current color // Find position based on the current color
readonly property point selectedPos: { readonly property point selectedPos: {
switch (root.editMode) { switch (root.editMode) {
case "r": case NColorPickerDialog.EditMode.R:
return Qt.point(root.selectedColor.g, root.selectedColor.b); return Qt.point(root.selectedColor.g, root.selectedColor.b);
case "g": case NColorPickerDialog.EditMode.G:
return Qt.point(root.selectedColor.r, root.selectedColor.b); return Qt.point(root.selectedColor.r, root.selectedColor.b);
case "b": case NColorPickerDialog.EditMode.B:
return Qt.point(root.selectedColor.r, root.selectedColor.g); return Qt.point(root.selectedColor.r, root.selectedColor.g);
case "h": case NColorPickerDialog.EditMode.H:
return Qt.point(root.selectedColor.hsvSaturation, root.selectedColor.hsvValue); return Qt.point(root.selectedColor.hsvSaturation, root.selectedColor.hsvValue);
case "s": case NColorPickerDialog.EditMode.S:
return Qt.point(root.displayHue, root.selectedColor.hsvValue); return Qt.point(root.displayHue, root.selectedColor.hsvValue);
case "v": case NColorPickerDialog.EditMode.V:
return Qt.point(root.displayHue, root.selectedColor.hsvSaturation); return Qt.point(root.displayHue, root.selectedColor.hsvSaturation);
default: default:
return Qt.point(0, 0); return Qt.point(0, 0);
@@ -648,7 +637,7 @@ Popup {
hoverEnabled: true hoverEnabled: true
onEntered: { onEntered: {
TooltipService.show(screen, parent, modelData.name + "\n" + parent.color.toString().toUpperCase(), "auto"); TooltipService.show(parent, modelData.name + "\n" + parent.color.toString().toUpperCase(), "auto");
} }
onExited: { onExited: {
TooltipService.hide(); TooltipService.hide();
@@ -671,7 +660,7 @@ Popup {
NLabel { NLabel {
Layout.columnSpan: 15 Layout.columnSpan: 15
Layout.fillWidth: true Layout.fillWidth: true
description: I18n.tr("widgets.color-picker.theme-colors.description") description: I18n.tr("widgets.color-picker.palette.theme-colors")
} }
Repeater { Repeater {