Skip to content

Commit

Permalink
Add word and letter spacing to PAL layer of adv labeling
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
dakcarto committed Aug 31, 2012
1 parent 6028b81 commit 31d1b95
Show file tree
Hide file tree
Showing 4 changed files with 307 additions and 288 deletions.
24 changes: 22 additions & 2 deletions src/app/qgslabelinggui.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ QgsLabelingGui::QgsLabelingGui( QgsPalLabeling* lbl, QgsVectorLayer* layer, QgsM


connect( btnTextColor, SIGNAL( clicked() ), this, SLOT( changeTextColor() ) ); connect( btnTextColor, SIGNAL( clicked() ), this, SLOT( changeTextColor() ) );
connect( btnChangeFont, SIGNAL( clicked() ), this, SLOT( changeTextFont() ) ); connect( btnChangeFont, SIGNAL( clicked() ), this, SLOT( changeTextFont() ) );
connect( mFontSizeUnitComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( updatePreview() ) );
connect( mFontTranspSpinBox, SIGNAL( valueChanged( int ) ), this, SLOT( updatePreview() ) ); connect( mFontTranspSpinBox, SIGNAL( valueChanged( int ) ), this, SLOT( updatePreview() ) );
connect( chkBuffer, SIGNAL( toggled( bool ) ), this, SLOT( updatePreview() ) ); connect( chkBuffer, SIGNAL( toggled( bool ) ), this, SLOT( updatePreview() ) );
connect( btnBufferColor, SIGNAL( clicked() ), this, SLOT( changeBufferColor() ) ); connect( btnBufferColor, SIGNAL( clicked() ), this, SLOT( changeBufferColor() ) );
Expand Down Expand Up @@ -512,6 +511,8 @@ void QgsLabelingGui::changeTextFont()
{ {
// store properties of QFont that might be stripped by font dialog // store properties of QFont that might be stripped by font dialog
QFont::Capitalization captials = mRefFont.capitalization(); QFont::Capitalization captials = mRefFont.capitalization();
double wordspacing = mRefFont.wordSpacing();
double letterspacing = mRefFont.letterSpacing();


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


// reassign possibly stripped QFont properties // reassign possibly stripped QFont properties
font.setCapitalization( captials ); font.setCapitalization( captials );
font.setWordSpacing( wordspacing );
font.setLetterSpacing( QFont::AbsoluteSpacing, letterspacing );


updateFont( font ); updateFont( font );
} }
Expand Down Expand Up @@ -569,6 +572,8 @@ void QgsLabelingGui::updateFontViaStyle( const QString & fontstyle )
styledfont.setCapitalization( mRefFont.capitalization() ); styledfont.setCapitalization( mRefFont.capitalization() );
styledfont.setUnderline( mRefFont.underline() ); styledfont.setUnderline( mRefFont.underline() );
styledfont.setStrikeOut( mRefFont.strikeOut() ); styledfont.setStrikeOut( mRefFont.strikeOut() );
styledfont.setWordSpacing( mRefFont.wordSpacing() );
styledfont.setLetterSpacing( QFont::AbsoluteSpacing, mRefFont.letterSpacing() );
mRefFont = styledfont; mRefFont = styledfont;
} }
// if no match, style combobox will be left blank, which should not affect engine labeling // if no match, style combobox will be left blank, which should not affect engine labeling
Expand Down Expand Up @@ -611,6 +616,8 @@ void QgsLabelingGui::updateFont( QFont font )
mFontCapitalsComboBox->setCurrentIndex( idx == -1 ? 0 : idx ); mFontCapitalsComboBox->setCurrentIndex( idx == -1 ? 0 : idx );
mFontUnderlineBtn->setChecked( mRefFont.underline() ); mFontUnderlineBtn->setChecked( mRefFont.underline() );
mFontStrikethroughBtn->setChecked( mRefFont.strikeOut() ); mFontStrikethroughBtn->setChecked( mRefFont.strikeOut() );
mFontWordSpacingSpinBox->setValue( mRefFont.wordSpacing() );
mFontLetterSpacingSpinBox->setValue( mRefFont.letterSpacing() );
blockFontChangeSignals( false ); blockFontChangeSignals( false );


