Skip to content

Commit c97733e

Browse files
committed
[FEATURE][labeling] Data defined control over whether a feature
acts as an obstacle for labels
1 parent 1325b98 commit c97733e

File tree

9 files changed

+92
-26
lines changed

9 files changed

+92
-26
lines changed

python/core/qgspallabeling.sip

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,8 @@ class QgsPalLayerSettings
298298
FontLimitPixel,
299299
FontMinPixel,
300300
FontMaxPixel,
301+
IsObstacle,
302+
301303
// (data defined only)
302304
Show,
303305
AlwaysShow

src/app/qgslabelinggui.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ QgsPalLayerSettings QgsLabelingGui::layerSettings()
656656
lyr.previewBkgrdColor = mPreviewBackgroundBtn->color();
657657

658658
lyr.priority = mPrioritySlider->value();
659-
lyr.obstacle = mChkNoObstacle->isChecked();
659+
lyr.obstacle = mChkNoObstacle->isChecked() || mLabelModeComboBox->currentIndex() == 2;
660660
lyr.obstacleType = ( QgsPalLayerSettings::ObstacleType )mObstacleTypeComboBox->itemData( mObstacleTypeComboBox->currentIndex() ).toInt();
661661
lyr.labelPerPart = chkLabelPerFeaturePart->isChecked();
662662
lyr.displayAll = mPalShowAllLabelsForLayerChkBx->isChecked();
@@ -856,6 +856,7 @@ QgsPalLayerSettings QgsLabelingGui::layerSettings()
856856
setDataDefinedProperty( mFontMaxPixelDDBtn, QgsPalLayerSettings::FontMaxPixel, lyr );
857857
setDataDefinedProperty( mShowLabelDDBtn, QgsPalLayerSettings::Show, lyr );
858858
setDataDefinedProperty( mAlwaysShowDDBtn, QgsPalLayerSettings::AlwaysShow, lyr );
859+
setDataDefinedProperty( mIsObstacleDDBtn, QgsPalLayerSettings::IsObstacle, lyr );
859860

860861
return lyr;
861862
}
@@ -1121,6 +1122,9 @@ void QgsLabelingGui::populateDataDefinedButtons( QgsPalLayerSettings& s )
11211122

11221123
mAlwaysShowDDBtn->init( mLayer, s.dataDefinedProperty( QgsPalLayerSettings::AlwaysShow ),
11231124
QgsDataDefinedButton::AnyType, QgsDataDefinedButton::boolDesc() );
1125+
1126+
mIsObstacleDDBtn->init( mLayer, s.dataDefinedProperty( QgsPalLayerSettings::IsObstacle ),
1127+
QgsDataDefinedButton::AnyType, QgsDataDefinedButton::boolDesc() );
11241128
}
11251129

11261130
void QgsLabelingGui::changeTextColor( const QColor &color )

