|
| 1 | +import QtQuick |
| 2 | +import QtQuick.Controls |
| 3 | + |
| 4 | +import Utils 1.0 |
| 5 | +import MaterialIcons 2.2 |
| 6 | + |
| 7 | +/** |
| 8 | + * ColorSelector is a color picker based on a set of predefined colors. |
| 9 | + * It takes the form of a ToolButton that pops-up its palette when pressed. |
| 10 | + */ |
| 11 | +MaterialToolButton { |
| 12 | + id: root |
| 13 | + |
| 14 | + text: MaterialIcons.palette |
| 15 | + |
| 16 | + // Internal property holding when the popup remains visible and when is it toggled off |
| 17 | + property var isVisible: false |
| 18 | + |
| 19 | + property var colors: [ |
| 20 | + "#FF6600", |
| 21 | + "#FFAD7D", |
| 22 | + "#FFF82F", |
| 23 | + "#FFFB8B", |
| 24 | + "#BDFF07", |
| 25 | + "#E8FF97", |
| 26 | + "#0BFFCA", |
| 27 | + "#8EFFF7", |
| 28 | + "#1158FF", |
| 29 | + "#77A7FF", |
| 30 | + "#9318FF", |
| 31 | + "#EAACFF", |
| 32 | + "#B61518", |
| 33 | + "#FF989A", |
| 34 | + ] |
| 35 | + |
| 36 | + // When a color gets selected/choosen |
| 37 | + signal colorSelected(var color) |
| 38 | + |
| 39 | + // Toggles the visibility of the popup |
| 40 | + onPressed: toggle() |
| 41 | + |
| 42 | + function toggle() { |
| 43 | + /* |
| 44 | + * Toggles the visibility of the color palette. |
| 45 | + */ |
| 46 | + if (!isVisible) { |
| 47 | + palettePopup.open() |
| 48 | + isVisible = true |
| 49 | + } |
| 50 | + else { |
| 51 | + palettePopup.close() |
| 52 | + isVisible = false |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // Popup for the color palette |
| 57 | + Popup { |
| 58 | + id: palettePopup |
| 59 | + |
| 60 | + // The popup will not get closed unless explicitly closed |
| 61 | + closePolicy: Popup.NoAutoClose |
| 62 | + |
| 63 | + // Bounds |
| 64 | + padding: 4 |
| 65 | + width: (root.height * 4) + (padding * 4) |
| 66 | + |
| 67 | + // center the current color |
| 68 | + y: -height |
| 69 | + x: -width + root.width + padding |
| 70 | + |
| 71 | + // Layout of the Colors |
| 72 | + Grid { |
| 73 | + // Allow only 4 columns and all the colors can be adjusted in row multiples of 4 |
| 74 | + columns: 4 |
| 75 | + |
| 76 | + spacing: 2 |
| 77 | + anchors.centerIn: parent |
| 78 | + |
| 79 | + // Default -- Reset Colour button |
| 80 | + ToolButton { |
| 81 | + id: defaultButton |
| 82 | + padding: 0 |
| 83 | + width: root.height |
| 84 | + height: root.height |
| 85 | + |
| 86 | + // Emit no color so the graph sets None as the color of the Node |
| 87 | + onClicked: { |
| 88 | + root.colorSelected("") |
| 89 | + } |
| 90 | + |
| 91 | + background: Rectangle { |
| 92 | + color: "#FFFFFF" |
| 93 | + // display border of current/selected item |
| 94 | + border.width: defaultButton.hovered ? 1 : 0 |
| 95 | + border.color: "#000000" |
| 96 | + |
| 97 | + // Another Rectangle |
| 98 | + Rectangle { |
| 99 | + color: "#FF0000" |
| 100 | + width: parent.width + 8 |
| 101 | + height: 2 |
| 102 | + anchors.centerIn: parent |
| 103 | + rotation: 135 // Diagonally creating a Red line from bottom left to top right |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + // Colors palette |
| 109 | + Repeater { |
| 110 | + model: root.colors |
| 111 | + // display each color as a ToolButton with a custom background |
| 112 | + delegate: ToolButton { |
| 113 | + padding: 0 |
| 114 | + width: root.height |
| 115 | + height: root.height |
| 116 | + |
| 117 | + // Emit the model data as the color to update |
| 118 | + onClicked: { |
| 119 | + colorSelected(modelData) |
| 120 | + } |
| 121 | + |
| 122 | + // Model color as the background of the button |
| 123 | + background: Rectangle { |
| 124 | + color: modelData |
| 125 | + // display border of current/selected item |
| 126 | + border.width: hovered ? 1 : 0 |
| 127 | + border.color: "#000000" |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments