Skip to content
Permalink
Browse files

[FEATURE][layouts] Use text renderer for drawing scalebar text

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 8, 2018
1 parent a06000e commit 9d2c4fca1986a144f4e1cc8ada8a4450acb47e59
@@ -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;
@@ -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;
@@ -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;
Binary file not shown.
@@ -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 );
@@ -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 );
@@ -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 )
@@ -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() );
@@ -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() ) ) );
@@ -300,7 +297,7 @@ void QgsLayoutScaleBarWidget::mHeightSpinBox_valueChanged( double d )
mScalebar->endCommand();
}

void QgsLayoutScaleBarWidget::fontChanged()
void QgsLayoutScaleBarWidget::textFormatChanged()
{
if ( !mScalebar )
{
@@ -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 )
@@ -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();
@@ -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 );
@@ -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 );
@@ -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;
@@ -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();
@@ -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() );
}


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

void QgsDecorationScaleBar::setupScaleBar()
{
mSettings.setTextFormat( mTextFormat );
switch ( mStyleIndex )
{
case 0:
@@ -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;
@@ -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;
@@ -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;

@@ -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()
@@ -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();
}

0 comments on commit 9d2c4fc

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