Debian Codegraph @

src/imports/publishsubscribe/qdeclarativevaluespacepublisher.cpp

raw symbols SCIP
1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtSystems module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include "qdeclarativevaluespacepublisher_p.h"
41 #include "qdeclarativevaluespacepublishermetaobject_p.h"
42 #include <QtCore/qregularexpression.h>
43
44 QT_BEGIN_NAMESPACE
45
46 /*!
47 \qmltype ValueSpacePublisher
48 \instantiates QDeclarativeValueSpacePublisher
49 \inqmlmodule QtPublishSubscribe
50 \ingroup qml-publishsubscribe
51
52 \brief The ValueSpacePublisher allows application to publish values to Value Space.
53
54 ValueSpacePublishers are constructed with a fixed \a path which cannot be changed. And you should
55 set the \a path property before publishing any values. If you need to publish within multiple
56 different paths, you will need multiple publishers.
57
58 For the keys within the path chosen, if the key names to be published are alphanumeric, they can
59 be accessed through dynamic properties by setting the \a keys list.
60
61 Example:
62 \code
63 ValueSpacePublisher {
64 id: battery
65 path: "/power/battery"
66 keys: ["charge", "charging"]
67 }
68
69 MouseArea {
70 onClicked: {
71 battery.charge = 50
72 battery.charging = true
73 }
74 }
75 \endcode
76
77 Alternatively, for key names that can't be mapped to properties, or for key names shadowed by
78 existing properties (like "value" or "path"), you can also access the \a value property of the
79 publisher itself.
80
81 \code
82 ValueSpacePublisher {
83 id: nonalpha
84 path: "/something/with a space/value"
85 }
86
87 MouseArea {
88 onClicked: {
89 nonalpha.value = "example"
90 }
91 }
92 \endcode
93 */
94
95 QDeclarativeValueSpacePublisher::QDeclarativeValueSpacePublisher(QObject *parent)
96 : QObject(parent)
97 , metaObj(new QDeclarativeValueSpacePublisherMetaObject(this))
98 , hasSubscriber(false)
99 , publisher(0)
100 {
101 d_ptr.data()->metaObject = metaObj;
102 }
103
104 QDeclarativeValueSpacePublisher::~QDeclarativeValueSpacePublisher()
105 {
106 d_ptr.data()->metaObject = 0;
107 delete metaObj;
108 }
109
110 /*!
111 \qmlproperty string ValueSpacePublisher::path
112
113 This property holds the base path of the publisher, and it should be written before publishing
114 any data. Note it can only be written once, and further writing has no effects.
115 */
116 QString QDeclarativeValueSpacePublisher::path() const
117 {
118 if (publisher)
119 return publisher->path();
120 else
121 return QString();
122 }
123
124 void QDeclarativeValueSpacePublisher::setPath(const QString &path)
125 {
126 if (!publisher) {
127 publisher = new QValueSpacePublisher(path);
128 connect(publisher, SIGNAL(interestChanged(QString,bool)),
129 this, SLOT(onInterestChanged(QString,bool)));
130 }
131 }
132
133 /*!
134 \qmlproperty QVariant ValueSpacePublisher::value
135
136 This property publishes a new value to the ValueSpace at the
137 path given through the \a path property.
138 This property is write only.
139 */
140 void QDeclarativeValueSpacePublisher::setValue(const QVariant &value)
141 {
142 if (publisher)
143 publisher->setValue(QString(), value);
144 }
145
146 QVariant QDeclarativeValueSpacePublisher::dummyValue() const
147 {
148 return QVariant();
149 }
150
151 /*!
152 \qmlproperty bool ValueSpacePublisher::hasSubscribers
153
154 This property is true if there are subscribers currently subscribed to
155 the ValueSpace path being published by this Publisher.
156
157 This property is read only.
158 */
159 bool QDeclarativeValueSpacePublisher::hasSubscribers() const
160 {
161 return hasSubscriber;
162 }
163
164 /*!
165 \qmlproperty QStringList ValueSpacePublisher::keys
166
167 Setting this property creates a set of dynamic properties allowing
168 easy access to set the values of keys under this Publisher's path.
169 */
170 void QDeclarativeValueSpacePublisher::setKeys(const QStringList &keys)
171 {
172 foreach (const QString &key, keys) {
173 if (key.contains(QRegularExpression(QStringLiteral("[^a-zA-Z0-9]")))
174 || key == QString(QStringLiteral("value"))
175 || key == QString(QStringLiteral("path"))
176 || key == QString(QStringLiteral("keys"))
177 || key == QString(QStringLiteral("hasSubscribers"))) {
178 continue;
179 }
180
181 metaObj->createProperty(key.toUtf8().constData(), "QVariant");
182 }
183 }
184
185 QStringList QDeclarativeValueSpacePublisher::keys() const
186 {
187 QStringList keys;
188 QList<QPair<QString, QVariant> > properties = metaObj->dynamicProperties.values();
189 for (QList<QPair<QString, QVariant> >::const_iterator i = properties.constBegin(); i != properties.constEnd(); ++i)
190 keys << (*i).first;
191 return keys;
192 }
193
194 /*!
195 \internal
196 */
197 void QDeclarativeValueSpacePublisher::onInterestChanged(const QString &path, bool interested)
198 {
199 Q_UNUSED(path)
200
201 hasSubscriber = interested;
202 emit subscribersChanged();
203 }
204
205 QT_END_NAMESPACE