Skip to content

Commit

Permalink
[FEATURE][layouts] Use text renderer for drawing scalebar text
Browse files Browse the repository at this point in the history
Allows the full range of formatting options exposed through
text renderer - e.g. scalebar text with buffers, shadows,
background shapes, letter spacing, etc.

Say goodbye to unreadable scale bar text!
  • Loading branch information
nyalldawson committed May 11, 2018
1 parent a06000e commit 9d2c4fc
Show file tree
Hide file tree
Showing 21 changed files with 351 additions and 208 deletions.
36 changes: 31 additions & 5 deletions python/core/layout/qgslayoutitemscalebar.sip.in
Expand Up @@ -192,36 +192,62 @@ Sets the ``label`` for units.
.. seealso:: :py:func:`unitLabel`
%End

QFont font() const;
QgsTextFormat textFormat() const;
%Docstring
Returns the text format used for drawing text in the scalebar.

.. seealso:: :py:func:`setTextFormat`

.. versionadded:: 3.2
%End

void setTextFormat( const QgsTextFormat &format );
%Docstring
Sets the text ``format`` used for drawing text in the scalebar.

.. seealso:: :py:func:`textFormat`

.. versionadded:: 3.2
%End

QFont font() const /Deprecated/;
%Docstring
Returns the font used for drawing text in the scalebar.

.. seealso:: :py:func:`setFont`

.. deprecated:: use textFormat() instead
%End

void setFont( const QFont &font );
void setFont( const QFont &font ) /Deprecated/;
%Docstring
Sets the ``font`` used for drawing text in the scalebar.

.. seealso:: :py:func:`setFont`
.. seealso:: :py:func:`font`

.. deprecated:: use setTextFormat() instead
%End

QColor fontColor() const;
QColor fontColor() const /Deprecated/;
%Docstring
Returns the color used for drawing text in the scalebar.

.. seealso:: :py:func:`setFontColor`

.. seealso:: :py:func:`font`

.. deprecated:: use textFormat() instead
%End

void setFontColor( const QColor &color );
void setFontColor( const QColor &color ) /Deprecated/;
%Docstring
Sets the ``color`` used for drawing text in the scalebar.

.. seealso:: :py:func:`fontColor`

.. seealso:: :py:func:`setFont`

.. deprecated:: use setTextFormat() instead
%End

QColor fillColor() const;
Expand Down
11 changes: 10 additions & 1 deletion python/core/scalebar/qgsscalebarrenderer.sip.in
Expand Up @@ -74,9 +74,18 @@ Draws default scalebar labels using the specified ``settings`` and ``scaleContex
Returns the text used for the first label in the scalebar.
%End

double firstLabelXOffset( const QgsScaleBarSettings &settings ) const;
double firstLabelXOffset( const QgsScaleBarSettings &settings ) const /Deprecated/;
%Docstring
Returns the x-offset (in millimeters) used for the first label in the scalebar.

.. deprecated:: Use the version with QgsRenderContext instead.
%End

double firstLabelXOffset( const QgsScaleBarSettings &settings, const QgsRenderContext &context ) const;
%Docstring
Returns the x-offset (in render context painter units) used for the first label in the scalebar.

.. versionadded:: 3.2
%End

QList<double> segmentPositions( const QgsScaleBarRenderer::ScaleBarContext &scaleContext, const QgsScaleBarSettings &settings ) const;
Expand Down
37 changes: 32 additions & 5 deletions python/core/scalebar/qgsscalebarsettings.sip.in
Expand Up @@ -206,36 +206,63 @@ Sets the ``label`` for units.
.. seealso:: :py:func:`unitLabel`
%End

QFont font() const;
QgsTextFormat &textFormat();
%Docstring
Returns the text format used for drawing text in the scalebar.

.. seealso:: :py:func:`setTextFormat`

.. versionadded:: 3.2
%End


void setTextFormat( const QgsTextFormat &format );
%Docstring
Sets the text ``format`` used for drawing text in the scalebar.

.. seealso:: :py:func:`textFormat`

.. versionadded:: 3.2
%End

QFont font() const /Deprecated/;
%Docstring
Returns the font used for drawing text in the scalebar.

.. seealso:: :py:func:`setFont`

.. deprecated:: use textFormat() instead
%End

void setFont( const QFont &font );
void setFont( const QFont &font ) /Deprecated/;
%Docstring
Sets the ``font`` used for drawing text in the scalebar.

.. seealso:: :py:func:`setFont`
.. seealso:: :py:func:`font`

.. deprecated:: use setTextFormat() instead
%End

QColor fontColor() const;
QColor fontColor() const /Deprecated/;
%Docstring
Returns the color used for drawing text in the scalebar.

