]>
vaikene.ee Git - evaf/blob - src/libs/Common/util.cpp
2 * @file Common/util.cpp
3 * @brief Global utility functions for eVaf
6 * Copyright (c) 2011 Enar Vaikene
8 * This file is part of the eVaf C++ cross-platform application development framework.
10 * This file can be used under the terms of the GNU General Public License
11 * version 3.0 as published by the Free Software Foundation and appearing in
12 * the file LICENSE included in the packaging of this file. Please review the
13 * the following information to ensure the GNU General Public License version
14 * 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
16 * Alternatively, this file may be used in accordance with the Commercial License
17 * Agreement provided with the Software.
24 //-------------------------------------------------------------------
26 QVariant
eVaf::Common::toVariant(QString
const & value
, QVariant
const & defaultValue
)
28 // If the default value is not valid, return the string value without conversions
29 if (!defaultValue
.isValid())
30 return QVariant(value
);
32 // Convert to the expected type
33 switch (defaultValue
.type()) {
34 case QVariant::UInt
: {
36 uint v
= value
.toUInt(&ok
, 0);
45 int v
= value
.toInt(&ok
, 0);
52 case QVariant::Double
: {
54 double v
= value
.toDouble(&ok
);
61 case QVariant::Bool
: {
62 if (eVaf::Common::isTrue(value
))
63 return QVariant(true);
64 else if (eVaf::Common::isFalse(value
))
65 return QVariant(false);
68 uint v
= value
.toUInt(&ok
, 0);
76 case QVariant::Char
: {
77 if (value
.size() > 0) {
78 if (value
.startsWith("\\0x")) {
80 char c
= value
.mid(1).toUInt(&ok
, 16);
84 else if (value
.startsWith("\\0")) {
86 char c
= value
.mid(1).toUInt(&ok
, 8);
91 return QVariant(value
.at(0));
97 return QVariant(value
);