25 #include <QFontDatabase>
26 #include <QDesktopWidget>
30 d =
new QgsTextSettingsPrivate();
34 : mBufferSettings( other.mBufferSettings )
35 , mBackgroundSettings( other.mBackgroundSettings )
36 , mShadowSettings( other.mShadowSettings )
37 , mMaskSettings( other.mMaskSettings )
38 , mTextFontFamily( other.mTextFontFamily )
39 , mTextFontFound( other.mTextFontFound )
48 mBufferSettings = other.mBufferSettings;
49 mBackgroundSettings = other.mBackgroundSettings;
50 mShadowSettings = other.mShadowSettings;
51 mMaskSettings = other.mMaskSettings;
52 mTextFontFamily = other.mTextFontFamily;
53 mTextFontFound = other.mTextFontFound;
69 QFont
font = d->textFont;
71 d->fontSizeMapUnitScale );
72 font.setPixelSize( fontPixelSize );
83 if ( !d->textNamedStyle.isEmpty() )
84 return d->textNamedStyle;
87 return db.styleString( d->textFont );
93 d->textNamedStyle = style;
98 return d->fontSizeUnits;
103 d->fontSizeUnits = unit;
108 return d->fontSizeMapUnitScale;
113 d->fontSizeMapUnitScale = scale;
133 d->textColor =
color;
158 return d->multilineHeight;
163 d->multilineHeight = height;
168 return d->orientation;
178 return d->allowHtmlFormatting;
183 d->allowHtmlFormatting = allow;
188 return d->previewBackgroundColor;
193 d->previewBackgroundColor =
color;
198 QFont appFont = QApplication::font();
199 mTextFontFamily = layer->
customProperty( QStringLiteral(
"labeling/fontFamily" ), QVariant( appFont.family() ) ).toString();
200 QString fontFamily = mTextFontFamily;
204 mTextFontFound =
false;
210 fontFamily = appFont.family();
214 mTextFontFound =
true;
217 if ( !layer->
customProperty( QStringLiteral(
"labeling/fontSize" ) ).isValid() )
219 d->fontSize = appFont.pointSizeF();
223 d->fontSize = layer->
customProperty( QStringLiteral(
"labeling/fontSize" ) ).toDouble();
226 if ( layer->
customProperty( QStringLiteral(
"labeling/fontSizeUnit" ) ).toString().isEmpty() )
228 d->fontSizeUnits = layer->
customProperty( QStringLiteral(
"labeling/fontSizeInMapUnits" ), QVariant(
false ) ).toBool() ?
238 if ( layer->
customProperty( QStringLiteral(
"labeling/fontSizeMapUnitScale" ) ).toString().isEmpty() )
241 double oldMin = layer->
customProperty( QStringLiteral(
"labeling/fontSizeMapUnitMinScale" ), 0.0 ).toDouble();
242 d->fontSizeMapUnitScale.minScale = oldMin != 0 ? 1.0 / oldMin : 0;
243 double oldMax = layer->
customProperty( QStringLiteral(
"labeling/fontSizeMapUnitMaxScale" ), 0.0 ).toDouble();
244 d->fontSizeMapUnitScale.maxScale = oldMax != 0 ? 1.0 / oldMax : 0;
250 int fontWeight = layer->
customProperty( QStringLiteral(
"labeling/fontWeight" ) ).toInt();
251 bool fontItalic = layer->
customProperty( QStringLiteral(
"labeling/fontItalic" ) ).toBool();
252 d->textFont = QFont( fontFamily, d->fontSize, fontWeight, fontItalic );
255 d->textFont.setCapitalization(
static_cast< QFont::Capitalization
>( layer->
customProperty( QStringLiteral(
"labeling/fontCapitals" ), QVariant( 0 ) ).toUInt() ) );
256 d->textFont.setUnderline( layer->
customProperty( QStringLiteral(
"labeling/fontUnderline" ) ).toBool() );
257 d->textFont.setStrikeOut( layer->
customProperty( QStringLiteral(
"labeling/fontStrikeout" ) ).toBool() );
258 d->textFont.setLetterSpacing( QFont::AbsoluteSpacing, layer->
customProperty( QStringLiteral(
"labeling/fontLetterSpacing" ), QVariant( 0.0 ) ).toDouble() );
259 d->textFont.setWordSpacing( layer->
customProperty( QStringLiteral(
"labeling/fontWordSpacing" ), QVariant( 0.0 ) ).toDouble() );
261 if ( layer->
customProperty( QStringLiteral(
"labeling/textOpacity" ) ).toString().isEmpty() )
263 d->opacity = ( 1 - layer->
customProperty( QStringLiteral(
"labeling/textTransp" ) ).toInt() / 100.0 );
267 d->opacity = ( layer->
customProperty( QStringLiteral(
"labeling/textOpacity" ) ).toDouble() );
271 d->multilineHeight = layer->
customProperty( QStringLiteral(
"labeling/multilineHeight" ), QVariant( 1.0 ) ).toDouble();
281 QDomElement textStyleElem;
282 if ( elem.nodeName() == QStringLiteral(
"text-style" ) )
283 textStyleElem = elem;
285 textStyleElem = elem.firstChildElement( QStringLiteral(
"text-style" ) );
286 QFont appFont = QApplication::font();
287 mTextFontFamily = textStyleElem.attribute( QStringLiteral(
"fontFamily" ), appFont.family() );
288 QString fontFamily = mTextFontFamily;
292 mTextFontFound =
false;
298 fontFamily = appFont.family();
302 mTextFontFound =
true;
305 if ( textStyleElem.hasAttribute( QStringLiteral(
"fontSize" ) ) )
307 d->fontSize = textStyleElem.attribute( QStringLiteral(
"fontSize" ) ).toDouble();
311 d->fontSize = appFont.pointSizeF();
314 if ( !textStyleElem.hasAttribute( QStringLiteral(
"fontSizeUnit" ) ) )
316 d->fontSizeUnits = textStyleElem.attribute( QStringLiteral(
"fontSizeInMapUnits" ) ).toUInt() == 0 ?
QgsUnitTypes::RenderPoints
324 if ( !textStyleElem.hasAttribute( QStringLiteral(
"fontSizeMapUnitScale" ) ) )
327 double oldMin = textStyleElem.attribute( QStringLiteral(
"fontSizeMapUnitMinScale" ), QStringLiteral(
"0" ) ).toDouble();
328 d->fontSizeMapUnitScale.minScale = oldMin != 0 ? 1.0 / oldMin : 0;
329 double oldMax = textStyleElem.attribute( QStringLiteral(
"fontSizeMapUnitMaxScale" ), QStringLiteral(
"0" ) ).toDouble();
330 d->fontSizeMapUnitScale.maxScale = oldMax != 0 ? 1.0 / oldMax : 0;
336 int fontWeight = textStyleElem.attribute( QStringLiteral(
"fontWeight" ) ).toInt();
337 bool fontItalic = textStyleElem.attribute( QStringLiteral(
"fontItalic" ) ).toInt();
338 d->textFont = QFont( fontFamily, d->fontSize, fontWeight, fontItalic );
339 d->textFont.setPointSizeF( d->fontSize );
342 d->textFont.setCapitalization(
static_cast< QFont::Capitalization
>( textStyleElem.attribute( QStringLiteral(
"fontCapitals" ), QStringLiteral(
"0" ) ).toUInt() ) );
343 d->textFont.setUnderline( textStyleElem.attribute( QStringLiteral(
"fontUnderline" ) ).toInt() );
344 d->textFont.setStrikeOut( textStyleElem.attribute( QStringLiteral(
"fontStrikeout" ) ).toInt() );
345 d->textFont.setKerning( textStyleElem.attribute( QStringLiteral(
"fontKerning" ), QStringLiteral(
"1" ) ).toInt() );
346 d->textFont.setLetterSpacing( QFont::AbsoluteSpacing, textStyleElem.attribute( QStringLiteral(
"fontLetterSpacing" ), QStringLiteral(
"0" ) ).toDouble() );
347 d->textFont.setWordSpacing( textStyleElem.attribute( QStringLiteral(
"fontWordSpacing" ), QStringLiteral(
"0" ) ).toDouble() );
349 if ( !textStyleElem.hasAttribute( QStringLiteral(
"textOpacity" ) ) )
351 d->opacity = ( 1 - textStyleElem.attribute( QStringLiteral(
"textTransp" ) ).toInt() / 100.0 );
355 d->opacity = ( textStyleElem.attribute( QStringLiteral(
"textOpacity" ) ).toDouble() );
363 if ( !textStyleElem.hasAttribute( QStringLiteral(
"multilineHeight" ) ) )
365 QDomElement textFormatElem = elem.firstChildElement( QStringLiteral(
"text-format" ) );
366 d->multilineHeight = textFormatElem.attribute( QStringLiteral(
"multilineHeight" ), QStringLiteral(
"1" ) ).toDouble();
370 d->multilineHeight = textStyleElem.attribute( QStringLiteral(
"multilineHeight" ), QStringLiteral(
"1" ) ).toDouble();
373 d->allowHtmlFormatting = textStyleElem.attribute( QStringLiteral(
"allowHtml" ), QStringLiteral(
"0" ) ).toInt();
375 if ( textStyleElem.firstChildElement( QStringLiteral(
"text-buffer" ) ).isNull() )
377 mBufferSettings.
readXml( elem );
381 mBufferSettings.
readXml( textStyleElem );
383 if ( textStyleElem.firstChildElement( QStringLiteral(
"text-mask" ) ).isNull() )
389 mMaskSettings.
readXml( textStyleElem );
391 if ( textStyleElem.firstChildElement( QStringLiteral(
"shadow" ) ).isNull() )
393 mShadowSettings.
readXml( elem );
397 mShadowSettings.
readXml( textStyleElem );
399 if ( textStyleElem.firstChildElement( QStringLiteral(
"background" ) ).isNull() )
401 mBackgroundSettings.
readXml( elem, context );
405 mBackgroundSettings.
readXml( textStyleElem, context );
408 QDomElement ddElem = textStyleElem.firstChildElement( QStringLiteral(
"dd_properties" ) );
409 if ( ddElem.isNull() )
411 ddElem = elem.firstChildElement( QStringLiteral(
"dd_properties" ) );
413 if ( !ddElem.isNull() )
419 d->mDataDefinedProperties.clear();
426 QDomElement textStyleElem = doc.createElement( QStringLiteral(
"text-style" ) );
427 textStyleElem.setAttribute( QStringLiteral(
"fontFamily" ), d->textFont.family() );
429 textStyleElem.setAttribute( QStringLiteral(
"fontSize" ), d->fontSize );
432 textStyleElem.setAttribute( QStringLiteral(
"fontWeight" ), d->textFont.weight() );
433 textStyleElem.setAttribute( QStringLiteral(
"fontItalic" ), d->textFont.italic() );
434 textStyleElem.setAttribute( QStringLiteral(
"fontStrikeout" ), d->textFont.strikeOut() );
435 textStyleElem.setAttribute( QStringLiteral(
"fontUnderline" ), d->textFont.underline() );
438 textStyleElem.setAttribute( QStringLiteral(
"fontCapitals" ),
static_cast< unsigned int >( d->textFont.capitalization() ) );
439 textStyleElem.setAttribute( QStringLiteral(
"fontLetterSpacing" ), d->textFont.letterSpacing() );
440 textStyleElem.setAttribute( QStringLiteral(
"fontWordSpacing" ), d->textFont.wordSpacing() );
441 textStyleElem.setAttribute( QStringLiteral(
"fontKerning" ), d->textFont.kerning() );
442 textStyleElem.setAttribute( QStringLiteral(
"textOpacity" ), d->opacity );
445 textStyleElem.setAttribute( QStringLiteral(
"multilineHeight" ), d->multilineHeight );
446 textStyleElem.setAttribute( QStringLiteral(
"allowHtml" ), d->allowHtmlFormatting ? QStringLiteral(
"1" ) : QStringLiteral(
"0" ) );
448 QDomElement ddElem = doc.createElement( QStringLiteral(
"dd_properties" ) );
451 textStyleElem.appendChild( mBufferSettings.
writeXml( doc ) );
452 textStyleElem.appendChild( mMaskSettings.
writeXml( doc ) );
453 textStyleElem.appendChild( mBackgroundSettings.
writeXml( doc, context ) );
454 textStyleElem.appendChild( mShadowSettings.
writeXml( doc ) );
455 textStyleElem.appendChild( ddElem );
457 return textStyleElem;
463 QMimeData *mimeData =
new QMimeData;
464 mimeData->setColorData( QVariant(
color() ) );
467 QDomDocument textDoc;
468 QDomElement textElem =
writeXml( textDoc, rwContext );
469 textDoc.appendChild( textElem );
470 mimeData->setText( textDoc.toString() );
479 if (
font.pointSizeF() > 0 )
484 else if (
font.pixelSize() > 0 )
499 f.setPointSizeF(
size() );
503 f.setPointSizeF(
size() * 2.83464567 );
507 f.setPointSizeF(
size() * 72 );
511 f.setPixelSize(
static_cast< int >( std::round(
size() ) ) );
532 QString text = data->text();
533 if ( !text.isEmpty() )
539 if ( doc.setContent( text ) )
541 elem = doc.documentElement();
543 format.
readXml( elem, rwContext );
554 if ( d->blendMode != QPainter::CompositionMode_SourceOver )
557 if ( mBufferSettings.
enabled() && mBufferSettings.
blendMode() != QPainter::CompositionMode_SourceOver )
560 if ( mBackgroundSettings.
enabled() && mBackgroundSettings.
blendMode() != QPainter::CompositionMode_SourceOver )
563 if ( mShadowSettings.
enabled() && mShadowSettings.
blendMode() != QPainter::CompositionMode_SourceOver )
571 return d->mDataDefinedProperties;
576 return d->mDataDefinedProperties;
581 QSet< QString > fields = d->mDataDefinedProperties.referencedFields( context.
expressionContext(),
true );
591 d->mDataDefinedProperties = collection;
596 if ( !d->mDataDefinedProperties.hasActiveProperties() )
599 QString ddFontFamily;
602 if ( exprVal.isValid() )
604 QString family = exprVal.toString().trimmed();
605 if ( d->textFont.family() != family )
611 ddFontFamily = family;
620 if ( exprVal.isValid() )
622 QString fontstyle = exprVal.toString().trimmed();
623 ddFontStyle = fontstyle;
633 bool ddItalic =
false;
643 QFontDatabase fontDb;
644 QFont appFont = QApplication::font();
645 bool newFontBuilt =
false;
646 if ( ddBold || ddItalic )
649 newFont = QFont( !ddFontFamily.isEmpty() ? ddFontFamily : d->textFont.family() );
651 newFont.setBold( ddBold );
652 newFont.setItalic( ddItalic );
654 else if ( !ddFontStyle.isEmpty()
655 && ddFontStyle.compare( QLatin1String(
"Ignore" ), Qt::CaseInsensitive ) != 0 )
657 if ( !ddFontFamily.isEmpty() )
660 QFont styledfont = fontDb.font( ddFontFamily, ddFontStyle, appFont.pointSize() );
661 if ( appFont != styledfont )
663 newFont = styledfont;
671 else if ( !ddFontFamily.isEmpty() )
673 if ( ddFontStyle.compare( QLatin1String(
"Ignore" ), Qt::CaseInsensitive ) != 0 )
676 QFont styledfont = fontDb.font( ddFontFamily, d->textNamedStyle, appFont.pointSize() );
677 if ( appFont != styledfont )
679 newFont = styledfont;
685 newFont = QFont( ddFontFamily );
693 newFont.setUnderline( d->textFont.underline() );
694 newFont.setStrikeOut( d->textFont.strikeOut() );
695 newFont.setWordSpacing( d->textFont.wordSpacing() );
696 newFont.setLetterSpacing( QFont::AbsoluteSpacing, d->textFont.letterSpacing() );
697 d->textFont = newFont;
725 if ( exprVal.isValid() )
727 QString units = exprVal.toString();
728 if ( !units.isEmpty() )
733 d->fontSizeUnits = res;
765 QString blendstr = exprVal.toString().trimmed();
766 if ( !blendstr.isEmpty() )
779 QPixmap pixmap(
size );
780 pixmap.fill( Qt::transparent );
782 painter.begin( &pixmap );
784 painter.setRenderHint( QPainter::Antialiasing );
786 QRect rect( 0, 0,
size.width(),
size.height() );
789 painter.setPen( Qt::NoPen );
791 if ( ( background1.lightnessF() < 0.7 ) )
793 background1 = background1.darker( 125 );
797 background1 = background1.lighter( 125 );
800 QLinearGradient linearGrad( QPointF( 0, 0 ), QPointF( 0, rect.height() ) );
801 linearGrad.setColorAt( 0, background1 );
802 linearGrad.setColorAt( 1, background2 );
803 painter.setBrush( QBrush( linearGrad ) );
804 if (
size.width() > 30 )
806 painter.drawRoundedRect( rect, 6, 6 );
811 painter.drawRect( rect );
813 painter.setBrush( Qt::NoBrush );
814 painter.setPen( Qt::NoPen );
822 context.
setScaleFactor( QgsApplication::desktop()->logicalDpiX() / 25.4 );
839 const QStringList text = QStringList() << ( previewText.isEmpty() ? QObject::tr(
"Aa" ) : previewText );
841 QRectF textRect = rect;
842 textRect.setLeft( xtrans + padding );
843 textRect.setWidth( rect.width() - xtrans - 2 * padding );
845 if ( textRect.width() > 2000 )
846 textRect.setWidth( 2000 - 2 * padding );
848 const double bottom = textRect.height() / 2 + textHeight / 2;
849 textRect.setTop( bottom - textHeight );
850 textRect.setBottom( bottom );
855 painter.setBrush( Qt::NoBrush );
857 if (
size.width() > 30 )
859 painter.drawRoundedRect( rect, 6, 6 );
864 painter.drawRect( rect );