QmlMaterial 0.1.0
Loading...
Searching...
No Matches
text_field.hpp
1#pragma once
2
3#include <QtQuick/private/qquicktextinput_p.h>
4#include "qml_material/util/text_control_background.hpp"
5#include "qml_material/util/control_environment.hpp"
6#include <QtQuick/private/qquickevents_p_p.h>
7#include <QTimer>
8#include <memory>
9
10namespace qml_material
11{
12
14class QML_MATERIAL_API TextField : public QQuickTextInput, public ControlEnvironment {
15 Q_OBJECT
16 Q_PROPERTY(qreal implicitWidth READ implicitWidth WRITE setImplicitWidth NOTIFY
17 fieldImplicitWidthChanged FINAL)
18 Q_PROPERTY(qreal implicitHeight READ implicitHeight WRITE setImplicitHeight NOTIFY
19 fieldImplicitHeightChanged FINAL)
20 Q_PROPERTY(QString placeholderText READ placeholderText WRITE setPlaceholderText NOTIFY
21 placeholderTextChanged FINAL)
22 Q_PROPERTY(QColor placeholderTextColor READ placeholderTextColor WRITE setPlaceholderTextColor
23 NOTIFY placeholderTextColorChanged FINAL)
24 Q_PROPERTY(Qt::FocusReason focusReason READ focusReason WRITE setFocusReason NOTIFY
25 focusReasonChanged FINAL)
26 Q_PROPERTY(bool hovered READ hovered NOTIFY hoveredChanged BINDABLE bindableHovered FINAL)
27 Q_PROPERTY(bool hoverEnabled READ hoverEnabled WRITE setHoverEnabled RESET resetHoverEnabled
28 NOTIFY hoverEnabledChanged FINAL)
29 QML_NAMED_ELEMENT(TextFieldBase)
30 Q_PROPERTY(QFont font READ font WRITE setFont RESET resetFont NOTIFY fontChanged FINAL)
31 Q_PROPERTY(
32 QQuickItem* background READ background WRITE setBackground NOTIFY backgroundChanged FINAL)
33 Q_PROPERTY(qreal implicitBackgroundWidth READ implicitBackgroundWidth NOTIFY
34 implicitBackgroundWidthChanged FINAL)
35 Q_PROPERTY(qreal implicitBackgroundHeight READ implicitBackgroundHeight NOTIFY
36 implicitBackgroundHeightChanged FINAL)
37 Q_PROPERTY(qreal topInset READ topInset WRITE setTopInset RESET resetTopInset NOTIFY
38 topInsetChanged FINAL)
39 Q_PROPERTY(qreal leftInset READ leftInset WRITE setLeftInset RESET resetLeftInset NOTIFY
40 leftInsetChanged FINAL)
41 Q_PROPERTY(qreal rightInset READ rightInset WRITE setRightInset RESET resetRightInset NOTIFY
42 rightInsetChanged FINAL)
43 Q_PROPERTY(qreal bottomInset READ bottomInset WRITE setBottomInset RESET resetBottomInset NOTIFY
44 bottomInsetChanged FINAL)
45
46public:
47 explicit TextField(QQuickItem* parent = nullptr);
48 ~TextField() override;
49 QFont font() const;
50 void setFont(const QFont&);
51 void resetFont();
52 QFont effectiveFont() const override { return m_effective_font; }
53 void inheritFont(const QFont&) override;
54 void inheritHoverEnabled(bool) override;
55 QQuickItem* background() const { return m_background.item(); }
56 void setBackground(QQuickItem*);
57 qreal implicitBackgroundWidth() const;
58 qreal implicitBackgroundHeight() const;
59 qreal topInset() const { return m_background.inset(Qt::TopEdge); }
60 void setTopInset(qreal);
61 void resetTopInset();
62 Q_SIGNAL void topInsetChanged();
63 qreal leftInset() const { return m_background.inset(Qt::LeftEdge); }
64 void setLeftInset(qreal);
65 void resetLeftInset();
66 Q_SIGNAL void leftInsetChanged();
67 qreal rightInset() const { return m_background.inset(Qt::RightEdge); }
68 void setRightInset(qreal);
69 void resetRightInset();
70 Q_SIGNAL void rightInsetChanged();
71 qreal bottomInset() const { return m_background.inset(Qt::BottomEdge); }
72 void setBottomInset(qreal);
73 void resetBottomInset();
74 Q_SIGNAL void bottomInsetChanged();
75
76 Q_SIGNAL void fontChanged();
77 Q_SIGNAL void backgroundChanged();
78 Q_SIGNAL void implicitBackgroundWidthChanged();
79 Q_SIGNAL void implicitBackgroundHeightChanged();
80
81 QString placeholderText() const { return m_placeholder; }
82 void setPlaceholderText(const QString&);
83 QColor placeholderTextColor() const { return m_placeholder_color; }
84 void setPlaceholderTextColor(const QColor&);
85 Qt::FocusReason focusReason() const { return m_focus_reason; }
86 void setFocusReason(Qt::FocusReason);
87 bool hovered() const { return m_hovered; }
88 QBindable<bool> bindableHovered() const { return QBindable<bool>(&m_hovered); }
89 bool hoverEnabled() const { return acceptHoverEvents(); }
90 void setHoverEnabled(bool);
91 void resetHoverEnabled();
92 std::optional<bool> effectiveHoverEnabled() const override { return hoverEnabled(); }
93 Q_SIGNAL void fieldImplicitWidthChanged();
94 Q_SIGNAL void fieldImplicitHeightChanged();
95 Q_SIGNAL void placeholderTextChanged();
96 Q_SIGNAL void placeholderTextColorChanged();
97 Q_SIGNAL void focusReasonChanged();
98 Q_SIGNAL void hoveredChanged();
99 Q_SIGNAL void hoverEnabledChanged();
100 Q_SIGNAL void pressed(QQuickMouseEvent* event);
101 Q_SIGNAL void released(QQuickMouseEvent* event);
102 Q_SIGNAL void pressAndHold(QQuickMouseEvent* event);
103
104protected:
105 QSGNode* updatePaintNode(QSGNode*, UpdatePaintNodeData*) override;
106 void focusInEvent(QFocusEvent*) override;
107 void focusOutEvent(QFocusEvent*) override;
108 void hoverEnterEvent(QHoverEvent*) override;
109 void hoverLeaveEvent(QHoverEvent*) override;
110 void mousePressEvent(QMouseEvent*) override;
111 void mouseMoveEvent(QMouseEvent*) override;
112 void mouseReleaseEvent(QMouseEvent*) override;
113 void mouseDoubleClickEvent(QMouseEvent*) override;
114 void mouseUngrabEvent() override;
115 void componentComplete() override;
116 void itemChange(ItemChange, const ItemChangeData&) override;
117 void geometryChange(const QRectF&, const QRectF&) override;
118
119private:
120 void setHovered(bool);
121 void resolveHoverEnabled();
122 void updateHoverEnabled(bool);
123 void cancelPress();
124 void deliverPress();
125 void holdTimeout();
126 QString m_placeholder;
127 QColor m_placeholder_color;
128 Qt::FocusReason m_focus_reason = Qt::OtherFocusReason;
129 std::optional<bool> m_requested_hover;
130 Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(TextField, bool, m_hovered, false,
131 &TextField::hoveredChanged)
132 bool m_pressed = false;
133 bool m_held = false;
134 QPointF m_press_pos;
135 QTimer m_hold_timer;
136 std::unique_ptr<QMouseEvent> m_delayed_press;
137 void resolveFont();
138 QFont m_requested_font;
139 QFont m_effective_font;
140 TextControlBackground m_background;
141};
142
143} // namespace qml_material