QString QgsExpression::formatPreviewString( const QVariant &value ) { static const int MAX_PREVIEW = 60; if ( value.canConvert<QgsGeometry>() ) { //result is a geometry QgsGeometry geom = value.value<QgsGeometry>(); if ( geom.isNull() ) return tr( "<i><empty geometry></i>" ); else return tr( "<i><geometry: %1></i>" ).arg( QgsWkbTypes::displayString( geom.constGet()->wkbType() ) ); } else if ( !value.isValid() ) { return tr( "<i>NULL</i>" ); } else if ( value.canConvert< QgsFeature >() ) { //result is a feature QgsFeature feat = value.value<QgsFeature>(); return tr( "<i><feature: %1></i>" ).arg( feat.id() ); } else if ( value.canConvert< QgsInterval >() ) { //result is a feature QgsInterval interval = value.value<QgsInterval>(); return tr( "<i><interval: %1 days></i>" ).arg( interval.days() ); } else if ( value.canConvert< QgsGradientColorRamp >() ) { return tr( "<i><gradient ramp></i>" ); } else if ( value.type() == QVariant::Date ) { QDate dt = value.toDate(); return tr( "<i><date: %1></i>" ).arg( dt.toString( QStringLiteral( "yyyy-MM-dd" ) ) ); } else if ( value.type() == QVariant::Time ) { QTime tm = value.toTime(); return tr( "<i><time: %1></i>" ).arg( tm.toString( QStringLiteral( "hh:mm:ss" ) ) ); } else if ( value.type() == QVariant::DateTime ) { QDateTime dt = value.toDateTime(); return tr( "<i><datetime: %1></i>" ).arg( dt.toString( QStringLiteral( "yyyy-MM-dd hh:mm:ss" ) ) ); } else if ( value.type() == QVariant::String ) { QString previewString = value.toString(); if ( previewString.length() > MAX_PREVIEW + 3 ) { return tr( "'%1…'" ).arg( previewString.left( MAX_PREVIEW ) ); } else { return previewString.prepend( '\'' ).append( '\'' ); } } else if ( value.type() == QVariant::Map ) { QString mapStr; const QVariantMap map = value.toMap(); for ( QVariantMap::const_iterator it = map.constBegin(); it != map.constEnd(); ++it ) { if ( !mapStr.isEmpty() ) mapStr.append( ", " ); mapStr.append( it.key() ).append( ": " ).append( formatPreviewString( it.value() ) ); if ( mapStr.length() > MAX_PREVIEW + 3 ) { mapStr = QString( tr( "%1…" ) ).arg( mapStr.left( MAX_PREVIEW ) ); break; } } return tr( "<i><map: %1></i>" ).arg( mapStr ); } else if ( value.type() == QVariant::List || value.type() == QVariant::StringList ) { QString listStr; const QVariantList list = value.toList(); for ( QVariantList::const_iterator it = list.constBegin(); it != list.constEnd(); ++it ) { if ( !listStr.isEmpty() ) listStr.append( ", " ); listStr.append( formatPreviewString( *it ) ); if ( listStr.length() > MAX_PREVIEW + 3 ) { listStr = QString( tr( "%1…" ) ).arg( listStr.left( MAX_PREVIEW ) ); break; } } return tr( "<i><array: %1></i>" ).arg( listStr ); } else { return value.toString(); } }
QString QgsExpression::formatPreviewString( const QVariant &value, const bool htmlOutput ) { static const int MAX_PREVIEW = 60; const QString startToken = htmlOutput ? QStringLiteral( "<i><" ) : QStringLiteral( "<" ); const QString endToken = htmlOutput ? QStringLiteral( "></i>" ) : QStringLiteral( ">" ); if ( value.canConvert<QgsGeometry>() ) { //result is a geometry QgsGeometry geom = value.value<QgsGeometry>(); if ( geom.isNull() ) return startToken + tr( "empty geometry" ) + endToken; else return startToken + tr( "geometry: %1" ).arg( QgsWkbTypes::displayString( geom.constGet()->wkbType() ) ) + endToken; } else if ( value.value< QgsWeakMapLayerPointer >().data() ) { return startToken + tr( "map layer" ) + endToken; } else if ( !value.isValid() ) { return htmlOutput ? tr( "<i>NULL</i>" ) : QString(); } else if ( value.canConvert< QgsFeature >() ) { //result is a feature QgsFeature feat = value.value<QgsFeature>(); return startToken + tr( "feature: %1" ).arg( feat.id() ) + endToken; } else if ( value.canConvert< QgsInterval >() ) { //result is a feature QgsInterval interval = value.value<QgsInterval>(); return startToken + tr( "interval: %1 days" ).arg( interval.days() ) + endToken; } else if ( value.canConvert< QgsGradientColorRamp >() ) { return startToken + tr( "gradient ramp" ) + endToken; } else if ( value.type() == QVariant::Date ) { QDate dt = value.toDate(); return startToken + tr( "date: %1" ).arg( dt.toString( QStringLiteral( "yyyy-MM-dd" ) ) ) + endToken; } else if ( value.type() == QVariant::Time ) { QTime tm = value.toTime(); return startToken + tr( "time: %1" ).arg( tm.toString( QStringLiteral( "hh:mm:ss" ) ) ) + endToken; } else if ( value.type() == QVariant::DateTime ) { QDateTime dt = value.toDateTime(); return startToken + tr( "datetime: %1" ).arg( dt.toString( QStringLiteral( "yyyy-MM-dd hh:mm:ss" ) ) ) + endToken; } else if ( value.type() == QVariant::String ) { const QString previewString = value.toString(); if ( previewString.length() > MAX_PREVIEW + 3 ) { return tr( "'%1…'" ).arg( previewString.left( MAX_PREVIEW ) ); } else { return '\'' + previewString + '\''; } } else if ( value.type() == QVariant::Map ) { QString mapStr = QStringLiteral( "{" ); const QVariantMap map = value.toMap(); QString separator; for ( QVariantMap::const_iterator it = map.constBegin(); it != map.constEnd(); ++it ) { mapStr.append( separator ); if ( separator.isEmpty() ) separator = QStringLiteral( "," ); mapStr.append( QStringLiteral( " '%1': %2" ).arg( it.key(), formatPreviewString( it.value(), htmlOutput ) ) ); if ( mapStr.length() > MAX_PREVIEW - 3 ) { mapStr = tr( "%1…" ).arg( mapStr.left( MAX_PREVIEW - 2 ) ); break; } } if ( !map.empty() ) mapStr += QStringLiteral( " " ); mapStr += QStringLiteral( "}" ); return mapStr; } else if ( value.type() == QVariant::List || value.type() == QVariant::StringList ) { QString listStr = QStringLiteral( "[" ); const QVariantList list = value.toList(); QString separator; for ( const QVariant &arrayValue : list ) { listStr.append( separator ); if ( separator.isEmpty() ) separator = QStringLiteral( "," ); listStr.append( " " ); listStr.append( formatPreviewString( arrayValue, htmlOutput ) ); if ( listStr.length() > MAX_PREVIEW - 3 ) { listStr = QString( tr( "%1…" ) ).arg( listStr.left( MAX_PREVIEW - 2 ) ); break; } } if ( !list.empty() ) listStr += QStringLiteral( " " ); listStr += QStringLiteral( "]" ); return listStr; } else { return value.toString(); } }