Skip to content

Commit cfa3464

Browse files
committed
Merge pull request #905 from yellow-sky/show_partial_labels
Show partials labels
2 parents a95f07c + 3e382da commit cfa3464

17 files changed

+175
-16
lines changed

python/core/qgspallabeling.sip

+3
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,9 @@ class QgsPalLabeling : QgsLabelingEngineInterface
617617

618618
bool isShowingAllLabels() const;
619619
void setShowingAllLabels( bool showing );
620+
621+
bool isShowingPartialsLabels() const;
622+
void setShowingPartialsLabels( bool showing );
620623

621624
// implemented methods from labeling engine interface
622625

src/app/qgslabelengineconfigdialog.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ QgsLabelEngineConfigDialog::QgsLabelEngineConfigDialog( QgsPalLabeling* lbl, QWi
4343
mShadowDebugRectChkBox->setChecked( mLBL->isShowingShadowRectangles() );
4444

4545
mSaveWithProjectChkBox->setChecked( mLBL->isStoredWithProject() );
46+
47+
chkShowPartialsLabels->setChecked( mLBL-> isShowingPartialsLabels() );
4648
}
4749

4850

@@ -58,6 +60,7 @@ void QgsLabelEngineConfigDialog::onOK()
5860
mLBL->setShowingCandidates( chkShowCandidates->isChecked() );
5961
mLBL->setShowingShadowRectangles( mShadowDebugRectChkBox->isChecked() );
6062
mLBL->setShowingAllLabels( chkShowAllLabels->isChecked() );
63+
mLBL->setShowingPartialsLabels( chkShowPartialsLabels->isChecked() );
6164

6265
if ( mSaveWithProjectChkBox->isChecked() )
6366
{
@@ -80,4 +83,5 @@ void QgsLabelEngineConfigDialog::setDefaults()
8083
chkShowCandidates->setChecked( false );
8184
chkShowAllLabels->setChecked( false );
8285
mShadowDebugRectChkBox->setChecked( false );
86+
chkShowPartialsLabels->setChecked( p.getShowPartial() );
8387
}

