Skip to content

Commit

Permalink
Add mode for drawing just label rects for debugging
Browse files Browse the repository at this point in the history
Sponsored by City of Uster
  • Loading branch information
nyalldawson committed Aug 28, 2015
1 parent 6bf5e91 commit 87022a4
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 7 deletions.
15 changes: 15 additions & 0 deletions python/core/qgspallabeling.sip
Expand Up @@ -714,6 +714,21 @@ class QgsPalLabeling : QgsLabelingEngineInterface
bool isDrawingOutlineLabels() const;
void setDrawingOutlineLabels( bool outline );

/** Returns whether the engine will only draw the outline rectangles of labels,
* not the label contents themselves. Used for debugging and testing purposes.
* @see setDrawLabelRectOnly
* @note added in QGIS 2.12
*/
bool drawLabelRectOnly() const;

/** Sets whether the engine should only draw the outline rectangles of labels,
* not the label contents themselves. Used for debugging and testing purposes.
* @param drawRect set to true to enable rect drawing only
* @see drawLabelRectOnly
* @note added in QGIS 2.12
*/
void setDrawLabelRectOnly( bool drawRect );

// implemented methods from labeling engine interface

//! called when we're going to start with rendering
Expand Down
52 changes: 45 additions & 7 deletions src/core/qgspallabeling.cpp
Expand Up @@ -3163,7 +3163,13 @@ double QgsPalLayerSettings::scaleToPixelContext( double size, const QgsRenderCon
// -------------

QgsPalLabeling::QgsPalLabeling()
: mMapSettings( NULL ), mPal( NULL )
: mMapSettings( NULL )
, mPal( NULL )
, mDrawLabelRectOnly( false )
, mShowingCandidates( false )
, mShowingAllLabels( false )
, mShowingShadowRects( false )
, mDrawOutlineLabels( true )
, mResults( 0 )
{

Expand All @@ -3172,6 +3178,7 @@ QgsPalLabeling::QgsPalLabeling()
mCandPoint = p.getPointP();
mCandLine = p.getLineP();
mCandPolygon = p.getPolyP();
mShowingPartialsLabels = p.getShowPartial();

switch ( p.getSearch() )
{
Expand All @@ -3181,12 +3188,6 @@ QgsPalLabeling::QgsPalLabeling()
case POPMUSIC_TABU_CHAIN: mSearch = Popmusic_Tabu_Chain; break;
case FALP: mSearch = Falp; break;
}

mShowingCandidates = false;
mShowingShadowRects = false;
mShowingAllLabels = false;
mShowingPartialsLabels = p.getShowPartial();
mDrawOutlineLabels = true;
}

QgsPalLabeling::~QgsPalLabeling()
Expand Down Expand Up @@ -4601,6 +4602,39 @@ void QgsPalLabeling::drawLabel( pal::LabelPosition* label, QgsRenderContext& con
component.setOrigin( outPt );
component.setRotation( label->getAlpha() );

if ( mDrawLabelRectOnly )
{
//debugging rect
if ( drawType != QgsPalLabeling::LabelText )
return;

QgsPoint outPt2 = xform.transform( label->getX() + label->getWidth(), label->getY() + label->getHeight() );
QRectF rect( 0, 0, outPt2.x() - outPt.x(), outPt2.y() - outPt.y() );
painter->save();
painter->setRenderHint( QPainter::Antialiasing, false );
painter->translate( QPointF( outPt.x(), outPt.y() ) );
painter->rotate( -label->getAlpha() * 180 / M_PI );

if ( label->conflictsWithObstacle() )
{
painter->setBrush( QColor( 255, 0, 0, 100 ) );
painter->setPen( QColor( 255, 0, 0, 150 ) );
}
else
{
painter->setBrush( QColor( 0, 255, 0, 100 ) );
painter->setPen( QColor( 0, 255, 0, 150 ) );
}

painter->drawRect( rect );
painter->restore();

if ( label->getNextPart() )
drawLabel( label->getNextPart(), context, tmpLyr, drawType, dpiRatio );

return;
}

if ( drawType == QgsPalLabeling::LabelShape )
{
// get rotated label's center point
Expand Down Expand Up @@ -5341,6 +5375,8 @@ void QgsPalLabeling::loadEngineSettings()
"PAL", "/CandidatesPolygon", p.getPolyP(), &saved );
mShowingCandidates = QgsProject::instance()->readBoolEntry(
"PAL", "/ShowingCandidates", false, &saved );
mDrawLabelRectOnly = QgsProject::instance()->readBoolEntry(
"PAL", "/DrawRectOnly", false, &saved );
mShowingShadowRects = QgsProject::instance()->readBoolEntry(
"PAL", "/ShowingShadowRects", false, &saved );
mShowingAllLabels = QgsProject::instance()->readBoolEntry(
Expand All @@ -5358,6 +5394,7 @@ void QgsPalLabeling::saveEngineSettings()
QgsProject::instance()->writeEntry( "PAL", "/CandidatesLine", mCandLine );
QgsProject::instance()->writeEntry( "PAL", "/CandidatesPolygon", mCandPolygon );
QgsProject::instance()->writeEntry( "PAL", "/ShowingCandidates", mShowingCandidates );
QgsProject::instance()->writeEntry( "PAL", "/DrawRectOnly", mDrawLabelRectOnly );
QgsProject::instance()->writeEntry( "PAL", "/ShowingShadowRects", mShowingShadowRects );
QgsProject::instance()->writeEntry( "PAL", "/ShowingAllLabels", mShowingAllLabels );
QgsProject::instance()->writeEntry( "PAL", "/ShowingPartialsLabels", mShowingPartialsLabels );
Expand All @@ -5382,6 +5419,7 @@ QgsLabelingEngineInterface* QgsPalLabeling::clone()
QgsPalLabeling* lbl = new QgsPalLabeling();
lbl->mShowingAllLabels = mShowingAllLabels;
lbl->mShowingCandidates = mShowingCandidates;
lbl->mDrawLabelRectOnly = mDrawLabelRectOnly;
lbl->mShowingShadowRects = mShowingShadowRects;
lbl->mShowingPartialsLabels = mShowingPartialsLabels;
lbl->mDrawOutlineLabels = mDrawOutlineLabels;
Expand Down
18 changes: 18 additions & 0 deletions src/core/qgspallabeling.h
Expand Up @@ -787,6 +787,21 @@ class CORE_EXPORT QgsPalLabeling : public QgsLabelingEngineInterface
bool isDrawingOutlineLabels() const { return mDrawOutlineLabels; }
void setDrawingOutlineLabels( bool outline ) { mDrawOutlineLabels = outline; }

/** Returns whether the engine will only draw the outline rectangles of labels,
* not the label contents themselves. Used for debugging and testing purposes.
* @see setDrawLabelRectOnly
* @note added in QGIS 2.12
*/
bool drawLabelRectOnly() const { return mDrawLabelRectOnly; }

/** Sets whether the engine should only draw the outline rectangles of labels,
* not the label contents themselves. Used for debugging and testing purposes.
* @param drawRect set to true to enable rect drawing only
* @see drawLabelRectOnly
* @note added in QGIS 2.12
*/
void setDrawLabelRectOnly( bool drawRect ) { mDrawLabelRectOnly = drawRect; }

// implemented methods from labeling engine interface

//! called when we're going to start with rendering
Expand Down Expand Up @@ -952,6 +967,9 @@ class CORE_EXPORT QgsPalLabeling : public QgsLabelingEngineInterface

// list of candidates from last labeling
QList<QgsLabelCandidate> mCandidates;

//! Whether to only draw the label rect and not the actual label text (used for unit tests)
bool mDrawLabelRectOnly;
bool mShowingCandidates;
bool mShowingAllLabels; // whether to avoid collisions or not
bool mShowingShadowRects; // whether to show debugging rectangles for drop shadows
Expand Down

0 comments on commit 87022a4

Please sign in to comment.