Skip to content
Permalink
Browse files

Add font and buffer transparency to adv labeling and data defined col…

…umns

- Data defined transparency field integer values 0->100 = opaque->transparent
  • Loading branch information
dakcarto committed Aug 14, 2012
1 parent a947e26 commit 538ebe01baeb25b24e6ec17e14478b37e8c0d445
@@ -160,7 +160,9 @@ QgsLabelingGui::QgsLabelingGui( QgsPalLabeling* lbl, QgsVectorLayer* layer, QgsM
spinBufferSize->setValue( lyr.bufferSize );

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

bool formattedNumbers = lyr.formatNumbers;
bool plusSign = lyr.plusSign;
@@ -282,6 +284,7 @@ QgsPalLayerSettings QgsLabelingGui::layerSettings()

lyr.textColor = btnTextColor->color();
lyr.textFont = mRefFont;
lyr.textTransp = mFontTranspSpinBox->value();
lyr.enabled = chkEnableLabeling->isChecked();
lyr.priority = sliderPriority->value();
lyr.obstacle = !chkNoObstacle->isChecked();
@@ -300,6 +303,7 @@ QgsPalLayerSettings QgsLabelingGui::layerSettings()
{
lyr.bufferSize = spinBufferSize->value();
lyr.bufferColor = btnBufferColor->color();
lyr.bufferTransp = mBufferTranspSpinBox->value();
}
else
{
@@ -356,6 +360,8 @@ QgsPalLayerSettings QgsLabelingGui::layerSettings()
setDataDefinedProperty( mShowLabelAttributeComboBox, QgsPalLayerSettings::Show, lyr );
setDataDefinedProperty( mMinScaleAttributeComboBox, QgsPalLayerSettings::MinScale, lyr );
setDataDefinedProperty( mMaxScaleAttributeComboBox, QgsPalLayerSettings::MaxScale, lyr );
setDataDefinedProperty( mTranspAttributeComboBox, QgsPalLayerSettings::FontTransp, lyr );
setDataDefinedProperty( mBufferTranspAttributeComboBox, QgsPalLayerSettings::BufferTransp, lyr );

return lyr;
}
@@ -423,6 +429,8 @@ void QgsLabelingGui::populateDataDefinedCombos( QgsPalLayerSettings& s )
comboList << mShowLabelAttributeComboBox;
comboList << mMinScaleAttributeComboBox;
comboList << mMaxScaleAttributeComboBox;
comboList << mTranspAttributeComboBox;
comboList << mBufferTranspAttributeComboBox;

QList<QComboBox*>::iterator comboIt = comboList.begin();
for ( ; comboIt != comboList.end(); ++comboIt )
@@ -459,6 +467,8 @@ void QgsLabelingGui::populateDataDefinedCombos( QgsPalLayerSettings& s )
setCurrentComboValue( mShowLabelAttributeComboBox, s, QgsPalLayerSettings::Show );
setCurrentComboValue( mMinScaleAttributeComboBox, s, QgsPalLayerSettings::MinScale );
setCurrentComboValue( mMaxScaleAttributeComboBox, s, QgsPalLayerSettings::MaxScale );
setCurrentComboValue( mTranspAttributeComboBox, s, QgsPalLayerSettings::FontTransp );
setCurrentComboValue( mBufferTranspAttributeComboBox, s, QgsPalLayerSettings::BufferTransp );
}

void QgsLabelingGui::changePreviewBackground()
@@ -535,11 +545,19 @@ void QgsLabelingGui::updatePreview()
}
lblFontPreview->setFont( previewFont );

lblFontPreview->setTextColor( btnTextColor->color() );
QColor prevColor = btnTextColor->color();
prevColor.setAlphaF( ( 100.0 - (double)(mFontTranspSpinBox->value()) ) / 100.0 );
lblFontPreview->setTextColor( prevColor );
if ( chkBuffer->isChecked() )
lblFontPreview->setBuffer( spinBufferSize->value(), btnBufferColor->color() );
{
QColor buffColor = btnBufferColor->color();
buffColor.setAlphaF( ( 100.0 - (double)(mBufferTranspSpinBox->value()) ) / 100.0 );
lblFontPreview->setBuffer( spinBufferSize->value(), buffColor );
}
else
{
lblFontPreview->setBuffer( 0, Qt::white );
}
}

