Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add map units, join styles and transp. fill to buffer options in adv …
…labeling

- Preview now shows when interior of buffer is set to transparent
- Text size in map units are now not reset to pts when choosing a font
- Preview background is now saved with PAL settings per layer
- Add groupbox title warning about missmatch of unit type between text and buffer
- Preview now does not show buffer if it is in map units and the text is in pts
- Preview now shows map units text/buffer visual ratio within reason (looks like map labels)
- Preview can now be dynamically scaled between 8 and 152 pts size
- Defaults for new setting methods set to match previous defaults (backwards compat.)
  • Loading branch information
dakcarto committed Aug 16, 2012
1 parent ac4486a commit 7079f20
Show file tree
Hide file tree
Showing 7 changed files with 858 additions and 666 deletions.
146 changes: 103 additions & 43 deletions src/app/qgslabelinggui.cpp
Expand Up @@ -43,14 +43,18 @@ QgsLabelingGui::QgsLabelingGui( QgsPalLabeling* lbl, QgsVectorLayer* layer, QgsM
setupUi( this );

mRefFont = lblFontPreview->font();
mPreviewBackgroundBtn->setColor( Qt::white );
connect( mPreviewBackgroundBtn, SIGNAL( clicked() ), this, SLOT( changePreviewBackground( ) ) );
mPreviewSize = 24;

connect( btnTextColor, SIGNAL( clicked() ), this, SLOT( changeTextColor() ) );
connect( btnChangeFont, SIGNAL( clicked() ), this, SLOT( changeTextFont() ) );
connect( mFontSizeUnitComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( updatePreview() ) );
connect( mFontTranspSpinBox, SIGNAL( valueChanged( int ) ), this, SLOT( updatePreview() ) );
connect( chkBuffer, SIGNAL( toggled( bool ) ), this, SLOT( updatePreview() ) );
connect( btnBufferColor, SIGNAL( clicked() ), this, SLOT( changeBufferColor() ) );
connect( spinBufferSize, SIGNAL( valueChanged( double ) ), this, SLOT( updatePreview() ) );
connect( mBufferTranspSpinBox, SIGNAL( valueChanged( int ) ), this, SLOT( updatePreview() ) );
connect( mBufferJoinStyleComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( updatePreview() ) );
connect( mBufferTranspFillChbx, SIGNAL( toggled( bool ) ), this, SLOT( updatePreview() ) );
connect( btnEngineSettings, SIGNAL( clicked() ), this, SLOT( showEngineConfigDialog() ) );
connect( btnExpression, SIGNAL( clicked() ), this, SLOT( showExpressionDialog() ) );

Expand Down Expand Up @@ -146,6 +150,9 @@ QgsLabelingGui::QgsLabelingGui( QgsPalLabeling* lbl, QgsVectorLayer* layer, QgsM
wrapCharacterEdit->setText( lyr.wrapChar );
chkPreserveRotation->setChecked( lyr.preserveRotation );

mPreviewBackgroundBtn->setColor( lyr.previewBkgrdColor );
setPreviewBackground( lyr.previewBkgrdColor );

bool scaleBased = ( lyr.scaleMin != 0 && lyr.scaleMax != 0 );
chkScaleBasedVisibility->setChecked( scaleBased );
if ( scaleBased )
Expand All @@ -157,12 +164,24 @@ QgsLabelingGui::QgsLabelingGui( QgsPalLabeling* lbl, QgsVectorLayer* layer, QgsM
bool buffer = ( lyr.bufferSize != 0 );
chkBuffer->setChecked( buffer );
if ( buffer )
{
spinBufferSize->setValue( lyr.bufferSize );
if ( lyr.bufferSizeInMapUnits )
{
mBufferUnitComboBox->setCurrentIndex( 1 );
}
else
{
mBufferUnitComboBox->setCurrentIndex( 0 );
}
btnBufferColor->setColor( lyr.bufferColor );
mBufferTranspSpinBox->setValue( lyr.bufferTransp );
mBufferJoinStyleComboBox->setPenJoinStyle( lyr.bufferJoinStyle );
mBufferTranspFillChbx->setChecked( !lyr.bufferNoFill );
}

btnTextColor->setColor( lyr.textColor );
mFontTranspSpinBox->setValue( lyr.textTransp );
btnBufferColor->setColor( lyr.bufferColor );
mBufferTranspSpinBox->setValue( lyr.bufferTransp );

bool formattedNumbers = lyr.formatNumbers;
bool plusSign = lyr.plusSign;
Expand All @@ -177,7 +196,6 @@ QgsLabelingGui::QgsLabelingGui( QgsPalLabeling* lbl, QgsVectorLayer* layer, QgsM
chkPlusSign->setChecked( plusSign );
}


if ( lyr.fontSizeInMapUnits )
{
mFontSizeUnitComboBox->setCurrentIndex( 1 );
Expand Down Expand Up @@ -285,6 +303,7 @@ QgsPalLayerSettings QgsLabelingGui::layerSettings()
lyr.textColor = btnTextColor->color();
lyr.textFont = mRefFont;
lyr.textTransp = mFontTranspSpinBox->value();
lyr.previewBkgrdColor = mPreviewBackgroundBtn->color();
lyr.enabled = chkEnableLabeling->isChecked();
lyr.priority = sliderPriority->value();
lyr.obstacle = !chkNoObstacle->isChecked();
Expand All @@ -304,6 +323,9 @@ QgsPalLayerSettings QgsLabelingGui::layerSettings()
lyr.bufferSize = spinBufferSize->value();
lyr.bufferColor = btnBufferColor->color();
lyr.bufferTransp = mBufferTranspSpinBox->value();
lyr.bufferSizeInMapUnits = ( mBufferUnitComboBox->currentIndex() == 1 );
lyr.bufferJoinStyle = mBufferJoinStyleComboBox->penJoinStyle();
lyr.bufferNoFill = !mBufferTranspFillChbx->isChecked();
}
else
{
Expand Down Expand Up @@ -438,6 +460,7 @@ void QgsLabelingGui::populateDataDefinedCombos( QgsPalLayerSettings& s )
( *comboIt )->addItem( "", QVariant() );
}

// TODO: don't add field that aren't of appropriate type for the data defined property
const QgsFieldMap& fields = mLayer->dataProvider()->fields();
for ( QgsFieldMap::const_iterator it = fields.constBegin(); it != fields.constEnd(); it++ )
{
Expand Down Expand Up @@ -471,18 +494,6 @@ void QgsLabelingGui::populateDataDefinedCombos( QgsPalLayerSettings& s )
setCurrentComboValue( mBufferTranspAttributeComboBox, s, QgsPalLayerSettings::BufferTransp );
}

void QgsLabelingGui::changePreviewBackground()
{
QColor color = QColorDialog::getColor( mPreviewBackgroundBtn->color(), this );
if ( !color.isValid() )
return;

mPreviewBackgroundBtn->setColor( color );
scrollArea_mPreview->widget()->setStyleSheet( QString( "background: rgb(%1, %2, %3);" ).arg( QString::number( color.red() ),
QString::number( color.green() ),
QString::number( color.blue() ) ) );
}

void QgsLabelingGui::changeTextColor()
{
QColor color = QColorDialog::getColor( btnTextColor->color(), this );
Expand All @@ -504,9 +515,17 @@ void QgsLabelingGui::changeTextFont()
#endif
if ( ok )
{
if ( mFontSizeUnitComboBox->currentIndex() == 1 )
{
// don't override map units size with selected size from font dialog
font.setPointSizeF( mFontSizeSpinBox->value() );
}
else
{
mFontSizeSpinBox->setValue( font.pointSizeF() );
}
updateFont( font );
}
mFontSizeSpinBox->setValue( mRefFont.pointSizeF() );
}

void QgsLabelingGui::updateFont( QFont font )
Expand All @@ -517,12 +536,14 @@ void QgsLabelingGui::updateFont( QFont font )
mRefFont = font;
}

QString fontSizeUnitString = tr( "pt" );
if ( mFontSizeUnitComboBox->currentIndex() == 1 )
{
fontSizeUnitString = tr( "map units" );
}
lblFontName->setText( QString( "%1, %2 %3" ).arg( font.family() ).arg( mRefFont.pointSizeF() ).arg( fontSizeUnitString ) );
lblFontName->setText( QString( "%1" ).arg( font.family() ) );

// update font name with font face
// QString dupFont = font.toString();
// QFont lblFont = lblFontName->font();
// lblFont.fromString( dupFont );
// lblFont.setPointSizeF( 14 );
// lblFontName->setFont(lblFont);

updatePreview();
}
Expand All @@ -532,31 +553,55 @@ void QgsLabelingGui::updatePreview()
scrollPreview();
lblFontPreview->setFont( mRefFont );
QFont previewFont = lblFontPreview->font();
double fontSize = mFontSizeSpinBox->value();
double bufferSize = spinBufferSize->value();
if ( mFontSizeUnitComboBox->currentIndex() == 1 )
{
// TODO: maybe match current map zoom level instead?
previewFont.setPointSize( 24 );
groupBox_mPreview->setTitle( tr( "Sample @ 24 pts (using map units)" ) );
previewFont.setPointSize( mPreviewSize );
mPreviewSizeSlider->setEnabled( true );
if ( mBufferUnitComboBox->currentIndex() == 1 )
{
groupBox_mPreview->setTitle( tr( "Sample @ %1 pts (using map units)" ).arg( mPreviewSize ) );
bufferSize = ( mPreviewSize / fontSize ) * bufferSize / 2.5;
}
else
{
groupBox_mPreview->setTitle( tr( "Sample @ %1 pts (using map units, STROKE IN mm)" ).arg( mPreviewSize ) );
}
}
else
{
previewFont.setPointSize( mFontSizeSpinBox->value() );
groupBox_mPreview->setTitle( tr( "Sample" ) );
mPreviewSizeSlider->setEnabled( false );
if ( mBufferUnitComboBox->currentIndex() == 1 )
{
groupBox_mPreview->setTitle( tr( "Sample (stroke in map units, NOT SHOWN)" ) );
bufferSize = 0;
}
else
{
previewFont.setPointSize( fontSize );
groupBox_mPreview->setTitle( tr( "Sample" ) );
}
}
lblFontPreview->setFont( previewFont );

QColor prevColor = btnTextColor->color();
prevColor.setAlphaF(( 100.0 - ( double )( mFontTranspSpinBox->value() ) ) / 100.0 );
lblFontPreview->setTextColor( prevColor );

bool bufferNoFill = false;
if ( chkBuffer->isChecked() )
{
QColor buffColor = btnBufferColor->color();
buffColor.setAlphaF(( 100.0 - ( double )( mBufferTranspSpinBox->value() ) ) / 100.0 );
lblFontPreview->setBuffer( spinBufferSize->value(), buffColor );

bufferNoFill = !mBufferTranspFillChbx->isChecked();
lblFontPreview->setBuffer( bufferSize, buffColor, mBufferJoinStyleComboBox->penJoinStyle(), bufferNoFill );
}
else
{
lblFontPreview->setBuffer( 0, Qt::white );
lblFontPreview->setBuffer( 0, Qt::white, Qt::BevelJoin, bufferNoFill );
}
}

