Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #905 from yellow-sky/show_partial_labels
Show partials labels
  • Loading branch information
dakcarto committed Sep 29, 2013
2 parents a95f07c + 3e382da commit cfa3464
Show file tree
Hide file tree
Showing 17 changed files with 175 additions and 16 deletions.
3 changes: 3 additions & 0 deletions python/core/qgspallabeling.sip
Expand Up @@ -617,6 +617,9 @@ class QgsPalLabeling : QgsLabelingEngineInterface

bool isShowingAllLabels() const;
void setShowingAllLabels( bool showing );

bool isShowingPartialsLabels() const;
void setShowingPartialsLabels( bool showing );

// implemented methods from labeling engine interface

Expand Down
4 changes: 4 additions & 0 deletions src/app/qgslabelengineconfigdialog.cpp
Expand Up @@ -43,6 +43,8 @@ QgsLabelEngineConfigDialog::QgsLabelEngineConfigDialog( QgsPalLabeling* lbl, QWi
mShadowDebugRectChkBox->setChecked( mLBL->isShowingShadowRectangles() );

mSaveWithProjectChkBox->setChecked( mLBL->isStoredWithProject() );

chkShowPartialsLabels->setChecked( mLBL-> isShowingPartialsLabels() );
}


Expand All @@ -58,6 +60,7 @@ void QgsLabelEngineConfigDialog::onOK()
mLBL->setShowingCandidates( chkShowCandidates->isChecked() );
mLBL->setShowingShadowRectangles( mShadowDebugRectChkBox->isChecked() );
mLBL->setShowingAllLabels( chkShowAllLabels->isChecked() );
mLBL->setShowingPartialsLabels( chkShowPartialsLabels->isChecked() );

