Skip to content

Commit

Permalink
[FEATURE][labeling] Data defined control over whether a feature
Browse files Browse the repository at this point in the history
acts as an obstacle for labels
  • Loading branch information
nyalldawson committed Jul 23, 2015
1 parent 1325b98 commit c97733e
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 26 deletions.
2 changes: 2 additions & 0 deletions python/core/qgspallabeling.sip
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ class QgsPalLayerSettings
FontLimitPixel,
FontMinPixel,
FontMaxPixel,
IsObstacle,

// (data defined only)
Show,
AlwaysShow
Expand Down
6 changes: 5 additions & 1 deletion src/app/qgslabelinggui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ QgsPalLayerSettings QgsLabelingGui::layerSettings()
lyr.previewBkgrdColor = mPreviewBackgroundBtn->color();

lyr.priority = mPrioritySlider->value();
lyr.obstacle = mChkNoObstacle->isChecked();
lyr.obstacle = mChkNoObstacle->isChecked() || mLabelModeComboBox->currentIndex() == 2;
lyr.obstacleType = ( QgsPalLayerSettings::ObstacleType )mObstacleTypeComboBox->itemData( mObstacleTypeComboBox->currentIndex() ).toInt();
lyr.labelPerPart = chkLabelPerFeaturePart->isChecked();
lyr.displayAll = mPalShowAllLabelsForLayerChkBx->isChecked();
Expand Down Expand Up @@ -856,6 +856,7 @@ QgsPalLayerSettings QgsLabelingGui::layerSettings()
setDataDefinedProperty( mFontMaxPixelDDBtn, QgsPalLayerSettings::FontMaxPixel, lyr );
setDataDefinedProperty( mShowLabelDDBtn, QgsPalLayerSettings::Show, lyr );
setDataDefinedProperty( mAlwaysShowDDBtn, QgsPalLayerSettings::AlwaysShow, lyr );
setDataDefinedProperty( mIsObstacleDDBtn, QgsPalLayerSettings::IsObstacle, lyr );

return lyr;
}
Expand Down Expand Up @@ -1121,6 +1122,9 @@ void QgsLabelingGui::populateDataDefinedButtons( QgsPalLayerSettings& s )

mAlwaysShowDDBtn->init( mLayer, s.dataDefinedProperty( QgsPalLayerSettings::AlwaysShow ),
QgsDataDefinedButton::AnyType, QgsDataDefinedButton::boolDesc() );

mIsObstacleDDBtn->init( mLayer, s.dataDefinedProperty( QgsPalLayerSettings::IsObstacle ),
QgsDataDefinedButton::AnyType, QgsDataDefinedButton::boolDesc() );
}

