QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
qgsvariantutils.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsvariantutils.h
3 ------------------
4 Date : January 2022
5 Copyright : (C) 2022 Nyall Dawson
6 Email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgsvariantutils.h"
17#include "qgslogger.h"
18
19#include <QDate>
20#include <QTime>
21#include <QDateTime>
22#include <QBitArray>
23#include <QRect>
24#include <QLine>
25#include <QUuid>
26#include <QImage>
27#include <QPixmap>
28#include <QBrush>
29#include <QColor>
30#include <QBitmap>
31#include <QIcon>
32#include <QVector2D>
33#include <QVector3D>
34#include <QVector4D>
35#include <QQuaternion>
36
37QString QgsVariantUtils::typeToDisplayString( QVariant::Type type, QVariant::Type subType )
38{
39 switch ( type )
40 {
41 case QVariant::Invalid:
42 break;
43 case QVariant::Bool:
44 return QObject::tr( "Boolean" );
45 case QVariant::Int:
46 return QObject::tr( "Integer (32 bit)" );
47 case QVariant::UInt:
48 return QObject::tr( "Integer (unsigned 32 bit)" );
49 case QVariant::LongLong:
50 return QObject::tr( "Integer (64 bit)" );
51 case QVariant::ULongLong:
52 return QObject::tr( "Integer (unsigned 64 bit)" );
53 case QVariant::Double:
54 return QObject::tr( "Decimal (double)" );
55 case QVariant::Char:
56 return QObject::tr( "Character" );
57 case QVariant::Map:
58 return QObject::tr( "Map" );
59 case QVariant::List:
60 {
61 switch ( subType )
62 {
63 case QVariant::Int:
64 return QObject::tr( "Integer List" );
65 case QVariant::LongLong:
66 return QObject::tr( "Integer (64 bit) List" );
67 case QVariant::Double:
68 return QObject::tr( "Decimal (double) List" );
69 default:
70 return QObject::tr( "List" );
71 }
72 }
73 case QVariant::String:
74 return QObject::tr( "Text (string)" );
75 case QVariant::StringList:
76 return QObject::tr( "String List" );
77 case QVariant::ByteArray:
78 return QObject::tr( "Binary Object (BLOB)" );
79 case QVariant::BitArray:
80 return QObject::tr( "Bit Array" );
81 case QVariant::Date:
82 return QObject::tr( "Date" );
83 case QVariant::Time:
84 return QObject::tr( "Time" );
85 case QVariant::DateTime:
86 return QObject::tr( "Date & Time" );
87 case QVariant::Url:
88 return QObject::tr( "URL" );
89 case QVariant::Locale:
90 return QObject::tr( "Locale" );
91 case QVariant::Rect:
92 case QVariant::RectF:
93 return QObject::tr( "Rectangle" );
94 case QVariant::Size:
95 case QVariant::SizeF:
96 return QObject::tr( "Size" );
97 case QVariant::Line:
98 case QVariant::LineF:
99 return QObject::tr( "Line" );
100 case QVariant::Point:
101 case QVariant::PointF:
102 return QObject::tr( "Point" );
103 case QVariant::RegularExpression:
104 return QObject::tr( "Regular Expression" );
105 case QVariant::Hash:
106 return QObject::tr( "Hash" );
107 case QVariant::EasingCurve:
108 return QObject::tr( "Easing Curve" );
109 case QVariant::Uuid:
110 return QObject::tr( "UUID" );
111 case QVariant::ModelIndex:
112 case QVariant::PersistentModelIndex:
113 return QObject::tr( "Model Index" );
114 case QVariant::Font:
115 return QObject::tr( "Font" );
116 case QVariant::Pixmap:
117 return QObject::tr( "Pixmap" );
118 case QVariant::Brush:
119 return QObject::tr( "Brush" );
120 case QVariant::Color:
121 return QObject::tr( "Color" );
122 case QVariant::Palette:
123 return QObject::tr( "Palette" );
124 case QVariant::Image:
125 return QObject::tr( "Image" );
126 case QVariant::Polygon:
127 case QVariant::PolygonF:
128 return QObject::tr( "Polygon" );
129 case QVariant::Region:
130 return QObject::tr( "Region" );
131 case QVariant::Bitmap:
132 return QObject::tr( "Bitmap" );
133 case QVariant::Cursor:
134 return QObject::tr( "Cursor" );
135 case QVariant::KeySequence:
136 return QObject::tr( "Key Sequence" );
137 case QVariant::Pen:
138 return QObject::tr( "Pen" );
139 case QVariant::TextLength:
140 return QObject::tr( "Text Length" );
141 case QVariant::TextFormat:
142 return QObject::tr( "Text Format" );
143 case QVariant::Matrix4x4:
144 return QObject::tr( "Matrix" );
145 case QVariant::Transform:
146 return QObject::tr( "Transform" );
147 case QVariant::Vector2D:
148 case QVariant::Vector3D:
149 case QVariant::Vector4D:
150 return QObject::tr( "Vector" );
151 case QVariant::Quaternion:
152 return QObject::tr( "Quaternion" );
153 case QVariant::Icon:
154 return QObject::tr( "Icon" );
155 case QVariant::SizePolicy:
156 return QObject::tr( "Size Policy" );
157
158 default:
159 break;
160 }
161 return QString();
162}
163
164bool QgsVariantUtils::isNull( const QVariant &variant )
165{
166 if ( variant.isNull() || !variant.isValid() )
167 return true;
168
169 switch ( variant.type() )
170 {
171 case QVariant::Invalid:
172 case QVariant::Bool:
173 case QVariant::Int:
174 case QVariant::UInt:
175 case QVariant::LongLong:
176 case QVariant::ULongLong:
177 case QVariant::Double:
178 case QVariant::Map:
179 case QVariant::List:
180 case QVariant::StringList:
181 case QVariant::Url:
182 case QVariant::Locale:
183 case QVariant::RegularExpression:
184 case QVariant::Hash:
185 case QVariant::EasingCurve:
186 case QVariant::ModelIndex:
187 case QVariant::PersistentModelIndex:
188
189 case QVariant::LastType:
190
191 return false;
192
193 case QVariant::Date:
194 if ( variant.toDate().isNull() )
195 {
196 QgsDebugMsg( QStringLiteral( "NULL QDateTime was stored in a QVariant -- stop it! Always use an invalid QVariant() instead." ) );
197 return true;
198 }
199 return false;
200 case QVariant::Time:
201 if ( variant.toTime().isNull() )
202 {
203 QgsDebugMsg( QStringLiteral( "NULL QTime was stored in a QVariant -- stop it! Always use an invalid QVariant() instead." ) );
204 return true;
205 }
206 return false;
207 case QVariant::DateTime:
208 if ( variant.toDate().isNull() )
209 {
210 QgsDebugMsg( QStringLiteral( "NULL QDate was stored in a QVariant -- stop it! Always use an invalid QVariant() instead." ) );
211 return true;
212 }
213 return false;
214 case QVariant::Char:
215 if ( variant.toChar().isNull() )
216 {
217 QgsDebugMsg( QStringLiteral( "NULL QChar was stored in a QVariant -- stop it! Always use an invalid QVariant() instead." ) );
218 return true;
219 }
220 return false;
221 case QVariant::String:
222 if ( variant.toString().isNull() )
223 {
224 QgsDebugMsg( QStringLiteral( "NULL QString was stored in a QVariant -- stop it! Always use an invalid QVariant() instead." ) );
225 return true;
226 }
227 return false;
228 case QVariant::ByteArray:
229 if ( variant.toByteArray().isNull() )
230 {
231 QgsDebugMsg( QStringLiteral( "NULL QByteArray was stored in a QVariant -- stop it! Always use an invalid QVariant() instead." ) );
232 return true;
233 }
234 return false;
235 case QVariant::BitArray:
236 if ( variant.toBitArray().isNull() )
237 {
238 QgsDebugMsg( QStringLiteral( "NULL QBitArray was stored in a QVariant -- stop it! Always use an invalid QVariant() instead." ) );
239 return true;
240 }
241 return false;
242 case QVariant::Rect:
243 if ( variant.toRect().isNull() )
244 {
245 QgsDebugMsg( QStringLiteral( "NULL QRect was stored in a QVariant -- stop it! Always use an invalid QVariant() instead." ) );
246 return true;
247 }
248 return false;
249 case QVariant::RectF:
250 if ( variant.toRectF().isNull() )
251 {
252 QgsDebugMsg( QStringLiteral( "NULL QRectF was stored in a QVariant -- stop it! Always use an invalid QVariant() instead." ) );
253 return true;
254 }
255 return false;
256 case QVariant::Size:
257 if ( variant.toSize().isNull() )
258 {
259 QgsDebugMsg( QStringLiteral( "NULL QSize was stored in a QVariant -- stop it! Always use an invalid QVariant() instead." ) );
260 return true;
261 }
262 return false;
263 case QVariant::SizeF:
264 if ( variant.toSizeF().isNull() )
265 {
266 QgsDebugMsg( QStringLiteral( "NULL QSizeF was stored in a QVariant -- stop it! Always use an invalid QVariant() instead." ) );
267 return true;
268 }
269 return false;
270 case QVariant::Line:
271 if ( variant.toLine().isNull() )
272 {
273 QgsDebugMsg( QStringLiteral( "NULL QLine was stored in a QVariant -- stop it! Always use an invalid QVariant() instead." ) );
274 return true;
275 }
276 return false;
277 case QVariant::LineF:
278 if ( variant.toLineF().isNull() )
279 {
280 QgsDebugMsg( QStringLiteral( "NULL QLineF was stored in a QVariant -- stop it! Always use an invalid QVariant() instead." ) );
281 return true;
282 }
283 return false;
284 case QVariant::Point:
285 if ( variant.toPoint().isNull() )
286 {
287 QgsDebugMsg( QStringLiteral( "NULL QPoint was stored in a QVariant -- stop it! Always use an invalid QVariant() instead." ) );
288 return true;
289 }
290 return false;
291 case QVariant::PointF:
292 if ( variant.toPointF().isNull() )
293 {
294 QgsDebugMsg( QStringLiteral( "NULL QPointF was stored in a QVariant -- stop it! Always use an invalid QVariant() instead." ) );
295 return true;
296 }
297 return false;
298 case QVariant::Uuid:
299 if ( variant.toUuid().isNull() )
300 {
301 QgsDebugMsg( QStringLiteral( "NULL QUuid was stored in a QVariant -- stop it! Always use an invalid QVariant() instead." ) );
302 return true;
303 }
304 return false;
305 case QVariant::Pixmap:
306 if ( variant.value< QPixmap >().isNull() )
307 {
308 QgsDebugMsg( QStringLiteral( "NULL QPixmap was stored in a QVariant -- stop it! Always use an invalid QVariant() instead." ) );
309 return true;
310 }
311 return false;
312 case QVariant::Image:
313 if ( variant.value< QImage >().isNull() )
314 {
315 QgsDebugMsg( QStringLiteral( "NULL QImage was stored in a QVariant -- stop it! Always use an invalid QVariant() instead." ) );
316 return true;
317 }
318 return false;
319 case QVariant::Region:
320 if ( variant.value< QRegion >().isNull() )
321 {
322 QgsDebugMsg( QStringLiteral( "NULL QRegion was stored in a QVariant -- stop it! Always use an invalid QVariant() instead." ) );
323 return true;
324 }
325 return false;
326 case QVariant::Bitmap:
327 if ( variant.value< QBitmap >().isNull() )
328 {
329 QgsDebugMsg( QStringLiteral( "NULL QBitmap was stored in a QVariant -- stop it! Always use an invalid QVariant() instead." ) );
330 return true;
331 }
332 return false;
333 case QVariant::Icon:
334 if ( variant.value< QIcon >().isNull() )
335 {
336 QgsDebugMsg( QStringLiteral( "NULL QIcon was stored in a QVariant -- stop it! Always use an invalid QVariant() instead." ) );
337 return true;
338 }
339 return false;
340 case QVariant::Vector2D:
341 if ( variant.value< QVector2D >().isNull() )
342 {
343 QgsDebugMsg( QStringLiteral( "NULL QVector2D was stored in a QVariant -- stop it! Always use an invalid QVariant() instead." ) );
344 return true;
345 }
346 return false;
347 case QVariant::Vector3D:
348 if ( variant.value< QVector3D >().isNull() )
349 {
350 QgsDebugMsg( QStringLiteral( "NULL QVector3D was stored in a QVariant -- stop it! Always use an invalid QVariant() instead." ) );
351 return true;
352 }
353 return false;
354 case QVariant::Vector4D:
355 if ( variant.value< QVector4D >().isNull() )
356 {
357 QgsDebugMsg( QStringLiteral( "NULL QVector4D was stored in a QVariant -- stop it! Always use an invalid QVariant() instead." ) );
358 return true;
359 }
360 return false;
361 case QVariant::Quaternion:
362 if ( variant.value< QQuaternion >().isNull() )
363 {
364 QgsDebugMsg( QStringLiteral( "NULL QQuaternion was stored in a QVariant -- stop it! Always use an invalid QVariant() instead." ) );
365 return true;
366 }
367 return false;
368
369 case QVariant::Color:
370 case QVariant::Font:
371 case QVariant::Brush:
372 case QVariant::Polygon:
373 case QVariant::Palette:
374 case QVariant::Cursor:
375 case QVariant::KeySequence:
376 case QVariant::Pen:
377 case QVariant::TextLength:
378 case QVariant::PolygonF:
379 case QVariant::TextFormat:
380 case QVariant::Transform:
381 case QVariant::Matrix4x4:
382 case QVariant::SizePolicy:
383 break;
384
385 case QVariant::UserType:
386 break;
387
388 default:
389 break;
390 }
391
392 return false;
393}
394
395
static QString typeToDisplayString(QVariant::Type type, QVariant::Type subType=QVariant::Type::Invalid)
Returns a user-friendly translated string representing a QVariant type.
static bool isNull(const QVariant &variant)
Returns true if the specified variant should be considered a NULL value.
#define QgsDebugMsg(str)
Definition: qgslogger.h:38