forked from qt-creator/qt-creator
QmlDesigner: Detect data types for CSV models
Task-number: QDS-11081 Change-Id: I88418e31f0fd033d7d5ec3c03fea26ae1e4e2362 Reviewed-by: Ali Kianian <ali.kianian@qt.io> Reviewed-by: Mahmoud Badri <mahmoud.badri@qt.io> Reviewed-by: Qt CI Patch Build Bot <ci_patchbuild_bot@qt.io>
This commit is contained in:
@@ -428,6 +428,7 @@ void CollectionDetails::resetPropertyType(CollectionProperty &property)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
property.type = type;
|
property.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -542,12 +542,52 @@ void CollectionDetailsModel::loadCsvCollection(const QString &source,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (const QString &header : std::as_const(headers)) {
|
||||||
|
for (QJsonObject &element: elements) {
|
||||||
|
QVariant variantValue;
|
||||||
|
if (element.contains(header)) {
|
||||||
|
variantValue = variantFromString(element.value(header).toString());
|
||||||
|
element[header] = variantValue.toJsonValue();
|
||||||
|
|
||||||
|
if (variantValue.isValid())
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
SourceFormat sourceFormat = csvFileIsOk ? SourceFormat::Csv : SourceFormat::Unknown;
|
SourceFormat sourceFormat = csvFileIsOk ? SourceFormat::Csv : SourceFormat::Unknown;
|
||||||
beginResetModel();
|
beginResetModel();
|
||||||
m_currentCollection.resetDetails(headers, elements, sourceFormat);
|
m_currentCollection.resetDetails(headers, elements, sourceFormat);
|
||||||
endResetModel();
|
endResetModel();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QVariant CollectionDetailsModel::variantFromString(const QString &value)
|
||||||
|
{
|
||||||
|
constexpr QStringView typesPattern{u"(?<boolean>^(?:true|false)$)|"
|
||||||
|
u"(?<number>^(?:-?(?:0|[1-9]\\d*)?(?:\\.\\d*)?(?<=\\d|\\.)"
|
||||||
|
u"(?:e-?(?:0|[1-9]\\d*))?|0x[0-9a-f]+)$)|"
|
||||||
|
u"(?<color>^(?:#(?:(?:[0-9a-fA-F]{2}){3,4}|"
|
||||||
|
u"(?:[0-9a-fA-F]){3,4}))$)|"
|
||||||
|
u"(?<string>[A-Za-z][A-Za-z0-9_ -]*)"};
|
||||||
|
static QRegularExpression validator(typesPattern.toString());
|
||||||
|
const QString trimmedValue = value.trimmed();
|
||||||
|
QRegularExpressionMatch match = validator.match(trimmedValue);
|
||||||
|
QVariant variantValue = value;
|
||||||
|
|
||||||
|
if (value.isEmpty())
|
||||||
|
return QVariant();
|
||||||
|
if (!match.captured(u"boolean").isEmpty())
|
||||||
|
return variantValue.toBool();
|
||||||
|
if (!match.captured(u"number").isEmpty())
|
||||||
|
return variantValue.toDouble();
|
||||||
|
if (!match.captured(u"color").isEmpty())
|
||||||
|
return variantValue.value<QColor>();
|
||||||
|
if (!match.captured(u"string").isEmpty())
|
||||||
|
return variantValue.toString();
|
||||||
|
|
||||||
|
return QVariant::fromValue(value);
|
||||||
|
}
|
||||||
|
|
||||||
void CollectionDetailsModel::setCollectionName(const QString &newCollectionName)
|
void CollectionDetailsModel::setCollectionName(const QString &newCollectionName)
|
||||||
{
|
{
|
||||||
if (m_collectionName != newCollectionName) {
|
if (m_collectionName != newCollectionName) {
|
||||||
|
|||||||
@@ -83,6 +83,7 @@ private:
|
|||||||
void loadCsvCollection(const QString &source, const QString &collectionName);
|
void loadCsvCollection(const QString &source, const QString &collectionName);
|
||||||
bool saveCollectionAsJson(const QString &path, const QJsonArray &content, const QString &collectionName);
|
bool saveCollectionAsJson(const QString &path, const QJsonArray &content, const QString &collectionName);
|
||||||
bool saveCollectionAsCsv(const QString &path, const QString &content);
|
bool saveCollectionAsCsv(const QString &path, const QString &content);
|
||||||
|
QVariant variantFromString(const QString &value);
|
||||||
|
|
||||||
QHash<CollectionReference, CollectionDetails> m_openedCollections;
|
QHash<CollectionReference, CollectionDetails> m_openedCollections;
|
||||||
CollectionDetails m_currentCollection;
|
CollectionDetails m_currentCollection;
|
||||||
|
|||||||
Reference in New Issue
Block a user