Add reverse-translation map for font styles

This commit is contained in:
Sandro Mani 2015-03-17 14:18:34 +01:00 committed by Nyall Dawson
parent feb3bee858
commit 87c05f1e4f
3 changed files with 55 additions and 4 deletions

View File

@ -282,7 +282,7 @@ QDomElement QgsFontUtils::toXmlElement( const QFont& font, QDomDocument& documen
{
QDomElement fontElem = document.createElement( elementName );
fontElem.setAttribute( "description", font.toString() );
fontElem.setAttribute( "style", font.styleName() );
fontElem.setAttribute( "style", untranslateNamedStyle( font.styleName() ) );
return fontElem;
}
@ -296,7 +296,7 @@ bool QgsFontUtils::setFromXmlElement( QFont& font, const QDomElement& element )
font.fromString( element.attribute( "description" ) );
if ( element.hasAttribute( "style" ) )
{
( void )updateFontViaStyle( font, element.attribute( "style" ) );
( void )updateFontViaStyle( font, translateNamedStyle( element.attribute( "style" ) ) );
}
return true;
@ -320,3 +320,42 @@ bool QgsFontUtils::setFromXmlChildNode( QFont& font, const QDomElement& element,
return false;
}
}
static QMap<QString, QString> createTranslatedStyleMap()
{
QMap<QString, QString> translatedStyleMap;
QStringList words = QStringList() << "Normal" << "Light" << "Bold" << "Black" << "Demi" << "Italic" << "Oblique";
foreach ( const QString& word, words )
{
translatedStyleMap.insert( QCoreApplication::translate( "QFontDatabase", qPrintable( word ) ), word );
}
return translatedStyleMap;
}
QString QgsFontUtils::translateNamedStyle( const QString& namedStyle )
{
QStringList words = namedStyle.split( " ", QString::SkipEmptyParts );
for ( int i = 0, n = words.length(); i < n; ++i )
{
words[i] = QCoreApplication::translate( "QFontDatabase", words[i].toUtf8(), 0, QCoreApplication::UnicodeUTF8 );
}
return words.join( " " );
}
QString QgsFontUtils::untranslateNamedStyle( const QString& namedStyle )
{
static QMap<QString, QString> translatedStyleMap = createTranslatedStyleMap();
QStringList words = namedStyle.split( " ", QString::SkipEmptyParts );
for ( int i = 0, n = words.length(); i < n; ++i )
{
if ( translatedStyleMap.contains( words[i] ) )
{
words[i] = translatedStyleMap.value( words[i] );
}
else
{
QgsDebugMsg( QString( "Warning: style map does not contain %1" ).arg( words[i] ) );
}
}
return words.join( " " );
}

View File

@ -113,6 +113,18 @@ class CORE_EXPORT QgsFontUtils
* @see toXmlElement
*/
static bool setFromXmlChildNode( QFont& font, const QDomElement& element, const QString& childNode );
/**Returns the localized named style of a font, if such a translation is available.
* @param namedStyle a named style, i.e. "Bold", "Italic", etc
* @returns The localized named style
*/
static QString translateNamedStyle( const QString& namedStyle );
/**Returns the english named style of a font, if possible.
* @param namedStyle a localized named style, i.e. "Fett", "Kursiv", etc
* @returns The english named style
*/
static QString untranslateNamedStyle( const QString& namedStyle );
};
#endif // QGSFONTUTILS_H

View File

@ -742,7 +742,7 @@ void QgsPalLayerSettings::readFromLayer( QgsVectorLayer* layer )
bool fontItalic = layer->customProperty( "labeling/fontItalic" ).toBool();
textFont = QFont( fontFamily, fontSize, fontWeight, fontItalic );
textFont.setPointSizeF( fontSize ); //double precision needed because of map units
textNamedStyle = layer->customProperty( "labeling/namedStyle", QVariant( "" ) ).toString();
textNamedStyle = QgsFontUtils::translateNamedStyle( layer->customProperty( "labeling/namedStyle", QVariant( "" ) ).toString() );
QgsFontUtils::updateFontViaStyle( textFont, textNamedStyle ); // must come after textFont.setPointSizeF()
textFont.setCapitalization(( QFont::Capitalization )layer->customProperty( "labeling/fontCapitals", QVariant( 0 ) ).toUInt() );
textFont.setUnderline( layer->customProperty( "labeling/fontUnderline" ).toBool() );
@ -930,7 +930,7 @@ void QgsPalLayerSettings::writeToLayer( QgsVectorLayer* layer )
layer->setCustomProperty( "labeling/fieldName", fieldName );
layer->setCustomProperty( "labeling/isExpression", isExpression );
layer->setCustomProperty( "labeling/fontFamily", textFont.family() );
layer->setCustomProperty( "labeling/namedStyle", textNamedStyle );
layer->setCustomProperty( "labeling/namedStyle", QgsFontUtils::untranslateNamedStyle( textNamedStyle ) );
layer->setCustomProperty( "labeling/fontSize", textFont.pointSizeF() );
layer->setCustomProperty( "labeling/fontSizeInMapUnits", fontSizeInMapUnits );
layer->setCustomProperty( "labeling/fontSizeMapUnitMinScale", fontSizeMapUnitScale.minScale );