void QgsLabelingGui::scrollPreview()
@@ -627,6 +645,12 @@ void QgsLabelingGui::on_mFontSizeUnitComboBox_currentIndexChanged( int index )
updateFont( mRefFont );
}

void QgsLabelingGui::on_mFontTranspSpinBox_valueChanged( int i )
{
Q_UNUSED( i );
updateFont( mRefFont );
}

void QgsLabelingGui::on_mFontWordSpacingSpinBox_valueChanged( double spacing )
{
mRefFont.setWordSpacing( spacing );
@@ -639,6 +663,12 @@ void QgsLabelingGui::on_mFontLetterSpacingSpinBox_valueChanged( double spacing )
updateFont( mRefFont );
}

void QgsLabelingGui::on_mBufferTranspSpinBox_valueChanged( int i )
{
Q_UNUSED( i );
updateFont( mRefFont );
}

void QgsLabelingGui::on_mXCoordinateComboBox_currentIndexChanged( const QString & text )
{
if ( text.isEmpty() ) //no data defined alignment without data defined position
@@ -52,8 +52,10 @@ class QgsLabelingGui : public QWidget, private Ui::QgsLabelingGuiBase

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_mXCoordinateComboBox_currentIndexChanged( const QString & text );
void on_mYCoordinateComboBox_currentIndexChanged( const QString & text );

@@ -47,8 +47,6 @@ void QgsLabelPreview::paintEvent( QPaintEvent *e )
QFontMetrics fm( font() );
p.translate( 0, fm.ascent() + 4 );

// mBufferColor.setAlpha( 125 );

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

@@ -143,6 +143,7 @@ QgsPalLayerSettings::QgsPalLayerSettings()
placementFlags = 0;
//textFont = QFont();
textColor = Qt::black;
textTransp = 0;
enabled = false;
priority = 5;
obstacle = true;
@@ -151,6 +152,7 @@ QgsPalLayerSettings::QgsPalLayerSettings()
scaleMax = 0;
bufferSize = 1;
bufferColor = Qt::white;
bufferTransp = 0;
formatNumbers = false;
decimals = 3;
plusSign = false;
@@ -175,6 +177,7 @@ QgsPalLayerSettings::QgsPalLayerSettings( const QgsPalLayerSettings& s )
placementFlags = s.placementFlags;
textFont = s.textFont;
textColor = s.textColor;
textTransp = s.textTransp;
enabled = s.enabled;
priority = s.priority;
obstacle = s.obstacle;
@@ -183,6 +186,7 @@ QgsPalLayerSettings::QgsPalLayerSettings( const QgsPalLayerSettings& s )
scaleMax = s.scaleMax;
bufferSize = s.bufferSize;
bufferColor = s.bufferColor;
bufferTransp = s.bufferTransp;
formatNumbers = s.formatNumbers;
decimals = s.decimals;
plusSign = s.plusSign;
@@ -245,7 +249,7 @@ static void _writeDataDefinedPropertyMap( QgsVectorLayer* layer, const QMap< Qgs
{
return;
}
for ( int i = 0; i < 18; ++i )
for ( int i = 0; i < 20; ++i )
{
QMap< QgsPalLayerSettings::DataDefinedProperties, int >::const_iterator it = propertyMap.find(( QgsPalLayerSettings::DataDefinedProperties )i );
QVariant propertyValue;
@@ -303,6 +307,8 @@ static void _readDataDefinedPropertyMap( QgsVectorLayer* layer, QMap< QgsPalLaye
_readDataDefinedProperty( layer, QgsPalLayerSettings::Show, propertyMap );
_readDataDefinedProperty( layer, QgsPalLayerSettings::MinScale, propertyMap );
_readDataDefinedProperty( layer, QgsPalLayerSettings::MaxScale, propertyMap );
_readDataDefinedProperty( layer, QgsPalLayerSettings::FontTransp, propertyMap );
_readDataDefinedProperty( layer, QgsPalLayerSettings::BufferTransp, propertyMap );
}

void QgsPalLayerSettings::readFromLayer( QgsVectorLayer* layer )
@@ -323,6 +329,7 @@ void QgsPalLayerSettings::readFromLayer( QgsVectorLayer* layer )
textFont.setStrikeOut( layer->customProperty( "labeling/fontStrikeout" ).toBool() );
textFont.setPointSizeF( fontSize ); //double precision needed because of map units
textColor = _readColor( layer, "labeling/textColor" );
textTransp = layer->customProperty( "labeling/textTransp" ).toInt();
enabled = layer->customProperty( "labeling/enabled" ).toBool();
priority = layer->customProperty( "labeling/priority" ).toInt();
obstacle = layer->customProperty( "labeling/obstacle" ).toBool();
@@ -331,6 +338,7 @@ void QgsPalLayerSettings::readFromLayer( QgsVectorLayer* layer )
scaleMax = layer->customProperty( "labeling/scaleMax" ).toInt();
bufferSize = layer->customProperty( "labeling/bufferSize" ).toDouble();
bufferColor = _readColor( layer, "labeling/bufferColor" );
bufferTransp = layer->customProperty( "labeling/bufferTransp" ).toInt();
formatNumbers = layer->customProperty( "labeling/formatNumbers" ).toBool();
decimals = layer->customProperty( "labeling/decimals" ).toInt();
plusSign = layer->customProperty( "labeling/plussign" ).toInt();
@@ -363,6 +371,7 @@ void QgsPalLayerSettings::writeToLayer( QgsVectorLayer* layer )
layer->setCustomProperty( "labeling/fontUnderline", textFont.underline() );

_writeColor( layer, "labeling/textColor", textColor );
layer->setCustomProperty( "labeling/textTransp", textTransp );
layer->setCustomProperty( "labeling/enabled", enabled );
layer->setCustomProperty( "labeling/priority", priority );
layer->setCustomProperty( "labeling/obstacle", obstacle );
@@ -371,6 +380,7 @@ void QgsPalLayerSettings::writeToLayer( QgsVectorLayer* layer )
layer->setCustomProperty( "labeling/scaleMax", scaleMax );
layer->setCustomProperty( "labeling/bufferSize", bufferSize );
_writeColor( layer, "labeling/bufferColor", bufferColor );
layer->setCustomProperty( "labeling/bufferTransp", bufferTransp );
layer->setCustomProperty( "labeling/formatNumbers", formatNumbers );
layer->setCustomProperty( "labeling/decimals", decimals );
layer->setCustomProperty( "labeling/plussign", plusSign );
@@ -1223,8 +1233,10 @@ void QgsPalLabeling::drawLabeling( QgsRenderContext& context )
const QgsPalLayerSettings& lyr = layer( layerNameUtf8 );
QFont fontForLabel = lyr.textFont;
QColor fontColor = lyr.textColor;
int fontTransp = lyr.textTransp;
double bufferSize = lyr.bufferSize;
QColor bufferColor = lyr.bufferColor;
int bufferTransp = lyr.bufferTransp;

//apply data defined settings for the label
//font size
@@ -1243,6 +1255,19 @@ void QgsPalLabeling::drawLabeling( QgsRenderContext& context )
fontColor = lyr.textColor;
}
}
//font transparency
QVariant dataDefinedFontTransp = palGeometry->dataDefinedValues().value( QgsPalLayerSettings::FontTransp );
if ( dataDefinedFontTransp.isValid() )
{
bool ftOk = false;
int ft = dataDefinedFontTransp.toInt( &ftOk );
if ( ftOk && ft >= 0 && ft <= 100 )
{
fontTransp = ft;
}
}
fontColor.setAlphaF( ( 100.0 - (double)(fontTransp) ) / 100.0 );

//font bold
QVariant dataDefinedBold = palGeometry->dataDefinedValues().value( QgsPalLayerSettings::Bold );
if ( dataDefinedBold.isValid() )
@@ -1290,6 +1315,18 @@ void QgsPalLabeling::drawLabeling( QgsRenderContext& context )
bufferColor = lyr.bufferColor;
}
}
//buffer transparency
QVariant dataDefinedBufTransp = palGeometry->dataDefinedValues().value( QgsPalLayerSettings::BufferTransp );
if ( dataDefinedBufTransp.isValid() )
{
bool btOk = false;
int bt = dataDefinedBufTransp.toInt( &btOk );
if ( btOk && bt >= 0 && bt <= 100 )
{
bufferTransp = bt;
}
}
bufferColor.setAlphaF( ( 100.0 - (double)(bufferTransp) ) / 100.0 );

