Skip to content

Commit 31d1b95

Browse files
committedAug 31, 2012
Add word and letter spacing to PAL layer of adv labeling
- Caveat: does not work with curved line labels (yet) - Reorganize form widgets, moving mulit-line options to own groupbox - Character width option removed due to very erratic QFont::setStretch() bugs
1 parent 6028b81 commit 31d1b95

File tree

4 files changed

+307
-288
lines changed

4 files changed

+307
-288
lines changed
 

‎src/app/qgslabelinggui.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ QgsLabelingGui::QgsLabelingGui( QgsPalLabeling* lbl, QgsVectorLayer* layer, QgsM
4747

4848
connect( btnTextColor, SIGNAL( clicked() ), this, SLOT( changeTextColor() ) );
4949
connect( btnChangeFont, SIGNAL( clicked() ), this, SLOT( changeTextFont() ) );
50-
connect( mFontSizeUnitComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( updatePreview() ) );
5150
connect( mFontTranspSpinBox, SIGNAL( valueChanged( int ) ), this, SLOT( updatePreview() ) );
5251
connect( chkBuffer, SIGNAL( toggled( bool ) ), this, SLOT( updatePreview() ) );
5352
connect( btnBufferColor, SIGNAL( clicked() ), this, SLOT( changeBufferColor() ) );
@@ -512,6 +511,8 @@ void QgsLabelingGui::changeTextFont()
512511
{
513512
// store properties of QFont that might be stripped by font dialog
514513
QFont::Capitalization captials = mRefFont.capitalization();
514+
double wordspacing = mRefFont.wordSpacing();
515+
double letterspacing = mRefFont.letterSpacing();
515516

516517
bool ok;
517518
#if defined(Q_WS_MAC) && QT_VERSION >= 0x040500 && defined(QT_MAC_USE_COCOA)
@@ -534,6 +535,8 @@ void QgsLabelingGui::changeTextFont()
534535

535536
// reassign possibly stripped QFont properties
536537
font.setCapitalization( captials );
538+
font.setWordSpacing( wordspacing );
539+
font.setLetterSpacing( QFont::AbsoluteSpacing, letterspacing );
537540

538541
updateFont( font );
539542
}
@@ -569,6 +572,8 @@ void QgsLabelingGui::updateFontViaStyle( const QString & fontstyle )
569572
styledfont.setCapitalization( mRefFont.capitalization() );
570573
styledfont.setUnderline( mRefFont.underline() );
571574
styledfont.setStrikeOut( mRefFont.strikeOut() );
575+
styledfont.setWordSpacing( mRefFont.wordSpacing() );
576+
styledfont.setLetterSpacing( QFont::AbsoluteSpacing, mRefFont.letterSpacing() );
572577
mRefFont = styledfont;
573578
}
574579
// if no match, style combobox will be left blank, which should not affect engine labeling
@@ -611,6 +616,8 @@ void QgsLabelingGui::updateFont( QFont font )
611616
mFontCapitalsComboBox->setCurrentIndex( idx == -1 ? 0 : idx );
612617
mFontUnderlineBtn->setChecked( mRefFont.underline() );
613618
mFontStrikethroughBtn->setChecked( mRefFont.strikeOut() );
619+
mFontWordSpacingSpinBox->setValue( mRefFont.wordSpacing() );
620+
mFontLetterSpacingSpinBox->setValue( mRefFont.letterSpacing() );
614621
blockFontChangeSignals( false );
615622

616623
// update font name with font face
@@ -633,6 +640,7 @@ void QgsLabelingGui::updatePreview()
633640
lblFontPreview->setFont( mRefFont );
634641
QFont previewFont = lblFontPreview->font();
635642
double fontSize = mFontSizeSpinBox->value();
643+
double previewRatio = mPreviewSize / fontSize;
636644
double bufferSize = 0.0;
637645
QString grpboxtitle;
638646

@@ -643,11 +651,14 @@ void QgsLabelingGui::updatePreview()
643651
mPreviewSizeSlider->setEnabled( true );
644652
grpboxtitle = tr( "Sample @ %1 pts (using map units)" ).arg( mPreviewSize );
645653

