Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project Translation considers descriptions of Value Map Widget #60744

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/core/project/qgsproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ void QgsProject::registerTranslatableObjects( QgsTranslationContext *translation
{
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mapLayer );

//register aliases and fields
//register aliases and widget settings
const QgsFields fields = vlayer->fields();
for ( const QgsField &field : fields )
{
Expand All @@ -679,10 +679,19 @@ void QgsProject::registerTranslatableObjects( QgsTranslationContext *translation

translationContext->registerTranslation( QStringLiteral( "project:layers:%1:fieldaliases" ).arg( vlayer->id() ), fieldName );

if ( field.editorWidgetSetup().type() == QLatin1String( "ValueRelation" ) )
if ( field.editorWidgetSetup().type() == QStringLiteral( "ValueRelation" ) )
{
translationContext->registerTranslation( QStringLiteral( "project:layers:%1:fields:%2:valuerelationvalue" ).arg( vlayer->id(), field.name() ), field.editorWidgetSetup().config().value( QStringLiteral( "Value" ) ).toString() );
}
if ( field.editorWidgetSetup().type() == QStringLiteral( "ValueMap" ) )
{
const QList<QVariant> valueList = field.editorWidgetSetup().config().value( QStringLiteral( "map" ) ).toList();

for ( int i = 0, row = 0; i < valueList.count(); i++, row++ )
{
translationContext->registerTranslation( QStringLiteral( "project:layers:%1:fields:%2:valuemapdescriptions" ).arg( vlayer->id(), field.name() ), valueList[i].toMap().constBegin().key() );
}
}
}

//register formcontainers
Expand Down
21 changes: 19 additions & 2 deletions src/core/vector/qgsvectorlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2674,17 +2674,34 @@ bool QgsVectorLayer::readSymbology( const QDomNode &layerNode, QString &errorMes
if ( categories.testFlag( Fields ) )
mFieldConfigurationFlags[fieldName] = qgsFlagKeysToValue( fieldConfigElement.attribute( QStringLiteral( "configurationFlags" ) ), Qgis::FieldConfigurationFlag::NoFlag );

// Load editor widget configuration
// load editor widget configuration
if ( categories.testFlag( Forms ) )
{
const QString widgetType = fieldWidgetElement.attribute( QStringLiteral( "type" ) );
const QDomElement cfgElem = fieldConfigElement.elementsByTagName( QStringLiteral( "config" ) ).at( 0 ).toElement();
const QDomElement optionsElem = cfgElem.childNodes().at( 0 ).toElement();
QVariantMap optionsMap = QgsXmlUtils::readVariant( optionsElem ).toMap();
if ( widgetType == QLatin1String( "ValueRelation" ) )
// translate widged configuration strings
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// translate widged configuration strings
// translate widget configuration strings

if ( widgetType == QStringLiteral( "ValueRelation" ) )
{
optionsMap[ QStringLiteral( "Value" ) ] = context.projectTranslator()->translate( QStringLiteral( "project:layers:%1:fields:%2:valuerelationvalue" ).arg( layerNode.namedItem( QStringLiteral( "id" ) ).toElement().text(), fieldName ), optionsMap[ QStringLiteral( "Value" ) ].toString() );
}
if ( widgetType == QStringLiteral( "ValueMap" ) )
{
QList<QVariant> translatedValueList;
if ( optionsMap[ QStringLiteral( "map" ) ].canConvert<QVariantList>() )
{
const QList<QVariant> valueList = optionsMap[ QStringLiteral( "map" )].toList();
for ( int i = 0, row = 0; i < valueList.count(); i++, row++ )
{
QMap<QString, QVariant> translatedValueMap;
QString translatedKey = context.projectTranslator()->translate( QStringLiteral( "project:layers:%1:fields:%2:valuemapdescriptions" ).arg( layerNode.namedItem( QStringLiteral( "id" ) ).toElement().text(), fieldName ), valueList[i].toMap().constBegin().key() );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so there's a map in the list? and only one entry in this map with key/value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. It changed from map to single-entry-map in list when the order-option was introduced. Why it's a single-entry-map and not e.g. pair I don't recall but I guess due to backwards compatibility...

translatedValueMap.insert( translatedKey, valueList[i].toMap().constBegin().value() );
translatedValueList.append( translatedValueMap );
}
optionsMap.insert( QStringLiteral( "map" ), translatedValueList );
}
}
QgsEditorWidgetSetup setup = QgsEditorWidgetSetup( widgetType, optionsMap );
mFieldWidgetSetups[fieldName] = setup;
}
Expand Down
20 changes: 18 additions & 2 deletions src/gui/vector/qgsattributesformproperties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2215,11 +2215,27 @@ void QgsAttributesFormProperties::pasteWidgetConfiguration()
const QDomElement optionsElem = configElement.childNodes().at( 0 ).toElement();
QVariantMap optionsMap = QgsXmlUtils::readVariant( optionsElem ).toMap();
QgsReadWriteContext context;
if ( widgetType == QLatin1String( "ValueRelation" ) )
// translate widged configuration strings
if ( widgetType == QStringLiteral( "ValueRelation" ) )
{
optionsMap[QStringLiteral( "Value" )] = context.projectTranslator()->translate( QStringLiteral( "project:layers:%1:fields:%2:valuerelationvalue" ).arg( mLayer->id(), fieldName ), optionsMap[QStringLiteral( "Value" )].toString() );
}

if ( widgetType == QStringLiteral( "ValueMap" ) )
{
if ( optionsMap[QStringLiteral( "map" )].canConvert<QVariantList>() )
{
QList<QVariant> translatedValueList;
const QList<QVariant> valueList = optionsMap[QStringLiteral( "map" )].toList();
for ( int i = 0, row = 0; i < valueList.count(); i++, row++ )
{
QMap<QString, QVariant> translatedValueMap;
QString translatedKey = context.projectTranslator()->translate( QStringLiteral( "project:layers:%1:fields:%2:valuemapdescriptions" ).arg( mLayer->id(), fieldName ), valueList[i].toMap().constBegin().key() );
translatedValueMap.insert( translatedKey, valueList[i].toMap().constBegin().value() );
translatedValueList.append( translatedValueMap );
}
optionsMap.insert( QStringLiteral( "map" ), translatedValueList );
}
}
config.mEditorWidgetType = widgetType;
config.mEditorWidgetConfig = optionsMap;
}
Expand Down
26 changes: 26 additions & 0 deletions tests/src/core/testqgstranslateproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,17 @@ void TestQgsTranslateProject::createTsFile()
//Sheepwalk
QVERIFY( tsFileContent.contains( "<source>Sheepwalk</source>" ) );

//WIDGETS
//ValueRelation value
QVERIFY( tsFileContent.contains( ":fields:Cabin Crew:valuerelationvalue</name>" ) );
QVERIFY( tsFileContent.contains( "<source>Name</source>" ) );

//ValueMap with descriptions
QVERIFY( tsFileContent.contains( ":fields:Name:valuemapdescriptions</name>" ) );
QVERIFY( tsFileContent.contains( "<source>Arterial road</source>" ) );
QVERIFY( tsFileContent.contains( "<source>Highway road</source>" ) );
QVERIFY( tsFileContent.contains( "<source>nothing</source>" ) );

tsFile.close();
}

Expand Down Expand Up @@ -232,6 +243,21 @@ void TestQgsTranslateProject::translateProject()
//Sheepwalk -> Schafweide
QCOMPARE( QgsProject::instance()->relationManager()->relation( QStringLiteral( "points_240_Importance_lines_a677_Value_1" ) ).name(), QStringLiteral( "Schafweide" ) );

//WIDGETS
//ValueRelation value is not anymore Name but Runwayid
QCOMPARE( points_fields.field( QStringLiteral( "Cabin Crew" ) ).editorWidgetSetup().config().value( QStringLiteral( "Value" ) ).toString(), QStringLiteral( "Runwayid" ) );

//ValueMap with descriptions
const QList<QString> expectedStringValueList = { "Hauptstrasse", "Autobahn", "nix" };
const QList<QVariant> valueList = lines_fields.field( QStringLiteral( "Name" ) ).editorWidgetSetup().config().value( QStringLiteral( "map" ) ).toList();
QList<QString> stringValueList;
for ( int i = 0, row = 0; i < valueList.count(); i++, row++ )
{
stringValueList.append( valueList[i].toMap().constBegin().key() );
}

QCOMPARE( stringValueList, expectedStringValueList );

QString deProjectFileName( TEST_DATA_DIR );
deProjectFileName = deProjectFileName + "/project_translation/points_translation_de.qgs";
const QFile deProjectFile( deProjectFileName );
Expand Down
Loading