Skip to content

Commit

Permalink
[FEATURE] Add label wrapping on character. Semi Fixes #3284
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanW2 committed Jan 6, 2012
1 parent 3f73c85 commit f57892c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/app/qgslabelinggui.cpp
Expand Up @@ -137,6 +137,7 @@ QgsLabelingGui::QgsLabelingGui( QgsPalLabeling* lbl, QgsVectorLayer* layer, QgsM
chkMergeLines->setChecked( lyr.mergeLines );
mMinSizeSpinBox->setValue( lyr.minFeatureSize );
chkAddDirectionSymbol->setChecked( lyr.addDirectionSymbol );
wrapCharacterEdit->setText( lyr.wrapChar );

bool scaleBased = ( lyr.scaleMin != 0 && lyr.scaleMax != 0 );
chkScaleBasedVisibility->setChecked( scaleBased );
Expand Down Expand Up @@ -320,6 +321,7 @@ QgsPalLayerSettings QgsLabelingGui::layerSettings()
}
lyr.minFeatureSize = mMinSizeSpinBox->value();
lyr.fontSizeInMapUnits = ( mFontSizeUnitComboBox->currentIndex() == 1 );
lyr.wrapChar = wrapCharacterEdit->text();

//data defined labeling
setDataDefinedProperty( mSizeAttributeComboBox, QgsPalLayerSettings::Size, lyr );
Expand Down
16 changes: 14 additions & 2 deletions src/core/qgspallabeling.cpp
Expand Up @@ -157,6 +157,7 @@ QgsPalLayerSettings::QgsPalLayerSettings()
addDirectionSymbol = false;
fontSizeInMapUnits = false;
distInMapUnits = false;
wrapChar = "";
}

QgsPalLayerSettings::QgsPalLayerSettings( const QgsPalLayerSettings& s )
Expand Down Expand Up @@ -187,6 +188,7 @@ QgsPalLayerSettings::QgsPalLayerSettings( const QgsPalLayerSettings& s )
addDirectionSymbol = s.addDirectionSymbol;
fontSizeInMapUnits = s.fontSizeInMapUnits;
distInMapUnits = s.distInMapUnits;
wrapChar = s.wrapChar;

dataDefinedProperties = s.dataDefinedProperties;
fontMetrics = NULL;
Expand Down Expand Up @@ -329,6 +331,7 @@ void QgsPalLayerSettings::readFromLayer( QgsVectorLayer* layer )
minFeatureSize = layer->customProperty( "labeling/minFeatureSize" ).toDouble();
fontSizeInMapUnits = layer->customProperty( "labeling/fontSizeInMapUnits" ).toBool();
distInMapUnits = layer->customProperty( "labeling/distInMapUnits" ).toBool();
wrapChar = layer->customProperty( "labeling/wrapChar" ).toString();
_readDataDefinedPropertyMap( layer, dataDefinedProperties );
}

Expand Down Expand Up @@ -367,6 +370,7 @@ void QgsPalLayerSettings::writeToLayer( QgsVectorLayer* layer )
layer->setCustomProperty( "labeling/minFeatureSize", minFeatureSize );
layer->setCustomProperty( "labeling/fontSizeInMapUnits", fontSizeInMapUnits );
layer->setCustomProperty( "labeling/distInMapUnits", distInMapUnits );
layer->setCustomProperty( "labeling/wrapChar", wrapChar );
_writeDataDefinedPropertyMap( layer, dataDefinedProperties );
}

Expand Down Expand Up @@ -432,7 +436,12 @@ void QgsPalLayerSettings::calculateLabelSize( const QFontMetricsF* fm, QString t
}

double w, h;
QStringList multiLineSplit = text.split( "\n" );
QStringList multiLineSplit;
if ( !wrapChar.isEmpty() )
multiLineSplit = text.split( wrapChar );
else
multiLineSplit = text.split( "\n" );

h = fm->height() * multiLineSplit.size() / rasterCompressFactor;
w = 0;
for ( int i = 0; i < multiLineSplit.size(); ++i )
Expand Down Expand Up @@ -1337,7 +1346,10 @@ void QgsPalLabeling::drawLabel( pal::LabelPosition* label, QPainter* painter, co
//QgsDebugMsg( "drawLabel " + QString::number( drawBuffer ) + " " + txt );

QStringList multiLineList;
multiLineList = txt.split( "\n" );
if ( !lyr.wrapChar.isEmpty() )
multiLineList = txt.split( lyr.wrapChar );
else
multiLineList = txt.split( "\n" );

for ( int i = 0; i < multiLineList.size(); ++i )
{
Expand Down
3 changes: 1 addition & 2 deletions src/core/qgspallabeling.h
Expand Up @@ -126,14 +126,13 @@ class CORE_EXPORT QgsPalLayerSettings
bool plusSign;
bool labelPerPart; // whether to label every feature's part or only the biggest one
bool mergeLines;
bool multiLineLabels; //draw labels on multiple lines if they contain '\n'
double minFeatureSize; // minimum feature size to be labelled (in mm)
// Adds '<' or '>' to the label string pointing to the direction of the line / polygon ring
// Works only if Placement == Line
bool addDirectionSymbol;
bool fontSizeInMapUnits; //true if font size is in map units (otherwise in points)
bool distInMapUnits; //true if distance is in map units (otherwise in mm)

QString wrapChar;
// called from register feature hook
void calculateLabelSize( const QFontMetricsF* fm, QString text, double& labelX, double& labelY );

Expand Down
36 changes: 30 additions & 6 deletions src/ui/qgslabelingguibase.ui
Expand Up @@ -473,8 +473,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>658</width>
<height>443</height>
<width>646</width>
<height>484</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_13">
Expand Down Expand Up @@ -547,7 +547,7 @@
</property>
</widget>
</item>
<item row="3" column="0">
<item row="4" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="label_19">
Expand All @@ -565,7 +565,7 @@
</item>
</layout>
</item>
<item row="4" column="0">
<item row="5" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_20">
<item>
<widget class="QCheckBox" name="chkNoObstacle">
Expand Down Expand Up @@ -602,6 +602,30 @@
</item>
</layout>
</item>
<item row="3" column="0">
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="topMargin">
<number>3</number>
</property>
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>Wrap label on character</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="wrapCharacterEdit">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
Expand Down Expand Up @@ -911,8 +935,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>249</width>
<height>567</height>
<width>646</width>
<height>585</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_11">
Expand Down

0 comments on commit f57892c

Please sign in to comment.