QmlMaterial 0.1.0
Loading...
Searching...
No Matches
color_input.hpp
1#pragma once
2
3#include "qml_material/input/interactive_item.hpp"
4#include <array>
5
6namespace qml_material
7{
9class QML_MATERIAL_API ColorInput : public InteractiveItem {
10 Q_OBJECT
11 QML_ANONYMOUS
12 Q_PROPERTY(qreal hue READ hue WRITE setHue NOTIFY hueChanged FINAL)
13 Q_PROPERTY(qreal saturation READ saturation WRITE setSaturation NOTIFY saturationChanged FINAL)
14 Q_PROPERTY(qreal value READ value WRITE setValue NOTIFY valueChanged FINAL)
15public:
16 explicit ColorInput(QQuickItem* parent = nullptr);
17 qreal hue() const { return m_hue; }
18 qreal saturation() const { return m_saturation; }
19 qreal value() const { return m_value; }
20 void setHue(qreal);
21 void setSaturation(qreal);
22 void setValue(qreal);
23 Q_SIGNAL void hueChanged();
24 Q_SIGNAL void saturationChanged();
25 Q_SIGNAL void valueChanged();
26 Q_SIGNAL void svChangeRequested(qreal saturation, qreal value);
27 Q_SIGNAL void geometryChanged();
28
29protected:
30 void activate() override {}
31 virtual void requestAt(QPointF) = 0;
32 void geometryChange(const QRectF&, const QRectF&) override;
33
34private:
35 qreal m_hue = 0, m_saturation = 1, m_value = 1;
36 std::optional<QPointF> m_lastPoint;
37};
38
40class QML_MATERIAL_API ColorTriangle : public ColorInput {
41 Q_OBJECT
42 QML_NAMED_ELEMENT(ColorTriangleBase)
43 Q_PROPERTY(QPointF hueVertex READ hueVertex NOTIFY geometryChanged FINAL)
44 Q_PROPERTY(QPointF whiteVertex READ whiteVertex NOTIFY geometryChanged FINAL)
45 Q_PROPERTY(QPointF blackVertex READ blackVertex NOTIFY geometryChanged FINAL)
46 Q_PROPERTY(QPointF handlePosition READ handlePosition NOTIFY handlePositionChanged FINAL)
47public:
48 explicit ColorTriangle(QQuickItem* parent = nullptr);
49 QPointF hueVertex() const;
50 QPointF whiteVertex() const;
51 QPointF blackVertex() const;
52 QPointF handlePosition() const;
53 bool contains(const QPointF&) const override;
54 Q_SIGNAL void handlePositionChanged();
55
56protected:
57 void requestAt(QPointF) override;
58 void keyPressEvent(QKeyEvent*) override;
59
60private:
61 QPointF vertex(qreal angle) const;
62 std::optional<std::array<qreal, 3>> coordinates(QPointF) const;
63};
64
66class QML_MATERIAL_API ColorWheel : public ColorInput {
67 Q_OBJECT
68 QML_NAMED_ELEMENT(ColorWheelBase)
69 Q_PROPERTY(qreal thickness READ thickness WRITE setThickness NOTIFY geometryChanged FINAL)
70 Q_PROPERTY(qreal outerRadius READ outerRadius NOTIFY geometryChanged FINAL)
71 Q_PROPERTY(qreal innerRadius READ innerRadius NOTIFY geometryChanged FINAL)
72 Q_PROPERTY(QPointF handlePosition READ handlePosition NOTIFY geometryChanged FINAL)
73 Q_PROPERTY(ColorTriangle* triangle READ triangle WRITE setTriangle NOTIFY triangleChanged FINAL)
74public:
75 explicit ColorWheel(QQuickItem* parent = nullptr);
76 ~ColorWheel() override;
77 qreal thickness() const { return m_thickness; }
78 void setThickness(qreal);
79 qreal outerRadius() const;
80 qreal innerRadius() const;
81 QPointF handlePosition() const;
82 bool contains(const QPointF&) const override;
83 ColorTriangle* triangle() const { return m_triangle; }
84 void setTriangle(ColorTriangle*);
85 Q_INVOKABLE void requestHue(qreal);
86 Q_SIGNAL void triangleChanged();
87 Q_SIGNAL void hueChangeRequested(qreal hue);
88
89protected:
90 void requestAt(QPointF) override;
91 void keyPressEvent(QKeyEvent*) override;
92
93private:
94 qreal m_thickness = 22;
95 QPointer<ColorTriangle> m_triangle;
96 QList<QMetaObject::Connection> m_triangleConnections;
97};
98} // namespace qml_material