void QgsLabelingGui::changeTextColor( const QColor &color )
Expand Down
1 change: 1 addition & 0 deletions src/core/pal/feature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ namespace pal
, repeatDist( 0.0 )
, alwaysShow( false )
, mFixedQuadrant( false )
, mIsObstacle( true )
, mPriority( -1.0 )
{
assert( finite( lx ) && finite( ly ) );
Expand Down
15 changes: 15 additions & 0 deletions src/core/pal/feature.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ namespace pal
double repeatDistance() const { return repeatDist; }
void setAlwaysShow( bool bl ) { alwaysShow = bl; }

/** Sets whether the feature will act as an obstacle for labels.
* @param obstacle whether feature will act as an obstacle
* @see isObstacle
*/
void setIsObstacle( bool obstacle ) { mIsObstacle = obstacle; }

/** Returns whether the feature will act as an obstacle for labels.
* @returns true if feature is an obstacle
* @see setIsObstacle
*/
double isObstacle() const { return mIsObstacle; }

/** Sets the priority for labeling the feature.
* @param priority feature's priority, as a value between 0 (highest priority)
* and 1 (lowest priority). Set to -1.0 to use the layer's default priority
Expand Down Expand Up @@ -161,9 +173,12 @@ namespace pal
private:

bool mFixedQuadrant;
bool mIsObstacle;

//-1 if layer priority should be used
double mPriority;


};

/**
Expand Down
3 changes: 3 additions & 0 deletions src/core/pal/layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ namespace pal

f->setAlwaysShow( alwaysShow );

// feature inherits layer setting for acting as an obstacle
f->setIsObstacle( mObstacle );

bool first_feat = true;

double geom_size = -1, biggest_size = -1;
Expand Down
2 changes: 1 addition & 1 deletion src/core/pal/pal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ namespace pal
#endif

// all feature which are obstacle will be inserted into obstacles
if ( context->layer->obstacle() )
if ( ft_ptr->getFeature()->isObstacle() )
{
ft_ptr->getBoundingBox( amin, amax );
context->obstacles->Insert( amin, amax, ft_ptr );
Expand Down
7 changes: 7 additions & 0 deletions src/core/qgspallabeling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ QgsPalLayerSettings::QgsPalLayerSettings()
mDataDefinedNames.insert( RepeatDistance, QPair<QString, int>( "RepeatDistance", -1 ) );
mDataDefinedNames.insert( RepeatDistanceUnit, QPair<QString, int>( "RepeatDistanceUnit", -1 ) );
mDataDefinedNames.insert( Priority, QPair<QString, int>( "Priority", -1 ) );
mDataDefinedNames.insert( IsObstacle, QPair<QString, int>( "IsObstacle", -1 ) );

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

// data defined is obstacle?
if ( dataDefinedEvaluate( QgsPalLayerSettings::IsObstacle, exprVal ) )
{
feat->setIsObstacle( exprVal.toBool() );
}

//add parameters for data defined labeling to QgsPalGeometry
QMap< DataDefinedProperties, QVariant >::const_iterator dIt = dataDefinedValues.constBegin();
for ( ; dIt != dataDefinedValues.constEnd(); ++dIt )
Expand Down
2 changes: 2 additions & 0 deletions src/core/qgspallabeling.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ class CORE_EXPORT QgsPalLayerSettings
FontLimitPixel = 24,
FontMinPixel = 25,
FontMaxPixel = 26,
IsObstacle = 88,

// (data defined only)
Show = 15,
AlwaysShow = 20
Expand Down
80 changes: 56 additions & 24 deletions src/ui/qgslabelingguibase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -4818,9 +4818,9 @@ font-style: italic;</string>
<property name="geometry">
<rect>
<x>0</x>
<y>-298</y>
<y>-361</y>
<width>578</width>
<height>683</height>
<height>724</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_8">
Expand Down Expand Up @@ -5461,15 +5461,61 @@ font-style: italic;</string>
</layout>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<spacer name="verticalSpacer_5">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>0</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QgsCollapsibleGroupBox" name="mObstaclesGroupBox">
<property name="title">
<string>Obstacles</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_17">
<item>
<widget class="QCheckBox" name="mChkNoObstacle">
<property name="enabled">
<bool>true</bool>
</property>
<property name="text">
<string>Discourage labels from covering features</string>
</property>
</widget>
<layout class="QHBoxLayout" name="horizontalLayout_16">
<item>
<widget class="QCheckBox" name="mChkNoObstacle">
<property name="enabled">
<bool>true</bool>
</property>
<property name="text">
<string>Discourage labels from covering features</string>
</property>
</widget>
</item>
<item>
<widget class="QgsDataDefinedButton" name="mIsObstacleDDBtn">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_24">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<widget class="QFrame" name="mPolygonObstacleTypeFrame">
Expand Down Expand Up @@ -5512,19 +5558,6 @@ font-style: italic;</string>
</layout>
</widget>
</item>
<item>
<spacer name="verticalSpacer_5">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>0</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</widget>
Expand Down Expand Up @@ -5935,7 +5968,6 @@ font-style: italic;</string>
<tabstop>mLimitLabelChkBox</tabstop>
<tabstop>mLimitLabelSpinBox</tabstop>
<tabstop>mMinSizeSpinBox</tabstop>
<tabstop>mChkNoObstacle</tabstop>
<tabstop>mRenderingLabelGrpBx</tabstop>
</tabstops>
<resources>
Expand Down

0 comments on commit c97733e

Please sign in to comment.