Skip to content

Commit e10c9f3

Browse files
committedMay 3, 2020
[labeling][FEATURE] Add a dedicated polygon placement mode for "outside"
When selected, labels will always be placed outside of polygons for the layer Sponsored by QGIS Swiss user group
1 parent 9fb85d3 commit e10c9f3

File tree

7 files changed

+64
-45
lines changed

7 files changed

+64
-45
lines changed
 

‎python/core/auto_generated/labeling/qgspallabeling.sip.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ class QgsPalLayerSettings
9999
Free,
100100
OrderedPositionsAroundPoint,
101101
PerimeterCurved,
102+
OutsidePolygons,
102103
};
103104

104105
enum PredefinedPointPosition

‎src/core/labeling/qgspallabeling.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ class CORE_EXPORT QgsPalLayerSettings
225225
Free, //!< Arranges candidates scattered throughout a polygon feature. Candidates are rotated to respect the polygon's orientation. Applies to polygon layers only.
226226
OrderedPositionsAroundPoint, //!< Candidates are placed in predefined positions around a point. Preference is given to positions with greatest cartographic appeal, e.g., top right, bottom right, etc. Applies to point layers only.
227227
PerimeterCurved, //!< Arranges candidates following the curvature of a polygon's boundary. Applies to polygon layers only.
228+
OutsidePolygons, //!< Candidates are placed outside of polygon boundaries. Applies to polygon layers only. Since QGIS 3.14
228229
};
229230

230231
//TODO QGIS 4.0 - move to QgsLabelingEngine

