QmlMaterial 0.1.0
Loading...
Searching...
No Matches
popup_presenter.hpp
1#pragma once
2
3#include <QObject>
4#include <QPointer>
5#include <QVariant>
6#include <qqmlregistration.h>
7#include <vector>
8
9#include "qml_material/export.hpp"
10#include "qml_material/util/pool.hpp"
11
12class QQuickItem;
13
14namespace qml_material
15{
16
17class PopupPresenter;
18class PresentationAttached;
19
20class QML_MATERIAL_API PopupPresentation : public QObject {
21 Q_OBJECT
22 QML_NAMED_ELEMENT(PopupPresentation)
23 QML_UNCREATABLE("PopupPresentation objects are returned by PopupPresenter.present()")
24
25 Q_PROPERTY(Status status READ status NOTIFY statusChanged FINAL)
26 Q_PROPERTY(bool active READ active NOTIFY activeChanged FINAL)
27 Q_PROPERTY(QString errorString READ errorString NOTIFY errorStringChanged FINAL)
28 Q_PROPERTY(QVariant result READ result NOTIFY resultChanged FINAL)
29
30public:
31 enum Status
32 {
33 Loading,
34 WaitingForReady,
35 Opening,
36 Open,
37 Closing,
38 Closed,
39 Error,
40 Cancelled,
41 };
42 Q_ENUM(Status)
43
44 ~PopupPresentation() override;
45
46 auto status() const -> Status;
47 auto active() const -> bool;
48 auto errorString() const -> QString;
49 auto result() const -> QVariant;
50
51 Q_INVOKABLE void close();
52 Q_INVOKABLE void cancel();
53
54 Q_SIGNAL void statusChanged();
55 Q_SIGNAL void activeChanged();
56 Q_SIGNAL void errorStringChanged();
57 Q_SIGNAL void resultChanged();
58 Q_SIGNAL void opened();
59 Q_SIGNAL void completed(const QVariant& result);
60 Q_SIGNAL void closed();
61 Q_SIGNAL void failed(const QString& error);
62 Q_SIGNAL void cancelled();
63
64private Q_SLOTS:
65 void onPopupOpened();
66 void onPopupClosed();
67
68private:
69 friend class PopupPresenter;
70 friend class PresentationAttached;
71
72 enum class Finish
73 {
74 None,
75 Closed,
76 Error,
77 Cancelled,
78 };
79
80 explicit PopupPresentation(PopupPresenter* presenter);
81
82 void setStatus(Status value);
83 void setErrorString(QString value);
84 void setResult(QVariant value);
85
86 QPointer<QQuickItem> m_anchor;
87 QList<QMetaObject::Connection> m_anchorConnections;
88 bool m_menu = false;
89 QPointer<PopupPresenter> m_presenter;
90 QPointer<PoolRequest> m_request;
91 QPointer<QObject> m_popup;
92 QPointer<PresentationAttached> m_attached;
93 Status m_status { Loading };
94 Finish m_finish { Finish::None };
95 QString m_errorString;
96 QVariant m_result;
97 bool m_terminal { false };
98 bool m_completed { false };
99};
100
101class QML_MATERIAL_API PresentationAttached final : public QObject {
102 Q_OBJECT
103
104 Q_PROPERTY(bool ready READ ready WRITE setReady NOTIFY readyChanged FINAL)
105
106public:
107 explicit PresentationAttached(QObject* parent = nullptr);
108
109 auto ready() const -> bool;
110 void setReady(bool value);
111
112 Q_INVOKABLE void fail(const QString& error);
113 Q_INVOKABLE void complete(const QVariant& result = {});
114
115 Q_SIGNAL void readyChanged();
116 Q_SIGNAL void failureReported(const QString& error);
117 Q_SIGNAL void completionRequested(const QVariant& result);
118
119private:
120 friend class PopupPresenter;
121
122 void bind(PopupPresentation* presentation);
123 void unbind(PopupPresentation* presentation);
124
125 QPointer<PopupPresentation> m_presentation;
126 QString m_errorString;
127 bool m_ready { true };
128 bool m_failed { false };
129};
130
131class QML_MATERIAL_API Presentation : public QObject {
132 Q_OBJECT
133 QML_NAMED_ELEMENT(Presentation)
134 QML_UNCREATABLE("Presentation is only available as an attached property")
135 QML_ATTACHED(PresentationAttached)
136
137public:
138 explicit Presentation(QObject* parent = nullptr);
139
140 static PresentationAttached* qmlAttachedProperties(QObject* object);
141};
142
143class QML_MATERIAL_API PopupPresenter : public QObject {
144 Q_OBJECT
145 QML_ELEMENT
146
147 Q_PROPERTY(QQuickItem* host READ host WRITE setHost NOTIFY hostChanged FINAL)
148 Q_PROPERTY(Pool::IncubationMode incubationMode READ incubationMode WRITE setIncubationMode
149 NOTIFY incubationModeChanged FINAL)
150
151public:
152 explicit PopupPresenter(QObject* parent = nullptr);
153 ~PopupPresenter() override;
154
155 auto host() const -> QQuickItem*;
156 void setHost(QQuickItem* value);
157
158 auto incubationMode() const -> Pool::IncubationMode;
159 void setIncubationMode(Pool::IncubationMode value);
160
161 Q_INVOKABLE PopupPresentation* present(const QVariant& source,
162 const QVariantMap& initialProperties = {});
163 Q_INVOKABLE PopupPresentation* presentMenu(const QVariant& source, QQuickItem* anchor,
164 const QVariantMap& initialProperties = {});
165 Q_INVOKABLE void closeAll();
166
167 Q_SIGNAL void hostChanged();
168 Q_SIGNAL void incubationModeChanged();
169
170private:
171 friend class PopupPresentation;
172
173 PopupPresentation* presentImpl(const QVariant& source, const QVariantMap& properties,
174 QQuickItem* anchor, bool menu);
175 void requestClose(PopupPresentation* presentation, PopupPresentation::Finish finish);
176 void handleRequest(PopupPresentation* presentation);
177 void attachPopup(PopupPresentation* presentation, QObject* popup);
178 void openPopup(PopupPresentation* presentation);
179 void failPresentation(PopupPresentation* presentation, QString error);
180 void finishPresentation(PopupPresentation* presentation, PopupPresentation::Finish finish);
181 void completePresentation(PopupPresentation* presentation, QVariant result);
182 void popupOpened(PopupPresentation* presentation);
183 void popupClosed(PopupPresentation* presentation);
184 void popupDestroyed(PopupPresentation* presentation);
185 void presentationDestroyed(PopupPresentation* presentation);
186 void retire(PopupPresentation* presentation);
187 void ensurePoolContext();
188
189 QPointer<QQuickItem> m_host;
190 Pool* m_pool;
191 Pool::IncubationMode m_incubationMode { Pool::AsynchronousIfNested };
192 std::vector<QPointer<PopupPresentation>> m_presentations;
193 bool m_destroying { false };
194};
195
196} // namespace qml_material