Skip to content
Permalink
Browse files

[FEATURE][symbology] Add a font style setting for font markers (#35197)

  • Loading branch information
nirvn committed Mar 21, 2020
1 parent d9df359 commit 07570562fc64c3a1d1bf5c8d0ab91edaa741e2d9
@@ -925,6 +925,24 @@ Returns the font family name for the associated font which will be used to rende
Sets the font ``family`` for the font which will be used to render the point.

.. seealso:: :py:func:`fontFamily`
%End

QString fontStyle() const;
%Docstring
Returns the font style for the associated font which will be used to render the point.

.. seealso:: :py:func:`setFontStyle`

.. versionadded:: 3.14
%End

void setFontStyle( const QString &style );
%Docstring
Sets the font ``style`` for the font which will be used to render the point.

.. seealso:: :py:func:`fontStyle`

.. versionadded:: 3.14
%End

QString character() const;
@@ -19,6 +19,7 @@
#include "qgsdxfexport.h"
#include "qgsdxfpaintdevice.h"
#include "qgsexpression.h"
#include "qgsfontutils.h"
#include "qgsimagecache.h"
#include "qgsimageoperation.h"
#include "qgsrendercontext.h"
@@ -2966,6 +2967,7 @@ QgsFontMarkerSymbolLayer::~QgsFontMarkerSymbolLayer()
QgsSymbolLayer *QgsFontMarkerSymbolLayer::create( const QgsStringMap &props )
{
QString fontFamily = DEFAULT_FONTMARKER_FONT;
QString fontStyle = DEFAULT_FONTMARKER_FONT;
QString string = DEFAULT_FONTMARKER_CHR;
double pointSize = DEFAULT_FONTMARKER_SIZE;
QColor color = DEFAULT_FONTMARKER_COLOR;
@@ -2984,6 +2986,8 @@ QgsSymbolLayer *QgsFontMarkerSymbolLayer::create( const QgsStringMap &props )

QgsFontMarkerSymbolLayer *m = new QgsFontMarkerSymbolLayer( fontFamily, string, pointSize, color, angle );

if ( props.contains( QStringLiteral( "font_style" ) ) )
m->setFontStyle( props[QStringLiteral( "font_style" )] );
if ( props.contains( QStringLiteral( "outline_color" ) ) )
m->setStrokeColor( QgsSymbolLayerUtils::decodeColor( props[QStringLiteral( "outline_color" )] ) );
if ( props.contains( QStringLiteral( "outline_width" ) ) )
@@ -3033,6 +3037,11 @@ void QgsFontMarkerSymbolLayer::startRender( QgsSymbolRenderContext &context )
mPen.setWidthF( context.renderContext().convertToPainterUnits( mStrokeWidth, mStrokeWidthUnit, mStrokeWidthMapUnitScale ) );

mFont = QFont( mFontFamily );
if ( !mFontStyle.isEmpty() )
{
mFont.setStyleName( QgsFontUtils::translateNamedStyle( mFontStyle ) );
}

const double sizePixels = context.renderContext().convertToPainterUnits( mSize, mSizeUnit, mSizeMapUnitScale );
mNonZeroFontSize = !qgsDoubleNear( sizePixels, 0.0 );
// if a non zero, but small pixel size results, round up to 2 pixels so that a "dot" is at least visible
@@ -3261,6 +3270,7 @@ QgsStringMap QgsFontMarkerSymbolLayer::properties() const
{
QgsStringMap props;
props[QStringLiteral( "font" )] = mFontFamily;
props[QStringLiteral( "font_style" )] = mFontStyle;
props[QStringLiteral( "chr" )] = mString;
props[QStringLiteral( "size" )] = QString::number( mSize );
props[QStringLiteral( "size_unit" )] = QgsUnitTypes::encodeUnit( mSizeUnit );
@@ -3283,6 +3293,7 @@ QgsStringMap QgsFontMarkerSymbolLayer::properties() const
QgsFontMarkerSymbolLayer *QgsFontMarkerSymbolLayer::clone() const
{
QgsFontMarkerSymbolLayer *m = new QgsFontMarkerSymbolLayer( mFontFamily, mString, mSize, mColor, mAngle );
m->setFontStyle( mFontStyle );
m->setStrokeColor( mStrokeColor );
m->setStrokeWidth( mStrokeWidth );
m->setStrokeWidthUnit( mStrokeWidthUnit );
@@ -854,6 +854,22 @@ class CORE_EXPORT QgsFontMarkerSymbolLayer : public QgsMarkerSymbolLayer
*/
void setFontFamily( const QString &family ) { mFontFamily = family; }

/**
* Returns the font style for the associated font which will be used to render the point.
*
* \see setFontStyle()
* \since QGIS 3.14
*/
QString fontStyle() const { return mFontStyle; }

/**
* Sets the font \a style for the font which will be used to render the point.
*
* \see fontStyle()
* \since QGIS 3.14
*/
void setFontStyle( const QString &style ) { mFontStyle = style; }

/**
* Returns the character(s) used when rendering points.
*
@@ -959,6 +975,7 @@ class CORE_EXPORT QgsFontMarkerSymbolLayer : public QgsMarkerSymbolLayer
protected:

QString mFontFamily;
QString mFontStyle;
QFontMetrics *mFontMetrics = nullptr;
QString mString;

@@ -30,6 +30,7 @@
#include "qgssymbollayerutils.h"
#include "qgscolorramp.h"
#include "qgscolorrampbutton.h"
#include "qgsfontutils.h"
#include "qgsgradientcolorrampdialog.h"
#include "qgsproperty.h"
#include "qgsstyle.h" //for symbol selector dialog
@@ -3316,6 +3317,7 @@ QgsFontMarkerSymbolLayerWidget::QgsFontMarkerSymbolLayerWidget( QgsVectorLayer *
mSizeDDBtn->setSymbol( mAssistantPreviewSymbol );

connect( cboFont, &QFontComboBox::currentFontChanged, this, &QgsFontMarkerSymbolLayerWidget::setFontFamily );
connect( mFontStyleComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsFontMarkerSymbolLayerWidget::mFontStyleComboBox_currentIndexChanged );
connect( spinSize, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsFontMarkerSymbolLayerWidget::setSize );
connect( cboJoinStyle, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsFontMarkerSymbolLayerWidget::penJoinStyleChanged );
connect( btnColor, &QgsColorButton::colorChanged, this, &QgsFontMarkerSymbolLayerWidget::setColor );
@@ -3338,24 +3340,30 @@ void QgsFontMarkerSymbolLayerWidget::setSymbolLayer( QgsSymbolLayer *layer )
// layer type is correct, we can do the cast
mLayer = static_cast<QgsFontMarkerSymbolLayer *>( layer );

QFont layerFont( mLayer->fontFamily() );
mRefFont.setFamily( mLayer->fontFamily() );
mRefFont.setStyleName( QgsFontUtils::translateNamedStyle( mLayer->fontStyle() ) );

mFontStyleComboBox->blockSignals( true );
populateFontStyleComboBox();
mFontStyleComboBox->blockSignals( false );

// set values
whileBlocking( cboFont )->setCurrentFont( layerFont );
whileBlocking( cboFont )->setCurrentFont( mRefFont );
whileBlocking( spinSize )->setValue( mLayer->size() );
whileBlocking( btnColor )->setColor( mLayer->color() );
whileBlocking( btnStrokeColor )->setColor( mLayer->strokeColor() );
whileBlocking( mStrokeWidthSpinBox )->setValue( mLayer->strokeWidth() );
whileBlocking( spinAngle )->setValue( mLayer->angle() );

widgetChar->blockSignals( true );
widgetChar->setFont( layerFont );
widgetChar->setFont( mRefFont );
if ( mLayer->character().length() == 1 )
{
widgetChar->setCharacter( mLayer->character().at( 0 ) );
}
widgetChar->blockSignals( false );
whileBlocking( mCharLineEdit )->setText( mLayer->character() );
mCharPreview->setFont( layerFont );
mCharPreview->setFont( mRefFont );

//block
whileBlocking( spinOffsetX )->setValue( mLayer->offset().x() );
@@ -3403,10 +3411,27 @@ QgsSymbolLayer *QgsFontMarkerSymbolLayerWidget::symbolLayer()

void QgsFontMarkerSymbolLayerWidget::setFontFamily( const QFont &font )
{
mLayer->setFontFamily( font.family() );
widgetChar->setFont( font );
mCharPreview->setFont( font );
emit changed();
if ( mLayer )
{
mLayer->setFontFamily( font.family() );
mRefFont.setFamily( font.family() );
widgetChar->setFont( mRefFont );
mCharPreview->setFont( mRefFont );
populateFontStyleComboBox();
emit changed();
}
}

void QgsFontMarkerSymbolLayerWidget::setFontStyle( const QString &style )
{
if ( mLayer )
{
QgsFontUtils::updateFontViaStyle( mRefFont, style );
mLayer->setFontStyle( QgsFontUtils::untranslateNamedStyle( style ) );
widgetChar->setFont( mRefFont );
mCharPreview->setFont( mRefFont );
emit changed();
}
}

void QgsFontMarkerSymbolLayerWidget::setColor( const QColor &color )
@@ -3525,6 +3550,39 @@ void QgsFontMarkerSymbolLayerWidget::mStrokeWidthUnitWidget_changed()
}
}

void QgsFontMarkerSymbolLayerWidget::populateFontStyleComboBox()
{
mFontStyleComboBox->clear();
QStringList styles = mFontDB.styles( mRefFont.family() );
const auto constStyles = styles;
for ( const QString &style : constStyles )
{
mFontStyleComboBox->addItem( style );
}

QString targetStyle = mFontDB.styleString( mRefFont );
if ( !styles.contains( targetStyle ) )
{
QFont f = QFont( mRefFont.family() );
targetStyle = QFontInfo( f ).styleName();
mRefFont.setStyleName( targetStyle );
}
int curIndx = 0;
int stylIndx = mFontStyleComboBox->findText( targetStyle );
if ( stylIndx > -1 )
{
curIndx = stylIndx;
}

mFontStyleComboBox->setCurrentIndex( curIndx );
}

void QgsFontMarkerSymbolLayerWidget::mFontStyleComboBox_currentIndexChanged( int index )
{
Q_UNUSED( index );
setFontStyle( mFontStyleComboBox->currentText() );
}

void QgsFontMarkerSymbolLayerWidget::mHorizontalAnchorComboBox_currentIndexChanged( int index )
{
if ( mLayer )
@@ -1003,11 +1003,22 @@ class GUI_EXPORT QgsFontMarkerSymbolLayerWidget : public QgsSymbolLayerWidget, p
CharacterWidget *widgetChar = nullptr;

private slots:

/**
* Sets the font \a style.
* \since QGIS 3.14
*/
void setFontStyle( const QString &style );

void setOffset();
void mSizeUnitWidget_changed();
void mOffsetUnitWidget_changed();
void mStrokeWidthUnitWidget_changed();
void mStrokeWidthSpinBox_valueChanged( double d );

void populateFontStyleComboBox();
void mFontStyleComboBox_currentIndexChanged( int index );

void mHorizontalAnchorComboBox_currentIndexChanged( int index );
void mVerticalAnchorComboBox_currentIndexChanged( int index );
void penJoinStyleChanged();
@@ -1017,6 +1028,9 @@ class GUI_EXPORT QgsFontMarkerSymbolLayerWidget : public QgsSymbolLayerWidget, p

std::shared_ptr< QgsMarkerSymbol > mAssistantPreviewSymbol;

QFont mRefFont;
QFontDatabase mFontDB;

};

//////////

0 comments on commit 0757056

Please sign in to comment.
You can’t perform that action at this time.