src/core/pal/feature.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -1361,7 +1361,12 @@ namespace pal
13611361
// purge candidates that are outside the bbox
13621362
for ( i = 0; i < nbp; i++ )
13631363
{
1364-
if ( !( *lPos )[i]->isIn( bbox ) )
1364+
bool outside = false;
1365+
if ( f->layer->pal->getShowPartial() )
1366+
outside = !( *lPos )[i]->isIntersect( bbox );
1367+
else
1368+
outside = !( *lPos )[i]->isInside( bbox );
1369+
if ( outside )
13651370
{
13661371
rnbp--;
13671372
( *lPos )[i]->setCost( DBL_MAX ); // infinite cost => do not use

src/core/pal/labelposition.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,39 @@ namespace pal
184184
return false;
185185

186186
}
187+
188+
bool LabelPosition::isIntersect( double *bbox )
189+
{
190+
int i;
191+
192+
for ( i = 0; i < 4; i++ )
193+
{
194+
if ( x[i] >= bbox[0] && x[i] <= bbox[2] &&
195+
y[i] >= bbox[1] && y[i] <= bbox[3] )
196+
return true;
197+
}
198+
199+
if ( nextPart )
200+
return nextPart->isIntersect( bbox );
201+
else
202+
return false;
203+
}
204+
205+
bool LabelPosition::isInside( double *bbox )
206+
{
207+
for (int i = 0; i < 4; i++ )
208+
{
209+
if ( !( x[i] >= bbox[0] && x[i] <= bbox[2] &&
210+
y[i] >= bbox[1] && y[i] <= bbox[3] ) )
211+
return false;
212+
}
213+
214+
if ( nextPart )
215+
return nextPart->isInside( bbox );
216+
else
217+
return true;
218+
219+
}
187220

188221
void LabelPosition::print()
189222
{

src/core/pal/labelposition.h

+15-1
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,26 @@ namespace pal
109109

110110

111111
/**
112-
* \brief is the labelposition in the bounding-box ?
112+
* \brief Is the labelposition in the bounding-box ? (intersect or inside????)
113113
*
114114
*\param bbox the bounding-box double[4] = {xmin, ymin, xmax, ymax}
115115
*/
116116
bool isIn( double *bbox );
117117

118+
/**
119+
* \brief Is the labelposition intersect the bounding-box ?
120+
*
121+
*\param bbox the bounding-box double[4] = {xmin, ymin, xmax, ymax}
122+
*/
123+
bool isIntersect( double *bbox );
124+
125+
/**
126+
* \brief Is the labelposition inside the bounding-box ?
127+
*
128+
*\param bbox the bounding-box double[4] = {xmin, ymin, xmax, ymax}
129+
*/
130+
bool isInside( double *bbox );
131+
118132
/**
119133
* \brief Check whether or not this overlap with another labelPosition
120134
*

src/core/pal/pal.cpp

+13-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ namespace pal
106106
point_p = 8;
107107
line_p = 8;
108108
poly_p = 8;
109-
109+
110+
showPartial = true;
111+
110112
this->map_unit = pal::METER;
111113

112114
std::cout.precision( 12 );
@@ -899,6 +901,11 @@ namespace pal
899901
if ( dpi > 0 )
900902
this->dpi = dpi;
901903
}
904+
905+
void Pal::setShowPartial(bool show)
906+
{
907+
this->showPartial = show;
908+
}
902909

903910
int Pal::getPointP()
904911
{
@@ -929,6 +936,11 @@ namespace pal
929936
{
930937
return dpi;
931938
}
939+
940+
bool Pal::getShowPartial()
941+
{
942+
return showPartial;
943+
}
932944

933945
SearchMethod Pal::getSearch()
934946
{

src/core/pal/pal.h

+19-2
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ namespace pal
166166
int ejChainDeg;
167167
int tenure;
168168
double candListSize;
169+
170+
/**
171+
* \brief show partial labels (cut-off by the map canvas) or not
172+
*/
173+
bool showPartial;
169174

170175
/**
171176
* \brief Problem factory
@@ -352,8 +357,20 @@ namespace pal
352357
* @return map resolution (dot per inch)
353358
*/
354359
int getDpi();
355-
356-
360+
361+
/**
362+
*\brief Set flag show partial label
363+
*
364+
* @param show flag value
365+
*/
366+
void setShowPartial(bool show);
367+
368+
/**
369+
* \brief Get flag show partial label
370+
*
371+
* @return value of flag
372+
*/
373+
bool getShowPartial();
357374

358375
/**
359376
* \brief set # candidates to generate for points features

src/core/qgspallabeling.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -3049,6 +3049,7 @@ QgsPalLabeling::QgsPalLabeling()
30493049
mShowingCandidates = false;
30503050
mShowingShadowRects = false;
30513051
mShowingAllLabels = false;
3052+
mShowingPartialsLabels = p.getShowPartial();
30523053

30533054
mLabelSearchTree = new QgsLabelSearchTree();
30543055
}
@@ -3444,6 +3445,8 @@ void QgsPalLabeling::init( QgsMapRenderer* mr )
34443445
mPal->setLineP( mCandLine );
34453446
mPal->setPolyP( mCandPolygon );
34463447

3448+
mPal->setShowPartial( mShowingPartialsLabels );
3449+
34473450
clearActiveLayers(); // free any previous QgsDataDefined objects
34483451
mActiveDiagramLayers.clear();
34493452
}
@@ -4812,6 +4815,8 @@ void QgsPalLabeling::loadEngineSettings()
48124815
"PAL", "/ShowingShadowRects", false, &saved );
48134816
mShowingAllLabels = QgsProject::instance()->readBoolEntry(
48144817
"PAL", "/ShowingAllLabels", false, &saved );
4818+
mShowingPartialsLabels = QgsProject::instance()->readBoolEntry(
4819+
"PAL", "/ShowingPartialsLabels", p.getShowPartial(), &saved );
48154820
mSavedWithProject = saved;
48164821
}
48174822

@@ -4824,6 +4829,7 @@ void QgsPalLabeling::saveEngineSettings()
48244829
QgsProject::instance()->writeEntry( "PAL", "/ShowingCandidates", mShowingCandidates );
48254830
QgsProject::instance()->writeEntry( "PAL", "/ShowingShadowRects", mShowingShadowRects );
48264831
QgsProject::instance()->writeEntry( "PAL", "/ShowingAllLabels", mShowingAllLabels );
4832+
QgsProject::instance()->writeEntry( "PAL", "/ShowingPartialsLabels", mShowingPartialsLabels );
48274833
mSavedWithProject = true;
48284834
}
48294835

@@ -4836,6 +4842,7 @@ void QgsPalLabeling::clearEngineSettings()
48364842
QgsProject::instance()->removeEntry( "PAL", "/ShowingCandidates" );
48374843
QgsProject::instance()->removeEntry( "PAL", "/ShowingShadowRects" );
48384844
QgsProject::instance()->removeEntry( "PAL", "/ShowingAllLabels" );
4845+
QgsProject::instance()->removeEntry( "PAL", "/ShowingPartialsLabels" );
48394846
mSavedWithProject = false;
48404847
}
48414848

@@ -4845,5 +4852,6 @@ QgsLabelingEngineInterface* QgsPalLabeling::clone()
48454852
lbl->mShowingAllLabels = mShowingAllLabels;
48464853
lbl->mShowingCandidates = mShowingCandidates;
48474854
lbl->mShowingShadowRects = mShowingShadowRects;
4855+
lbl->mShowingPartialsLabels = mShowingPartialsLabels;
48484856
return lbl;
48494857
}

src/core/qgspallabeling.h

+4
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,9 @@ class CORE_EXPORT QgsPalLabeling : public QgsLabelingEngineInterface
685685
bool isShowingAllLabels() const { return mShowingAllLabels; }
686686
void setShowingAllLabels( bool showing ) { mShowingAllLabels = showing; }
687687

688+
bool isShowingPartialsLabels() const { return mShowingPartialsLabels; }
689+
void setShowingPartialsLabels( bool showing ) { mShowingPartialsLabels = showing; }
690+
688691
// implemented methods from labeling engine interface
689692

690693
//! called when we're going to start with rendering
@@ -781,6 +784,7 @@ class CORE_EXPORT QgsPalLabeling : public QgsLabelingEngineInterface
781784
bool mShowingAllLabels; // whether to avoid collisions or not
782785
bool mSavedWithProject; // whether engine settings have been read from project file
783786
bool mShowingShadowRects; // whether to show debugging rectangles for drop shadows
787+
bool mShowingPartialsLabels; // whether to avoid partials labels or not
784788

785789
QgsLabelSearchTree* mLabelSearchTree;
786790
};

src/mapserver/qgsprojectparser.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -3661,5 +3661,12 @@ void QgsProjectParser::loadLabelSettings( QgsLabelingEngineInterface* lbl )
36613661
{
36623662
pal->setShowingAllLabels( showAllLabelsElem.text().compare( "true", Qt::CaseInsensitive ) == 0 );
36633663
}
3664+
3665+
//mShowingPartialsLabels
3666+
QDomElement showPartialsLabelsElem = palElem.firstChildElement( "ShowingPartialsLabels" );
3667+
if ( !showPartialsLabelsElem.isNull() )
3668+
{
3669+
pal->setShowingPartialsLabels( showPartialsLabelsElem.text().compare( "true", Qt::CaseInsensitive ) == 0 );
3670+
}
36643671
}
36653672
}

src/ui/qgsengineconfigdialog.ui

+15-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
</size>
1818
</property>
1919
<property name="windowTitle">
20-
<string>Dialog</string>
20+
<string>Automated Placement Engine</string>
2121
</property>
2222
<layout class="QVBoxLayout" name="verticalLayout">
2323
<item>
@@ -215,7 +215,7 @@
215215
<property name="verticalSpacing">
216216
<number>6</number>
217217
</property>
218-
<item row="1" column="0">
218+
<item row="2" column="0">
219219
<spacer name="horizontalSpacer_3">
220220
<property name="orientation">
221221
<enum>Qt::Horizontal</enum>
@@ -231,7 +231,7 @@
231231
</property>
232232
</spacer>
233233
</item>
234-
<item row="0" column="0" colspan="3">
234+
<item row="1" column="0" colspan="3">
235235
<widget class="QCheckBox" name="chkShowAllLabels">
236236
<property name="sizePolicy">
237237
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
@@ -244,7 +244,7 @@
244244
</property>
245245
</widget>
246246
</item>
247-
<item row="4" column="0" colspan="3">
247+
<item row="5" column="0" colspan="3">
248248
<widget class="QCheckBox" name="mSaveWithProjectChkBox">
249249
<property name="layoutDirection">
250250
<enum>Qt::LeftToRight</enum>
@@ -257,14 +257,14 @@
257257
</property>
258258
</widget>
259259
</item>
260-
<item row="2" column="0" colspan="3">
260+
<item row="3" column="0" colspan="3">
261261
<widget class="QCheckBox" name="chkShowCandidates">
262262
<property name="text">
263263
<string>Show candidates (for debugging)</string>
264264
</property>
265265
</widget>
266266
</item>
267-
<item row="1" column="1">
267+
<item row="2" column="1">
268268
<widget class="QLabel" name="label_6">
269269
<property name="sizePolicy">
270270
<sizepolicy hsizetype="Maximum" vsizetype="Preferred">
@@ -277,7 +277,7 @@
277277
</property>
278278
</widget>
279279
</item>
280-
<item row="1" column="2">
280+
<item row="2" column="2">
281281
<spacer name="horizontalSpacer_4">
282282
<property name="orientation">
283283
<enum>Qt::Horizontal</enum>
@@ -290,13 +290,20 @@
290290
</property>
291291
</spacer>
292292
</item>
293-
<item row="3" column="0" colspan="3">
293+
<item row="4" column="0" colspan="3">
294294
<widget class="QCheckBox" name="mShadowDebugRectChkBox">
295295
<property name="text">
296296
<string>Show shadow rectangles (for debugging)</string>
297297
</property>
298298
</widget>
299299
</item>
300+
<item row="0" column="0" colspan="3">
301+
<widget class="QCheckBox" name="chkShowPartialsLabels">
302+
<property name="text">
303+
<string>Show partials labels</string>
304+
</property>
305+
</widget>
306+
</item>
300307
</layout>
301308
</item>
302309
<item>

tests/src/python/test_qgspallabeling_base.py

+26-3
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,18 @@ def setUpClass(cls):
106106
cls._MapRenderer.setDestinationCrs(cls._CRS)
107107
# use platform's native logical output dpi for QgsMapRenderer on launch
108108

109-
cls._Pal = QgsPalLabeling()
110-
cls._MapRenderer.setLabelingEngine(cls._Pal)
111-
cls._PalEngine = cls._MapRenderer.labelingEngine()
109+
cls.setDefaultEngineSettings()
112110
msg = ('\nCould not initialize PAL labeling engine, '
113111
'SKIPPING TEST SUITE')
114112
assert cls._PalEngine, msg
115113

114+
@classmethod
115+
def setDefaultEngineSettings(cls):
116+
"""Restore default settings for pal labelling"""
117+
cls._Pal = QgsPalLabeling()
118+
cls._MapRenderer.setLabelingEngine(cls._Pal)
119+
cls._PalEngine = cls._MapRenderer.labelingEngine()
120+
116121
@classmethod
117122
def tearDownClass(cls):
118123
"""Run after all tests"""
@@ -294,6 +299,24 @@ def test_write_read_settings(self):
294299

295300
msg = '\nLayer settings read not same as settings written'
296301
self.assertDictEqual(lyr1dict, lyr2dict, msg)
302+
303+
def test_default_partials_labels_enabled(self):
304+
# Verify ShowingPartialsLabels is enabled for PAL by default
305+
pal = QgsPalLabeling()
306+
self.assertTrue(pal.isShowingPartialsLabels())
307+
308+
def test_partials_labels_activate(self):
309+
pal = QgsPalLabeling()
310+
# Enable partials labels
311+
pal.setShowingPartialsLabels(True)
312+
self.assertTrue(pal.isShowingPartialsLabels())
313+
314+
def test_partials_labels_deactivate(self):
315+
pal = QgsPalLabeling()
316+
# Disable partials labels
317+
pal.setShowingPartialsLabels(False)
318+
self.assertFalse(pal.isShowingPartialsLabels())
319+
297320

298321

299322
def runSuite(module, tests):

0 commit comments

Comments
 (0)