if ( mSaveWithProjectChkBox->isChecked() )
{
Expand All @@ -80,4 +83,5 @@ void QgsLabelEngineConfigDialog::setDefaults()
chkShowCandidates->setChecked( false );
chkShowAllLabels->setChecked( false );
mShadowDebugRectChkBox->setChecked( false );
chkShowPartialsLabels->setChecked( p.getShowPartial() );
}
7 changes: 6 additions & 1 deletion src/core/pal/feature.cpp
Expand Up @@ -1361,7 +1361,12 @@ namespace pal
// purge candidates that are outside the bbox
for ( i = 0; i < nbp; i++ )
{
if ( !( *lPos )[i]->isIn( bbox ) )
bool outside = false;
if ( f->layer->pal->getShowPartial() )
outside = !( *lPos )[i]->isIntersect( bbox );
else
outside = !( *lPos )[i]->isInside( bbox );
if ( outside )
{
rnbp--;
( *lPos )[i]->setCost( DBL_MAX ); // infinite cost => do not use
Expand Down
33 changes: 33 additions & 0 deletions src/core/pal/labelposition.cpp
Expand Up @@ -184,6 +184,39 @@ namespace pal
return false;

}

bool LabelPosition::isIntersect( double *bbox )
{
int i;

for ( i = 0; i < 4; i++ )
{
if ( x[i] >= bbox[0] && x[i] <= bbox[2] &&
y[i] >= bbox[1] && y[i] <= bbox[3] )
return true;
}

if ( nextPart )
return nextPart->isIntersect( bbox );
else
return false;
}

bool LabelPosition::isInside( double *bbox )
{
for (int i = 0; i < 4; i++ )
{
if ( !( x[i] >= bbox[0] && x[i] <= bbox[2] &&
y[i] >= bbox[1] && y[i] <= bbox[3] ) )
return false;
}

if ( nextPart )
return nextPart->isInside( bbox );
else
return true;

}

void LabelPosition::print()
{
Expand Down
16 changes: 15 additions & 1 deletion src/core/pal/labelposition.h
Expand Up @@ -109,12 +109,26 @@ namespace pal


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

/**
* \brief Is the labelposition intersect the bounding-box ?
*
*\param bbox the bounding-box double[4] = {xmin, ymin, xmax, ymax}
*/
bool isIntersect( double *bbox );

/**
* \brief Is the labelposition inside the bounding-box ?
*
*\param bbox the bounding-box double[4] = {xmin, ymin, xmax, ymax}
*/
bool isInside( double *bbox );

/**
* \brief Check whether or not this overlap with another labelPosition
*
Expand Down
14 changes: 13 additions & 1 deletion src/core/pal/pal.cpp
Expand Up @@ -106,7 +106,9 @@ namespace pal
point_p = 8;
line_p = 8;
poly_p = 8;


showPartial = true;

this->map_unit = pal::METER;

std::cout.precision( 12 );
Expand Down Expand Up @@ -899,6 +901,11 @@ namespace pal
if ( dpi > 0 )
this->dpi = dpi;
}

void Pal::setShowPartial(bool show)
{
this->showPartial = show;
}

int Pal::getPointP()
{
Expand Down Expand Up @@ -929,6 +936,11 @@ namespace pal
{
return dpi;
}

bool Pal::getShowPartial()
{
return showPartial;
}

SearchMethod Pal::getSearch()
{
Expand Down
21 changes: 19 additions & 2 deletions src/core/pal/pal.h
Expand Up @@ -166,6 +166,11 @@ namespace pal
int ejChainDeg;
int tenure;
double candListSize;

/**
* \brief show partial labels (cut-off by the map canvas) or not
*/
bool showPartial;

/**
* \brief Problem factory
Expand Down Expand Up @@ -352,8 +357,20 @@ namespace pal
* @return map resolution (dot per inch)
*/
int getDpi();



/**
*\brief Set flag show partial label
*
* @param show flag value
*/
void setShowPartial(bool show);

/**
* \brief Get flag show partial label
*
* @return value of flag
*/
bool getShowPartial();

/**
* \brief set # candidates to generate for points features
Expand Down
8 changes: 8 additions & 0 deletions src/core/qgspallabeling.cpp
Expand Up @@ -3049,6 +3049,7 @@ QgsPalLabeling::QgsPalLabeling()
mShowingCandidates = false;
mShowingShadowRects = false;
mShowingAllLabels = false;
mShowingPartialsLabels = p.getShowPartial();

mLabelSearchTree = new QgsLabelSearchTree();
}
Expand Down Expand Up @@ -3444,6 +3445,8 @@ void QgsPalLabeling::init( QgsMapRenderer* mr )
mPal->setLineP( mCandLine );
mPal->setPolyP( mCandPolygon );

mPal->setShowPartial( mShowingPartialsLabels );

clearActiveLayers(); // free any previous QgsDataDefined objects
mActiveDiagramLayers.clear();
}
Expand Down Expand Up @@ -4812,6 +4815,8 @@ void QgsPalLabeling::loadEngineSettings()
"PAL", "/ShowingShadowRects", false, &saved );
mShowingAllLabels = QgsProject::instance()->readBoolEntry(
"PAL", "/ShowingAllLabels", false, &saved );
mShowingPartialsLabels = QgsProject::instance()->readBoolEntry(
"PAL", "/ShowingPartialsLabels", p.getShowPartial(), &saved );
mSavedWithProject = saved;
}

Expand All @@ -4824,6 +4829,7 @@ void QgsPalLabeling::saveEngineSettings()
QgsProject::instance()->writeEntry( "PAL", "/ShowingCandidates", mShowingCandidates );
QgsProject::instance()->writeEntry( "PAL", "/ShowingShadowRects", mShowingShadowRects );
QgsProject::instance()->writeEntry( "PAL", "/ShowingAllLabels", mShowingAllLabels );
QgsProject::instance()->writeEntry( "PAL", "/ShowingPartialsLabels", mShowingPartialsLabels );
mSavedWithProject = true;
}

Expand All @@ -4836,6 +4842,7 @@ void QgsPalLabeling::clearEngineSettings()
QgsProject::instance()->removeEntry( "PAL", "/ShowingCandidates" );
QgsProject::instance()->removeEntry( "PAL", "/ShowingShadowRects" );
QgsProject::instance()->removeEntry( "PAL", "/ShowingAllLabels" );
QgsProject::instance()->removeEntry( "PAL", "/ShowingPartialsLabels" );
mSavedWithProject = false;
}

Expand All @@ -4845,5 +4852,6 @@ QgsLabelingEngineInterface* QgsPalLabeling::clone()
lbl->mShowingAllLabels = mShowingAllLabels;
lbl->mShowingCandidates = mShowingCandidates;
lbl->mShowingShadowRects = mShowingShadowRects;
lbl->mShowingPartialsLabels = mShowingPartialsLabels;
return lbl;
}
4 changes: 4 additions & 0 deletions src/core/qgspallabeling.h
Expand Up @@ -685,6 +685,9 @@ class CORE_EXPORT QgsPalLabeling : public QgsLabelingEngineInterface
bool isShowingAllLabels() const { return mShowingAllLabels; }
void setShowingAllLabels( bool showing ) { mShowingAllLabels = showing; }

bool isShowingPartialsLabels() const { return mShowingPartialsLabels; }
void setShowingPartialsLabels( bool showing ) { mShowingPartialsLabels = showing; }

// implemented methods from labeling engine interface

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

QgsLabelSearchTree* mLabelSearchTree;
};
Expand Down
7 changes: 7 additions & 0 deletions src/mapserver/qgsprojectparser.cpp
Expand Up @@ -3661,5 +3661,12 @@ void QgsProjectParser::loadLabelSettings( QgsLabelingEngineInterface* lbl )
{
pal->setShowingAllLabels( showAllLabelsElem.text().compare( "true", Qt::CaseInsensitive ) == 0 );
}

//mShowingPartialsLabels
QDomElement showPartialsLabelsElem = palElem.firstChildElement( "ShowingPartialsLabels" );
if ( !showPartialsLabelsElem.isNull() )
{
pal->setShowingPartialsLabels( showPartialsLabelsElem.text().compare( "true", Qt::CaseInsensitive ) == 0 );
}
}
}
23 changes: 15 additions & 8 deletions src/ui/qgsengineconfigdialog.ui
Expand Up @@ -17,7 +17,7 @@
</size>
</property>
<property name="windowTitle">
<string>Dialog</string>
<string>Automated Placement Engine</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
Expand Down Expand Up @@ -215,7 +215,7 @@
<property name="verticalSpacing">
<number>6</number>
</property>
<item row="1" column="0">
<item row="2" column="0">
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
Expand All @@ -231,7 +231,7 @@
</property>
</spacer>
</item>
<item row="0" column="0" colspan="3">
<item row="1" column="0" colspan="3">
<widget class="QCheckBox" name="chkShowAllLabels">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
Expand All @@ -244,7 +244,7 @@
</property>
</widget>
</item>
<item row="4" column="0" colspan="3">
<item row="5" column="0" colspan="3">
<widget class="QCheckBox" name="mSaveWithProjectChkBox">
<property name="layoutDirection">
<enum>Qt::LeftToRight</enum>
Expand All @@ -257,14 +257,14 @@
</property>
</widget>
</item>
<item row="2" column="0" colspan="3">
<item row="3" 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="1">
<item row="2" column="1">
<widget class="QLabel" name="label_6">
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Preferred">
Expand All @@ -277,7 +277,7 @@
</property>
</widget>
</item>
<item row="1" column="2">
<item row="2" column="2">
<spacer name="horizontalSpacer_4">
<property name="orientation">
<enum>Qt::Horizontal</enum>
Expand All @@ -290,13 +290,20 @@
</property>
</spacer>
</item>
<item row="3" column="0" colspan="3">
<item row="4" column="0" colspan="3">
<widget class="QCheckBox" name="mShadowDebugRectChkBox">
<property name="text">
<string>Show shadow rectangles (for debugging)</string>
</property>
</widget>
</item>
<item row="0" column="0" colspan="3">
<widget class="QCheckBox" name="chkShowPartialsLabels">
<property name="text">
<string>Show partials labels</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
Expand Down
29 changes: 26 additions & 3 deletions tests/src/python/test_qgspallabeling_base.py
Expand Up @@ -106,13 +106,18 @@ def setUpClass(cls):
cls._MapRenderer.setDestinationCrs(cls._CRS)
# use platform's native logical output dpi for QgsMapRenderer on launch

cls._Pal = QgsPalLabeling()
cls._MapRenderer.setLabelingEngine(cls._Pal)
cls._PalEngine = cls._MapRenderer.labelingEngine()
cls.setDefaultEngineSettings()
msg = ('\nCould not initialize PAL labeling engine, '
'SKIPPING TEST SUITE')
assert cls._PalEngine, msg

@classmethod
def setDefaultEngineSettings(cls):
"""Restore default settings for pal labelling"""
cls._Pal = QgsPalLabeling()
cls._MapRenderer.setLabelingEngine(cls._Pal)
cls._PalEngine = cls._MapRenderer.labelingEngine()

@classmethod
def tearDownClass(cls):
"""Run after all tests"""
Expand Down Expand Up @@ -294,6 +299,24 @@ def test_write_read_settings(self):

msg = '\nLayer settings read not same as settings written'
self.assertDictEqual(lyr1dict, lyr2dict, msg)

def test_default_partials_labels_enabled(self):
# Verify ShowingPartialsLabels is enabled for PAL by default
pal = QgsPalLabeling()
self.assertTrue(pal.isShowingPartialsLabels())

def test_partials_labels_activate(self):
pal = QgsPalLabeling()
# Enable partials labels
pal.setShowingPartialsLabels(True)
self.assertTrue(pal.isShowingPartialsLabels())

def test_partials_labels_deactivate(self):
pal = QgsPalLabeling()
# Disable partials labels
pal.setShowingPartialsLabels(False)
self.assertFalse(pal.isShowingPartialsLabels())



def runSuite(module, tests):
Expand Down

0 comments on commit cfa3464

Please sign in to comment.