654+
previewFont.setWordSpacing( previewRatio * mFontWordSpacingSpinBox->value() );
655+
previewFont.setLetterSpacing( QFont::AbsoluteSpacing, previewRatio * mFontLetterSpacingSpinBox->value() );
656+
646657
if ( chkBuffer->isChecked() )
647658
{
648659
if ( mBufferUnitComboBox->currentIndex() == 1 ) // map units
649660
{
650-
bufferSize = ( mPreviewSize / fontSize ) * spinBufferSize->value() / 2.5;
661+
bufferSize = previewRatio * spinBufferSize->value() / 3.527;
651662
}
652663
else // millimeters
653664
{
@@ -850,6 +861,15 @@ void QgsLabelingGui::on_mFontLetterSpacingSpinBox_valueChanged( double spacing )
850861
updateFont( mRefFont );
851862
}
852863

864+
void QgsLabelingGui::on_mFontSizeUnitComboBox_currentIndexChanged( int index )
865+
{
866+
double singleStep = ( index == 1 ) ? 10.0 : 1.0 ; //10 for map units, 1 for mm
867+
mFontSizeSpinBox->setSingleStep( singleStep );
868+
mFontWordSpacingSpinBox->setSingleStep( singleStep );
869+
mFontLetterSpacingSpinBox->setSingleStep( singleStep / 10 );
870+
updateFont( mRefFont );
871+
}
872+
853873
void QgsLabelingGui::on_mBufferUnitComboBox_currentIndexChanged( int index )
854874
{
855875
double singleStep = ( index == 1 ) ? 1.0 : 0.1 ; //1.0 for map units, 0.1 for mm

‎src/app/qgslabelinggui.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class QgsLabelingGui : public QWidget, private Ui::QgsLabelingGuiBase
5858
void on_mFontStrikethroughBtn_toggled( bool ckd );
5959
void on_mFontWordSpacingSpinBox_valueChanged( double spacing );
6060
void on_mFontLetterSpacingSpinBox_valueChanged( double spacing );
61+
void on_mFontSizeUnitComboBox_currentIndexChanged( int index );
6162
void on_mBufferUnitComboBox_currentIndexChanged( int index );
6263
void on_mXCoordinateComboBox_currentIndexChanged( const QString & text );
6364
void on_mYCoordinateComboBox_currentIndexChanged( const QString & text );

‎src/core/qgspallabeling.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,8 @@ void QgsPalLayerSettings::readFromLayer( QgsVectorLayer* layer )
354354
textFont.setCapitalization(( QFont::Capitalization ) layer->customProperty( "labeling/fontCapitals", QVariant( 0 ) ).toUInt() );
355355
textFont.setUnderline( layer->customProperty( "labeling/fontUnderline" ).toBool() );
356356
textFont.setStrikeOut( layer->customProperty( "labeling/fontStrikeout" ).toBool() );
357+
textFont.setLetterSpacing( QFont::AbsoluteSpacing, layer->customProperty( "labeling/fontLetterSpacing", QVariant( 0.0 ) ).toDouble() );
358+
textFont.setWordSpacing( layer->customProperty( "labeling/fontWordSpacing", QVariant( 0.0 ) ).toDouble() );
357359
textColor = _readColor( layer, "labeling/textColor" );
358360
textTransp = layer->customProperty( "labeling/textTransp" ).toInt();
359361
previewBkgrdColor = QColor( layer->customProperty( "labeling/previewBkgrdColor", "#ffffff" ).toString() );
@@ -401,6 +403,8 @@ void QgsPalLayerSettings::writeToLayer( QgsVectorLayer* layer )
401403
layer->setCustomProperty( "labeling/fontItalic", textFont.italic() );
402404
layer->setCustomProperty( "labeling/fontStrikeout", textFont.strikeOut() );
403405
layer->setCustomProperty( "labeling/fontUnderline", textFont.underline() );
406+
layer->setCustomProperty( "labeling/fontLetterSpacing", textFont.letterSpacing() );
407+
layer->setCustomProperty( "labeling/fontWordSpacing", textFont.wordSpacing() );
404408

405409
_writeColor( layer, "labeling/textColor", textColor );
406410
layer->setCustomProperty( "labeling/textTransp", textTransp );
@@ -974,6 +978,16 @@ int QgsPalLabeling::prepareLayer( QgsVectorLayer* layer, QSet<int>& attrIndices,
974978
{
975979
lyr.textFont.setPixelSize( pixelFontSize );
976980
}
981+
982+
if ( lyr.fontSizeInMapUnits )
983+
{
984+
double spacingPixelSize = lyr.textFont.wordSpacing() / ctx.mapToPixel().mapUnitsPerPixel() * ctx.rasterScaleFactor();
985+
lyr.textFont.setWordSpacing( spacingPixelSize );
986+
987+
spacingPixelSize = lyr.textFont.letterSpacing() / ctx.mapToPixel().mapUnitsPerPixel() * ctx.rasterScaleFactor();
988+
lyr.textFont.setLetterSpacing( QFont::AbsoluteSpacing, spacingPixelSize );
989+
}
990+
977991
//raster and vector scale factors
978992
lyr.vectorScaleFactor = ctx.scaleFactor();
979993
lyr.rasterCompressFactor = ctx.rasterScaleFactor();

0 commit comments

Comments
 (0)
Please sign in to comment.