Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add reverse-translation map for font styles
  • Loading branch information
manisandro authored and nyalldawson committed Jun 27, 2015
1 parent feb3bee commit 87c05f1
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
43 changes: 41 additions & 2 deletions src/core/qgsfontutils.cpp
Expand Up @@ -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;
}

Expand All @@ -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;
Expand All @@ -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( " " );
}
12 changes: 12 additions & 0 deletions src/core/qgsfontutils.h
Expand Up @@ -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
4 changes: 2 additions & 2 deletions src/core/qgspallabeling.cpp
Expand Up @@ -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() );
Expand Down Expand Up @@ -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 );
Expand Down

0 comments on commit 87c05f1

Please sign in to comment.