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;
64 if ( d->isValid != other.
isValid()
65 || d->textFont != other.
font()
67 || d->fontSizeUnits != other.
sizeUnit()
69 || d->fontSize != other.
size()
70 || d->textColor != other.
color()
71 || d->opacity != other.
opacity()
78 || mBufferSettings != other.mBufferSettings
79 || mBackgroundSettings != other.mBackgroundSettings
80 || mShadowSettings != other.mShadowSettings
81 || mMaskSettings != other.mMaskSettings
90 return !( *
this == other );
106 return mBufferSettings;
112 mBufferSettings = bufferSettings;
118 return mBackgroundSettings;
124 mBackgroundSettings = backgroundSettings;
130 return mShadowSettings;
136 mShadowSettings = shadowSettings;
142 return mMaskSettings;
148 mMaskSettings = maskSettings;
158 QFont
font = d->textFont;
159 if ( scaleFactor == 1 )
162 d->fontSizeMapUnitScale );
163 font.setPixelSize( fontPixelSize );
167 double fontPixelSize = context.
convertToPainterUnits( d->fontSize, d->fontSizeUnits, d->fontSizeMapUnitScale );
168 font.setPixelSize( std::round( scaleFactor * fontPixelSize + 0.5 ) );
171 font.setLetterSpacing( QFont::AbsoluteSpacing, context.
convertToPainterUnits( d->textFont.letterSpacing(), d->fontSizeUnits, d->fontSizeMapUnitScale ) * scaleFactor );
172 font.setWordSpacing( context.
convertToPainterUnits( d->textFont.wordSpacing(), d->fontSizeUnits, d->fontSizeMapUnitScale ) * scaleFactor * scaleFactor );
185 if ( !d->textNamedStyle.isEmpty() )
186 return d->textNamedStyle;
189 return db.styleString( d->textFont );
196 d->textNamedStyle = style;
201 return d->fontSizeUnits;
207 d->fontSizeUnits = unit;
212 return d->fontSizeMapUnitScale;
218 d->fontSizeMapUnitScale = scale;
240 d->textColor =
color;
267 return d->multilineHeight;
273 d->multilineHeight = height;
278 return d->orientation;
297 d->textFont.setCapitalization( QFont::MixedCase );
302 return d->allowHtmlFormatting;
308 d->allowHtmlFormatting = allow;
313 return d->previewBackgroundColor;
319 d->previewBackgroundColor =
color;
325 QFont appFont = QApplication::font();
326 mTextFontFamily = layer->
customProperty( QStringLiteral(
"labeling/fontFamily" ), QVariant( appFont.family() ) ).toString();
327 QString fontFamily = mTextFontFamily;
331 mTextFontFound =
false;
337 fontFamily = appFont.family();
341 mTextFontFound =
true;
346 d->fontSize = appFont.pointSizeF();
350 d->fontSize = layer->
customProperty( QStringLiteral(
"labeling/fontSize" ) ).toDouble();
353 if ( layer->
customProperty( QStringLiteral(
"labeling/fontSizeUnit" ) ).toString().isEmpty() )
355 d->fontSizeUnits = layer->
customProperty( QStringLiteral(
"labeling/fontSizeInMapUnits" ), QVariant(
false ) ).toBool() ?
365 if ( layer->
customProperty( QStringLiteral(
"labeling/fontSizeMapUnitScale" ) ).toString().isEmpty() )
368 double oldMin = layer->
customProperty( QStringLiteral(
"labeling/fontSizeMapUnitMinScale" ), 0.0 ).toDouble();
369 d->fontSizeMapUnitScale.minScale = oldMin != 0 ? 1.0 / oldMin : 0;
370 double oldMax = layer->
customProperty( QStringLiteral(
"labeling/fontSizeMapUnitMaxScale" ), 0.0 ).toDouble();
371 d->fontSizeMapUnitScale.maxScale = oldMax != 0 ? 1.0 / oldMax : 0;
377 int fontWeight = layer->
customProperty( QStringLiteral(
"labeling/fontWeight" ) ).toInt();
378 bool fontItalic = layer->
customProperty( QStringLiteral(
"labeling/fontItalic" ) ).toBool();
379 d->textFont = QFont( fontFamily, d->fontSize, fontWeight, fontItalic );
383 d->textFont.setUnderline( layer->
customProperty( QStringLiteral(
"labeling/fontUnderline" ) ).toBool() );
384 d->textFont.setStrikeOut( layer->
customProperty( QStringLiteral(
"labeling/fontStrikeout" ) ).toBool() );
385 d->textFont.setLetterSpacing( QFont::AbsoluteSpacing, layer->
customProperty( QStringLiteral(
"labeling/fontLetterSpacing" ), QVariant( 0.0 ) ).toDouble() );
386 d->textFont.setWordSpacing( layer->
customProperty( QStringLiteral(
"labeling/fontWordSpacing" ), QVariant( 0.0 ) ).toDouble() );
388 if ( layer->
customProperty( QStringLiteral(
"labeling/textOpacity" ) ).toString().isEmpty() )
390 d->opacity = ( 1 - layer->
customProperty( QStringLiteral(
"labeling/textTransp" ) ).toInt() / 100.0 );
394 d->opacity = ( layer->
customProperty( QStringLiteral(
"labeling/textOpacity" ) ).toDouble() );
398 d->multilineHeight = layer->
customProperty( QStringLiteral(
"labeling/multilineHeight" ), QVariant( 1.0 ) ).toDouble();
409 QDomElement textStyleElem;
410 if ( elem.nodeName() == QLatin1String(
"text-style" ) )
411 textStyleElem = elem;
413 textStyleElem = elem.firstChildElement( QStringLiteral(
"text-style" ) );
414 QFont appFont = QApplication::font();
415 mTextFontFamily = textStyleElem.attribute( QStringLiteral(
"fontFamily" ), appFont.family() );
416 QString fontFamily = mTextFontFamily;
420 mTextFontFound =
false;
426 fontFamily = appFont.family();
430 mTextFontFound =
true;
433 if ( textStyleElem.hasAttribute( QStringLiteral(
"fontSize" ) ) )
435 d->fontSize = textStyleElem.attribute( QStringLiteral(
"fontSize" ) ).toDouble();
439 d->fontSize = appFont.pointSizeF();
442 if ( !textStyleElem.hasAttribute( QStringLiteral(
"fontSizeUnit" ) ) )
444 d->fontSizeUnits = textStyleElem.attribute( QStringLiteral(
"fontSizeInMapUnits" ) ).toUInt() == 0 ?
QgsUnitTypes::RenderPoints
452 if ( !textStyleElem.hasAttribute( QStringLiteral(
"fontSizeMapUnitScale" ) ) )
455 double oldMin = textStyleElem.attribute( QStringLiteral(
"fontSizeMapUnitMinScale" ), QStringLiteral(
"0" ) ).toDouble();
456 d->fontSizeMapUnitScale.minScale = oldMin != 0 ? 1.0 / oldMin : 0;
457 double oldMax = textStyleElem.attribute( QStringLiteral(
"fontSizeMapUnitMaxScale" ), QStringLiteral(
"0" ) ).toDouble();
458 d->fontSizeMapUnitScale.maxScale = oldMax != 0 ? 1.0 / oldMax : 0;
464 int fontWeight = textStyleElem.attribute( QStringLiteral(
"fontWeight" ) ).toInt();
465 bool fontItalic = textStyleElem.attribute( QStringLiteral(
"fontItalic" ) ).toInt();
466 d->textFont = QFont( fontFamily, d->fontSize, fontWeight, fontItalic );
467 d->textFont.setPointSizeF( d->fontSize );
470 d->textFont.setUnderline( textStyleElem.attribute( QStringLiteral(
"fontUnderline" ) ).toInt() );
471 d->textFont.setStrikeOut( textStyleElem.attribute( QStringLiteral(
"fontStrikeout" ) ).toInt() );
472 d->textFont.setKerning( textStyleElem.attribute( QStringLiteral(
"fontKerning" ), QStringLiteral(
"1" ) ).toInt() );
473 d->textFont.setLetterSpacing( QFont::AbsoluteSpacing, textStyleElem.attribute( QStringLiteral(
"fontLetterSpacing" ), QStringLiteral(
"0" ) ).toDouble() );
474 d->textFont.setWordSpacing( textStyleElem.attribute( QStringLiteral(
"fontWordSpacing" ), QStringLiteral(
"0" ) ).toDouble() );
476 if ( !textStyleElem.hasAttribute( QStringLiteral(
"textOpacity" ) ) )
478 d->opacity = ( 1 - textStyleElem.attribute( QStringLiteral(
"textTransp" ) ).toInt() / 100.0 );
482 d->opacity = ( textStyleElem.attribute( QStringLiteral(
"textOpacity" ) ).toDouble() );
490 if ( !textStyleElem.hasAttribute( QStringLiteral(
"multilineHeight" ) ) )
492 QDomElement textFormatElem = elem.firstChildElement( QStringLiteral(
"text-format" ) );
493 d->multilineHeight = textFormatElem.attribute( QStringLiteral(
"multilineHeight" ), QStringLiteral(
"1" ) ).toDouble();
497 d->multilineHeight = textStyleElem.attribute( QStringLiteral(
"multilineHeight" ), QStringLiteral(
"1" ) ).toDouble();
500 if ( textStyleElem.hasAttribute( QStringLiteral(
"capitalization" ) ) )
503 d->capitalization =
static_cast< QgsStringUtils::Capitalization >( textStyleElem.attribute( QStringLiteral(
"fontCapitals" ), QStringLiteral(
"0" ) ).toUInt() );
505 d->allowHtmlFormatting = textStyleElem.attribute( QStringLiteral(
"allowHtml" ), QStringLiteral(
"0" ) ).toInt();
507 if ( textStyleElem.firstChildElement( QStringLiteral(
"text-buffer" ) ).isNull() )
509 mBufferSettings.
readXml( elem );
513 mBufferSettings.
readXml( textStyleElem );
515 if ( textStyleElem.firstChildElement( QStringLiteral(
"text-mask" ) ).isNull() )
521 mMaskSettings.
readXml( textStyleElem );
523 if ( textStyleElem.firstChildElement( QStringLiteral(
"shadow" ) ).isNull() )
525 mShadowSettings.
readXml( elem );
529 mShadowSettings.
readXml( textStyleElem );
531 if ( textStyleElem.firstChildElement( QStringLiteral(
"background" ) ).isNull() )
533 mBackgroundSettings.
readXml( elem, context );
537 mBackgroundSettings.
readXml( textStyleElem, context );
540 QDomElement ddElem = textStyleElem.firstChildElement( QStringLiteral(
"dd_properties" ) );
541 if ( ddElem.isNull() )
543 ddElem = elem.firstChildElement( QStringLiteral(
"dd_properties" ) );
545 if ( !ddElem.isNull() )
551 d->mDataDefinedProperties.clear();
558 QDomElement textStyleElem = doc.createElement( QStringLiteral(
"text-style" ) );
559 textStyleElem.setAttribute( QStringLiteral(
"fontFamily" ), d->textFont.family() );
561 textStyleElem.setAttribute( QStringLiteral(
"fontSize" ), d->fontSize );
564 textStyleElem.setAttribute( QStringLiteral(
"fontWeight" ), d->textFont.weight() );
565 textStyleElem.setAttribute( QStringLiteral(
"fontItalic" ), d->textFont.italic() );
566 textStyleElem.setAttribute( QStringLiteral(
"fontStrikeout" ), d->textFont.strikeOut() );
567 textStyleElem.setAttribute( QStringLiteral(
"fontUnderline" ), d->textFont.underline() );
570 textStyleElem.setAttribute( QStringLiteral(
"fontLetterSpacing" ), d->textFont.letterSpacing() );
571 textStyleElem.setAttribute( QStringLiteral(
"fontWordSpacing" ), d->textFont.wordSpacing() );
572 textStyleElem.setAttribute( QStringLiteral(
"fontKerning" ), d->textFont.kerning() );
573 textStyleElem.setAttribute( QStringLiteral(
"textOpacity" ), d->opacity );
576 textStyleElem.setAttribute( QStringLiteral(
"multilineHeight" ), d->multilineHeight );
577 textStyleElem.setAttribute( QStringLiteral(
"allowHtml" ), d->allowHtmlFormatting ? QStringLiteral(
"1" ) : QStringLiteral(
"0" ) );
578 textStyleElem.setAttribute( QStringLiteral(
"capitalization" ), QString::number(
static_cast< int >( d->capitalization ) ) );
580 QDomElement ddElem = doc.createElement( QStringLiteral(
"dd_properties" ) );
583 textStyleElem.appendChild( mBufferSettings.
writeXml( doc ) );
584 textStyleElem.appendChild( mMaskSettings.
writeXml( doc ) );
585 textStyleElem.appendChild( mBackgroundSettings.
writeXml( doc, context ) );
586 textStyleElem.appendChild( mShadowSettings.
writeXml( doc ) );
587 textStyleElem.appendChild( ddElem );
589 return textStyleElem;
595 QMimeData *mimeData =
new QMimeData;
596 mimeData->setColorData( QVariant(
color() ) );
599 QDomDocument textDoc;
600 QDomElement textElem =
writeXml( textDoc, rwContext );
601 textDoc.appendChild( textElem );
602 mimeData->setText( textDoc.toString() );
611 if (
font.pointSizeF() > 0 )
616 else if (
font.pixelSize() > 0 )
631 f.setPointSizeF(
size() );
635 f.setPointSizeF(
size() * 2.83464567 );
639 f.setPointSizeF(
size() * 72 );
643 f.setPixelSize(
static_cast< int >( std::round(
size() ) ) );
664 QString text = data->text();
665 if ( !text.isEmpty() )
671 if ( doc.setContent( text ) )
673 elem = doc.documentElement();
675 format.
readXml( elem, rwContext );
686 if ( d->blendMode != QPainter::CompositionMode_SourceOver )
689 if ( mBufferSettings.
enabled() && mBufferSettings.
blendMode() != QPainter::CompositionMode_SourceOver )
692 if ( mBackgroundSettings.
enabled() && mBackgroundSettings.
blendMode() != QPainter::CompositionMode_SourceOver )
695 if ( mShadowSettings.
enabled() && mShadowSettings.
blendMode() != QPainter::CompositionMode_SourceOver )
704 return d->mDataDefinedProperties;
709 return d->mDataDefinedProperties;
714 QSet< QString > fields = d->mDataDefinedProperties.referencedFields( context.
expressionContext(),
true );
725 d->mDataDefinedProperties = collection;
731 if ( !d->mDataDefinedProperties.hasActiveProperties() )
734 QString ddFontFamily;
737 if ( exprVal.isValid() )
739 QString family = exprVal.toString().trimmed();
740 if ( d->textFont.family() != family )
746 ddFontFamily = family;
755 if ( exprVal.isValid() )
757 QString fontstyle = exprVal.toString().trimmed();
758 ddFontStyle = fontstyle;
768 bool ddItalic =
false;
778 QFontDatabase fontDb;
779 QFont appFont = QApplication::font();
780 bool newFontBuilt =
false;
781 if ( ddBold || ddItalic )
784 newFont = QFont( !ddFontFamily.isEmpty() ? ddFontFamily : d->textFont.family() );
786 newFont.setBold( ddBold );
787 newFont.setItalic( ddItalic );
789 else if ( !ddFontStyle.isEmpty()
790 && ddFontStyle.compare( QLatin1String(
"Ignore" ), Qt::CaseInsensitive ) != 0 )
792 if ( !ddFontFamily.isEmpty() )
795 QFont styledfont = fontDb.font( ddFontFamily, ddFontStyle, appFont.pointSize() );
796 if ( appFont != styledfont )
798 newFont = styledfont;
806 else if ( !ddFontFamily.isEmpty() )
808 if ( ddFontStyle.compare( QLatin1String(
"Ignore" ), Qt::CaseInsensitive ) != 0 )
811 QFont styledfont = fontDb.font( ddFontFamily, d->textNamedStyle, appFont.pointSize() );
812 if ( appFont != styledfont )
814 newFont = styledfont;
820 newFont = QFont( ddFontFamily );
828 newFont.setUnderline( d->textFont.underline() );
829 newFont.setStrikeOut( d->textFont.strikeOut() );
830 newFont.setWordSpacing( d->textFont.wordSpacing() );
831 newFont.setLetterSpacing( QFont::AbsoluteSpacing, d->textFont.letterSpacing() );
832 d->textFont = newFont;
860 if ( exprVal.isValid() )
862 QString units = exprVal.toString();
863 if ( !units.isEmpty() )
868 d->fontSizeUnits = res;
900 QString blendstr = exprVal.toString().trimmed();
901 if ( !blendstr.isEmpty() )
914 QPixmap pixmap(
size );
915 pixmap.fill( Qt::transparent );
917 painter.begin( &pixmap );
919 painter.setRenderHint( QPainter::Antialiasing );
921 QRect rect( 0, 0,
size.width(),
size.height() );
924 painter.setPen( Qt::NoPen );
926 if ( ( background1.lightnessF() < 0.7 ) )
928 background1 = background1.darker( 125 );
932 background1 = background1.lighter( 125 );
935 QLinearGradient linearGrad( QPointF( 0, 0 ), QPointF( 0, rect.height() ) );
936 linearGrad.setColorAt( 0, background1 );
937 linearGrad.setColorAt( 1, background2 );
938 painter.setBrush( QBrush( linearGrad ) );
939 if (
size.width() > 30 )
941 painter.drawRoundedRect( rect, 6, 6 );
946 painter.drawRect( rect );
948 painter.setBrush( Qt::NoBrush );
949 painter.setPen( Qt::NoPen );
957 context.
setScaleFactor( QgsApplication::desktop()->logicalDpiX() / 25.4 );
976 const QStringList text = QStringList() << ( previewText.isEmpty() ? QObject::tr(
"Aa" ) : previewText );
978 QRectF textRect = rect;
979 textRect.setLeft( xtrans + padding );
980 textRect.setWidth( rect.width() - xtrans - 2 * padding );
982 if ( textRect.width() > 2000 )
983 textRect.setWidth( 2000 - 2 * padding );
985 const double bottom = textRect.height() / 2 + textHeight / 2;
986 textRect.setTop( bottom - textHeight );
987 textRect.setBottom( bottom );
992 painter.setBrush( Qt::NoBrush );
994 if (
size.width() > 30 )
996 painter.drawRoundedRect( rect, 6, 6 );
1001 painter.drawRect( rect );