if ( lyr.bufferSize != 0 )
drawLabel( *it, painter, fontForLabel, fontColor, xform, bufferSize, bufferColor, true );
@@ -1482,10 +1519,11 @@ void QgsPalLabeling::drawLabelBuffer( QPainter* p, QString text, const QFont& fo
{
QPainterPath path;
path.addText( 0, 0, font, text );
// color.setAlpha( 125 );
QPen pen( color );
pen.setWidthF( size );
p->setPen( pen );
// TODO: make pref for whether to fill buffer
// color.setAlpha( 0 );
p->setBrush( color );
p->drawPath( path );
}
@@ -100,7 +100,9 @@ class CORE_EXPORT QgsPalLayerSettings
Rotation, //data defined rotation (only useful in connection with data defined position)
Show,
MinScale,
MaxScale
MaxScale,
FontTransp,
BufferTransp
};

QString fieldName;
@@ -117,6 +119,7 @@ class CORE_EXPORT QgsPalLayerSettings
unsigned int placementFlags;
QFont textFont;
QColor textColor;
int textTransp;
bool enabled;
int priority; // 0 = low, 10 = high
bool obstacle; // whether it's an obstacle
@@ -126,6 +129,7 @@ class CORE_EXPORT QgsPalLayerSettings
int scaleMin, scaleMax; // disabled if both are zero
double bufferSize; //buffer size (in mm)
QColor bufferColor;
int bufferTransp;
bool formatNumbers;
int decimals;
bool plusSign;
@@ -470,7 +470,7 @@
<item>
<widget class="QSlider" name="mBufferTranspSlider">
<property name="enabled">
<bool>false</bool>
<bool>true</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
@@ -499,9 +499,9 @@
</widget>
</item>
<item>
<widget class="QSpinBox" name="spinBox_2">
<widget class="QSpinBox" name="mBufferTranspSpinBox">
<property name="enabled">
<bool>false</bool>
<bool>true</bool>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
@@ -1009,7 +1009,7 @@
<item>
<widget class="QSlider" name="mFontTranspSlider">
<property name="enabled">
<bool>false</bool>
<bool>true</bool>
</property>
<property name="minimumSize">
<size>
@@ -1034,7 +1034,7 @@
<item>
<widget class="QSpinBox" name="mFontTranspSpinBox">
<property name="enabled">
<bool>false</bool>
<bool>true</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
@@ -1933,7 +1933,7 @@
<item row="2" column="1">
<widget class="QComboBox" name="mBufferTranspAttributeComboBox">
<property name="enabled">
<bool>false</bool>
<bool>true</bool>
</property>
</widget>
</item>
@@ -2211,7 +2211,7 @@
<item row="2" column="1">
<widget class="QComboBox" name="mTranspAttributeComboBox">
<property name="enabled">
<bool>false</bool>
<bool>true</bool>
</property>
</widget>
</item>
@@ -2417,7 +2417,7 @@
<connection>
<sender>mBufferTranspSlider</sender>
<signal>valueChanged(int)</signal>
<receiver>spinBox_2</receiver>
<receiver>mBufferTranspSpinBox</receiver>
<slot>setValue(int)</slot>
<hints>
<hint type="sourcelabel">
@@ -2431,7 +2431,7 @@
</hints>
</connection>
<connection>
<sender>spinBox_2</sender>
<sender>mBufferTranspSpinBox</sender>
<signal>valueChanged(int)</signal>
<receiver>mBufferTranspSlider</receiver>
<slot>setValue(int)</slot>

0 comments on commit 538ebe0

Please sign in to comment.
You can’t perform that action at this time.