Skip to content
Permalink
Browse files

Add data defined option to always show a label, unless overridden

- Label is skipped during PAL priority calculation, permitting collisions and overlaps
- Overridden by layer and data defined 'scale/pixel size visibility' and data defined 'show label'  options
- Add option to Change Label tool
  • Loading branch information
dakcarto committed Nov 10, 2012
1 parent a7699e2 commit 15fe9926ce24b0fff67c073c2f78eb00a5351b88
@@ -64,6 +64,7 @@ class QgsPalLayerSettings
MaxScale,
FontTransp,
BufferTransp,
AlwaysShow,
PropertyCount, // keep last entry
};

@@ -507,6 +507,7 @@ QgsPalLayerSettings QgsLabelingGui::layerSettings()
setDataDefinedProperty( mMaxScaleAttributeComboBox, QgsPalLayerSettings::MaxScale, lyr );
setDataDefinedProperty( mTranspAttributeComboBox, QgsPalLayerSettings::FontTransp, lyr );
setDataDefinedProperty( mBufferTranspAttributeComboBox, QgsPalLayerSettings::BufferTransp, lyr );
setDataDefinedProperty( mAlwaysShowAttributeComboBox, QgsPalLayerSettings::AlwaysShow, lyr );

return lyr;
}
@@ -576,6 +577,7 @@ void QgsLabelingGui::populateDataDefinedCombos( QgsPalLayerSettings& s )
comboList << mMaxScaleAttributeComboBox;
comboList << mTranspAttributeComboBox;
comboList << mBufferTranspAttributeComboBox;
comboList << mAlwaysShowAttributeComboBox;

QList<QComboBox*>::iterator comboIt = comboList.begin();
for ( ; comboIt != comboList.end(); ++comboIt )
@@ -615,6 +617,7 @@ void QgsLabelingGui::populateDataDefinedCombos( QgsPalLayerSettings& s )
setCurrentComboValue( mMaxScaleAttributeComboBox, s, QgsPalLayerSettings::MaxScale );
setCurrentComboValue( mTranspAttributeComboBox, s, QgsPalLayerSettings::FontTransp );
setCurrentComboValue( mBufferTranspAttributeComboBox, s, QgsPalLayerSettings::BufferTransp );
setCurrentComboValue( mAlwaysShowAttributeComboBox, s, QgsPalLayerSettings::AlwaysShow );
}