‎src/core/labeling/qgsvectorlayerlabeling.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ void QgsAbstractVectorLayerLabeling::writeTextSymbolizer( QDomNode &parent, QgsP
353353
break;
354354
case QgsPalLayerSettings::Horizontal:
355355
case QgsPalLayerSettings::Free:
356+
case QgsPalLayerSettings::OutsidePolygons:
356357
{
357358
// still a point placement (for "free" it's a fallback, there is no SLD equivalent)
358359
QDomElement pointPlacement = doc.createElement( "se:PointPlacement" );

‎src/core/pal/feature.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2034,7 +2034,7 @@ std::vector< std::unique_ptr< LabelPosition > > FeaturePart::createCandidates( P
20342034
const bool allowInside = mLF->polygonPlacementFlags() & QgsLabeling::PolygonPlacementFlag::AllowPlacementInsideOfPolygon;
20352035
//check width/height of bbox is sufficient for label
20362036

2037-
if ( allowOutside && !allowInside )
2037+
if ( ( allowOutside && !allowInside ) || ( mLF->layer()->arrangement() == QgsPalLayerSettings::OutsidePolygons ) )
20382038
{
20392039
// only allowed to place outside of polygon
20402040
createCandidatesOutsidePolygon( lPos, pal );

‎src/gui/labeling/qgslabelinggui.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,9 @@ void QgsLabelingGui::setLayer( QgsMapLayer *mapLayer )
327327
case QgsPalLayerSettings::PerimeterCurved:
328328
radPolygonPerimeterCurved->setChecked( true );
329329
break;
330+
case QgsPalLayerSettings::OutsidePolygons:
331+
radPolygonOutside->setChecked( true );
332+
break;
330333
}
331334

332335
// Label repeat distance
@@ -530,6 +533,10 @@ QgsPalLayerSettings QgsLabelingGui::layerSettings()
530533
{
531534
lyr.placement = QgsPalLayerSettings::Free;
532535
}
536+
else if ( radPolygonOutside->isChecked() )
537+
{
538+
lyr.placement = QgsPalLayerSettings::OutsidePolygons;
539+
}
533540
else
534541
{
535542
qFatal( "Invalid settings" );

‎src/gui/qgstextformatwidget.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ void QgsTextFormatWidget::initWidget()
286286
mPlacePolygonBtnGrp->addButton( radPolygonFree, static_cast<int>( QgsPalLayerSettings::Free ) );
287287
mPlacePolygonBtnGrp->addButton( radPolygonPerimeter, static_cast<int>( QgsPalLayerSettings::Line ) );
288288
mPlacePolygonBtnGrp->addButton( radPolygonPerimeterCurved, static_cast<int>( QgsPalLayerSettings::PerimeterCurved ) );
289+
mPlacePolygonBtnGrp->addButton( radPolygonOutside, static_cast<int>( QgsPalLayerSettings::OutsidePolygons ) );
289290
mPlacePolygonBtnGrp->setExclusive( true );
290291
connect( mPlacePolygonBtnGrp, static_cast<void ( QButtonGroup::* )( int )>( &QButtonGroup::buttonClicked ), this, &QgsTextFormatWidget::updatePlacementWidgets );
291292

@@ -459,6 +460,7 @@ void QgsTextFormatWidget::initWidget()
459460
<< radPolygonHorizontal
460461
<< radPolygonPerimeter
461462
<< radPolygonPerimeterCurved
463+
<< radPolygonOutside
462464
<< radPredefinedOrder
463465
<< mFieldExpressionWidget
464466
<< mCheckBoxSubstituteText
@@ -1275,7 +1277,7 @@ void QgsTextFormatWidget::updatePlacementWidgets()
12751277
bool showDistanceFrame = false;
12761278
bool showRotationFrame = false;
12771279
bool showMaxCharAngleFrame = false;
1278-
bool showPolygonPlacementOptions = ( curWdgt == pagePolygon && !radPolygonPerimeter->isChecked() && !radPolygonPerimeterCurved->isChecked() );
1280+
bool showPolygonPlacementOptions = ( curWdgt == pagePolygon && !radPolygonPerimeter->isChecked() && !radPolygonPerimeterCurved->isChecked() && !radPolygonOutside->isChecked() );
12791281

12801282
bool enableMultiLinesFrame = true;
12811283

‎src/ui/qgstextformatwidgetbase.ui

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>880</width>
9+
<width>612</width>
1010
<height>589</height>
1111
</rect>
1212
</property>
@@ -172,7 +172,7 @@
172172
<rect>
173173
<x>0</x>
174174
<y>0</y>
175-
<width>858</width>
175+
<width>590</width>
176176
<height>300</height>
177177
</rect>
178178
</property>
@@ -723,8 +723,8 @@
723723
<rect>
724724
<x>0</x>
725725
<y>0</y>
726-
<width>317</width>
727-
<height>260</height>
726+
<width>323</width>
727+
<height>292</height>
728728
</rect>
729729
</property>
730730
<layout class="QVBoxLayout" name="verticalLayout_2" stretch="0,1">
@@ -1303,8 +1303,8 @@ font-style: italic;</string>
13031303
<rect>
13041304
<x>0</x>
13051305
<y>0</y>
1306-
<width>348</width>
1307-
<height>624</height>
1306+
<width>373</width>
1307+
<height>708</height>
13081308
</rect>
13091309
</property>
13101310
<layout class="QGridLayout" name="gridLayout_42">
@@ -2187,8 +2187,8 @@ font-style: italic;</string>
21872187
<rect>
21882188
<x>0</x>
21892189
<y>0</y>
2190-
<width>284</width>
2191-
<height>273</height>
2190+
<width>299</width>
2191+
<height>308</height>
21922192
</rect>
21932193
</property>
21942194
<layout class="QVBoxLayout" name="verticalLayout_12">
@@ -2533,8 +2533,8 @@ font-style: italic;</string>
25332533
<rect>
25342534
<x>0</x>
25352535
<y>0</y>
2536-
<width>830</width>
2537-
<height>376</height>
2536+
<width>294</width>
2537+
<height>291</height>
25382538
</rect>
25392539
</property>
25402540
<layout class="QVBoxLayout" name="verticalLayout_121">
@@ -2811,8 +2811,8 @@ font-style: italic;</string>
28112811
<rect>
28122812
<x>0</x>
28132813
<y>0</y>
2814-
<width>816</width>
2815-
<height>696</height>
2814+
<width>440</width>
2815+
<height>786</height>
28162816
</rect>
28172817
</property>
28182818
<layout class="QVBoxLayout" name="verticalLayout_21">
@@ -3572,8 +3572,8 @@ font-style: italic;</string>
35723572
<rect>
35733573
<x>0</x>
35743574
<y>0</y>
3575-
<width>816</width>
3576-
<height>406</height>
3575+
<width>324</width>
3576+
<height>457</height>
35773577
</rect>
35783578
</property>
35793579
<layout class="QVBoxLayout" name="verticalLayout_22">
@@ -4000,8 +4000,8 @@ font-style: italic;</string>
40004000
<rect>
40014001
<x>0</x>
40024002
<y>0</y>
4003-
<width>830</width>
4004-
<height>376</height>
4003+
<width>833</width>
4004+
<height>368</height>
40054005
</rect>
40064006
</property>
40074007
<layout class="QGridLayout" name="gridLayout_46">
@@ -4150,8 +4150,8 @@ font-style: italic;</string>
41504150
<rect>
41514151
<x>0</x>
41524152
<y>0</y>
4153-
<width>431</width>
4154-
<height>1108</height>
4153+
<width>562</width>
4154+
<height>1266</height>
41554155
</rect>
41564156
</property>
41574157
<layout class="QVBoxLayout" name="verticalLayout_11">
@@ -4321,6 +4321,33 @@ font-style: italic;</string>
43214321
</widget>
43224322
<widget class="QWidget" name="pagePolygon">
43234323
<layout class="QGridLayout" name="gridLayout_18">
4324+
<item row="1" column="1">
4325+
<widget class="QRadioButton" name="radPolygonFree">
4326+
<property name="text">
4327+
<string>Free (angled)</string>
4328+
</property>
4329+
</widget>
4330+
</item>
4331+
<item row="0" column="2">
4332+
<widget class="QRadioButton" name="radPolygonOutside">
4333+
<property name="text">
4334+
<string>Outside polygons</string>
4335+
</property>
4336+
</widget>
4337+
</item>
4338+
<item row="2" column="1">
4339+
<widget class="QRadioButton" name="radPolygonPerimeterCurved">
4340+
<property name="sizePolicy">
4341+
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
4342+
<horstretch>0</horstretch>
4343+
<verstretch>0</verstretch>
4344+
</sizepolicy>
4345+
</property>
4346+
<property name="text">
4347+
<string>Using perimeter (curved)</string>
4348+
</property>
4349+
</widget>
4350+
</item>
43244351
<item row="0" column="0">
43254352
<widget class="QRadioButton" name="radOverCentroid">
43264353
<property name="sizePolicy">
@@ -4337,13 +4364,6 @@ font-style: italic;</string>
43374364
</property>
43384365
</widget>
43394366
</item>
4340-
<item row="0" column="1">
4341-
<widget class="QRadioButton" name="radPolygonHorizontal">
4342-
<property name="text">
4343-
<string>Horizontal</string>
4344-
</property>
4345-
</widget>
4346-
</item>
43474367
<item row="1" column="0">
43484368
<widget class="QRadioButton" name="radAroundCentroid">
43494369
<property name="sizePolicy">
@@ -4357,13 +4377,6 @@ font-style: italic;</string>
43574377
</property>
43584378
</widget>
43594379
</item>
4360-
<item row="1" column="1">
4361-
<widget class="QRadioButton" name="radPolygonFree">
4362-
<property name="text">
4363-
<string>Free</string>
4364-
</property>
4365-
</widget>
4366-
</item>
43674380
<item row="2" column="0">
43684381
<widget class="QRadioButton" name="radPolygonPerimeter">
43694382
<property name="sizePolicy">
@@ -4377,20 +4390,14 @@ font-style: italic;</string>
43774390
</property>
43784391
</widget>
43794392
</item>
4380-
<item row="2" column="1">
4381-
<widget class="QRadioButton" name="radPolygonPerimeterCurved">
4382-
<property name="sizePolicy">
4383-
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Fixed">
4384-
<horstretch>0</horstretch>
4385-
<verstretch>0</verstretch>
4386-
</sizepolicy>
4387-
</property>
4393+
<item row="0" column="1">
4394+
<widget class="QRadioButton" name="radPolygonHorizontal">
43884395
<property name="text">
4389-
<string>Using perimeter (curved)</string>
4396+
<string>Horizontal</string>
43904397
</property>
43914398
</widget>
43924399
</item>
4393-
<item row="1" column="2">
4400+
<item row="1" column="3">
43944401
<spacer name="horizontalSpacer_26">
43954402
<property name="orientation">
43964403
<enum>Qt::Horizontal</enum>
@@ -6039,7 +6046,7 @@ font-style: italic;</string>
60396046
<rect>
60406047
<x>0</x>
60416048
<y>0</y>
6042-
<width>819</width>
6049+
<width>430</width>
60436050
<height>708</height>
60446051
</rect>
60456052
</property>

0 commit comments

Comments
 (0)
Please sign in to comment.