Skip to content

Commit

Permalink
Append additional "QgsCalloutContext" member to callout rendering vir…
Browse files Browse the repository at this point in the history
…tual

methods

While unused for now, this gives us flexibility in future to specify
additional useful contextual information about how a callout should
be rendered without breaking API (e.g. label text alignment, label
font settings, etc)
  • Loading branch information
nyalldawson committed Jul 21, 2019
1 parent 8986ea2 commit 39d4145
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 23 deletions.
19 changes: 19 additions & 0 deletions python/core/auto_generated/qgslabelingenginesettings.sip.in
Expand Up @@ -28,6 +28,7 @@ Stores global configuration for labeling engine
RenderOutlineLabels,
DrawLabelRectOnly,
DrawCandidates,
DrawUnplacedLabels,
};
typedef QFlags<QgsLabelingEngineSettings::Flag> Flags;

Expand Down Expand Up @@ -109,6 +110,24 @@ Sets the default text rendering ``format`` for the labels.
.. seealso:: :py:func:`defaultTextRenderFormat`

.. versionadded:: 3.4.3
%End

QColor unplacedLabelColor() const;
%Docstring
Returns the color to use when rendering unplaced labels.

.. seealso:: :py:func:`setUnplacedLabelColor`

.. versionadded:: 3.10
%End

void setUnplacedLabelColor( const QColor &color );
%Docstring
Sets the ``color`` to use when rendering unplaced labels.

.. seealso:: :py:func:`unplacedLabelColor`

.. versionadded:: 3.10
%End

};
Expand Down
12 changes: 10 additions & 2 deletions src/app/qgslabelengineconfigdialog.cpp
Expand Up @@ -48,9 +48,14 @@ QgsLabelEngineConfigDialog::QgsLabelEngineConfigDialog( QWidget *parent )

chkShowCandidates->setChecked( engineSettings.testFlag( QgsLabelingEngineSettings::DrawCandidates ) );
chkShowAllLabels->setChecked( engineSettings.testFlag( QgsLabelingEngineSettings::UseAllLabels ) );

chkShowUnplaced->setChecked( engineSettings.testFlag( QgsLabelingEngineSettings::DrawUnplacedLabels ) );
chkShowPartialsLabels->setChecked( engineSettings.testFlag( QgsLabelingEngineSettings::UsePartialCandidates ) );

mUnplacedColorButton->setColor( engineSettings.unplacedLabelColor() );
mUnplacedColorButton->setAllowOpacity( false );
mUnplacedColorButton->setDefaultColor( QColor( 255, 0, 0 ) );
mUnplacedColorButton->setWindowTitle( tr( "Unplaced Label Color" ) );

mTextRenderFormatComboBox->setCurrentIndex( mTextRenderFormatComboBox->findData( engineSettings.defaultTextRenderFormat() ) );
}

Expand All @@ -59,16 +64,19 @@ void QgsLabelEngineConfigDialog::onOK()
QgsLabelingEngineSettings engineSettings;

// save
engineSettings.setSearchMethod( ( QgsLabelingEngineSettings::Search ) cboSearchMethod->currentIndex() );
engineSettings.setSearchMethod( static_cast< QgsLabelingEngineSettings::Search >( cboSearchMethod->currentIndex() ) );

engineSettings.setNumCandidatePositions( spinCandPoint->value(), spinCandLine->value(), spinCandPolygon->value() );

engineSettings.setFlag( QgsLabelingEngineSettings::DrawCandidates, chkShowCandidates->isChecked() );
engineSettings.setFlag( QgsLabelingEngineSettings::UseAllLabels, chkShowAllLabels->isChecked() );
engineSettings.setFlag( QgsLabelingEngineSettings::DrawUnplacedLabels, chkShowUnplaced->isChecked() );
engineSettings.setFlag( QgsLabelingEngineSettings::UsePartialCandidates, chkShowPartialsLabels->isChecked() );

engineSettings.setDefaultTextRenderFormat( static_cast< QgsRenderContext::TextRenderFormat >( mTextRenderFormatComboBox->currentData().toInt() ) );

engineSettings.setUnplacedLabelColor( mUnplacedColorButton->color() );

QgsProject::instance()->setLabelingEngineSettings( engineSettings );

accept();
Expand Down
18 changes: 17 additions & 1 deletion src/core/qgslabelingenginesettings.cpp
Expand Up @@ -16,7 +16,7 @@
#include "qgslabelingenginesettings.h"

#include "qgsproject.h"

#include "qgssymbollayerutils.h"

