Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FEATURE][symbology] Add a font style setting for font markers (#35197)
  • Loading branch information
nirvn committed Mar 21, 2020
1 parent d9df359 commit 0757056
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 41 deletions.
18 changes: 18 additions & 0 deletions python/core/auto_generated/symbology/qgsmarkersymbollayer.sip.in
Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions src/core/symbology/qgsmarkersymbollayer.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand All @@ -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" ) ) )
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 );
Expand All @@ -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 );
Expand Down
17 changes: 17 additions & 0 deletions src/core/symbology/qgsmarkersymbollayer.h
Expand Up @@ -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.
*
Expand Down Expand Up @@ -959,6 +975,7 @@ class CORE_EXPORT QgsFontMarkerSymbolLayer : public QgsMarkerSymbolLayer
protected:

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

Expand Down
74 changes: 66 additions & 8 deletions src/gui/symbology/qgssymbollayerwidget.cpp
Expand Up @@ -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
Expand Down Expand Up @@ -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 );
Expand All @@ -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() );
Expand Down Expand Up @@ -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 )
Expand Down Expand Up @@ -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 )
Expand Down
14 changes: 14 additions & 0 deletions src/gui/symbology/qgssymbollayerwidget.h
Expand Up @@ -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();
Expand All @@ -1017,6 +1028,9 @@ class GUI_EXPORT QgsFontMarkerSymbolLayerWidget : public QgsSymbolLayerWidget, p

std::shared_ptr< QgsMarkerSymbol > mAssistantPreviewSymbol;

QFont mRefFont;
QFontDatabase mFontDB;

};

//////////
Expand Down

0 comments on commit 0757056

Please sign in to comment.