Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Make legend/preview text configurable
  • Loading branch information
mhugent committed May 11, 2021
1 parent 3ecb5e5 commit 62d7337
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 47 deletions.
2 changes: 2 additions & 0 deletions python/core/auto_generated/labeling/qgspallabeling.sip.in
Expand Up @@ -482,6 +482,8 @@ Sets the polygon placement ``flags``, which dictate how polygon labels can be pl

QgsWkbTypes::GeometryType layerType;

QString legendString;

void calculateLabelSize( const QFontMetricsF *fm, const QString &text, double &labelX, double &labelY, const QgsFeature *f = 0, QgsRenderContext *context = 0, double *rotatedLabelX /Out/ = 0, double *rotatedLabelY /Out/ = 0 );
%Docstring
Calculates the space required to render the provided ``text`` in map units.
Expand Down
6 changes: 5 additions & 1 deletion src/core/labeling/qgspallabeling.cpp
Expand Up @@ -366,6 +366,8 @@ QgsPalLayerSettings &QgsPalLayerSettings::operator=( const QgsPalLayerSettings &
geometryGeneratorType = s.geometryGeneratorType;
layerType = s.layerType;

legendString = s.legendString;

return *this;
}

Expand Down Expand Up @@ -928,6 +930,7 @@ void QgsPalLayerSettings::readXml( const QDomElement &elem, const QgsReadWriteCo
Q_NOWARN_DEPRECATED_POP
substitutions.readXml( textStyleElem.firstChildElement( QStringLiteral( "substitutions" ) ) );
useSubstitutions = textStyleElem.attribute( QStringLiteral( "useSubstitutions" ) ).toInt();
legendString = textStyleElem.attribute( QStringLiteral( "legendString" ), QObject::tr( "Aa" ) );

// text formatting
QDomElement textFormatElem = elem.firstChildElement( QStringLiteral( "text-format" ) );
Expand Down Expand Up @@ -1167,6 +1170,7 @@ QDomElement QgsPalLayerSettings::writeXml( QDomDocument &doc, const QgsReadWrite
substitutions.writeXml( replacementElem, doc );
textStyleElem.appendChild( replacementElem );
textStyleElem.setAttribute( QStringLiteral( "useSubstitutions" ), useSubstitutions );
textStyleElem.setAttribute( QStringLiteral( "legendString" ), legendString );

// text formatting
QDomElement textFormatElem = doc.createElement( QStringLiteral( "text-format" ) );
Expand Down Expand Up @@ -1341,7 +1345,7 @@ QPixmap QgsPalLayerSettings::labelSettingsPreviewPixmap( const QgsPalLayerSettin
if ( tempFormat.background().enabled() )
ytrans = std::max( ytrans, context.convertToPainterUnits( tempFormat.background().size().height(), tempFormat.background().sizeUnit(), tempFormat.background().sizeMapUnitScale() ) );

const QStringList text = QStringList() << ( previewText.isEmpty() ? QObject::tr( "Aa" ) : previewText );
const QStringList text = QStringList() << ( previewText.isEmpty() ? settings.legendString : previewText );
const double textHeight = QgsTextRenderer::textHeight( context, tempFormat, text, QgsTextRenderer::Rect );
QRectF textRect = rect;
textRect.setLeft( xtrans + padding );
Expand Down
6 changes: 6 additions & 0 deletions src/core/labeling/qgspallabeling.h
Expand Up @@ -798,6 +798,12 @@ class CORE_EXPORT QgsPalLayerSettings
*/
QgsWkbTypes::GeometryType layerType = QgsWkbTypes::UnknownGeometry;

/**
* \brief string to show in the legend and in the preview icon
* \since QGIS 3.20
*/
QString legendString = QObject::tr( "Aa" );

/**
* Calculates the space required to render the provided \a text in map units.
* Results will be written to \a labelX and \a labelY.
Expand Down
8 changes: 4 additions & 4 deletions src/core/layertree/qgslayertreemodellegendnode.cpp
Expand Up @@ -1451,7 +1451,7 @@ QVariant QgsVectorLabelLegendNode::data( int role ) const
if ( role == Qt::DecorationRole )
{
const int iconSize = QgsLayerTreeModel::scaleIconSize( 16 );
return QgsPalLayerSettings::labelSettingsPreviewPixmap( mLabelSettings, QSize( iconSize, iconSize ), QStringLiteral( "Aa" ) );
return QgsPalLayerSettings::labelSettingsPreviewPixmap( mLabelSettings, QSize( iconSize, iconSize ), mLabelSettings.legendString );
}
return QVariant();
}
Expand All @@ -1475,7 +1475,7 @@ QSizeF QgsVectorLabelLegendNode::drawSymbol( const QgsLegendSettings &settings,

QSizeF QgsVectorLabelLegendNode::drawSymbol( const QgsLegendSettings &settings, const QgsRenderContext &renderContext, double xOffset, double yOffset ) const
{
QStringList textLines( "Aa" );
QStringList textLines( mLabelSettings.legendString );
QgsTextFormat textFormat = mLabelSettings.format();
textFormat.setSize( textFormat.size() / renderContext.scaleFactor() ); //painter is usually scaled (e.g. mm unit)
QgsRenderContext ctx( renderContext );
Expand All @@ -1496,14 +1496,14 @@ QJsonObject QgsVectorLabelLegendNode::exportSymbolToJson( const QgsLegendSetting

const double mmToPixel = 96.0 / 25.4; //settings.dpi() is deprecated

const QStringList textLines( "Aa" );
const QStringList textLines( mLabelSettings.legendString );
const QgsTextFormat textFormat = mLabelSettings.format();
QgsRenderContext ctx( context );
ctx.setScaleFactor( mmToPixel );

double textWidth, textHeight;
textWidthHeight( textWidth, textHeight, ctx, textFormat, textLines );
QPixmap previewPixmap = mLabelSettings.labelSettingsPreviewPixmap( mLabelSettings, QSize( textWidth, textHeight ), QStringLiteral( "Aa" ) );
QPixmap previewPixmap = mLabelSettings.labelSettingsPreviewPixmap( mLabelSettings, QSize( textWidth, textHeight ), mLabelSettings.legendString );

QByteArray byteArray;
QBuffer buffer( &byteArray );
Expand Down
4 changes: 4 additions & 0 deletions src/gui/labeling/qgslabelinggui.cpp
Expand Up @@ -266,6 +266,8 @@ QgsLabelingGui::QgsLabelingGui( QgsVectorLayer *layer, QgsMapCanvas *mapCanvas,

mFieldExpressionWidget->registerExpressionContextGenerator( this );

mLegendStringLineEdit->setText( layerSettings.legendString );

mMinScaleWidget->setMapCanvas( mCanvas );
mMinScaleWidget->setShowCurrentScaleButton( true );
mMaxScaleWidget->setMapCanvas( mCanvas );
Expand Down Expand Up @@ -502,6 +504,8 @@ QgsPalLayerSettings QgsLabelingGui::layerSettings()
lyr.fieldName = mFieldExpressionWidget->currentField( &isExpression );
lyr.isExpression = isExpression;

lyr.legendString = mLegendStringLineEdit->text();

lyr.dist = 0;

QgsLabeling::PolygonPlacementFlags polygonPlacementFlags = QgsLabeling::PolygonPlacementFlag::AllowPlacementInsideOfPolygon;
Expand Down
83 changes: 41 additions & 42 deletions src/ui/qgstextformatwidgetbase.ui
Expand Up @@ -14,21 +14,6 @@
<string>Layer Labeling Settings</string>
</property>
<layout class="QGridLayout" name="gridLayout_8">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<property name="verticalSpacing">
<number>6</number>
</property>
<item row="0" column="0">
<widget class="QFrame" name="frameLabelWith">
<layout class="QHBoxLayout" name="horizontalLayout_19">
Expand Down Expand Up @@ -89,7 +74,21 @@
</layout>
</widget>
</item>
<item row="3" column="0">
<item row="1" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_15">
<item>
<widget class="QLabel" name="mLegendStringLabel">
<property name="text">
<string>Legend text</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="mLegendStringLineEdit"/>
</item>
</layout>
</item>
<item row="2" column="0">
<widget class="QFrame" name="mLabelingFrame">
<property name="minimumSize">
<size>
Expand Down Expand Up @@ -172,7 +171,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>499</width>
<width>480</width>
<height>300</height>
</rect>
</property>
Expand Down Expand Up @@ -717,8 +716,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>485</width>
<height>433</height>
<width>455</width>
<height>414</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2" stretch="0,1">
Expand Down Expand Up @@ -1280,8 +1279,8 @@ font-style: italic;</string>
<rect>
<x>0</x>
<y>0</y>
<width>471</width>
<height>666</height>
<width>467</width>
<height>931</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_42">
Expand Down Expand Up @@ -2164,8 +2163,8 @@ font-style: italic;</string>
<rect>
<x>0</x>
<y>0</y>
<width>485</width>
<height>433</height>
<width>467</width>
<height>401</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_12">
Expand Down Expand Up @@ -2510,8 +2509,8 @@ font-style: italic;</string>
<rect>
<x>0</x>
<y>0</y>
<width>485</width>
<height>433</height>
<width>488</width>
<height>392</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_121">
Expand Down Expand Up @@ -2788,8 +2787,8 @@ font-style: italic;</string>
<rect>
<x>0</x>
<y>0</y>
<width>471</width>
<height>740</height>
<width>513</width>
<height>1053</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_21">
Expand Down Expand Up @@ -3549,8 +3548,8 @@ font-style: italic;</string>
<rect>
<x>0</x>
<y>0</y>
<width>485</width>
<height>433</height>
<width>467</width>
<height>592</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_22">
Expand Down Expand Up @@ -3977,8 +3976,8 @@ font-style: italic;</string>
<rect>
<x>0</x>
<y>0</y>
<width>485</width>
<height>433</height>
<width>488</width>
<height>392</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_46">
Expand Down Expand Up @@ -4044,7 +4043,7 @@ font-style: italic;</string>
</property>
<property name="topMargin">
<number>1</number>
</property>
</property>
<item row="0" column="1" colspan="2">
<widget class="QComboBox" name="mCalloutStyleComboBox"/>
</item>
Expand Down Expand Up @@ -4127,8 +4126,8 @@ font-style: italic;</string>
<rect>
<x>0</x>
<y>0</y>
<width>474</width>
<height>1599</height>
<width>510</width>
<height>2059</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_11">
Expand Down Expand Up @@ -5827,8 +5826,8 @@ font-style: italic;</string>
<rect>
<x>0</x>
<y>0</y>
<width>471</width>
<height>666</height>
<width>467</width>
<height>858</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_8">
Expand Down Expand Up @@ -6577,6 +6576,12 @@ font-style: italic;</string>
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>
<customwidget>
<class>QgsScrollArea</class>
<extends>QScrollArea</extends>
<header>qgsscrollarea.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsCollapsibleGroupBox</class>
<extends>QGroupBox</extends>
Expand All @@ -6593,12 +6598,6 @@ font-style: italic;</string>
<extends>QDoubleSpinBox</extends>
<header>qgsdoublespinbox.h</header>
</customwidget>
<customwidget>
<class>QgsScrollArea</class>
<extends>QScrollArea</extends>
<header>qgsscrollarea.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsFieldExpressionWidget</class>
<extends>QWidget</extends>
Expand Down

0 comments on commit 62d7337

Please sign in to comment.