void QgsLabelingGui::changeTextColor()
@@ -114,6 +114,10 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId )
mShowLabelChkbx->setEnabled( true );
mShowLabelChkbx->setChecked( attributeValues[propIt.value()].toInt() != 0 );
break;
case QgsPalLayerSettings::AlwaysShow:
mAlwaysShowChkbx->setEnabled( true );
mAlwaysShowChkbx->setChecked( attributeValues[propIt.value()].toBool() );
break;
case QgsPalLayerSettings::MinScale:
mMinScaleSpinBox->setEnabled( true );
mMinScaleSpinBox->setValue( attributeValues[propIt.value()].toInt() );
@@ -191,6 +195,7 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId )
void QgsLabelPropertyDialog::disableGuiElements()
{
mShowLabelChkbx->setEnabled( false );
mAlwaysShowChkbx->setEnabled( false );
mMinScaleSpinBox->setEnabled( false );
mMaxScaleSpinBox->setEnabled( false );
mFontSizeSpinBox->setEnabled( false );
@@ -209,6 +214,7 @@ void QgsLabelPropertyDialog::disableGuiElements()
void QgsLabelPropertyDialog::blockElementSignals( bool block )
{
mShowLabelChkbx->blockSignals( block );
mAlwaysShowChkbx->blockSignals( block );
mMinScaleSpinBox->blockSignals( block );
mMaxScaleSpinBox->blockSignals( block );
mFontSizeSpinBox->blockSignals( block );
@@ -243,6 +249,11 @@ void QgsLabelPropertyDialog::on_mShowLabelChkbx_toggled( bool chkd )
insertChangedValue( QgsPalLayerSettings::Show, ( chkd ? 1 : 0 ) );
}

void QgsLabelPropertyDialog::on_mAlwaysShowChkbx_toggled( bool chkd )
{
insertChangedValue( QgsPalLayerSettings::AlwaysShow, ( chkd ? 1 : 0 ) );
}

void QgsLabelPropertyDialog::on_mMinScaleSpinBox_valueChanged( int i )
{
insertChangedValue( QgsPalLayerSettings::MinScale, i );
@@ -38,6 +38,7 @@ class QgsLabelPropertyDialog: public QDialog, private Ui::QgsLabelPropertyDialog

private slots:
void on_mShowLabelChkbx_toggled( bool chkd );
void on_mAlwaysShowChkbx_toggled( bool chkd );
void on_mMinScaleSpinBox_valueChanged( int i );
void on_mMaxScaleSpinBox_valueChanged( int i );
void on_mLabelDistanceSpinBox_valueChanged( double d );
@@ -63,7 +63,7 @@ namespace pal
{
Feature::Feature( Layer* l, const char* geom_id, PalGeometry* userG, double lx, double ly )
: layer( l ), userGeom( userG ), label_x( lx ), label_y( ly ), distlabel( 0 ), labelInfo( NULL ), fixedPos( false ),
quadOffset( false ), offsetPos( false ), fixedRotation( false )
quadOffset( false ), offsetPos( false ), fixedRotation( false ), alwaysShow( false )
{
uid = new char[strlen( geom_id ) +1];
strcpy( uid, geom_id );
@@ -93,6 +93,7 @@ namespace pal
bool fixedPosition() const { return fixedPos; }
//Set label rotation to fixed value
void setFixedAngle( double a ) { fixedRotation = true; fixedAngle = a; }
void setAlwaysShow( bool bl ) { alwaysShow = bl; }

protected:
Layer *layer;
@@ -117,6 +118,8 @@ namespace pal
bool fixedRotation;
double fixedAngle; //fixed angle value (in rad)

bool alwaysShow; //true is label is to always be shown (but causes overlapping)

// array of parts - possibly not necessary
//int nPart;
//FeaturePart** parts;
@@ -279,6 +282,7 @@ namespace pal
bool getFixedRotation() { return f->fixedRotation; }
double getLabelAngle() { return f->fixedAngle; }
bool getFixedPosition() { return f->fixedPos; }
bool getAlwaysShow() { return f->alwaysShow; }

int getNumSelfObstacles() const { return nbHoles; }
PointSet* getSelfObstacle( int i ) { return holes[i]; }
@@ -228,7 +228,7 @@ namespace pal

bool Layer::registerFeature( const char *geom_id, PalGeometry *userGeom, double label_x, double label_y, const char* labelText,
double labelPosX, double labelPosY, bool fixedPos, double angle, bool fixedAngle,
int xQuadOffset, int yQuadOffset, double xOffset, double yOffset )
int xQuadOffset, int yQuadOffset, double xOffset, double yOffset, bool alwaysShow )
{
if ( !geom_id || label_x < 0 || label_y < 0 )
return false;
@@ -269,6 +269,8 @@ namespace pal
f->setFixedAngle( angle );
}

f->setAlwaysShow( alwaysShow );

bool first_feat = true;

double geom_size = -1, biggest_size = -1;
@@ -301,14 +301,23 @@ namespace pal
* @param labelPosX x position of the label (in case of fixed label position)
* @param labelPosY y position of the label (in case of fixed label position)
* @param fixedPos true if a single fixed position for this label is needed
* @param angle fixed angle (in radians) to rotate the label
* @param fixedAngle whether to use provided fixed angle
* @param xQuadOffset move label to quadrant: left, don't move, right (-1, 0, 1)
* @param yQuadOffset move label to quadrant: down, don't move, up (-1, 0, 1)
* @param xOffset map unit (+/-) to x-offset the label
* @param yOffset map unit (+/-) to y-offset the label
* @param alwaysShow whether to skip priority and always show the label (causes overlapping)
*
* @throws PalException::FeatureExists
*
* @return true on success (i.e. valid geometry)
*/
bool registerFeature( const char *geom_id, PalGeometry *userGeom, double label_x = -1, double label_y = -1,
const char* labelText = NULL, double labelPosX = 0.0, double labelPosY = 0.0, bool fixedPos = false, double angle = 0.0, bool fixedAngle = false,
int xQuadOffset = 0, int yQuadOffset = 0, double xOffset = 0.0, double yOffset = 0.0 );
const char* labelText = NULL, double labelPosX = 0.0, double labelPosY = 0.0,
bool fixedPos = false, double angle = 0.0, bool fixedAngle = false,
int xQuadOffset = 0, int yQuadOffset = 0, double xOffset = 0.0, double yOffset = 0.0,
bool alwaysShow = false );

/** return pointer to feature or NULL if doesn't exist */
Feature* getFeature( const char* geom_id );
@@ -2961,7 +2961,9 @@ namespace pal
{
solList->push_back( labelpositions[sol->s[i]] ); // active labels
}
else if ( returnInactive || labelpositions[featStartId[i]]->getFeaturePart()->getLayer()->getDisplayAll() )
else if ( returnInactive
|| labelpositions[featStartId[i]]->getFeaturePart()->getLayer()->getDisplayAll()
|| labelpositions[featStartId[i]]->getFeaturePart()->getAlwaysShow() )
{
solList->push_back( labelpositions[featStartId[i]] ); // unplaced label
}
@@ -385,6 +385,7 @@ static void _readDataDefinedPropertyMap( QgsVectorLayer* layer, QMap< QgsPalLaye
_readDataDefinedProperty( layer, QgsPalLayerSettings::MaxScale, propertyMap );
_readDataDefinedProperty( layer, QgsPalLayerSettings::FontTransp, propertyMap );
_readDataDefinedProperty( layer, QgsPalLayerSettings::BufferTransp, propertyMap );
_readDataDefinedProperty( layer, QgsPalLayerSettings::AlwaysShow, propertyMap );
}

void QgsPalLayerSettings::updateFontViaStyle( const QString & fontstyle )
@@ -933,6 +934,18 @@ void QgsPalLayerSettings::registerFeature( QgsVectorLayer* layer, QgsFeature& f
}
}

// data defined always show?
bool alwaysShow = false;
QMap< DataDefinedProperties, int >::const_iterator dAlwShowIt = dataDefinedProperties.find( QgsPalLayerSettings::AlwaysShow );
if ( dAlwShowIt != dataDefinedProperties.constEnd() )
{
QVariant alwShow = f.attributeMap().value( *dAlwShowIt );
if ( alwShow.isValid() )
{
alwaysShow = alwShow.toBool();
}
}

QgsPalGeometry* lbl = new QgsPalGeometry(
f.id(),
labelText,
@@ -949,7 +962,7 @@ void QgsPalLayerSettings::registerFeature( QgsVectorLayer* layer, QgsFeature& f
{
if ( !palLayer->registerFeature( lbl->strId(), lbl, labelX, labelY, labelText.toUtf8().constData(),
xPos, yPos, dataDefinedPosition, angle, dataDefinedRotation,
quadOffsetX, quadOffsetY, offsetX, offsetY ) )
quadOffsetX, quadOffsetY, offsetX, offsetY, alwaysShow ) )
return;
}
catch ( std::exception &e )
@@ -118,6 +118,7 @@ class CORE_EXPORT QgsPalLayerSettings
MaxScale,
FontTransp,
BufferTransp,
AlwaysShow,
PropertyCount, // keep last entry
};