Expand All @@ -565,6 +610,13 @@ void QgsLabelingGui::scrollPreview()
scrollArea_mPreview->ensureVisible( 0, 0, 0, 0 );
}

void QgsLabelingGui::setPreviewBackground( QColor color )
{
scrollArea_mPreview->widget()->setStyleSheet( QString( "background: rgb(%1, %2, %3);" ).arg( QString::number( color.red() ),
QString::number( color.green() ),
QString::number( color.blue() ) ) );
}

void QgsLabelingGui::showEngineConfigDialog()
{
QgsLabelEngineConfigDialog dlg( mLBL, this );
Expand Down Expand Up @@ -593,6 +645,9 @@ void QgsLabelingGui::updateUi()
bool buf = chkBuffer->isChecked();
spinBufferSize->setEnabled( buf );
btnBufferColor->setEnabled( buf );
mBufferUnitComboBox->setEnabled( buf );
mBufferTranspSlider->setEnabled( buf );
mBufferTranspSpinBox->setEnabled( buf );

bool scale = chkScaleBasedVisibility->isChecked();
spinScaleMin->setEnabled( scale );
Expand Down Expand Up @@ -633,21 +688,15 @@ void QgsLabelingGui::updateOptions()
}
}

void QgsLabelingGui::on_mFontSizeSpinBox_valueChanged( double d )
{
mRefFont.setPointSizeF( d );
updateFont( mRefFont );
}

