VoltAir
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Groups Pages
VariantConverter.h
1 /*
2  * Copyright (C) 2014 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef VARIANTCONVERTER_H
18 #define VARIANTCONVERTER_H
19 
20 #include <QDebug>
21 #include <QJsonObject>
22 #include <QPointF>
23 #include <QString>
24 #include <QVariant>
25 
35 template <typename T>
37 public:
45  QVariant toVariant(const T& value) {
46  return QVariant(value);
47  }
48 };
49 
58 template <typename T>
60 };
61 
67 template <>
68 class VariantConverter<int> : public VariantConverterBase<int> {
69 public:
74  int fromVariant(const QVariant& value) {
75  return value.toInt();
76  }
77 };
78 
84 template <>
85 class VariantConverter<QString> : public VariantConverterBase<QString> {
86 public:
91  QString fromVariant(const QVariant& value) {
92  return value.toString();
93  }
94 };
95 
101 template <>
102 class VariantConverter<QJsonObject> : public VariantConverterBase<QJsonObject> {
103 public:
113  if (value.canConvert(QMetaType::QVariantMap)) {
114  return QJsonObject::fromVariantMap(value.toMap());
115  } else {
116  return value.toJsonObject();
117  }
118  }
119 };
120 
126 template <>
128 public:
136  QPointF fromVariant(const QVariant& value) {
137  if (value.canConvert(QMetaType::QVariantList)) {
138  QVariantList floatList = value.toList();
139  float x = floatList.size() >= 1 ? floatList[0].toFloat() : 0.0f;
140  float y = floatList.size() >= 2 ? floatList[1].toFloat() : 0.0f;
141  return QPointF(x, y);
142  } else {
143  return value.toPointF();
144  }
145  }
146 };
147 
155 template <typename T>
157 public:
164  QMap<QString, T> convertedMap;
165  VariantConverter<T> converter;
166  QVariantMap map = value.toMap();
167  for (auto it = map.cbegin(); it != map.cend(); ++it) {
168  convertedMap.insert(it.key(), converter.fromVariant(it.value()));
169  }
170  return convertedMap;
171  }
172 
179  QVariantMap map;
180  VariantConverter<T> converter;
181  for (auto it = value.cbegin(); it != value.cend(); ++it) {
182  map.insert(it.key(), converter.toVariant(it.value()));
183  }
184  return QVariant(map);
185  }
186 };
187 
195 template <typename T>
197 public:
203  QSet<T> fromVariant(const QVariant& value) {
204  QSet<T> convertedSet;
205  VariantConverter<T> converter;
206  for (const QVariant& var : value.toList()) {
207  convertedSet.insert(converter.fromVariant(var));
208  }
209  return convertedSet;
210  }
211 
217  QVariant toVariant(const QSet<T>& value) {
218  QVariantList list;
219  list.reserve(value.size());
220  VariantConverter<T> converter;
221  for (const T& val : value) {
222  list.append(converter.toVariant(val));
223  }
224  return QVariant(list);
225  }
226 };
227 
228 #endif // VARIANTCONVERTER_H
bool canConvert(int targetTypeId) const
const_iterator cend() const
QPointF toPointF() const
int size() const
QList< QVariant > toList() const
QJsonObject fromVariantMap(const QVariantMap &map)
QMap< QString, T > fromVariant(const QVariant &value)
Returns value converted to a QMap.
Definition: VariantConverter.h:163
iterator insert(const T &value)
QVariant toVariant(const QMap< QString, T > &value)
Returns value converted to a QVariant of type QVariantMap.
Definition: VariantConverter.h:178
int fromVariant(const QVariant &value)
Returns value converted to an int.
Definition: VariantConverter.h:74
QString fromVariant(const QVariant &value)
Returns value converted to a QString.
Definition: VariantConverter.h:91
QJsonObject toJsonObject() const
int toInt(bool *ok) const
QVariant toVariant(const T &value)
Returns value converted as a QVariant.
Definition: VariantConverter.h:45
QJsonObject fromVariant(const QVariant &value)
Returns value converted to a QJsonObject.
Definition: VariantConverter.h:112
QSet< T > fromVariant(const QVariant &value)
Returns value converted to a QSet.
Definition: VariantConverter.h:203
Base template class for VariantConverter.
Definition: VariantConverter.h:36
const_iterator cbegin() const
QMap< QString, QVariant > toMap() const
QVariant toVariant(const QSet< T > &value)
Returns value converted to a QVariant of type QVariantList.
Definition: VariantConverter.h:217
QPointF fromVariant(const QVariant &value)
Returns value converted to a QPointF.
Definition: VariantConverter.h:136
iterator insert(const Key &key, const T &value)
Helper template class to convert QVariants to and from a specific type, for example, QPointFs.
Definition: VariantConverter.h:59
QString toString() const