X-Git-Url: https://vaikene.ee/gitweb/gitweb.cgi?a=blobdiff_plain;f=src%2Flibs%2FCommon%2Futil.cpp;fp=src%2Flibs%2FCommon%2Futil.cpp;h=ccabdab922921a0ac2b927149cfd8e6dc22c86d8;hb=814d12e0a340ae11fa4a22077b37316aa41716d7;hp=0000000000000000000000000000000000000000;hpb=28615229f7e2667ea5632293ca13f8ffdaaa9700;p=evaf diff --git a/src/libs/Common/util.cpp b/src/libs/Common/util.cpp new file mode 100644 index 0000000..ccabdab --- /dev/null +++ b/src/libs/Common/util.cpp @@ -0,0 +1,81 @@ +/** + * @file Common/util.cpp + * @brief Global utility functions for eVaf + * @author Enar Vaikene + * + * Copyright (c) 2011 Enar Vaikene + * + * This file is part of the eVaf C++ cross-platform application development framework. + * + * This file can be used under the terms of the GNU General Public License + * version 3.0 as published by the Free Software Foundation and appearing in + * the file LICENSE included in the packaging of this file. Please review the + * the following information to ensure the GNU General Public License version + * 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html. + * + * Alternatively, this file may be used in accordance with the Commercial License + * Agreement provided with the Software. + */ + +#include "util.h" + + +//------------------------------------------------------------------- + +QVariant eVaf::Common::toVariant(QString const & value, QVariant const & defaultValue) +{ + // If the default value is not valid, return the string value without conversions + if (!defaultValue.isValid()) + return QVariant(value); + + // Convert to the expected type + switch (defaultValue.type()) { + case QVariant::UInt: { + bool ok; + uint v = value.toUInt(&ok, 0); + if (ok) + return QVariant(v); + else + return defaultValue; + break; + } + case QVariant::Int: { + bool ok; + int v = value.toInt(&ok, 0); + if (ok) + return QVariant(v); + else + return defaultValue; + break; + } + case QVariant::Double: { + bool ok; + double v = value.toDouble(&ok); + if (ok) + return QVariant(v); + else + return defaultValue; + break; + } + case QVariant::Bool: { + bool ok; + uint v = value.toUInt(&ok, 0); + if (eVaf::Common::isTrue(value)) + return QVariant(true); + else if (eVaf::Common::isFalse(value)) + return QVariant(false); + else + return QVariant(v); + break; + } + case QVariant::Char: { + if (value.size() > 0) + return QVariant(value.at(0)); + else + return defaultValue; + break; + } + default: + return QVariant(value); + } +}