void QgsLabelingGui::on_mFontSizeUnitComboBox_currentIndexChanged( int index )
void QgsLabelingGui::on_mPreviewSizeSlider_valueChanged( int i )
{
Q_UNUSED( index );
updateFont( mRefFont );
mPreviewSize = i;
updatePreview();
}

void QgsLabelingGui::on_mFontTranspSpinBox_valueChanged( int i )
void QgsLabelingGui::on_mFontSizeSpinBox_valueChanged( double d )
{
Q_UNUSED( i );
mRefFont.setPointSizeF( d );
updateFont( mRefFont );
}

Expand All @@ -663,9 +712,10 @@ void QgsLabelingGui::on_mFontLetterSpacingSpinBox_valueChanged( double spacing )
updateFont( mRefFont );
}

void QgsLabelingGui::on_mBufferTranspSpinBox_valueChanged( int i )
void QgsLabelingGui::on_mBufferUnitComboBox_currentIndexChanged( int index )
{
Q_UNUSED( i );
double singleStep = ( index == 1 ) ? 1.0 : 0.1 ; //1.0 for map units, 0.1 for mm
spinBufferSize->setSingleStep( singleStep );
updateFont( mRefFont );
}

Expand Down Expand Up @@ -705,6 +755,16 @@ void QgsLabelingGui::on_mPreviewTextBtn_clicked()
updatePreview();
}

