]> vaikene.ee Git - evaf/blob - src/libs/Common/util.cpp
Added global utility functions.
[evaf] / src / libs / Common / util.cpp
1 /**
2 * @file Common/util.cpp
3 * @brief Global utility functions for eVaf
4 * @author Enar Vaikene
5 *
6 * Copyright (c) 2011 Enar Vaikene
7 *
8 * This file is part of the eVaf C++ cross-platform application development framework.
9 *
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.
15 *
16 * Alternatively, this file may be used in accordance with the Commercial License
17 * Agreement provided with the Software.
18 */
19
20 #include "util.h"
21
22
23 //-------------------------------------------------------------------
24
25 QVariant eVaf::Common::toVariant(QString const & value, QVariant const & defaultValue)
26 {
27 // If the default value is not valid, return the string value without conversions
28 if (!defaultValue.isValid())
29 return QVariant(value);
30
31 // Convert to the expected type
32 switch (defaultValue.type()) {
33 case QVariant::UInt: {
34 bool ok;
35 uint v = value.toUInt(&ok, 0);
36 if (ok)
37 return QVariant(v);
38 else
39 return defaultValue;
40 break;
41 }
42 case QVariant::Int: {
43 bool ok;
44 int v = value.toInt(&ok, 0);
45 if (ok)
46 return QVariant(v);
47 else
48 return defaultValue;
49 break;
50 }
51 case QVariant::Double: {
52 bool ok;
53 double v = value.toDouble(&ok);
54 if (ok)
55 return QVariant(v);
56 else
57 return defaultValue;
58 break;
59 }
60 case QVariant::Bool: {
61 bool ok;
62 uint v = value.toUInt(&ok, 0);
63 if (eVaf::Common::isTrue(value))
64 return QVariant(true);
65 else if (eVaf::Common::isFalse(value))
66 return QVariant(false);
67 else
68 return QVariant(v);
69 break;
70 }
71 case QVariant::Char: {
72 if (value.size() > 0)
73 return QVariant(value.at(0));
74 else
75 return defaultValue;
76 break;
77 }
78 default:
79 return QVariant(value);
80 }
81 }