QmlMaterial 0.1.0
Loading...
Searching...
No Matches
combo_box_model.hpp
1#pragma once
2
3#include <QObject>
4#include <QPointer>
5#include <QVariant>
6#include <memory>
7#include "qml_material/export.hpp"
8
9class QQmlContext;
10class QQmlComponent;
11class QQmlInstanceModel;
12class QQmlDelegateModel;
13
14namespace qml_material
15{
16
17// Owned models need a delegate component before exposing rows, as in QQmlDelegateModel.
18// Role queries never instantiate delegates; external instance models remain borrowed.
19class QML_MATERIAL_API ComboBoxModel : public QObject {
20 Q_OBJECT
21public:
22 explicit ComboBoxModel(QQmlContext*, QObject* parent = nullptr);
23 ~ComboBoxModel() override;
24 QVariant model() const;
25 void setModel(const QVariant&);
26 void setDelegate(QQmlComponent*);
27 QQmlComponent* delegate() const;
28 QQmlInstanceModel* delegateModel() const;
29 void complete();
30 int count() const;
31 QString textRole() const { return m_text_role; }
32 void setTextRole(const QString&);
33 QString valueRole() const { return m_value_role; }
34 void setValueRole(const QString&);
35 QString textAt(int) const;
36 QVariant valueAt(int) const;
37 int indexOfValue(const QVariant&) const;
38 int indexOf(QObject*) const;
39
40 Q_SIGNAL void modelChanged();
41 Q_SIGNAL void delegateModelChanged();
42 Q_SIGNAL void countChanged();
43 Q_SIGNAL void changed();
44 Q_SIGNAL void itemCreated(QObject* object);
45
46private:
47 QVariant roleAt(int, const QString&) const;
48 void observe();
49 void observeData();
50 void updateCount();
51 QPointer<QQmlContext> m_context;
52 QVariant m_model;
53 QPointer<QObject> m_source;
54 QPointer<QQmlComponent> m_delegate;
55 QPointer<QQmlInstanceModel> m_instance;
56 std::unique_ptr<QQmlDelegateModel> m_owned;
57 QList<QMetaObject::Connection> m_connections;
58 QMetaObject::Connection m_data_connection;
59 QString m_text_role, m_value_role;
60 int m_count = 0;
61 bool m_complete = false;
62};
63
64} // namespace qml_material