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 "qml_material/input/input_host.hpp"
7
8namespace qml_material
9{
10
12class QML_MATERIAL_API TextField : public QQuickTextInput, public ControlEnvironment {
13 Q_OBJECT
14 Q_PROPERTY(InteractionSource* interactionSource READ interactionSource WRITE
15 setInteractionSource NOTIFY interactionSourceChanged FINAL)
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 hoverEnabled READ hoverEnabled WRITE setHoverEnabled RESET resetHoverEnabled
27 NOTIFY hoverEnabledChanged FINAL)
28 QML_NAMED_ELEMENT(TextFieldBase)
29 Q_PROPERTY(QFont font READ font WRITE setFont RESET resetFont NOTIFY fontChanged FINAL)
30 Q_PROPERTY(
31 QQuickItem* background READ background WRITE setBackground NOTIFY backgroundChanged FINAL)
32 Q_PROPERTY(qreal implicitBackgroundWidth READ implicitBackgroundWidth NOTIFY
33 implicitBackgroundWidthChanged FINAL)
34 Q_PROPERTY(qreal implicitBackgroundHeight READ implicitBackgroundHeight NOTIFY
35 implicitBackgroundHeightChanged FINAL)
36 Q_PROPERTY(qreal topInset READ topInset WRITE setTopInset RESET resetTopInset NOTIFY
37 topInsetChanged FINAL)
38 Q_PROPERTY(qreal leftInset READ leftInset WRITE setLeftInset RESET resetLeftInset NOTIFY
39 leftInsetChanged FINAL)
40 Q_PROPERTY(qreal rightInset READ rightInset WRITE setRightInset RESET resetRightInset NOTIFY
41 rightInsetChanged FINAL)
42 Q_PROPERTY(qreal bottomInset READ bottomInset WRITE setBottomInset RESET resetBottomInset NOTIFY
43 bottomInsetChanged FINAL)
44
45public:
46 explicit TextField(QQuickItem* parent = nullptr);
47 ~TextField() override;
48 InteractionSource* interactionSource() { return m_input.source(); }
49 void setInteractionSource(InteractionSource* source) { m_input.setSource(source); }
50 Q_SIGNAL void interactionSourceChanged();
51 QFont font() const;
52 void setFont(const QFont&);
53 void resetFont();
54 QFont effectiveFont() const override { return m_effective_font; }
55 void inheritFont(const QFont&) override;
56 void inheritHoverEnabled(bool) override;
57 QQuickItem* background() const { return m_background.item(); }
58 void setBackground(QQuickItem*);
59 qreal implicitBackgroundWidth() const;
60 qreal implicitBackgroundHeight() const;
61 qreal topInset() const { return m_background.inset(Qt::TopEdge); }
62 void setTopInset(qreal);
63 void resetTopInset();
64 Q_SIGNAL void topInsetChanged();
65 qreal leftInset() const { return m_background.inset(Qt::LeftEdge); }
66 void setLeftInset(qreal);
67 void resetLeftInset();
68 Q_SIGNAL void leftInsetChanged();
69 qreal rightInset() const { return m_background.inset(Qt::RightEdge); }
70 void setRightInset(qreal);
71 void resetRightInset();
72 Q_SIGNAL void rightInsetChanged();
73 qreal bottomInset() const { return m_background.inset(Qt::BottomEdge); }
74 void setBottomInset(qreal);
75 void resetBottomInset();
76 Q_SIGNAL void bottomInsetChanged();
77
78 Q_SIGNAL void fontChanged();
79 Q_SIGNAL void backgroundChanged();
80 Q_SIGNAL void implicitBackgroundWidthChanged();
81 Q_SIGNAL void implicitBackgroundHeightChanged();
82
83 QString placeholderText() const { return m_placeholder; }
84 void setPlaceholderText(const QString&);
85 QColor placeholderTextColor() const { return m_placeholder_color; }
86 void setPlaceholderTextColor(const QColor&);
87 Qt::FocusReason focusReason() const { return m_focus_reason; }
88 void setFocusReason(Qt::FocusReason);
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 hoverEnabledChanged();
99
100protected:
101 InputHost& inputHost() { return m_input; }
102 virtual bool usesEditorInput() const { return true; }
103 bool event(QEvent*) override;
104 QSGNode* updatePaintNode(QSGNode*, UpdatePaintNodeData*) override;
105 void focusInEvent(QFocusEvent*) override;
106 void focusOutEvent(QFocusEvent*) override;
107 void hoverEnterEvent(QHoverEvent*) override;
108 void hoverLeaveEvent(QHoverEvent*) override;
109 void mouseUngrabEvent() override;
110 void componentComplete() override;
111 void itemChange(ItemChange, const ItemChangeData&) override;
112 void geometryChange(const QRectF&, const QRectF&) override;
113
114private:
115 void resolveHoverEnabled();
116 void updateHoverEnabled(bool);
117 QString m_placeholder;
118 QColor m_placeholder_color;
119 Qt::FocusReason m_focus_reason = Qt::OtherFocusReason;
120 std::optional<bool> m_requested_hover;
121 void resolveFont();
122 QFont m_requested_font;
123 QFont m_effective_font;
124 TextControlBackground m_background;
125 InputHost m_input;
126};
127
128} // namespace qml_material