.. seealso:: :py:func:`setFontColor`

.. seealso:: :py:func:`font`

.. deprecated:: use textFormat() instead
%End

void setFontColor( const QColor &color );
void setFontColor( const QColor &color ) /Deprecated/;
%Docstring
Sets the ``color`` used for drawing text in the scalebar.

.. seealso:: :py:func:`fontColor`

.. seealso:: :py:func:`setFont`

.. deprecated:: use textFormat() instead
%End

QColor fillColor() const;
Expand Down
Binary file not shown.
35 changes: 8 additions & 27 deletions src/app/layout/qgslayoutscalebarwidget.cpp
Expand Up @@ -36,7 +36,6 @@ QgsLayoutScaleBarWidget::QgsLayoutScaleBarWidget( QgsLayoutItemScaleBar *scaleBa
connect( mNumberOfSegmentsSpinBox, static_cast < void ( QSpinBox::* )( int ) > ( &QSpinBox::valueChanged ), this, &QgsLayoutScaleBarWidget::mNumberOfSegmentsSpinBox_valueChanged );
connect( mUnitLabelLineEdit, &QLineEdit::textChanged, this, &QgsLayoutScaleBarWidget::mUnitLabelLineEdit_textChanged );
connect( mMapUnitsPerBarUnitSpinBox, static_cast < void ( QDoubleSpinBox::* )( double ) > ( &QDoubleSpinBox::valueChanged ), this, &QgsLayoutScaleBarWidget::mMapUnitsPerBarUnitSpinBox_valueChanged );
connect( mFontColorButton, &QgsColorButton::colorChanged, this, &QgsLayoutScaleBarWidget::mFontColorButton_colorChanged );
connect( mFillColorButton, &QgsColorButton::colorChanged, this, &QgsLayoutScaleBarWidget::mFillColorButton_colorChanged );
connect( mFillColor2Button, &QgsColorButton::colorChanged, this, &QgsLayoutScaleBarWidget::mFillColor2Button_colorChanged );
connect( mStrokeColorButton, &QgsColorButton::colorChanged, this, &QgsLayoutScaleBarWidget::mStrokeColorButton_colorChanged );
Expand Down Expand Up @@ -101,9 +100,8 @@ QgsLayoutScaleBarWidget::QgsLayoutScaleBarWidget( QgsLayoutItemScaleBar *scaleBa
mFillColor2Button->setNoColorString( tr( "Transparent fill" ) );
mFillColor2Button->setShowNoColor( true );

mFontColorButton->setColorDialogTitle( tr( "Select Font Color" ) );
mFontColorButton->setAllowOpacity( true );
mFontColorButton->setContext( QStringLiteral( "composer" ) );
mFontButton->setDialogTitle( tr( "Scalebar Font" ) );
mFontButton->setMode( QgsFontButton::ModeTextRenderer );

mStrokeColorButton->setColorDialogTitle( tr( "Select Line Color" ) );
mStrokeColorButton->setAllowOpacity( true );
Expand Down Expand Up @@ -135,7 +133,7 @@ QgsLayoutScaleBarWidget::QgsLayoutScaleBarWidget( QgsLayoutItemScaleBar *scaleBa
blockMemberSignals( false );
setGuiElements(); //set the GUI elements to the state of scaleBar

connect( mFontButton, &QgsFontButton::changed, this, &QgsLayoutScaleBarWidget::fontChanged );
connect( mFontButton, &QgsFontButton::changed, this, &QgsLayoutScaleBarWidget::textFormatChanged );
}

bool QgsLayoutScaleBarWidget::setNewItem( QgsLayoutItem *item )
Expand Down Expand Up @@ -181,11 +179,10 @@ void QgsLayoutScaleBarWidget::setGuiElements()
mUnitLabelLineEdit->setText( mScalebar->unitLabel() );
mLineJoinStyleCombo->setPenJoinStyle( mScalebar->lineJoinStyle() );
mLineCapStyleCombo->setPenCapStyle( mScalebar->lineCapStyle() );
mFontColorButton->setColor( mScalebar->fontColor() );
mFillColorButton->setColor( mScalebar->fillColor() );
mFillColor2Button->setColor( mScalebar->fillColor2() );
mStrokeColorButton->setColor( mScalebar->lineColor() );
mFontButton->setCurrentFont( mScalebar->font() );
mFontButton->setTextFormat( mScalebar->textFormat() );

//map combo box
mMapItemComboBox->setItem( mScalebar->linkedMap() );
Expand All @@ -196,7 +193,7 @@ void QgsLayoutScaleBarWidget::setGuiElements()
toggleStyleSpecificControls( style );

//alignment
mAlignmentComboBox->setCurrentIndex( ( int )( mScalebar->alignment() ) );
mAlignmentComboBox->setCurrentIndex( static_cast< int >( mScalebar->alignment() ) );

//units
mUnitsComboBox->setCurrentIndex( mUnitsComboBox->findData( static_cast< int >( mScalebar->units() ) ) );
Expand Down Expand Up @@ -300,7 +297,7 @@ void QgsLayoutScaleBarWidget::mHeightSpinBox_valueChanged( double d )
mScalebar->endCommand();
}

void QgsLayoutScaleBarWidget::fontChanged()
void QgsLayoutScaleBarWidget::textFormatChanged()
{
if ( !mScalebar )
{
Expand All @@ -309,27 +306,12 @@ void QgsLayoutScaleBarWidget::fontChanged()

mScalebar->beginCommand( tr( "Set Scalebar Font" ) );
disconnectUpdateSignal();
mScalebar->setFont( mFontButton->currentFont() );
mScalebar->setTextFormat( mFontButton->textFormat() );
connectUpdateSignal();
mScalebar->endCommand();
mScalebar->update();
}

void QgsLayoutScaleBarWidget::mFontColorButton_colorChanged( const QColor &newColor )
{
if ( !mScalebar )
{
return;
}

mScalebar->beginCommand( tr( "Set Scalebar Font Color" ), QgsLayoutItem::UndoScaleBarFontColor );
disconnectUpdateSignal();
mScalebar->setFontColor( newColor );
mScalebar->update();
connectUpdateSignal();
mScalebar->endCommand();
}

void QgsLayoutScaleBarWidget::mFillColorButton_colorChanged( const QColor &newColor )
{
if ( !mScalebar )
Expand Down Expand Up @@ -535,7 +517,7 @@ void QgsLayoutScaleBarWidget::mAlignmentComboBox_currentIndexChanged( int index

mScalebar->beginCommand( tr( "Set Scalebar Alignment" ) );
disconnectUpdateSignal();
mScalebar->setAlignment( ( QgsScaleBarSettings::Alignment ) index );
mScalebar->setAlignment( static_cast< QgsScaleBarSettings::Alignment >( index ) );
mScalebar->update();
connectUpdateSignal();
mScalebar->endCommand();
Expand Down Expand Up @@ -583,7 +565,6 @@ void QgsLayoutScaleBarWidget::blockMemberSignals( bool block )
mUnitsComboBox->blockSignals( block );
mLineJoinStyleCombo->blockSignals( block );
mLineCapStyleCombo->blockSignals( block );
mFontColorButton->blockSignals( block );
mFillColorButton->blockSignals( block );
mFillColor2Button->blockSignals( block );
mStrokeColorButton->blockSignals( block );
Expand Down
3 changes: 1 addition & 2 deletions src/app/layout/qgslayoutscalebarwidget.h
Expand Up @@ -48,7 +48,6 @@ class QgsLayoutScaleBarWidget: public QgsLayoutItemBaseWidget, private Ui::QgsLa
void mNumberOfSegmentsSpinBox_valueChanged( int i );
void mUnitLabelLineEdit_textChanged( const QString &text );
void mMapUnitsPerBarUnitSpinBox_valueChanged( double d );
void mFontColorButton_colorChanged( const QColor &newColor );
void mFillColorButton_colorChanged( const QColor &newColor );
void mFillColor2Button_colorChanged( const QColor &newColor );
void mStrokeColorButton_colorChanged( const QColor &newColor );
Expand All @@ -66,7 +65,7 @@ class QgsLayoutScaleBarWidget: public QgsLayoutItemBaseWidget, private Ui::QgsLa
void setGuiElements();
void segmentSizeRadioChanged( QAbstractButton *radio );
void mapChanged( QgsLayoutItem *item );
void fontChanged();
void textFormatChanged();

private:
QPointer< QgsLayoutItemScaleBar > mScalebar;
Expand Down
40 changes: 27 additions & 13 deletions src/app/qgsdecorationscalebar.cpp
Expand Up @@ -85,17 +85,31 @@ void QgsDecorationScaleBar::projectRead()

QDomDocument doc;
QDomElement elem;
QString fontXml = QgsProject::instance()->readEntry( mNameConfig, QStringLiteral( "/Font" ) );
if ( !fontXml.isEmpty() )
QString textFormatXml = QgsProject::instance()->readEntry( mNameConfig, QStringLiteral( "/TextFormat" ) );
if ( !textFormatXml.isEmpty() )
{
doc.setContent( fontXml );
doc.setContent( textFormatXml );
elem = doc.documentElement();
QgsDebugMsg( doc.toString() );
QgsFontUtils::setFromXmlElement( mFont, elem );
QgsReadWriteContext context;
context.setPathResolver( QgsProject::instance()->pathResolver() );
mTextFormat.readXml( elem, context );
}
else
{
mFont = QFont();
QString fontXml = QgsProject::instance()->readEntry( mNameConfig, QStringLiteral( "/Font" ) );
if ( !fontXml.isEmpty() )
{
doc.setContent( fontXml );
elem = doc.documentElement();
QFont font;
QgsFontUtils::setFromXmlElement( font, elem );
mTextFormat = QgsTextFormat::fromQFont( font );
mTextFormat.setColor( mColor );
}
else
{
mTextFormat = QgsTextFormat();
}
}

setupScaleBar();
Expand All @@ -113,9 +127,12 @@ void QgsDecorationScaleBar::saveToProject()
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/MarginV" ), mMarginVertical );

QDomDocument fontDoc;
QDomElement font = QgsFontUtils::toXmlElement( mFont, fontDoc, QStringLiteral( "BarFont" ) );
fontDoc.appendChild( font );
QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/Font" ), fontDoc.toString() );
QgsReadWriteContext context;
context.setPathResolver( QgsProject::instance()->pathResolver() );
QDomElement textElem = mTextFormat.writeXml( fontDoc, context );
fontDoc.appendChild( textElem );

QgsProject::instance()->writeEntry( mNameConfig, QStringLiteral( "/TextFormat" ), fontDoc.toString() );
}


Expand All @@ -127,6 +144,7 @@ void QgsDecorationScaleBar::run()

void QgsDecorationScaleBar::setupScaleBar()
{
mSettings.setTextFormat( mTextFormat );
switch ( mStyleIndex )
{
case 0:
Expand All @@ -137,8 +155,6 @@ void QgsDecorationScaleBar::setupScaleBar()
mStyle = std::move( tickStyle );
mSettings.setFillColor( mColor );
mSettings.setLineColor( mColor ); // Compatibility with pre 3.2 configuration
mSettings.setFont( mFont );
mSettings.setFontColor( mColor );
mSettings.setHeight( 2.2 );
mSettings.setLineWidth( 0.3 );
break;
Expand All @@ -149,8 +165,6 @@ void QgsDecorationScaleBar::setupScaleBar()
mSettings.setFillColor( mColor );
mSettings.setFillColor2( QColor( "transparent" ) );
mSettings.setLineColor( mOutlineColor );
mSettings.setFont( mFont );
mSettings.setFontColor( mColor );
mSettings.setHeight( mStyleIndex == 2 ? 1 : 3 );
mSettings.setLineWidth( mStyleIndex == 2 ? 0.2 : 0.3 );
break;
Expand Down
4 changes: 2 additions & 2 deletions src/app/qgsdecorationscalebar.h
Expand Up @@ -64,8 +64,8 @@ class APP_EXPORT QgsDecorationScaleBar: public QgsDecorationItem
QColor mColor;
//! The scale bar otuline color
QColor mOutlineColor;
//! The scale bar font
QFont mFont;

QgsTextFormat mTextFormat;

QgsScaleBarSettings mSettings;

Expand Down
6 changes: 3 additions & 3 deletions src/app/qgsdecorationscalebardialog.cpp
Expand Up @@ -84,8 +84,8 @@ QgsDecorationScaleBarDialog::QgsDecorationScaleBarDialog( QgsDecorationScaleBar
pbnChangeOutlineColor->setContext( QStringLiteral( "gui" ) );
pbnChangeOutlineColor->setColorDialogTitle( tr( "Select Scale Bar Outline Color" ) );

mButtonFontStyle->setMode( QgsFontButton::ModeQFont );
mButtonFontStyle->setCurrentFont( mDeco.mFont );
mButtonFontStyle->setMode( QgsFontButton::ModeTextRenderer );
mButtonFontStyle->setTextFormat( mDeco.mTextFormat );
}

QgsDecorationScaleBarDialog::~QgsDecorationScaleBarDialog()
Expand All @@ -111,7 +111,7 @@ void QgsDecorationScaleBarDialog::apply()
mDeco.mStyleIndex = cboStyle->currentIndex();
mDeco.mColor = pbnChangeColor->color();
mDeco.mOutlineColor = pbnChangeOutlineColor->color();
mDeco.mFont = mButtonFontStyle->currentFont();
mDeco.mTextFormat = mButtonFontStyle->textFormat();
mDeco.setupScaleBar();
mDeco.update();
}
Expand Down

0 comments on commit 9d2c4fc

Please sign in to comment.