Debian Codegraph @

src/timeline/EventDelegateChooser.h

raw symbols SCIP
1 // SPDX-FileCopyrightText: Nheko Contributors
2 //
3 // SPDX-License-Identifier: GPL-3.0-or-later
4
5 #pragma once
6
7 #include <QAbstractItemModel>
8 #include <QQmlComponent>
9 #include <QQmlIncubator>
10 #include <QQmlListProperty>
11 #include <QQuickItem>
12 #include <QtCore/QObject>
13 #include <QtCore/QVariant>
14
15 class TimelineModel;
16
17 class EventDelegateChooserAttachedType : public QObject
18 {
19 Q_OBJECT
20 Q_PROPERTY(bool keepAspectRatio READ keepAspectRatio WRITE setKeepAspectRatio NOTIFY
21 keepAspectRatioChanged)
22 Q_PROPERTY(double aspectRatio READ aspectRatio WRITE setAspectRatio NOTIFY aspectRatioChanged)
23 Q_PROPERTY(int maxWidth READ maxWidth WRITE setMaxWidth NOTIFY maxWidthChanged)
24 Q_PROPERTY(int maxHeight READ maxHeight WRITE setMaxHeight NOTIFY maxHeightChanged)
25 Q_PROPERTY(bool isReply READ isReply WRITE setIsReply NOTIFY isReplyChanged)
26
27 QML_ANONYMOUS
28 public:
29 EventDelegateChooserAttachedType(QObject *parent)
30 : QObject(parent)
31 {
32 }
33
34 bool keepAspectRatio() const { return keepAspectRatio_; }
35 void setKeepAspectRatio(bool fill)
36 {
37 if (fill != keepAspectRatio_) {
38 keepAspectRatio_ = fill;
39 emit keepAspectRatioChanged();
40 polishChooser();
41 }
42 }
43
44 double aspectRatio() const { return aspectRatio_; }
45 void setAspectRatio(double fill)
46 {
47 aspectRatio_ = fill;
48 emit aspectRatioChanged();
49 polishChooser();
50 }
51
52 int maxWidth() const { return maxWidth_; }
53 void setMaxWidth(int fill)
54 {
55 maxWidth_ = fill;
56 emit maxWidthChanged();
57 polishChooser();
58 }
59
60 int maxHeight() const { return maxHeight_; }
61 void setMaxHeight(int fill)
62 {
63 maxHeight_ = fill;
64 emit maxHeightChanged();
65 }
66
67 bool isReply() const { return isReply_; }
68 void setIsReply(bool fill)
69 {
70 if (fill != isReply_) {
71 isReply_ = fill;
72 emit isReplyChanged();
73 polishChooser();
74 }
75 }
76
77 signals:
78 void keepAspectRatioChanged();
79 void aspectRatioChanged();
80 void maxWidthChanged();
81 void maxHeightChanged();
82 void isReplyChanged();
83
84 private:
85 void polishChooser();
86
87 double aspectRatio_ = 1.;
88 int maxWidth_ = -1;
89 int maxHeight_ = -1;
90 bool keepAspectRatio_ = false;
91 bool isReply_ = false;
92 };
93
94 class EventDelegateChoice : public QObject
95 {
96 Q_OBJECT
97 QML_ELEMENT
98 Q_CLASSINFO("DefaultProperty", "delegate")
99
100 public:
101 Q_PROPERTY(QList<int> roleValues READ roleValues WRITE setRoleValues NOTIFY roleValuesChanged
102 REQUIRED FINAL)
103 Q_PROPERTY(
104 QQmlComponent *delegate READ delegate WRITE setDelegate NOTIFY delegateChanged REQUIRED FINAL)
105
106 [[nodiscard]] QQmlComponent *delegate() const;
107 void setDelegate(QQmlComponent *delegate);
108
109 [[nodiscard]] QList<int> roleValues() const;
110 void setRoleValues(const QList<int> &value);
111
112 signals:
113 void delegateChanged();
114 void roleValuesChanged();
115 void changed();
116
117 private:
118 QList<int> roleValues_;
119 QQmlComponent *delegate_ = nullptr;
120 };
121
122 class EventDelegateChooser : public QQuickItem
123 {
124 Q_OBJECT
125 QML_ELEMENT
126 Q_CLASSINFO("DefaultProperty", "choices")
127
128 QML_ATTACHED(EventDelegateChooserAttachedType)
129
130 Q_PROPERTY(QQmlListProperty<EventDelegateChoice> choices READ choices CONSTANT FINAL)
131 Q_PROPERTY(QQuickItem *main READ main NOTIFY mainChanged FINAL)
132 Q_PROPERTY(QQuickItem *reply READ reply NOTIFY replyChanged FINAL)
133 Q_PROPERTY(QString eventId READ eventId WRITE setEventId NOTIFY eventIdChanged REQUIRED FINAL)
134 Q_PROPERTY(QString replyTo READ replyTo WRITE setReplyTo NOTIFY replyToChanged REQUIRED FINAL)
135 Q_PROPERTY(TimelineModel *room READ room WRITE setRoom NOTIFY roomChanged REQUIRED FINAL)
136 Q_PROPERTY(bool sameWidth READ sameWidth WRITE setSameWidth NOTIFY sameWidthChanged)
137 Q_PROPERTY(int maxWidth READ maxWidth WRITE setMaxWidth NOTIFY maxWidthChanged)
138 Q_PROPERTY(int replyInset READ replyInset WRITE setReplyInset NOTIFY replyInsetChanged)
139 Q_PROPERTY(int mainInset READ mainInset WRITE setMainInset NOTIFY mainInsetChanged)
140 Q_PROPERTY(bool limitAsReply READ limitAsReply WRITE setLimitAsReply NOTIFY limitAsReplyChanged)
141
142 public:
143 QQmlListProperty<EventDelegateChoice> choices();
144
145 [[nodiscard]] QQuickItem *main() const
146 {
147 return qobject_cast<QQuickItem *>(eventIncubator.object());
148 }
149 [[nodiscard]] QQuickItem *reply() const
150 {
151 return qobject_cast<QQuickItem *>(replyIncubator.object());
152 }
153
154 bool sameWidth() const { return sameWidth_; }
155 void setSameWidth(bool width)
156 {
157 sameWidth_ = width;
158 emit sameWidthChanged();
159 }
160 bool limitAsReply() const { return limitAsReply_; }
161 void setLimitAsReply(bool width)
162 {
163 limitAsReply_ = width;
164 emit limitAsReplyChanged();
165 }
166 int maxWidth() const { return maxWidth_; }
167 void setMaxWidth(int width)
168 {
169 maxWidth_ = width;
170 emit maxWidthChanged();
171 polish();
172 }
173
174 int replyInset() const { return replyInset_; }
175 void setReplyInset(int width)
176 {
177 replyInset_ = width;
178 emit replyInsetChanged();
179 polish();
180 }
181
182 int mainInset() const { return mainInset_; }
183 void setMainInset(int width)
184 {
185 mainInset_ = width;
186 emit mainInsetChanged();
187 polish();
188 }
189
190 void setRoom(TimelineModel *m)
191 {
192 if (m != room_) {
193 room_ = m;
194 emit roomChanged();
195
196 if (isComponentComplete()) {
197 eventIncubator.reset(eventId_);
198 replyIncubator.reset(replyId);
199 }
200 }
201 }
202 [[nodiscard]] TimelineModel *room() { return room_; }
203
204 void setEventId(QString idx)
205 {
206 eventId_ = idx;
207 emit eventIdChanged();
208
209 if (isComponentComplete())
210 eventIncubator.reset(eventId_);
211 }
212 [[nodiscard]] QString eventId() const { return eventId_; }
213 void setReplyTo(QString id)
214 {
215 replyId = id;
216 emit replyToChanged();
217
218 if (isComponentComplete())
219 replyIncubator.reset(replyId);
220 }
221 [[nodiscard]] QString replyTo() const { return replyId; }
222
223 void componentComplete() override;
224
225 static EventDelegateChooserAttachedType *qmlAttachedProperties(QObject *object)
226 {
227 return new EventDelegateChooserAttachedType(object);
228 }
229
230 void updatePolish() override;
231
232 signals:
233 void mainChanged();
234 void replyChanged();
235 void roomChanged();
236 void eventIdChanged();
237 void replyToChanged();
238 void sameWidthChanged();
239 void limitAsReplyChanged();
240 void maxWidthChanged();
241 void replyInsetChanged();
242 void mainInsetChanged();
243
244 private:
245 struct DelegateIncubator final : public QQmlIncubator
246 {
247 DelegateIncubator(EventDelegateChooser &parent, bool forReply)
248 : QQmlIncubator(QQmlIncubator::AsynchronousIfNested)
249 , chooser(parent)
250 , forReply(forReply)
251 {
252 }
253 void setInitialState(QObject *object) override;
254 void statusChanged(QQmlIncubator::Status status) override;
255
256 void reset(QString id);
257
258 EventDelegateChooser &chooser;
259 bool forReply;
260 QString currentId;
261
262 QString instantiatedId;
263 int instantiatedRole = -1;
264 QAbstractItemModel *instantiatedModel = nullptr;
265 int oldType = -1;
266 };
267
268 QVariant roleValue_;
269 QList<EventDelegateChoice *> choices_;
270 DelegateIncubator eventIncubator{*this, false};
271 DelegateIncubator replyIncubator{*this, true};
272 TimelineModel *room_{nullptr};
273 QString eventId_;
274 QString replyId;
275 bool sameWidth_ = false;
276 bool limitAsReply_ = false;
277 int maxWidth_ = 400;
278 int replyInset_ = 0;
279 int mainInset_ = 0;
280
281 static void appendChoice(QQmlListProperty<EventDelegateChoice> *, EventDelegateChoice *);
282 static qsizetype choiceCount(QQmlListProperty<EventDelegateChoice> *);
283 static EventDelegateChoice *choice(QQmlListProperty<EventDelegateChoice> *, qsizetype index);
284 static void clearChoices(QQmlListProperty<EventDelegateChoice> *);
285 };