mirror of
https://github.com/qgis/QGIS.git
synced 2025-07-04 00:02:42 -04:00
Compare commits
4 Commits
5261b7ff07
...
1eab67f03c
Author | SHA1 | Date | |
---|---|---|---|
|
1eab67f03c | ||
|
488856c04e | ||
|
0f0521ee67 | ||
|
7b2415ce31 |
@ -392,7 +392,9 @@ void QgsValueMapConfigDlg::loadMapFromCSV( const QString &filePath )
|
|||||||
}
|
}
|
||||||
|
|
||||||
QTextStream s( &f );
|
QTextStream s( &f );
|
||||||
s.setAutoDetectUnicode( true );
|
#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
|
||||||
|
s.setCodec( "UTF-8" );
|
||||||
|
#endif
|
||||||
|
|
||||||
const thread_local QRegularExpression re( "(?:^\"|[;,]\")(\"\"|[\\w\\W]*?)(?=\"[;,]|\"$)|(?:^(?!\")|[;,](?!\"))([^;,]*?)(?=$|[;,])|(\\r\\n|\\n)" );
|
const thread_local QRegularExpression re( "(?:^\"|[;,]\")(\"\"|[\\w\\W]*?)(?=\"[;,]|\"$)|(?:^(?!\")|[;,](?!\"))([^;,]*?)(?=$|[;,])|(\\r\\n|\\n)" );
|
||||||
QList<QPair<QString, QVariant>> map;
|
QList<QPair<QString, QVariant>> map;
|
||||||
@ -407,11 +409,11 @@ void QgsValueMapConfigDlg::loadMapFromCSV( const QString &filePath )
|
|||||||
ceils << match.capturedTexts().last().trimmed().replace( QLatin1String( "\"\"" ), QLatin1String( "\"" ) );
|
ceils << match.capturedTexts().last().trimmed().replace( QLatin1String( "\"\"" ), QLatin1String( "\"" ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ceils.size() != 2 )
|
if ( ceils.empty() )
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
QString key = ceils[0];
|
QString key = ceils[0];
|
||||||
QString val = ceils[1];
|
QString val = ceils.size() == 2 ? ceils[1] : QString();
|
||||||
if ( key == QgsApplication::nullRepresentation() )
|
if ( key == QgsApplication::nullRepresentation() )
|
||||||
key = QgsValueMapFieldFormatter::NULL_VALUE;
|
key = QgsValueMapFieldFormatter::NULL_VALUE;
|
||||||
map.append( qMakePair( key, val ) );
|
map.append( qMakePair( key, val ) );
|
||||||
|
@ -37,6 +37,8 @@ class TestQgsValueMapConfigDlg : public QObject
|
|||||||
void cleanup(); // will be called after every testfunction.
|
void cleanup(); // will be called after every testfunction.
|
||||||
|
|
||||||
void testLoadFromCSV();
|
void testLoadFromCSV();
|
||||||
|
void testLoadFromCSVSingleColumn();
|
||||||
|
void testLoadFromCSVUTF8();
|
||||||
void testTestTrimValues();
|
void testTestTrimValues();
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -88,6 +90,60 @@ void TestQgsValueMapConfigDlg::testLoadFromCSV()
|
|||||||
delete valueMapConfig;
|
delete valueMapConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TestQgsValueMapConfigDlg::testLoadFromCSVSingleColumn()
|
||||||
|
{
|
||||||
|
const QString dataDir( TEST_DATA_DIR );
|
||||||
|
QgsVectorLayer vl( QStringLiteral( "LineString?crs=epsg:3111&field=pk:int&field=name:string" ), QStringLiteral( "vl1" ), QStringLiteral( "memory" ) );
|
||||||
|
|
||||||
|
QList<QVariant> valueList;
|
||||||
|
QVariantMap value;
|
||||||
|
value.insert( QString(), QString( "1" ) );
|
||||||
|
valueList << value;
|
||||||
|
value.clear();
|
||||||
|
value.insert( QString(), QString( "2" ) );
|
||||||
|
valueList << value;
|
||||||
|
value.clear();
|
||||||
|
value.insert( QString(), QString( "three" ) );
|
||||||
|
valueList << value;
|
||||||
|
value.clear();
|
||||||
|
value.insert( QString(), QString( "4" ) );
|
||||||
|
valueList << value;
|
||||||
|
value.clear();
|
||||||
|
value.insert( QString(), QString( "5" ) );
|
||||||
|
valueList << value;
|
||||||
|
|
||||||
|
QgsValueMapConfigDlg *valueMapConfig = static_cast<QgsValueMapConfigDlg *>( QgsGui::editorWidgetRegistry()->createConfigWidget( QStringLiteral( "ValueMap" ), &vl, 1, nullptr ) );
|
||||||
|
valueMapConfig->loadMapFromCSV( dataDir + QStringLiteral( "/valuemapsample1col1.csv" ) );
|
||||||
|
QCOMPARE( valueMapConfig->config().value( QStringLiteral( "map" ) ).toList(), valueList );
|
||||||
|
|
||||||
|
valueMapConfig->loadMapFromCSV( dataDir + QStringLiteral( "/valuemapsample1col2.csv" ) );
|
||||||
|
QCOMPARE( valueMapConfig->config().value( QStringLiteral( "map" ) ).toList(), valueList );
|
||||||
|
delete valueMapConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TestQgsValueMapConfigDlg::testLoadFromCSVUTF8()
|
||||||
|
{
|
||||||
|
const QString dataDir( TEST_DATA_DIR );
|
||||||
|
QgsVectorLayer vl( QStringLiteral( "LineString?crs=epsg:3111&field=pk:int&field=name:string" ), QStringLiteral( "vl1" ), QStringLiteral( "memory" ) );
|
||||||
|
|
||||||
|
QList<QVariant> valueList;
|
||||||
|
QVariantMap value;
|
||||||
|
value.insert( QStringLiteral( "char è" ), QString( "1" ) );
|
||||||
|
valueList << value;
|
||||||
|
value.clear();
|
||||||
|
value.insert( QStringLiteral( "char Ü" ), QString( "2" ) );
|
||||||
|
valueList << value;
|
||||||
|
value.clear();
|
||||||
|
value.insert( QStringLiteral( "char Σ" ), QString( "3" ) );
|
||||||
|
valueList << value;
|
||||||
|
value.clear();
|
||||||
|
|
||||||
|
QgsValueMapConfigDlg *valueMapConfig = static_cast<QgsValueMapConfigDlg *>( QgsGui::editorWidgetRegistry()->createConfigWidget( QStringLiteral( "ValueMap" ), &vl, 1, nullptr ) );
|
||||||
|
valueMapConfig->loadMapFromCSV( dataDir + QStringLiteral( "/valuemapsampleutf8.csv" ) );
|
||||||
|
QCOMPARE( valueMapConfig->config().value( QStringLiteral( "map" ) ).toList(), valueList );
|
||||||
|
delete valueMapConfig;
|
||||||
|
}
|
||||||
|
|
||||||
void TestQgsValueMapConfigDlg::testTestTrimValues()
|
void TestQgsValueMapConfigDlg::testTestTrimValues()
|
||||||
{
|
{
|
||||||
// Create a GPKG layer in a temporary file using GDAL
|
// Create a GPKG layer in a temporary file using GDAL
|
||||||
|
5
tests/testdata/valuemapsample1col1.csv
vendored
Normal file
5
tests/testdata/valuemapsample1col1.csv
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
1,
|
||||||
|
2,
|
||||||
|
three,
|
||||||
|
4,
|
||||||
|
5,
|
|
5
tests/testdata/valuemapsample1col2.csv
vendored
Normal file
5
tests/testdata/valuemapsample1col2.csv
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
1
|
||||||
|
2
|
||||||
|
three
|
||||||
|
4
|
||||||
|
5
|
|
3
tests/testdata/valuemapsampleutf8.csv
vendored
Normal file
3
tests/testdata/valuemapsampleutf8.csv
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
1,char è
|
||||||
|
2,char Ü
|
||||||
|
3,char Σ
|
|
Loading…
x
Reference in New Issue
Block a user