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
Expand Up @@ -298,6 +298,8 @@ class QgsPalLayerSettings
FontLimitPixel, FontLimitPixel,
FontMinPixel, FontMinPixel,
FontMaxPixel, FontMaxPixel,
IsObstacle,

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


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


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


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

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


void QgsLabelingGui::changeTextColor( const QColor &color ) void QgsLabelingGui::changeTextColor( const QColor &color )
Expand Down
1 change: 1 addition & 0 deletions src/core/pal/feature.cpp
Expand Up @@ -75,6 +75,7 @@ namespace pal
, repeatDist( 0.0 ) , repeatDist( 0.0 )
, alwaysShow( false ) , alwaysShow( false )
, mFixedQuadrant( false ) , mFixedQuadrant( false )
, mIsObstacle( true )
, mPriority( -1.0 ) , mPriority( -1.0 )
{ {
assert( finite( lx ) && finite( ly ) ); assert( finite( lx ) && finite( ly ) );
Expand Down
15 changes: 15 additions & 0 deletions src/core/pal/feature.h
Expand Up @@ -108,6 +108,18 @@ namespace pal
double repeatDistance() const { return repeatDist; } double repeatDistance() const { return repeatDist; }
void setAlwaysShow( bool bl ) { alwaysShow = bl; } 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. /** Sets the priority for labeling the feature.
* @param priority feature's priority, as a value between 0 (highest priority) * @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 * 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: private:


bool mFixedQuadrant; bool mFixedQuadrant;
bool mIsObstacle;


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


}; };


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


f->setAlwaysShow( alwaysShow ); f->setAlwaysShow( alwaysShow );


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

bool first_feat = true; bool first_feat = true;


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


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


// (data defined only) // (data defined only)
mDataDefinedNames.insert( PositionX, QPair<QString, int>( "PositionX", 9 ) ); 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 //add parameters for data defined labeling to QgsPalGeometry
QMap< DataDefinedProperties, QVariant >::const_iterator dIt = dataDefinedValues.constBegin(); QMap< DataDefinedProperties, QVariant >::const_iterator dIt = dataDefinedValues.constBegin();
for ( ; dIt != dataDefinedValues.constEnd(); ++dIt ) for ( ; dIt != dataDefinedValues.constEnd(); ++dIt )
Expand Down
2 changes: 2 additions & 0 deletions src/core/qgspallabeling.h
Expand Up @@ -273,6 +273,8 @@ class CORE_EXPORT QgsPalLayerSettings
FontLimitPixel = 24, FontLimitPixel = 24,
FontMinPixel = 25, FontMinPixel = 25,
FontMaxPixel = 26, FontMaxPixel = 26,
IsObstacle = 88,

// (data defined only) // (data defined only)
Show = 15, Show = 15,
AlwaysShow = 20 AlwaysShow = 20
Expand Down
80 changes: 56 additions & 24 deletions src/ui/qgslabelingguibase.ui
Expand Up @@ -4818,9 +4818,9 @@ font-style: italic;</string>
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>0</x> <x>0</x>
<y>-298</y> <y>-361</y>
<width>578</width> <width>578</width>
<height>683</height> <height>724</height>
</rect> </rect>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout_8"> <layout class="QVBoxLayout" name="verticalLayout_8">
Expand Down Expand Up @@ -5461,15 +5461,61 @@ font-style: italic;</string>
</layout> </layout>
</widget> </widget>
</item> </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> <item>
<widget class="QCheckBox" name="mChkNoObstacle"> <layout class="QHBoxLayout" name="horizontalLayout_16">
<property name="enabled"> <item>
<bool>true</bool> <widget class="QCheckBox" name="mChkNoObstacle">
</property> <property name="enabled">
<property name="text"> <bool>true</bool>
<string>Discourage labels from covering features</string> </property>
</property> <property name="text">
</widget> <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>
<item> <item>
<widget class="QFrame" name="mPolygonObstacleTypeFrame"> <widget class="QFrame" name="mPolygonObstacleTypeFrame">
Expand Down Expand Up @@ -5512,19 +5558,6 @@ font-style: italic;</string>
</layout> </layout>
</widget> </widget>
</item> </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> </layout>
</widget> </widget>
</widget> </widget>
Expand Down Expand Up @@ -5935,7 +5968,6 @@ font-style: italic;</string>
<tabstop>mLimitLabelChkBox</tabstop> <tabstop>mLimitLabelChkBox</tabstop>
<tabstop>mLimitLabelSpinBox</tabstop> <tabstop>mLimitLabelSpinBox</tabstop>
<tabstop>mMinSizeSpinBox</tabstop> <tabstop>mMinSizeSpinBox</tabstop>
<tabstop>mChkNoObstacle</tabstop>
<tabstop>mRenderingLabelGrpBx</tabstop> <tabstop>mRenderingLabelGrpBx</tabstop>
</tabstops> </tabstops>
<resources> <resources>
Expand Down

0 comments on commit c97733e

Please sign in to comment.