QgsLabelingEngineSettings::QgsLabelingEngineSettings()
: mFlags( UsePartialCandidates )
Expand All @@ -41,6 +41,7 @@ void QgsLabelingEngineSettings::readSettingsFromProject( QgsProject *prj )
if ( prj->readBoolEntry( QStringLiteral( "PAL" ), QStringLiteral( "/DrawRectOnly" ), false, &saved ) ) mFlags |= DrawLabelRectOnly;
if ( prj->readBoolEntry( QStringLiteral( "PAL" ), QStringLiteral( "/ShowingAllLabels" ), false, &saved ) ) mFlags |= UseAllLabels;
if ( prj->readBoolEntry( QStringLiteral( "PAL" ), QStringLiteral( "/ShowingPartialsLabels" ), true, &saved ) ) mFlags |= UsePartialCandidates;
if ( prj->readBoolEntry( QStringLiteral( "PAL" ), QStringLiteral( "/DrawUnplaced" ), false, &saved ) ) mFlags |= DrawUnplacedLabels;

mDefaultTextRenderFormat = QgsRenderContext::TextFormatAlwaysOutlines;
// if users have disabled the older PAL "DrawOutlineLabels" setting, respect that
Expand All @@ -50,6 +51,8 @@ void QgsLabelingEngineSettings::readSettingsFromProject( QgsProject *prj )
const int projectTextFormat = prj->readNumEntry( QStringLiteral( "PAL" ), QStringLiteral( "/TextFormat" ), -1 );
if ( projectTextFormat >= 0 )
mDefaultTextRenderFormat = static_cast< QgsRenderContext::TextRenderFormat >( projectTextFormat );

mUnplacedLabelColor = QgsSymbolLayerUtils::decodeColor( prj->readEntry( QStringLiteral( "PAL" ), QStringLiteral( "/UnplacedColor" ), QStringLiteral( "#ff0000" ) ) );
}

void QgsLabelingEngineSettings::writeSettingsToProject( QgsProject *project )
Expand All @@ -61,10 +64,23 @@ void QgsLabelingEngineSettings::writeSettingsToProject( QgsProject *project )

project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/ShowingCandidates" ), mFlags.testFlag( DrawCandidates ) );
project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/DrawRectOnly" ), mFlags.testFlag( DrawLabelRectOnly ) );
project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/DrawUnplaced" ), mFlags.testFlag( DrawUnplacedLabels ) );
project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/ShowingAllLabels" ), mFlags.testFlag( UseAllLabels ) );
project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/ShowingPartialsLabels" ), mFlags.testFlag( UsePartialCandidates ) );

project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/TextFormat" ), static_cast< int >( mDefaultTextRenderFormat ) );

project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/UnplacedColor" ), QgsSymbolLayerUtils::encodeColor( mUnplacedLabelColor ) );
}

QColor QgsLabelingEngineSettings::unplacedLabelColor() const
{
return mUnplacedLabelColor;
}

void QgsLabelingEngineSettings::setUnplacedLabelColor( const QColor &unplacedLabelColor )
{
mUnplacedLabelColor = unplacedLabelColor;
}


19 changes: 19 additions & 0 deletions src/core/qgslabelingenginesettings.h
Expand Up @@ -38,6 +38,7 @@ class CORE_EXPORT QgsLabelingEngineSettings
RenderOutlineLabels = 1 << 3, //!< Whether to render labels as text or outlines. Deprecated and of QGIS 3.4.3 - use defaultTextRenderFormat() instead.
DrawLabelRectOnly = 1 << 4, //!< Whether to only draw the label rect and not the actual label text (used for unit tests)
DrawCandidates = 1 << 5, //!< Whether to draw rectangles of generated candidates (good for debugging)
DrawUnplacedLabels = 1 << 6, //!< Whether to render unplaced labels as an indicator/warning for users
};
Q_DECLARE_FLAGS( Flags, Flag )

Expand Down Expand Up @@ -109,6 +110,22 @@ class CORE_EXPORT QgsLabelingEngineSettings
mDefaultTextRenderFormat = format;
}

/**
* Returns the color to use when rendering unplaced labels.
*
* \see setUnplacedLabelColor()
* \since QGIS 3.10
*/
QColor unplacedLabelColor() const;

/**
* Sets the \a color to use when rendering unplaced labels.
*
* \see unplacedLabelColor()
* \since QGIS 3.10
*/
void setUnplacedLabelColor( const QColor &color );

private:
//! Flags
Flags mFlags;
Expand All @@ -117,6 +134,8 @@ class CORE_EXPORT QgsLabelingEngineSettings
//! Number of candedate positions that will be generated for features
int mCandPoint = 16, mCandLine = 50, mCandPolygon = 30;

QColor mUnplacedLabelColor = QColor( 255, 0, 0 );

