DtkDeclarative
DTK Declarative module
载入中...
搜索中...
未找到
dmessagemanager_p.h
1// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
2//
3// SPDX-License-Identifier: LGPL-3.0-or-later
4
5#pragma once
6
7#include <dtkdeclarative_global.h>
8
9#include <QQmlComponent>
10#include <QQuickItem>
11
12DQUICK_BEGIN_NAMESPACE
13
14class MessageManager;
15class FloatingMessageContainer : public QObject
16{
17 Q_OBJECT
18 Q_PROPERTY(QQuickItem *panel READ panel WRITE setPanel)
19 Q_PROPERTY(QVariant message READ message WRITE setMessage NOTIFY messageChanged)
20 Q_PROPERTY(int duration READ duration WRITE setDuration NOTIFY durationChanged)
21 Q_CLASSINFO("DefaultProperty", "panel")
22#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
23 QML_NAMED_ELEMENT(FloatingMessageContainer)
24#endif
25public:
26 explicit FloatingMessageContainer(QObject *parent = nullptr);
27
28 QVariant message() const;
29 void setMessage(const QVariant &message);
30 QQuickItem *panel() const;
31 void setPanel(QQuickItem *panel);
32 QString messageId() const;
33 void setMessageId(const QString &msgId);
34 int duration() const;
35 void setDuration(int duration);
36
37public Q_SLOT:
38 void close();
39
40Q_SIGNALS:
41 void messageChanged();
42 void durationChanged();
43
44private:
45 friend MessageManager;
46
47 QQuickItem *m_panel = nullptr;
48 QVariant m_message;
49 QString m_msgId;
50 int m_duration = 4000;
51};
52
53class MessageManager : public QObject
54{
55 Q_OBJECT
56 Q_PROPERTY(QQmlComponent *delegate READ delegate WRITE setDelegate)
57 Q_PROPERTY(QQuickItem *layout READ layout WRITE setLayout)
58 Q_PROPERTY(int capacity READ capacity WRITE setCapacity)
59 Q_PROPERTY(int count READ count NOTIFY countChanged)
60#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
61 QML_UNCREATABLE("MessageManager Attached.")
62 QML_NAMED_ELEMENT(MessageManager)
63 QML_ATTACHED(MessageManager)
64#endif
65
66public:
67 explicit MessageManager(QQuickWindow *parent = nullptr);
68
69 QQuickWindow *window() const;
70 QQmlComponent *delegate() const;
71 void setDelegate(QQmlComponent *delegate);
72 QQuickItem *layout() const;
73 void setLayout(QQuickItem * layout);
74 int capacity() const;
75 void setCapacity(int capacity);
76 int count() const;
77 QList<FloatingMessageContainer *> messages(const QString &msgId) const;
78
79 static MessageManager *qmlAttachedProperties(QObject *object);
80
81public Q_SLOTS:
82 bool sendMessage(const QString &content, const QString &iconName = QString(), int duration = 4000, const QString &msgId = QString());
83 bool sendMessage(QQmlComponent *delegate, const QVariant &message, int duration = 4000, const QString &msgId = QString());
84 void close(DTK_QUICK_NAMESPACE::FloatingMessageContainer *message);
85 void close(const QString &msgId);
86
87Q_SIGNALS:
88 void countChanged();
89
90protected:
91 virtual void timerEvent(QTimerEvent *e) override;
92
93private:
94 void ensureLayout();
95 void ensureDelegate();
96 bool reachedUpperLimit() const;
97 FloatingMessageContainer *replaceMessage(const QString &msgId);
98 void stackBeforeMessage(QQuickItem *message);
99 FloatingMessageContainer *beginCreateMessage(QQmlComponent *component);
100 void endCreateMessage(QQmlComponent *component, FloatingMessageContainer *container);
101
102private:
103 QQmlComponent *m_delegate;
104 QQuickItem *m_layout;
105 QList<QPair<int, FloatingMessageContainer *>> m_timers;
106 int m_capacity = 3;
107};
108
109DQUICK_END_NAMESPACE
110
111QML_DECLARE_TYPEINFO(DTK_QUICK_NAMESPACE::MessageManager, QML_HAS_ATTACHED_PROPERTIES)
Definition dmessagemanager_p.h:16
Definition dmessagemanager_p.h:54
一种控制 FloatingMessage 的附加对象.