-
Notifications
You must be signed in to change notification settings - Fork 0
/
observer.h
96 lines (83 loc) · 2.58 KB
/
observer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#ifndef OBSERVER_H
#define OBSERVER_H
#include <QObject>
class Subject;
/**
* @brief The Observer class
* This Observer class is a simple implementation of the Observer design pattern.
* However, this class is more simple, where the observer can assign any of application events
* and receive notifications in update method to notify qml components in the updated signal.
*
* This class is registered in the application as customContextProperties
*/
class Observer : public QObject
{
Q_OBJECT
Q_PROPERTY(QStringList events READ events WRITE setEvents NOTIFY eventsChanged)
public:
/**
* @brief Observer
* The object construct
* @param parent
*/
explicit Observer(QObject *parent = nullptr);
/**
* Detach the object from all assigned events in the Subject
*/
~Observer();
/**
* @brief events
* Return a list with all assigned events by this observer
* @return QStringList
*/
QStringList events();
/**
* @brief setSubject
* Set the Subject pointer to m_subject member
* m_subject is used in destructor to detach from all signed events
* @param subject Subject*
*/
void setSubject(Subject *subject);
/**
* @brief setEvents
* Set a event list to m_events member
* @param events QStringList
*/
void setEvents(const QStringList &events);
/**
* @brief update
* Set a event notification to observer instance, where reply with updated signal
* @param eventName QString the name of event where the observer are notificated
* @param eventData QVariant the event generic arguments. Can be a integer, string or QVariant[Map|List]
* @param sender QObject* a pointer to the event sender
*/
void update(const QString &eventName, const QVariant &eventData, QObject *sender);
signals:
/**
* @brief eventsChanged
* Notify the observer to events changes
* @param events QStringList
*/
void eventsChanged(const QStringList &events);
/**
* @brief updated
* Notify the observer from new update from any of signed events
* @param eventName QString
* @param eventData QVariant
* @param sender QObject*
*/
void updated(const QString &eventName, const QVariant &eventData, QObject *sender);
private:
/**
* @brief m_events
* A list with the names of signed events
*/
QStringList m_events;
/**
* @brief m_subject
* Keeps a pointer to Subject instance and
* is used to detach the observer from all signed events
*/
Subject *m_subject;
};
#endif // OBSERVER_H