Skip to content

Commit 87c05f1

Browse files
manisandronyalldawson
authored andcommittedJun 27, 2015
Add reverse-translation map for font styles
1 parent feb3bee commit 87c05f1

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed
 

‎src/core/qgsfontutils.cpp

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ QDomElement QgsFontUtils::toXmlElement( const QFont& font, QDomDocument& documen
282282
{
283283
QDomElement fontElem = document.createElement( elementName );
284284
fontElem.setAttribute( "description", font.toString() );
285-
fontElem.setAttribute( "style", font.styleName() );
285+
fontElem.setAttribute( "style", untranslateNamedStyle( font.styleName() ) );
286286
return fontElem;
287287
}
288288

@@ -296,7 +296,7 @@ bool QgsFontUtils::setFromXmlElement( QFont& font, const QDomElement& element )
296296
font.fromString( element.attribute( "description" ) );
297297
if ( element.hasAttribute( "style" ) )
298298
{
299-
( void )updateFontViaStyle( font, element.attribute( "style" ) );
299+
( void )updateFontViaStyle( font, translateNamedStyle( element.attribute( "style" ) ) );
300300
}
301301

302302
return true;
@@ -320,3 +320,42 @@ bool QgsFontUtils::setFromXmlChildNode( QFont& font, const QDomElement& element,
320320
return false;
321321
}
322322
}
323+
324+
static QMap<QString, QString> createTranslatedStyleMap()
325+
{
326+
QMap<QString, QString> translatedStyleMap;
327+
QStringList words = QStringList() << "Normal" << "Light" << "Bold" << "Black" << "Demi" << "Italic" << "Oblique";
328+
foreach ( const QString& word, words )
329+
{
330+
translatedStyleMap.insert( QCoreApplication::translate( "QFontDatabase", qPrintable( word ) ), word );
331+
}
332+
return translatedStyleMap;
333+
}
334+
335+
QString QgsFontUtils::translateNamedStyle( const QString& namedStyle )
336+
{
337+
QStringList words = namedStyle.split( " ", QString::SkipEmptyParts );
338+
for ( int i = 0, n = words.length(); i < n; ++i )
339+
{
340+
words[i] = QCoreApplication::translate( "QFontDatabase", words[i].toUtf8(), 0, QCoreApplication::UnicodeUTF8 );
341+
}
342+
return words.join( " " );
343+
}
344+
345+
QString QgsFontUtils::untranslateNamedStyle( const QString& namedStyle )
346+
{
347+
static QMap<QString, QString> translatedStyleMap = createTranslatedStyleMap();
348+
QStringList words = namedStyle.split( " ", QString::SkipEmptyParts );
349+
for ( int i = 0, n = words.length(); i < n; ++i )
350+
{
351+
if ( translatedStyleMap.contains( words[i] ) )
352+
{
353+
words[i] = translatedStyleMap.value( words[i] );
354+
}
355+
else
356+
{
357+
QgsDebugMsg( QString( "Warning: style map does not contain %1" ).arg( words[i] ) );
358+
}
359+
}
360+
return words.join( " " );
361+
}

‎src/core/qgsfontutils.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,18 @@ class CORE_EXPORT QgsFontUtils
113113
* @see toXmlElement
114114
*/
115115
static bool setFromXmlChildNode( QFont& font, const QDomElement& element, const QString& childNode );
116+
117+
/**Returns the localized named style of a font, if such a translation is available.
118+
* @param namedStyle a named style, i.e. "Bold", "Italic", etc
119+
* @returns The localized named style
120+
*/
121+
static QString translateNamedStyle( const QString& namedStyle );
122+
123+
/**Returns the english named style of a font, if possible.
124+
* @param namedStyle a localized named style, i.e. "Fett", "Kursiv", etc
125+
* @returns The english named style
126+
*/
127+
static QString untranslateNamedStyle( const QString& namedStyle );
116128
};
117129

118130
#endif // QGSFONTUTILS_H

‎src/core/qgspallabeling.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ void QgsPalLayerSettings::readFromLayer( QgsVectorLayer* layer )
742742
bool fontItalic = layer->customProperty( "labeling/fontItalic" ).toBool();
743743
textFont = QFont( fontFamily, fontSize, fontWeight, fontItalic );
744744
textFont.setPointSizeF( fontSize ); //double precision needed because of map units
745-
textNamedStyle = layer->customProperty( "labeling/namedStyle", QVariant( "" ) ).toString();
745+
textNamedStyle = QgsFontUtils::translateNamedStyle( layer->customProperty( "labeling/namedStyle", QVariant( "" ) ).toString() );
746746
QgsFontUtils::updateFontViaStyle( textFont, textNamedStyle ); // must come after textFont.setPointSizeF()
747747
textFont.setCapitalization(( QFont::Capitalization )layer->customProperty( "labeling/fontCapitals", QVariant( 0 ) ).toUInt() );
748748
textFont.setUnderline( layer->customProperty( "labeling/fontUnderline" ).toBool() );
@@ -930,7 +930,7 @@ void QgsPalLayerSettings::writeToLayer( QgsVectorLayer* layer )
930930
layer->setCustomProperty( "labeling/fieldName", fieldName );
931931
layer->setCustomProperty( "labeling/isExpression", isExpression );
932932
layer->setCustomProperty( "labeling/fontFamily", textFont.family() );
933-
layer->setCustomProperty( "labeling/namedStyle", textNamedStyle );
933+
layer->setCustomProperty( "labeling/namedStyle", QgsFontUtils::untranslateNamedStyle( textNamedStyle ) );
934934
layer->setCustomProperty( "labeling/fontSize", textFont.pointSizeF() );
935935
layer->setCustomProperty( "labeling/fontSizeInMapUnits", fontSizeInMapUnits );
936936
layer->setCustomProperty( "labeling/fontSizeMapUnitMinScale", fontSizeMapUnitScale.minScale );

0 commit comments

Comments
 (0)