@@ -449,7 +449,7 @@
<property name="geometry">
<rect>
<x>0</x>
<y>-365</y>
<y>0</y>
<width>686</width>
<height>714</height>
</rect>
@@ -2496,7 +2496,7 @@
<item row="0" column="0">
<widget class="QCheckBox" name="mPalShowAllLabelsForLayerChkBx">
<property name="text">
<string>Show all labels for this layer (i.e. including colliding labels)</string>
<string>Show all labels for this layer (including colliding labels)</string>
</property>
</widget>
</item>
@@ -2591,7 +2591,7 @@
<x>0</x>
<y>0</y>
<width>686</width>
<height>951</height>
<height>981</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_11">
@@ -2749,13 +2749,23 @@
</property>
<layout class="QGridLayout" name="gridLayout_16">
<item row="1" column="1">
<widget class="QComboBox" name="mAlwaysShowAttributeComboBox"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="mAlwaysShowLabel">
<property name="text">
<string>Always show</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QComboBox" name="mMinScaleAttributeComboBox">
<property name="enabled">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="0">
<item row="2" column="0">
<widget class="QLabel" name="mMinScaleLabel">
<property name="text">
<string>Minimum scale</string>
@@ -2779,14 +2789,14 @@
</property>
</widget>
</item>
<item row="2" column="0">
<item row="3" column="0">
<widget class="QLabel" name="mMaxScaleLabel">
<property name="text">
<string>Maximum scale</string>
</property>
</widget>
</item>
<item row="2" column="1">
<item row="3" column="1">
<widget class="QComboBox" name="mMaxScaleAttributeComboBox">
<property name="enabled">
<bool>true</bool>

0 comments on commit 15fe992

Please sign in to comment.
You can’t perform that action at this time.