27 #include <QStringBuilder> 35 QObject(parent), m_outerScope(0)
43 qmlRegisterType<ZScopedMap>(
"org.zuble.qml", 1, 0,
"ZScopedMap");
54 zWarning() <<
"ZScopedMap programming error - outerScope must be a ZScopedMap object.";
78 if(jsonString.isEmpty())
83 zWarning() <<
"loadJSON failed! - JSON initializer string has already been set.";
90 QJsonDocument doc = QJsonDocument::fromJson(jsonString.toUtf8(), &er);
92 if(er.error != QJsonParseError::NoError)
94 zWarning() <<
"loadJSON parse error: " << er.errorString();
95 zWarning() <<
"JSON failed: " << jsonString;
102 zWarning() <<
"loadJSON format error: " <<
103 "JSON initializer should be a JSON object of key/value pairs.";
104 zWarning() <<
"JSON failed: " << jsonString;
107 QJsonObject ob = doc.object();
109 QJsonObject::const_iterator i = ob.begin();
113 for(;i != ob.end(); i++)
115 QString key = i.key();
116 QJsonValue value = *i;
118 QVariant qValue(value.toVariant());
122 tempMap.insert(key, qValue);
132 zDebug() <<
"Getting JSON string from scoped map.";
134 QJsonDocument jdoc = QJsonDocument::fromVariant(
m_map);
136 QString json = jdoc.toJson();
138 zDebug() <<
"JSON property string = " << json;
156 if(
m_map.contains(key))
164 if(
m_map.contains(key))
165 return m_map.value(key);
177 QVariant qValue = value.toVariant();
184 if(map.contains(key))
186 QVariant old =
m_map.value(key);
188 if(old.type() == value.type() && old == value)
192 zDebug() <<
"Modifying scoped map - key: " << key <<
", value: " << value;
194 map.insert(key, value);
204 return m_map.remove(key) ? true :
false;
213 QList<QString> k =
m_map.keys();
215 const int count = k.count();
219 for(
int i=0; i<count; i++)
221 qDebug() <<
"mapkey = " << k.at(i);
223 vk.append(QVariant(k.at(i)));
226 for(
int i = 0; i<vk.count(); i++)
227 qDebug() <<
"variant key = " << vk.at(i);
241 case QJsonValue::Bool:
242 case QJsonValue::Double:
243 case QJsonValue::String:
247 zWarning() <<
"Value type error: " <<
248 "type must be either number, boolean or string.";
249 zWarning() <<
"Failed value: " << value.toString();
259 case QMetaType::Bool:
260 case QMetaType::Double:
262 case QMetaType::UInt:
263 case QMetaType::LongLong:
264 case QMetaType::ULongLong:
265 case QMetaType::QString:
269 zWarning() <<
"Value type error: " <<
270 "type must be either number, boolean or string.";
271 zWarning() <<
"Failed value: " << value.toString();
283 QVariantMap::const_iterator i = map.begin();
285 for(;i != map.end(); i++)
287 QVariant nextValue(i.value());
292 if(!
setValue(tempMap, i.key(), i.value()),
false)
296 if(tempMap.isEmpty())
301 for(;i != tempMap.end(); i++)
302 m_map.insert(i.key(), i.value());
315 zWarning() <<
"setMap invalid parameter type: object must be of type " 316 "ZScopedMap or QVariantMap (javascript native object)";
325 zDebug() <<
"Emitting ZScopedMap::reset signal";
bool isValidType(const QJsonValue &value)
Test if a value has a type that can be stored in the scope.
QVariantMap m_map
A dictionary of scoped objects.
QObject outerScope
The number of items in the map.
#define ZBL_REGISTER_LOGGED_OBJECT
int getSize() const
Obtain number of items in the map.
bool setMap(QVariantMap map)
Inserts a set of key/value pairs into the map. Existing values are overwritten.
ZBL_DECLARE_LOGGED_OBJECT QString getInitializer() const
Return the JSON string used to initialize this object.
bool setScopedMap(QObject *map)
Inserts a set of key/value pairs into the map. Existing values are overwritten.
Q_INVOKABLE QVariantList keys() const
Obtain the key names in the map.
#define ZBL_DEFINE_LOGGED_OBJECT(class_name)
void set(const QString &key, const QJsonValue &value)
Sets the specified key in the map to the specified value. Existing values are overwritten.
ZScopedMap * m_outerScope
The scope enclosing this scope, or null if this is outermost scope.
QObject * getOuterScope()
Get the enclosing scope. May be null if this is outermost scope.
Q_INVOKABLE bool has(const QString &key) const
Returns true if the map contains the specified key.
bool setValue(QVariantMap &map, const QString &key, QVariant value, bool emitSignal=false)
Sets the specified key in the map to the specified value. Existing values are overwritten.
void loadJSON(const QString &jsonString)
Parses the supplied JSON string and writes the contained key/value pairs to the map.
Q_INVOKABLE void invalidate()
Issue a scopeChanged signal. All clients should query their values.
QString m_JSON
JSON string used to initialize this scope.
void clear()
Remove all key/value pairs from the map.
void itemChanged(const QString key, QVariant value)
void mapChanged(QVariantMap newValues)
A hierarchical map object for creating scopes.
Q_INVOKABLE QString toJSON() const
Obtain a JSON string representation of the map.
Q_INVOKABLE QVariant get(const QString &key) const
Returns the value associated with a specified key in the map.
static void registerType()
Register ZMailbox as a QML type.
bool remove(const QString &key)
Removes the specified key from the map.
void setOuterScope(QObject *outerScope)
Set the enclosing scope to which unresolved name lookups will be forwarded.