void QgsLabelingGui::on_mPreviewBackgroundBtn_clicked()
{
QColor color = QColorDialog::getColor( mPreviewBackgroundBtn->color(), this );
if ( !color.isValid() )
return;

mPreviewBackgroundBtn->setColor( color );
setPreviewBackground( color );
}

void QgsLabelingGui::disableDataDefinedAlignment()
{
mHorizontalAlignmentComboBox->setCurrentIndex( mHorizontalAlignmentComboBox->findText( "" ) );
Expand Down
9 changes: 5 additions & 4 deletions src/app/qgslabelinggui.h
Expand Up @@ -38,7 +38,6 @@ class QgsLabelingGui : public QWidget, private Ui::QgsLabelingGuiBase

public slots:
void apply();
void changePreviewBackground();
void changeTextColor();
void changeTextFont();
void showEngineConfigDialog();
Expand All @@ -50,17 +49,17 @@ class QgsLabelingGui : public QWidget, private Ui::QgsLabelingGuiBase
void scrollPreview();
void updateOptions();

void on_mPreviewSizeSlider_valueChanged( int i );
void on_mFontSizeSpinBox_valueChanged( double d );
void on_mFontSizeUnitComboBox_currentIndexChanged( int index );
void on_mFontTranspSpinBox_valueChanged( int i );
void on_mFontWordSpacingSpinBox_valueChanged( double spacing );
void on_mFontLetterSpacingSpinBox_valueChanged( double spacing );
void on_mBufferTranspSpinBox_valueChanged( int i );
void on_mBufferUnitComboBox_currentIndexChanged( int index );
void on_mXCoordinateComboBox_currentIndexChanged( const QString & text );
void on_mYCoordinateComboBox_currentIndexChanged( const QString & text );

void on_mPreviewTextEdit_textChanged( const QString & text );
void on_mPreviewTextBtn_clicked();
void on_mPreviewBackgroundBtn_clicked();

protected:
void populatePlacementMethods();
Expand All @@ -78,6 +77,8 @@ class QgsLabelingGui : public QWidget, private Ui::QgsLabelingGuiBase

// background reference font
QFont mRefFont;
int mPreviewSize;
void setPreviewBackground( QColor color );

void disableDataDefinedAlignment();
void enableDataDefinedAlignment();
Expand Down
13 changes: 10 additions & 3 deletions src/app/qgslabelpreview.cpp
Expand Up @@ -30,10 +30,12 @@ void QgsLabelPreview::setTextColor( QColor color )
update();
}

void QgsLabelPreview::setBuffer( double size, QColor color )
void QgsLabelPreview::setBuffer( double size, QColor color, Qt::PenJoinStyle joinStyle, bool noFill )
{
mBufferSize = size * 88 / 25.4; //assume standard dpi for preview
mBufferColor = color;
mBufferJoinStyle = joinStyle;
mBufferNoFill = noFill;
update();
}

Expand All @@ -45,10 +47,15 @@ void QgsLabelPreview::paintEvent( QPaintEvent *e )
p.setRenderHint( QPainter::Antialiasing );
p.setFont( font() );
QFontMetrics fm( font() );
p.translate( 0, fm.ascent() + 4 );

double xtrans = 0;
if ( mBufferSize != 0 )
QgsPalLabeling::drawLabelBuffer( &p, text(), font(), mBufferSize, mBufferColor );
xtrans = mBufferSize / 4;

p.translate( xtrans, fm.ascent() + 4 );

if ( mBufferSize != 0 )
QgsPalLabeling::drawLabelBuffer( &p, text(), font(), mBufferSize, mBufferColor, mBufferJoinStyle, mBufferNoFill );

p.setPen( mTextColor );
p.drawText( 0, 0, text() );
Expand Down
4 changes: 3 additions & 1 deletion src/app/qgslabelpreview.h
Expand Up @@ -24,7 +24,7 @@ class QgsLabelPreview : public QLabel

void setTextColor( QColor color );

void setBuffer( double size, QColor color );
void setBuffer( double size, QColor color, Qt::PenJoinStyle joinStyle, bool noFill = false );

void setFont( QFont f ) { mFont = f; }
QFont font() { return mFont; }
Expand All @@ -34,6 +34,8 @@ class QgsLabelPreview : public QLabel
private:
int mBufferSize;
QColor mBufferColor;
Qt::PenJoinStyle mBufferJoinStyle;
bool mBufferNoFill;
QColor mTextColor;
QFont mFont;
};
Expand Down

0 comments on commit 7079f20

Please sign in to comment.