QmlMaterial 0.1.0
Loading...
Searching...
No Matches
combo_box.hpp
1#pragma once
2
3#include "qml_material/control/control.hpp"
4#include "qml_material/control/popup.hpp"
5#include "qml_material/input/input_host.hpp"
6#include "qml_material/model/combo_box_model.hpp"
7#include <QQmlComponent>
8#include <QValidator>
9#include <QtQuick/private/qquicktextinput_p.h>
10
11namespace qml_material
12{
13
14class QML_MATERIAL_API ComboBox : public Control {
15 Q_OBJECT
16 QML_NAMED_ELEMENT(ComboBoxBase)
17 Q_PROPERTY(Popup* popup READ popup WRITE setPopup NOTIFY popupChanged FINAL)
18 Q_PROPERTY(
19 QQuickItem* indicator READ indicator WRITE setIndicator NOTIFY indicatorChanged FINAL)
20 Q_PROPERTY(bool popupVisible READ popupVisible NOTIFY popupVisibleChanged FINAL)
21 Q_PROPERTY(bool popupActiveFocus READ popupActiveFocus NOTIFY popupActiveFocusChanged FINAL)
22 Q_PROPERTY(InteractionSource* interactionSource READ interactionSource WRITE
23 setInteractionSource NOTIFY interactionSourceChanged FINAL)
24 Q_PROPERTY(bool focusVisible READ focusVisible NOTIFY focusVisibleChanged FINAL)
25 Q_PROPERTY(int highlightedIndex READ highlightedIndex NOTIFY highlightedIndexChanged FINAL)
26 Q_PROPERTY(
27 bool wheelEnabled READ wheelEnabled WRITE setWheelEnabled NOTIFY wheelEnabledChanged FINAL)
28 Q_PROPERTY(bool editable READ editable WRITE setEditable NOTIFY editableChanged FINAL)
29 Q_PROPERTY(QString editText READ editText WRITE setEditText RESET resetEditText NOTIFY
30 editTextChanged FINAL)
31 Q_PROPERTY(bool acceptableInput READ acceptableInput NOTIFY acceptableInputChanged BINDABLE
32 bindableAcceptableInput FINAL)
33 Q_PROPERTY(bool inputMethodComposing READ inputMethodComposing NOTIFY
34 inputMethodComposingChanged FINAL)
35 Q_PROPERTY(
36 QValidator* validator READ validator WRITE setValidator NOTIFY validatorChanged FINAL)
37 Q_PROPERTY(Qt::InputMethodHints inputMethodHints READ inputMethodHints WRITE setInputMethodHints
38 NOTIFY inputMethodHintsChanged FINAL)
39 Q_PROPERTY(bool selectTextByMouse READ selectTextByMouse WRITE setSelectTextByMouse NOTIFY
40 selectTextByMouseChanged FINAL)
41 Q_PROPERTY(QVariant model READ model WRITE setModel NOTIFY modelChanged FINAL)
42 Q_PROPERTY(QQmlComponent* delegate READ delegate WRITE setDelegate NOTIFY delegateChanged FINAL)
43 Q_PROPERTY(QObject* delegateModel READ delegateModel NOTIFY delegateModelChanged FINAL)
44 Q_PROPERTY(int count READ count NOTIFY countChanged FINAL)
45 Q_PROPERTY(QString textRole READ textRole WRITE setTextRole NOTIFY textRoleChanged FINAL)
46 Q_PROPERTY(QString valueRole READ valueRole WRITE setValueRole NOTIFY valueRoleChanged FINAL)
47 Q_PROPERTY(
48 int currentIndex READ currentIndex WRITE setCurrentIndex NOTIFY currentIndexChanged FINAL)
49 Q_PROPERTY(QVariant currentValue READ currentValue WRITE setCurrentValue NOTIFY
50 currentValueChanged FINAL)
51 Q_PROPERTY(QString currentText READ currentText NOTIFY currentTextChanged FINAL)
52 Q_PROPERTY(QString displayText READ displayText WRITE setDisplayText RESET resetDisplayText
53 NOTIFY displayTextChanged FINAL)
54public:
55 explicit ComboBox(QQuickItem* parent = nullptr);
56 ~ComboBox() override;
57 bool popupVisible() const { return m_popup_visible; }
58 bool popupActiveFocus() const { return m_popup_focus; }
59 int highlightedIndex() const { return m_highlight; }
60 Popup* popup() const { return m_popup; }
61 void setPopup(Popup*);
62 QQuickItem* indicator() const { return m_indicator; }
63 void setIndicator(QQuickItem*);
64 InteractionSource* interactionSource() { return m_input.source(); }
65 void setInteractionSource(InteractionSource* value) { m_input.setSource(value); }
66 bool focusVisible() const { return m_input.focusVisible(); }
67 Q_SIGNAL void popupChanged();
68 Q_SIGNAL void indicatorChanged();
69 Q_SIGNAL void interactionSourceChanged();
70 Q_SIGNAL void focusVisibleChanged();
71 Q_INVOKABLE void openPopup();
72 Q_INVOKABLE void closePopup(bool accept = false);
73 Q_INVOKABLE void highlightIndex(int);
74 Q_SIGNAL void popupVisibleChanged();
75 Q_SIGNAL void popupActiveFocusChanged();
76 Q_SIGNAL void highlightedIndexChanged();
77 Q_SIGNAL void highlighted(int index);
78 bool wheelEnabled() const { return m_wheel_enabled; }
79 void setWheelEnabled(bool);
80 Q_SIGNAL void wheelEnabledChanged();
81 Q_INVOKABLE int find(const QString&, Qt::MatchFlags flags = Qt::MatchExactly) const;
82 Q_INVOKABLE void incrementCurrentIndex();
83 Q_INVOKABLE void decrementCurrentIndex();
84 bool editable() const { return m_editable; }
85 void setEditable(bool);
86 QString editText() const { return m_edit_text; }
87 void setEditText(const QString&);
88 void resetEditText() { setEditText({}); }
89 bool acceptableInput() const;
90 QBindable<bool> bindableAcceptableInput() const { return QBindable<bool>(&m_acceptable); }
91 bool inputMethodComposing() const;
92 QValidator* validator() const { return m_validator; }
93 void setValidator(QValidator*);
94 Qt::InputMethodHints inputMethodHints() const { return m_input_hints; }
95 void setInputMethodHints(Qt::InputMethodHints);
96 bool selectTextByMouse() const { return m_select_by_mouse; }
97 void setSelectTextByMouse(bool);
98 Q_INVOKABLE void selectAll();
99 Q_INVOKABLE void acceptInput();
100 Q_SIGNAL void editableChanged();
101 Q_SIGNAL void editTextChanged();
102 Q_SIGNAL void acceptableInputChanged();
103 Q_SIGNAL void inputMethodComposingChanged();
104 Q_SIGNAL void validatorChanged();
105 Q_SIGNAL void inputMethodHintsChanged();
106 Q_SIGNAL void selectTextByMouseChanged();
107 Q_SIGNAL void accepted();
108 QVariant model() const;
109 void setModel(const QVariant&);
110 QQmlComponent* delegate() const;
111 void setDelegate(QQmlComponent*);
112 QObject* delegateModel() const;
113 int count() const;
114 QString textRole() const;
115 void setTextRole(const QString&);
116 QString valueRole() const;
117 void setValueRole(const QString&);
118 int currentIndex() const { return m_index; }
119 void setCurrentIndex(int);
120 QVariant currentValue() const { return m_value; }
121 void setCurrentValue(const QVariant&);
122 QString currentText() const { return m_text; }
123 QString displayText() const { return m_display; }
124 void setDisplayText(const QString&);
125 void resetDisplayText();
126 Q_INVOKABLE QString textAt(int) const;
127 Q_INVOKABLE QVariant valueAt(int) const;
128 Q_INVOKABLE int indexOfValue(const QVariant&) const;
129 Q_INVOKABLE void activateIndex(int);
130
131 Q_SIGNAL void modelChanged();
132 Q_SIGNAL void delegateChanged();
133 Q_SIGNAL void delegateModelChanged();
134 Q_SIGNAL void countChanged();
135 Q_SIGNAL void textRoleChanged();
136 Q_SIGNAL void valueRoleChanged();
137 Q_SIGNAL void currentIndexChanged();
138 Q_SIGNAL void currentValueChanged();
139 Q_SIGNAL void currentTextChanged();
140 Q_SIGNAL void displayTextChanged();
141 Q_SIGNAL void selectionChangeRequested(int index, const QVariant& value);
142
143protected:
144 bool event(QEvent*) override;
145 void hoverEnterEvent(QHoverEvent*) override;
146 void hoverMoveEvent(QHoverEvent*) override;
147 void hoverLeaveEvent(QHoverEvent*) override;
148 void classBegin() override;
149 void componentComplete() override;
150 void focusInEvent(QFocusEvent*) override;
151 void focusOutEvent(QFocusEvent*) override;
152 bool eventFilter(QObject*, QEvent*) override;
153 void keyPressEvent(QKeyEvent*) override;
154 void keyReleaseEvent(QKeyEvent*) override;
155 void mousePressEvent(QMouseEvent*) override;
156 void mouseMoveEvent(QMouseEvent*) override;
157 void mouseReleaseEvent(QMouseEvent*) override;
158 void mouseUngrabEvent() override;
159 void touchEvent(QTouchEvent*) override;
160 void touchUngrabEvent() override;
161 void wheelEvent(QWheelEvent*) override;
162 void inputMethodEvent(QInputMethodEvent*) override;
163
164private:
165 void delegateClicked();
166 void setHighlight(int, bool notify = false);
167 void navigate(int);
168 void checkFocus();
169 void syncPopupState();
170 bool m_popup_visible = false;
171 bool m_popup_focus = false;
172 int m_highlight = -1;
173 InputHost m_input;
174 QPointer<Popup> m_popup;
175 QPointer<QQuickItem> m_indicator;
176 QList<QMetaObject::Connection> m_popupConnections;
177 QMetaObject::Connection m_indicatorDestroyed;
178 quint64 m_revision = 0;
179 enum class Selection
180 {
181 None,
182 Index,
183 Value
184 };
185 void ensureModel();
186 void synchronize();
187 void publish(int, const QVariant&);
188 std::unique_ptr<ComboBoxModel> m_data;
189 Selection m_selection = Selection::None;
190 int m_index = -1;
191 QVariant m_value;
192 QString m_text, m_display;
193 std::optional<QString> m_explicit_display;
194 bool m_ready = false;
195 void attachEditor();
196 void updateEditText();
197 int match(int start, const QString&, Qt::MatchFlags) const;
198 void keySearch(const QString&);
199 bool m_wheel_enabled = false;
200 bool m_user_edit = false;
201 bool m_allow_complete = true;
202 void updateEditorState();
203 int findEditText(const QString&) const;
204 QPointer<QQuickTextInput> m_editor;
205 QList<QMetaObject::Connection> m_editor_connections;
206 QPointer<QValidator> m_validator;
207 QMetaObject::Connection m_validator_connection;
208 QString m_edit_text;
209 Qt::InputMethodHints m_input_hints = Qt::ImhNoPredictiveText;
210 bool m_editable = false;
211 bool m_select_by_mouse = false;
212 bool m_accepting = false;
213 Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(ComboBox, bool, m_acceptable, true,
214 &ComboBox::acceptableInputChanged)
215 bool m_composing = false;
216};
217
218} // namespace qml_material