void ScanFoldersModel::configure()
{
    const QVariantHash dirs = Preferences::instance()->getScanDirs();

    for (QVariantHash::const_iterator i = dirs.begin(), e = dirs.end(); i != e; ++i) {
        if (i.value().type() == QVariant::Int)
            addPath(i.key(), static_cast<PathType>(i.value().toInt()), QString());
        else
            addPath(i.key(), CUSTOM_LOCATION, i.value().toString());
    }
}
Beispiel #2
0
QString QJson::encodeData(const QVariant &data, EncodeOptions options, QString *errorMessage,
	int indentation, QString currentLinePrefix)
{
	QString indentedLinePrefix = options.testFlag(Compact) ?
		QString::fromLatin1("") :
		(currentLinePrefix + QString::fromLatin1(" ").repeated(indentation));

	QString optionalNewLine = options.testFlag(Compact) ?
		QString::fromLatin1("") :
		(QString::fromLatin1("\n") + currentLinePrefix);

	QString optionalIndentedNewLine = options.testFlag(Compact) ?
		QString::fromLatin1("") :
		(QString::fromLatin1("\n") + indentedLinePrefix);

	QString encoded;

	switch (data.type())
	{
	case(QVariant::Bool) :
		encoded += QString::fromLatin1(data.toBool() ? "true" : "false");
		break;

	case(QVariant::Int) :
	case(QVariant::UInt) :
	case(QVariant::LongLong) :
	case(QVariant::ULongLong) :
							  Q_ASSERT(data.canConvert(QVariant::String));
		encoded = data.toString();
		break;

	case(QVariant::Double) :
		encoded = QString::number(data.toDouble(), 'g', 16);
		if (!encoded.contains(QString::fromLatin1(".")) && !encoded.contains(QString::fromLatin1("e")))
			encoded += ".0";
		break;

	case(QVariant::String) :
		encoded = encodeString(data.toString());
		break;

	case(QVariant::ByteArray) :
		encoded = encodeByteArray(data.toByteArray());
		break;

	case(QVariant::List) :
	{
		encoded = QString::fromLatin1("[") + optionalIndentedNewLine;
		QVariantList list = data.toList();
		for (int i = 0; i < list.count(); ++i)
		{
			if (i) encoded += QString::fromLatin1(",") + optionalIndentedNewLine;
			encoded += encodeData(list.at(i), options, errorMessage, indentation, indentedLinePrefix);
			if (errorMessage && !errorMessage->isNull())
				return QString();
		}
		encoded += optionalNewLine + QString::fromLatin1("]");
	}
						 break;

	case(QVariant::StringList) :
	{
		encoded = QString::fromLatin1("[") + optionalIndentedNewLine;
		QStringList list = data.toStringList();
		for (int i = 0; i < list.count(); ++i)
		{
			if (i) encoded += QString::fromLatin1(",") + optionalIndentedNewLine;
			encoded += encodeData(list.at(i), options, errorMessage, indentation, indentedLinePrefix);
			if (errorMessage && !errorMessage->isNull())
				return QString();
		}
		encoded += optionalNewLine + QString::fromLatin1("]");
	}
							   break;

	case(QVariant::Map) :
	{
		encoded = QString::fromLatin1("{") + optionalIndentedNewLine;
		QVariantMap map = data.toMap();
		QVariantMap::iterator i;
		bool first = true;
		for (i = map.begin(); i != map.end(); ++i)
		{
			if (!first)
				encoded += QString::fromLatin1(",") + optionalIndentedNewLine;
			first = false;
			encoded += encodeString(i.key());
			encoded += options.testFlag(Compact) ? QString::fromLatin1(":") : QString::fromLatin1(" : ");
			encoded += encodeData(i.value(), options, errorMessage, indentation, indentedLinePrefix);
			if (errorMessage && !errorMessage->isNull())
				return QString();
		}
		encoded += optionalNewLine + QString::fromLatin1("}");
	}
						break;

	case(QVariant::Hash) :
	{
		encoded = QString::fromLatin1("{") + optionalIndentedNewLine;
		QVariantHash hash = data.toHash();
		QVariantHash::iterator i;
		bool first = true;
		for (i = hash.begin(); i != hash.end(); ++i)
		{
			if (!first)
				encoded += QString::fromLatin1(",") + optionalIndentedNewLine;
			first = false;
			encoded += encodeString(i.key());
			encoded += options.testFlag(Compact) ? QString::fromLatin1(":") : QString::fromLatin1(" : ");
			encoded += encodeData(i.value(), options, errorMessage, indentation, indentedLinePrefix);
			if (errorMessage && !errorMessage->isNull())
				return QString();
		}
		encoded += optionalNewLine + QString::fromLatin1("}");
	}
						 break;

	case(QVariant::Invalid) :
		encoded = QString::fromLatin1("null");
		break;

	default:
		if (!options.testFlag(EncodeUnknownTypesAsNull))
		{
			if (errorMessage)
				*errorMessage = QString::fromLatin1("Can't encode this type of data to JSON: %1")
				.arg(data.typeName());
			return QString();
		}
		encoded = QString::fromLatin1("null");
		break;
	}

	return encoded;
}
Beispiel #3
0
/*static*/
PyObject * PythonScript::VariantToPython(const QVariant & v)
{
    int i;
    QVariantList::const_iterator iList;
    QVariantList list;
#if QT_VERSION >= 0x040500
    QVariantHash::const_iterator iHash;
    QVariantHash hash;
#endif
    QVariantMap::const_iterator iMap;
    QVariantMap map;
    PyObject * pyList, * pyDict;

    if (v.isNull()) Py_RETURN_NONE;

    switch ((QMetaType::Type)v.type()) {
    case QVariant::Double:
        return Py_BuildValue("d", v.toDouble());
    case QVariant::Bool:
        if (v.toBool()) Py_RETURN_TRUE;
        else Py_RETURN_FALSE;
    case QVariant::Int:
        return Py_BuildValue("i", v.toInt());
    case QVariant::LongLong:
        return Py_BuildValue("L", v.toLongLong());
    case QVariant::UInt:
        return Py_BuildValue("I", v.toUInt());
    case QVariant::ULongLong:
        return Py_BuildValue("K", v.toULongLong());
    case QVariant::Char:
    case QVariant::String:
#ifdef Py_UNICODE_WIDE
    {
        QVector<uint> tmp = v.toString().toUcs4();
        return Py_BuildValue("u#", tmp.constData(), tmp.count());
    }
#else
    return Py_BuildValue("u", v.toString().constData());
#endif
    case QVariant::List:
    case QVariant::StringList:
        list = v.toList();

        pyList = PyList_New(list.size());
        for (i = 0, iList = list.begin(); iList != list.end(); ++iList, ++i) {
            PyList_SetItem(pyList, i, PythonScript::VariantToPython(*iList));
        }
        return pyList;
#if QT_VERSION >= 0x040500
    case QVariant::Hash:
        hash = v.toHash();

        pyDict = PyDict_New();
        for (iHash = hash.begin(); iHash != hash.end(); ++iHash) {
            PyDict_SetItemString(pyDict, qPrintable(iHash.key()), PythonScript::VariantToPython(iHash.value()));
        }
        return pyDict;
#endif
    case QVariant::Map:
        map = v.toMap();

        pyDict = PyDict_New();
        for (iMap = map.begin(); iMap != map.end(); ++iMap) {
            PyDict_SetItemString(pyDict, qPrintable(iMap.key()), PythonScript::VariantToPython(iMap.value()));
        }
        return pyDict;
    case QMetaType::QObjectStar:
        return PythonScript::QObjectToPython(v.value<QObject*>());
#if QT_VERSION < 0x050000
    case QMetaType::QWidgetStar:
        return PythonScript::QObjectToPython(qobject_cast<QObject*>(v.value<QWidget*>()));
#endif
    default:
        PyErr_Format(PyExc_TypeError, qPrintable(tr("the type %s is currently not supported")), v.typeName());
        return NULL;
    }
    Py_RETURN_NONE;
}