QgsRenderContext::TextRenderFormat mDefaultTextRenderFormat = QgsRenderContext::TextFormatAlwaysOutlines;

};
Expand Down
96 changes: 76 additions & 20 deletions src/ui/qgslabelengineconfigdialog.ui
Expand Up @@ -205,25 +205,8 @@
</layout>
</item>
<item>
<layout class="QGridLayout" name="gridLayout_2" columnstretch="0,1">
<item row="0" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
<string>Text rendering</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="mTextRenderFormatComboBox"/>
</item>
<item row="1" column="0" colspan="2">
<widget class="QCheckBox" name="chkShowPartialsLabels">
<property name="text">
<string>Allow truncated labels on edges of map</string>
</property>
</widget>
</item>
<item row="2" column="0" colspan="2">
<layout class="QGridLayout" name="gridLayout_2" columnstretch="0,0,0">
<item row="2" column="0" colspan="3">
<widget class="QCheckBox" name="chkShowAllLabels">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
Expand All @@ -239,13 +222,76 @@
</property>
</widget>
</item>
<item row="3" column="0" colspan="2">
<item row="4" column="0" colspan="3">
<layout class="QHBoxLayout" name="horizontalLayout_3">
<property name="leftMargin">
<number>40</number>
</property>
<item>
<widget class="QLabel" name="label_6">
<property name="text">
<string>Color for unplaced labels</string>
</property>
</widget>
</item>
<item>
<widget class="QgsColorButton" name="mUnplacedColorButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>120</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>16777215</height>
</size>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
</layout>
</item>
<item row="0" column="1" colspan="2">
<widget class="QComboBox" name="mTextRenderFormatComboBox"/>
</item>
<item row="5" column="0" colspan="3">
<widget class="QCheckBox" name="chkShowCandidates">
<property name="text">
<string>Show candidates (for debugging)</string>
</property>
</widget>
</item>
<item row="1" column="0" colspan="3">
<widget class="QCheckBox" name="chkShowPartialsLabels">
<property name="text">
<string>Allow truncated labels on edges of map</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
<string>Text rendering</string>
</property>
</widget>
</item>
<item row="3" column="0" colspan="3">
<widget class="QCheckBox" name="chkShowUnplaced">
<property name="text">
<string>Show unplaced labels</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
Expand Down Expand Up @@ -273,6 +319,14 @@
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>QgsColorButton</class>
<extends>QToolButton</extends>
<header>qgscolorbutton.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>cboSearchMethod</tabstop>
<tabstop>spinCandPoint</tabstop>
Expand All @@ -281,6 +335,8 @@
<tabstop>mTextRenderFormatComboBox</tabstop>
<tabstop>chkShowPartialsLabels</tabstop>
<tabstop>chkShowAllLabels</tabstop>
<tabstop>chkShowUnplaced</tabstop>
<tabstop>mUnplacedColorButton</tabstop>
<tabstop>chkShowCandidates</tabstop>
</tabstops>
<resources/>
Expand Down
14 changes: 14 additions & 0 deletions tests/src/core/testqgslabelingengine.cpp
Expand Up @@ -123,18 +123,32 @@ void TestQgsLabelingEngine::testEngineSettings()
settings.setDefaultTextRenderFormat( QgsRenderContext::TextFormatAlwaysOutlines );
QCOMPARE( settings.defaultTextRenderFormat(), QgsRenderContext::TextFormatAlwaysOutlines );

settings.setFlag( QgsLabelingEngineSettings::DrawUnplacedLabels, true );
QVERIFY( settings.testFlag( QgsLabelingEngineSettings::DrawUnplacedLabels ) );
settings.setFlag( QgsLabelingEngineSettings::DrawUnplacedLabels, false );
QVERIFY( !settings.testFlag( QgsLabelingEngineSettings::DrawUnplacedLabels ) );

settings.setUnplacedLabelColor( QColor( 0, 255, 0 ) );
QCOMPARE( settings.unplacedLabelColor().name(), QStringLiteral( "#00ff00" ) );

// reading from project
QgsProject p;
settings.setDefaultTextRenderFormat( QgsRenderContext::TextFormatAlwaysText );
settings.setFlag( QgsLabelingEngineSettings::DrawUnplacedLabels, true );
settings.setUnplacedLabelColor( QColor( 0, 255, 0 ) );
settings.writeSettingsToProject( &p );
QgsLabelingEngineSettings settings2;
settings2.readSettingsFromProject( &p );
QCOMPARE( settings2.defaultTextRenderFormat(), QgsRenderContext::TextFormatAlwaysText );
QVERIFY( settings2.testFlag( QgsLabelingEngineSettings::DrawUnplacedLabels ) );
QCOMPARE( settings2.unplacedLabelColor().name(), QStringLiteral( "#00ff00" ) );

settings.setDefaultTextRenderFormat( QgsRenderContext::TextFormatAlwaysOutlines );
settings.setFlag( QgsLabelingEngineSettings::DrawUnplacedLabels, false );
settings.writeSettingsToProject( &p );
settings2.readSettingsFromProject( &p );
QCOMPARE( settings2.defaultTextRenderFormat(), QgsRenderContext::TextFormatAlwaysOutlines );
QVERIFY( !settings2.testFlag( QgsLabelingEngineSettings::DrawUnplacedLabels ) );

// test that older setting is still respected as a fallback
QgsProject p2;
Expand Down

0 comments on commit 39d4145

Please sign in to comment.