Qt 4.8
qdeclarativepropertymap.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtDeclarative 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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
43 
44 #include <private/qmetaobjectbuilder_p.h>
45 #include "private/qdeclarativeopenmetaobject_p.h"
46 
47 #include <QDebug>
48 
50 
51 //QDeclarativePropertyMapMetaObject lets us listen for changes coming from QML
52 //so we can emit the changed signal.
54 {
55 public:
57 
58 protected:
59  virtual void propertyWritten(int index);
60  virtual void propertyCreated(int, QMetaPropertyBuilder &);
61 
62 private:
65 };
66 
68 {
70 public:
73  void emitChanged(const QString &key, const QVariant &value);
74 };
75 
77 {
79  emit q->valueChanged(key, value);
80 }
81 
83 {
84  map = obj;
85  priv = objPriv;
86 }
87 
89 {
90  priv->emitChanged(QString::fromUtf8(name(index)), operator[](index));
91 }
92 
94 {
96 }
97 
146 : QObject(*(new QDeclarativePropertyMapPrivate), parent)
147 {
149  d->mo = new QDeclarativePropertyMapMetaObject(this, d);
150 }
151 
156 {
157 }
158 
163 {
165  d->mo->setValue(key.toUtf8(), QVariant());
166 }
167 
175 {
177  return d->mo->value(key.toUtf8());
178 }
179 
186 {
188  //The following strings shouldn't be used as property names
189  if (key != QLatin1String("keys")
190  && key != QLatin1String("valueChanged")
191  && key != QLatin1String("QObject")
192  && key != QLatin1String("destroyed")
193  && key != QLatin1String("deleteLater")) {
194  d->mo->setValue(key.toUtf8(), value);
195  } else {
196  qWarning() << "Creating property with name"
197  << key
198  << "is not permitted, conflicts with internal symbols.";
199  }
200 }
201 
209 {
211  return d->keys;
212 }
213 
223 {
225  return d->keys.count();
226 }
227 
234 {
236  return d->keys.size();
237 }
238 
246 {
248  return d->keys.isEmpty();
249 }
250 
257 {
259  return d->keys.contains(key);
260 }
261 
273 {
274  //### optimize
276  QByteArray utf8key = key.toUtf8();
277  if (!d->keys.contains(key))
278  insert(key, QVariant());//force creation -- needed below
279 
280  return (*(d->mo))[utf8key];
281 }
282 
292 {
293  return value(key);
294 }
295 
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
void clear(const QString &key)
Clears the value (if any) associated with key.
double d
Definition: qnumeric_p.h:62
virtual ~QDeclarativePropertyMap()
Destroys the bindable map.
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QByteArray name() const
Returns the name associated with this property.
bool isEmpty() const
Returns true if the map contains no keys; otherwise returns false.
The QMetaPropertyBuilder class enables modifications to a property definition on a meta object builde...
QByteArray toUtf8() const Q_REQUIRED_RESULT
Returns a UTF-8 representation of the string as a QByteArray.
Definition: qstring.cpp:4074
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
void insert(const QString &key, const QVariant &value)
Sets the value associated with key to value.
bool contains(const QString &key) const
Returns true if the map contains key.
QDeclarativePropertyMapMetaObject * mo
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
QVariant & operator[](const QString &key)
Returns the value associated with the key key as a modifiable reference.
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define Q_D(Class)
Definition: qglobal.h:2482
QDeclarativePropertyMapPrivate * priv
The QDeclarativePropertyMap class allows you to set key-value pairs that can be used in QML bindings...
#define Q_Q(Class)
Definition: qglobal.h:2483
int size() const
Returns the number of keys in the map.
QVariant value(const QByteArray &) const
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
QVariant value(const QString &key) const
Returns the value associated with key.
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
virtual void propertyCreated(int, QMetaPropertyBuilder &)
#define emit
Definition: qobjectdefs.h:76
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
QDeclarativePropertyMap(QObject *parent=0)
Constructs a bindable map with parent object parent.
static QString fromUtf8(const char *, int size=-1)
Returns a QString initialized with the first size bytes of the UTF-8 string str.
Definition: qstring.cpp:4302
Q_CORE_EXPORT void qWarning(const char *,...)
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
int count() const
Same as size().
Q_INVOKABLE QStringList keys() const
Returns the list of keys.
int key
void emitChanged(const QString &key, const QVariant &value)
QDeclarativePropertyMapMetaObject(QDeclarativePropertyMap *obj, QDeclarativePropertyMapPrivate *objPriv)
quint16 index
QAbstractDynamicMetaObject * parent() const