// update font name with font face // update font name with font face
Expand All @@ -633,6 +640,7 @@ void QgsLabelingGui::updatePreview()
lblFontPreview->setFont( mRefFont ); lblFontPreview->setFont( mRefFont );
QFont previewFont = lblFontPreview->font(); QFont previewFont = lblFontPreview->font();
double fontSize = mFontSizeSpinBox->value(); double fontSize = mFontSizeSpinBox->value();
double previewRatio = mPreviewSize / fontSize;
double bufferSize = 0.0; double bufferSize = 0.0;
QString grpboxtitle; QString grpboxtitle;


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


previewFont.setWordSpacing( previewRatio * mFontWordSpacingSpinBox->value() );
previewFont.setLetterSpacing( QFont::AbsoluteSpacing, previewRatio * mFontLetterSpacingSpinBox->value() );

if ( chkBuffer->isChecked() ) if ( chkBuffer->isChecked() )
{ {
if ( mBufferUnitComboBox->currentIndex() == 1 ) // map units if ( mBufferUnitComboBox->currentIndex() == 1 ) // map units
{ {
bufferSize = ( mPreviewSize / fontSize ) * spinBufferSize->value() / 2.5; bufferSize = previewRatio * spinBufferSize->value() / 3.527;
} }
else // millimeters else // millimeters
{ {
Expand Down Expand Up @@ -850,6 +861,15 @@ void QgsLabelingGui::on_mFontLetterSpacingSpinBox_valueChanged( double spacing )
updateFont( mRefFont ); updateFont( mRefFont );
} }


void QgsLabelingGui::on_mFontSizeUnitComboBox_currentIndexChanged( int index )
{
double singleStep = ( index == 1 ) ? 10.0 : 1.0 ; //10 for map units, 1 for mm
mFontSizeSpinBox->setSingleStep( singleStep );
mFontWordSpacingSpinBox->setSingleStep( singleStep );
mFontLetterSpacingSpinBox->setSingleStep( singleStep / 10 );
updateFont( mRefFont );
}

void QgsLabelingGui::on_mBufferUnitComboBox_currentIndexChanged( int index ) void QgsLabelingGui::on_mBufferUnitComboBox_currentIndexChanged( int index )
{ {
double singleStep = ( index == 1 ) ? 1.0 : 0.1 ; //1.0 for map units, 0.1 for mm double singleStep = ( index == 1 ) ? 1.0 : 0.1 ; //1.0 for map units, 0.1 for mm
Expand Down
1 change: 1 addition & 0 deletions src/app/qgslabelinggui.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class QgsLabelingGui : public QWidget, private Ui::QgsLabelingGuiBase
void on_mFontStrikethroughBtn_toggled( bool ckd ); void on_mFontStrikethroughBtn_toggled( bool ckd );
void on_mFontWordSpacingSpinBox_valueChanged( double spacing ); void on_mFontWordSpacingSpinBox_valueChanged( double spacing );
void on_mFontLetterSpacingSpinBox_valueChanged( double spacing ); void on_mFontLetterSpacingSpinBox_valueChanged( double spacing );
void on_mFontSizeUnitComboBox_currentIndexChanged( int index );
void on_mBufferUnitComboBox_currentIndexChanged( int index ); void on_mBufferUnitComboBox_currentIndexChanged( int index );
void on_mXCoordinateComboBox_currentIndexChanged( const QString & text ); void on_mXCoordinateComboBox_currentIndexChanged( const QString & text );
void on_mYCoordinateComboBox_currentIndexChanged( const QString & text ); void on_mYCoordinateComboBox_currentIndexChanged( const QString & text );
Expand Down
14 changes: 14 additions & 0 deletions src/core/qgspallabeling.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ void QgsPalLayerSettings::readFromLayer( QgsVectorLayer* layer )
textFont.setCapitalization(( QFont::Capitalization ) layer->customProperty( "labeling/fontCapitals", QVariant( 0 ) ).toUInt() ); textFont.setCapitalization(( QFont::Capitalization ) layer->customProperty( "labeling/fontCapitals", QVariant( 0 ) ).toUInt() );
textFont.setUnderline( layer->customProperty( "labeling/fontUnderline" ).toBool() ); textFont.setUnderline( layer->customProperty( "labeling/fontUnderline" ).toBool() );
textFont.setStrikeOut( layer->customProperty( "labeling/fontStrikeout" ).toBool() ); textFont.setStrikeOut( layer->customProperty( "labeling/fontStrikeout" ).toBool() );
textFont.setLetterSpacing( QFont::AbsoluteSpacing, layer->customProperty( "labeling/fontLetterSpacing", QVariant( 0.0 ) ).toDouble() );
textFont.setWordSpacing( layer->customProperty( "labeling/fontWordSpacing", QVariant( 0.0 ) ).toDouble() );
textColor = _readColor( layer, "labeling/textColor" ); textColor = _readColor( layer, "labeling/textColor" );
textTransp = layer->customProperty( "labeling/textTransp" ).toInt(); textTransp = layer->customProperty( "labeling/textTransp" ).toInt();
previewBkgrdColor = QColor( layer->customProperty( "labeling/previewBkgrdColor", "#ffffff" ).toString() ); previewBkgrdColor = QColor( layer->customProperty( "labeling/previewBkgrdColor", "#ffffff" ).toString() );
Expand Down Expand Up @@ -401,6 +403,8 @@ void QgsPalLayerSettings::writeToLayer( QgsVectorLayer* layer )
layer->setCustomProperty( "labeling/fontItalic", textFont.italic() ); layer->setCustomProperty( "labeling/fontItalic", textFont.italic() );
layer->setCustomProperty( "labeling/fontStrikeout", textFont.strikeOut() ); layer->setCustomProperty( "labeling/fontStrikeout", textFont.strikeOut() );
layer->setCustomProperty( "labeling/fontUnderline", textFont.underline() ); layer->setCustomProperty( "labeling/fontUnderline", textFont.underline() );
layer->setCustomProperty( "labeling/fontLetterSpacing", textFont.letterSpacing() );
layer->setCustomProperty( "labeling/fontWordSpacing", textFont.wordSpacing() );


_writeColor( layer, "labeling/textColor", textColor ); _writeColor( layer, "labeling/textColor", textColor );
layer->setCustomProperty( "labeling/textTransp", textTransp ); layer->setCustomProperty( "labeling/textTransp", textTransp );
Expand Down Expand Up @@ -974,6 +978,16 @@ int QgsPalLabeling::prepareLayer( QgsVectorLayer* layer, QSet<int>& attrIndices,
{ {
lyr.textFont.setPixelSize( pixelFontSize ); lyr.textFont.setPixelSize( pixelFontSize );
} }

if ( lyr.fontSizeInMapUnits )
{
double spacingPixelSize = lyr.textFont.wordSpacing() / ctx.mapToPixel().mapUnitsPerPixel() * ctx.rasterScaleFactor();
lyr.textFont.setWordSpacing( spacingPixelSize );

spacingPixelSize = lyr.textFont.letterSpacing() / ctx.mapToPixel().mapUnitsPerPixel() * ctx.rasterScaleFactor();
lyr.textFont.setLetterSpacing( QFont::AbsoluteSpacing, spacingPixelSize );
}

//raster and vector scale factors //raster and vector scale factors
lyr.vectorScaleFactor = ctx.scaleFactor(); lyr.vectorScaleFactor = ctx.scaleFactor();
lyr.rasterCompressFactor = ctx.rasterScaleFactor(); lyr.rasterCompressFactor = ctx.rasterScaleFactor();
Expand Down
Loading

0 comments on commit 31d1b95

Please sign in to comment.