src/core/pal/feature.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ namespace pal
7575
, repeatDist( 0.0 )
7676
, alwaysShow( false )
7777
, mFixedQuadrant( false )
78+
, mIsObstacle( true )
7879
, mPriority( -1.0 )
7980
{
8081
assert( finite( lx ) && finite( ly ) );

src/core/pal/feature.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,18 @@ namespace pal
108108
double repeatDistance() const { return repeatDist; }
109109
void setAlwaysShow( bool bl ) { alwaysShow = bl; }
110110

111+
/** Sets whether the feature will act as an obstacle for labels.
112+
* @param obstacle whether feature will act as an obstacle
113+
* @see isObstacle
114+
*/
115+
void setIsObstacle( bool obstacle ) { mIsObstacle = obstacle; }
116+
117+
/** Returns whether the feature will act as an obstacle for labels.
118+
* @returns true if feature is an obstacle
119+
* @see setIsObstacle
120+
*/
121+
double isObstacle() const { return mIsObstacle; }
122+
111123
/** Sets the priority for labeling the feature.
112124
* @param priority feature's priority, as a value between 0 (highest priority)
113125
* and 1 (lowest priority). Set to -1.0 to use the layer's default priority
@@ -161,9 +173,12 @@ namespace pal
161173
private:
162174

163175
bool mFixedQuadrant;
176+
bool mIsObstacle;
164177

165178
//-1 if layer priority should be used
166179
double mPriority;
180+
181+
167182
};
168183

169184
/**

src/core/pal/layer.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ namespace pal
168168

169169
f->setAlwaysShow( alwaysShow );
170170

171+
// feature inherits layer setting for acting as an obstacle
172+
f->setIsObstacle( mObstacle );
173+
171174
bool first_feat = true;
172175

173176
double geom_size = -1, biggest_size = -1;

src/core/pal/pal.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ namespace pal
203203
#endif
204204

205205
// all feature which are obstacle will be inserted into obstacles
206-
if ( context->layer->obstacle() )
206+
if ( ft_ptr->getFeature()->isObstacle() )
207207
{
208208
ft_ptr->getBoundingBox( amin, amax );
209209
context->obstacles->Insert( amin, amax, ft_ptr );

src/core/qgspallabeling.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ QgsPalLayerSettings::QgsPalLayerSettings()
294294
mDataDefinedNames.insert( RepeatDistance, QPair<QString, int>( "RepeatDistance", -1 ) );
295295
mDataDefinedNames.insert( RepeatDistanceUnit, QPair<QString, int>( "RepeatDistanceUnit", -1 ) );
296296
mDataDefinedNames.insert( Priority, QPair<QString, int>( "Priority", -1 ) );
297+
mDataDefinedNames.insert( IsObstacle, QPair<QString, int>( "IsObstacle", -1 ) );
297298

298299
// (data defined only)
299300
mDataDefinedNames.insert( PositionX, QPair<QString, int>( "PositionX", 9 ) );
@@ -2207,6 +2208,12 @@ void QgsPalLayerSettings::registerFeature( QgsFeature& f, const QgsRenderContext
22072208
}
22082209
}
22092210

2211+
// data defined is obstacle?
2212+
if ( dataDefinedEvaluate( QgsPalLayerSettings::IsObstacle, exprVal ) )
2213+
{
2214+
feat->setIsObstacle( exprVal.toBool() );
2215+
}
2216+
22102217
//add parameters for data defined labeling to QgsPalGeometry
22112218
QMap< DataDefinedProperties, QVariant >::const_iterator dIt = dataDefinedValues.constBegin();
22122219
for ( ; dIt != dataDefinedValues.constEnd(); ++dIt )

src/core/qgspallabeling.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ class CORE_EXPORT QgsPalLayerSettings
273273
FontLimitPixel = 24,
274274
FontMinPixel = 25,
275275
FontMaxPixel = 26,
276+
IsObstacle = 88,
277+
276278
// (data defined only)
277279
Show = 15,
278280
AlwaysShow = 20

src/ui/qgslabelingguibase.ui

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4818,9 +4818,9 @@ font-style: italic;</string>
48184818
<property name="geometry">
48194819
<rect>
48204820
<x>0</x>
4821-
<y>-298</y>
4821+
<y>-361</y>
48224822
<width>578</width>
4823-
<height>683</height>
4823+
<height>724</height>
48244824
</rect>
48254825
</property>
48264826
<layout class="QVBoxLayout" name="verticalLayout_8">
@@ -5461,15 +5461,61 @@ font-style: italic;</string>
54615461
</layout>
54625462
</widget>
54635463
</item>
5464+
</layout>
5465+
</widget>
5466+
</item>
5467+
<item>
5468+
<spacer name="verticalSpacer_5">
5469+
<property name="orientation">
5470+
<enum>Qt::Vertical</enum>
5471+
</property>
5472+
<property name="sizeHint" stdset="0">
5473+
<size>
5474+
<width>20</width>
5475+
<height>0</height>
5476+
</size>
5477+
</property>
5478+
</spacer>
5479+
</item>
5480+
<item>
5481+
<widget class="QgsCollapsibleGroupBox" name="mObstaclesGroupBox">
5482+
<property name="title">
5483+
<string>Obstacles</string>
5484+
</property>
5485+
<layout class="QVBoxLayout" name="verticalLayout_17">
54645486
<item>
5465-
<widget class="QCheckBox" name="mChkNoObstacle">
5466-
<property name="enabled">
5467-
<bool>true</bool>
5468-
</property>
5469-
<property name="text">
5470-
<string>Discourage labels from covering features</string>
5471-
</property>
5472-
</widget>
5487+
<layout class="QHBoxLayout" name="horizontalLayout_16">
5488+
<item>
5489+
<widget class="QCheckBox" name="mChkNoObstacle">
5490+
<property name="enabled">
5491+
<bool>true</bool>
5492+
</property>
5493+
<property name="text">
5494+
<string>Discourage labels from covering features</string>
5495+
</property>
5496+
</widget>
5497+
</item>
5498+
<item>
5499+
<widget class="QgsDataDefinedButton" name="mIsObstacleDDBtn">
5500+
<property name="text">
5501+
<string>...</string>
5502+
</property>
5503+
</widget>
5504+
</item>
5505+
<item>
5506+
<spacer name="horizontalSpacer_24">
5507+
<property name="orientation">
5508+
<enum>Qt::Horizontal</enum>
5509+
</property>
5510+
<property name="sizeHint" stdset="0">
5511+
<size>
5512+
<width>40</width>
5513+
<height>20</height>
5514+
</size>
5515+
</property>
5516+
</spacer>
5517+
</item>
5518+
</layout>
54735519
</item>
54745520
<item>
54755521
<widget class="QFrame" name="mPolygonObstacleTypeFrame">
@@ -5512,19 +5558,6 @@ font-style: italic;</string>
55125558
</layout>
55135559
</widget>
55145560
</item>
5515-
<item>
5516-
<spacer name="verticalSpacer_5">
5517-
<property name="orientation">
5518-
<enum>Qt::Vertical</enum>
5519-
</property>
5520-
<property name="sizeHint" stdset="0">
5521-
<size>
5522-
<width>20</width>
5523-
<height>0</height>
5524-
</size>
5525-
</property>
5526-
</spacer>
5527-
</item>
55285561
</layout>
55295562
</widget>
55305563
</widget>
@@ -5935,7 +5968,6 @@ font-style: italic;</string>
59355968
<tabstop>mLimitLabelChkBox</tabstop>
59365969
<tabstop>mLimitLabelSpinBox</tabstop>
59375970
<tabstop>mMinSizeSpinBox</tabstop>
5938-
<tabstop>mChkNoObstacle</tabstop>
59395971
<tabstop>mRenderingLabelGrpBx</tabstop>
59405972
</tabstops>
59415973
<resources>

0 commit comments

Comments
 (0)