Skip to content

Commit 538ebe0

Browse files
committedAug 14, 2012
Add font and buffer transparency to adv labeling and data defined columns
- Data defined transparency field integer values 0->100 = opaque->transparent
1 parent a947e26 commit 538ebe0

File tree

6 files changed

+88
-16
lines changed

6 files changed

+88
-16
lines changed
 

‎src/app/qgslabelinggui.cpp

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,9 @@ QgsLabelingGui::QgsLabelingGui( QgsPalLabeling* lbl, QgsVectorLayer* layer, QgsM
160160
spinBufferSize->setValue( lyr.bufferSize );
161161

162162
btnTextColor->setColor( lyr.textColor );
163+
mFontTranspSpinBox->setValue( lyr.textTransp );
163164
btnBufferColor->setColor( lyr.bufferColor );
165+
mBufferTranspSpinBox->setValue( lyr.bufferTransp );
164166

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

283285
lyr.textColor = btnTextColor->color();
284286
lyr.textFont = mRefFont;
287+
lyr.textTransp = mFontTranspSpinBox->value();
285288
lyr.enabled = chkEnableLabeling->isChecked();
286289
lyr.priority = sliderPriority->value();
287290
lyr.obstacle = !chkNoObstacle->isChecked();
@@ -300,6 +303,7 @@ QgsPalLayerSettings QgsLabelingGui::layerSettings()
300303
{
301304
lyr.bufferSize = spinBufferSize->value();
302305
lyr.bufferColor = btnBufferColor->color();
306+
lyr.bufferTransp = mBufferTranspSpinBox->value();
303307
}
304308
else
305309
{
@@ -356,6 +360,8 @@ QgsPalLayerSettings QgsLabelingGui::layerSettings()
356360
setDataDefinedProperty( mShowLabelAttributeComboBox, QgsPalLayerSettings::Show, lyr );
357361
setDataDefinedProperty( mMinScaleAttributeComboBox, QgsPalLayerSettings::MinScale, lyr );
358362
setDataDefinedProperty( mMaxScaleAttributeComboBox, QgsPalLayerSettings::MaxScale, lyr );
363+
setDataDefinedProperty( mTranspAttributeComboBox, QgsPalLayerSettings::FontTransp, lyr );
364+
setDataDefinedProperty( mBufferTranspAttributeComboBox, QgsPalLayerSettings::BufferTransp, lyr );
359365

360366
return lyr;
361367
}
@@ -423,6 +429,8 @@ void QgsLabelingGui::populateDataDefinedCombos( QgsPalLayerSettings& s )
423429
comboList << mShowLabelAttributeComboBox;
424430
comboList << mMinScaleAttributeComboBox;
425431
comboList << mMaxScaleAttributeComboBox;
432+
comboList << mTranspAttributeComboBox;
433+
comboList << mBufferTranspAttributeComboBox;
426434

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

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

538-
lblFontPreview->setTextColor( btnTextColor->color() );
548+
QColor prevColor = btnTextColor->color();
549+
prevColor.setAlphaF( ( 100.0 - (double)(mFontTranspSpinBox->value()) ) / 100.0 );
550+
lblFontPreview->setTextColor( prevColor );
539551
if ( chkBuffer->isChecked() )
540-
lblFontPreview->setBuffer( spinBufferSize->value(), btnBufferColor->color() );
552+
{
553+
QColor buffColor = btnBufferColor->color();
554+
buffColor.setAlphaF( ( 100.0 - (double)(mBufferTranspSpinBox->value()) ) / 100.0 );
555+
lblFontPreview->setBuffer( spinBufferSize->value(), buffColor );
556+
}
541557
else
558+
{
542559
lblFontPreview->setBuffer( 0, Qt::white );
560+
}
543561
}
544562

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

648+
void QgsLabelingGui::on_mFontTranspSpinBox_valueChanged( int i )
649+
{
650+
Q_UNUSED( i );
651+
updateFont( mRefFont );
652+
}
653+
630654
void QgsLabelingGui::on_mFontWordSpacingSpinBox_valueChanged( double spacing )
631655
{
632656
mRefFont.setWordSpacing( spacing );
@@ -639,6 +663,12 @@ void QgsLabelingGui::on_mFontLetterSpacingSpinBox_valueChanged( double spacing )
639663
updateFont( mRefFont );
640664
}
641665

666+
void QgsLabelingGui::on_mBufferTranspSpinBox_valueChanged( int i )
667+
{
668+
Q_UNUSED( i );
669+
updateFont( mRefFont );
670+
}
671+
642672
void QgsLabelingGui::on_mXCoordinateComboBox_currentIndexChanged( const QString & text )
643673
{
644674
if ( text.isEmpty() ) //no data defined alignment without data defined position

‎src/app/qgslabelinggui.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ class QgsLabelingGui : public QWidget, private Ui::QgsLabelingGuiBase
5252

5353
void on_mFontSizeSpinBox_valueChanged( double d );
5454
void on_mFontSizeUnitComboBox_currentIndexChanged( int index );
55+
void on_mFontTranspSpinBox_valueChanged( int i );
5556
void on_mFontWordSpacingSpinBox_valueChanged(double spacing );
5657
void on_mFontLetterSpacingSpinBox_valueChanged(double spacing );
58+
void on_mBufferTranspSpinBox_valueChanged( int i );
5759
void on_mXCoordinateComboBox_currentIndexChanged( const QString & text );
5860
void on_mYCoordinateComboBox_currentIndexChanged( const QString & text );
5961

‎src/app/qgslabelpreview.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ void QgsLabelPreview::paintEvent( QPaintEvent *e )
4747
QFontMetrics fm( font() );
4848
p.translate( 0, fm.ascent() + 4 );
4949

50-
// mBufferColor.setAlpha( 125 );
51-
5250
if ( mBufferSize != 0 )
5351
QgsPalLabeling::drawLabelBuffer( &p, text(), font(), mBufferSize, mBufferColor );
5452

‎src/core/qgspallabeling.cpp

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ QgsPalLayerSettings::QgsPalLayerSettings()
143143
placementFlags = 0;
144144
//textFont = QFont();
145145
textColor = Qt::black;
146+
textTransp = 0;
146147
enabled = false;
147148
priority = 5;
148149
obstacle = true;
@@ -151,6 +152,7 @@ QgsPalLayerSettings::QgsPalLayerSettings()
151152
scaleMax = 0;
152153
bufferSize = 1;
153154
bufferColor = Qt::white;
155+
bufferTransp = 0;
154156
formatNumbers = false;
155157
decimals = 3;
156158
plusSign = false;
@@ -175,6 +177,7 @@ QgsPalLayerSettings::QgsPalLayerSettings( const QgsPalLayerSettings& s )
175177
placementFlags = s.placementFlags;
176178
textFont = s.textFont;
177179
textColor = s.textColor;
180+
textTransp = s.textTransp;
178181
enabled = s.enabled;
179182
priority = s.priority;
180183
obstacle = s.obstacle;
@@ -183,6 +186,7 @@ QgsPalLayerSettings::QgsPalLayerSettings( const QgsPalLayerSettings& s )
183186
scaleMax = s.scaleMax;
184187
bufferSize = s.bufferSize;
185188
bufferColor = s.bufferColor;
189+
bufferTransp = s.bufferTransp;
186190
formatNumbers = s.formatNumbers;
187191
decimals = s.decimals;
188192
plusSign = s.plusSign;
@@ -245,7 +249,7 @@ static void _writeDataDefinedPropertyMap( QgsVectorLayer* layer, const QMap< Qgs
245249
{
246250
return;
247251
}
248-
for ( int i = 0; i < 18; ++i )
252+
for ( int i = 0; i < 20; ++i )
249253
{
250254
QMap< QgsPalLayerSettings::DataDefinedProperties, int >::const_iterator it = propertyMap.find(( QgsPalLayerSettings::DataDefinedProperties )i );
251255
QVariant propertyValue;
@@ -303,6 +307,8 @@ static void _readDataDefinedPropertyMap( QgsVectorLayer* layer, QMap< QgsPalLaye
303307
_readDataDefinedProperty( layer, QgsPalLayerSettings::Show, propertyMap );
304308
_readDataDefinedProperty( layer, QgsPalLayerSettings::MinScale, propertyMap );
305309
_readDataDefinedProperty( layer, QgsPalLayerSettings::MaxScale, propertyMap );
310+
_readDataDefinedProperty( layer, QgsPalLayerSettings::FontTransp, propertyMap );
311+
_readDataDefinedProperty( layer, QgsPalLayerSettings::BufferTransp, propertyMap );
306312
}
307313

308314
void QgsPalLayerSettings::readFromLayer( QgsVectorLayer* layer )
@@ -323,6 +329,7 @@ void QgsPalLayerSettings::readFromLayer( QgsVectorLayer* layer )
323329
textFont.setStrikeOut( layer->customProperty( "labeling/fontStrikeout" ).toBool() );
324330
textFont.setPointSizeF( fontSize ); //double precision needed because of map units
325331
textColor = _readColor( layer, "labeling/textColor" );
332+
textTransp = layer->customProperty( "labeling/textTransp" ).toInt();
326333
enabled = layer->customProperty( "labeling/enabled" ).toBool();
327334
priority = layer->customProperty( "labeling/priority" ).toInt();
328335
obstacle = layer->customProperty( "labeling/obstacle" ).toBool();
@@ -331,6 +338,7 @@ void QgsPalLayerSettings::readFromLayer( QgsVectorLayer* layer )
331338
scaleMax = layer->customProperty( "labeling/scaleMax" ).toInt();
332339
bufferSize = layer->customProperty( "labeling/bufferSize" ).toDouble();
333340
bufferColor = _readColor( layer, "labeling/bufferColor" );
341+
bufferTransp = layer->customProperty( "labeling/bufferTransp" ).toInt();
334342
formatNumbers = layer->customProperty( "labeling/formatNumbers" ).toBool();
335343
decimals = layer->customProperty( "labeling/decimals" ).toInt();
336344
plusSign = layer->customProperty( "labeling/plussign" ).toInt();
@@ -363,6 +371,7 @@ void QgsPalLayerSettings::writeToLayer( QgsVectorLayer* layer )
363371
layer->setCustomProperty( "labeling/fontUnderline", textFont.underline() );
364372

365373
_writeColor( layer, "labeling/textColor", textColor );
374+
layer->setCustomProperty( "labeling/textTransp", textTransp );
366375
layer->setCustomProperty( "labeling/enabled", enabled );
367376
layer->setCustomProperty( "labeling/priority", priority );
368377
layer->setCustomProperty( "labeling/obstacle", obstacle );
@@ -371,6 +380,7 @@ void QgsPalLayerSettings::writeToLayer( QgsVectorLayer* layer )
371380
layer->setCustomProperty( "labeling/scaleMax", scaleMax );
372381
layer->setCustomProperty( "labeling/bufferSize", bufferSize );
373382
_writeColor( layer, "labeling/bufferColor", bufferColor );
383+
layer->setCustomProperty( "labeling/bufferTransp", bufferTransp );
374384
layer->setCustomProperty( "labeling/formatNumbers", formatNumbers );
375385
layer->setCustomProperty( "labeling/decimals", decimals );
376386
layer->setCustomProperty( "labeling/plussign", plusSign );
@@ -1223,8 +1233,10 @@ void QgsPalLabeling::drawLabeling( QgsRenderContext& context )
12231233
const QgsPalLayerSettings& lyr = layer( layerNameUtf8 );
12241234
QFont fontForLabel = lyr.textFont;
12251235
QColor fontColor = lyr.textColor;
1236+
int fontTransp = lyr.textTransp;
12261237
double bufferSize = lyr.bufferSize;
12271238
QColor bufferColor = lyr.bufferColor;
1239+
int bufferTransp = lyr.bufferTransp;
12281240

12291241
//apply data defined settings for the label
12301242
//font size
@@ -1243,6 +1255,19 @@ void QgsPalLabeling::drawLabeling( QgsRenderContext& context )
12431255
fontColor = lyr.textColor;
12441256
}
12451257
}
1258+
//font transparency
1259+
QVariant dataDefinedFontTransp = palGeometry->dataDefinedValues().value( QgsPalLayerSettings::FontTransp );
1260+
if ( dataDefinedFontTransp.isValid() )
1261+
{
1262+
bool ftOk = false;
1263+
int ft = dataDefinedFontTransp.toInt( &ftOk );
1264+
if ( ftOk && ft >= 0 && ft <= 100 )
1265+
{
1266+
fontTransp = ft;
1267+
}
1268+
}
1269+
fontColor.setAlphaF( ( 100.0 - (double)(fontTransp) ) / 100.0 );
1270+
12461271
//font bold
12471272
QVariant dataDefinedBold = palGeometry->dataDefinedValues().value( QgsPalLayerSettings::Bold );
12481273
if ( dataDefinedBold.isValid() )
@@ -1290,6 +1315,18 @@ void QgsPalLabeling::drawLabeling( QgsRenderContext& context )
12901315
bufferColor = lyr.bufferColor;
12911316
}
12921317
}
1318+
//buffer transparency
1319+
QVariant dataDefinedBufTransp = palGeometry->dataDefinedValues().value( QgsPalLayerSettings::BufferTransp );
1320+
if ( dataDefinedBufTransp.isValid() )
1321+
{
1322+
bool btOk = false;
1323+
int bt = dataDefinedBufTransp.toInt( &btOk );
1324+
if ( btOk && bt >= 0 && bt <= 100 )
1325+
{
1326+
bufferTransp = bt;
1327+
}
1328+
}
1329+
bufferColor.setAlphaF( ( 100.0 - (double)(bufferTransp) ) / 100.0 );
12931330

12941331
if ( lyr.bufferSize != 0 )
12951332
drawLabel( *it, painter, fontForLabel, fontColor, xform, bufferSize, bufferColor, true );
@@ -1482,10 +1519,11 @@ void QgsPalLabeling::drawLabelBuffer( QPainter* p, QString text, const QFont& fo
14821519
{
14831520
QPainterPath path;
14841521
path.addText( 0, 0, font, text );
1485-
// color.setAlpha( 125 );
14861522
QPen pen( color );
14871523
pen.setWidthF( size );
14881524
p->setPen( pen );
1525+
// TODO: make pref for whether to fill buffer
1526+
// color.setAlpha( 0 );
14891527
p->setBrush( color );
14901528
p->drawPath( path );
14911529
}

‎src/core/qgspallabeling.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ class CORE_EXPORT QgsPalLayerSettings
100100
Rotation, //data defined rotation (only useful in connection with data defined position)
101101
Show,
102102
MinScale,
103-
MaxScale
103+
MaxScale,
104+
FontTransp,
105+
BufferTransp
104106
};
105107

106108
QString fieldName;
@@ -117,6 +119,7 @@ class CORE_EXPORT QgsPalLayerSettings
117119
unsigned int placementFlags;
118120
QFont textFont;
119121
QColor textColor;
122+
int textTransp;
120123
bool enabled;
121124
int priority; // 0 = low, 10 = high
122125
bool obstacle; // whether it's an obstacle
@@ -126,6 +129,7 @@ class CORE_EXPORT QgsPalLayerSettings
126129
int scaleMin, scaleMax; // disabled if both are zero
127130
double bufferSize; //buffer size (in mm)
128131
QColor bufferColor;
132+
int bufferTransp;
129133
bool formatNumbers;
130134
int decimals;
131135
bool plusSign;

‎src/ui/qgslabelingguibase.ui

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@
470470
<item>
471471
<widget class="QSlider" name="mBufferTranspSlider">
472472
<property name="enabled">
473-
<bool>false</bool>
473+
<bool>true</bool>
474474
</property>
475475
<property name="sizePolicy">
476476
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
@@ -499,9 +499,9 @@
499499
</widget>
500500
</item>
501501
<item>
502-
<widget class="QSpinBox" name="spinBox_2">
502+
<widget class="QSpinBox" name="mBufferTranspSpinBox">
503503
<property name="enabled">
504-
<bool>false</bool>
504+
<bool>true</bool>
505505
</property>
506506
<property name="alignment">
507507
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
@@ -1009,7 +1009,7 @@
10091009
<item>
10101010
<widget class="QSlider" name="mFontTranspSlider">
10111011
<property name="enabled">
1012-
<bool>false</bool>
1012+
<bool>true</bool>
10131013
</property>
10141014
<property name="minimumSize">
10151015
<size>
@@ -1034,7 +1034,7 @@
10341034
<item>
10351035
<widget class="QSpinBox" name="mFontTranspSpinBox">
10361036
<property name="enabled">
1037-
<bool>false</bool>
1037+
<bool>true</bool>
10381038
</property>
10391039
<property name="sizePolicy">
10401040
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
@@ -1933,7 +1933,7 @@
19331933
<item row="2" column="1">
19341934
<widget class="QComboBox" name="mBufferTranspAttributeComboBox">
19351935
<property name="enabled">
1936-
<bool>false</bool>
1936+
<bool>true</bool>
19371937
</property>
19381938
</widget>
19391939
</item>
@@ -2211,7 +2211,7 @@
22112211
<item row="2" column="1">
22122212
<widget class="QComboBox" name="mTranspAttributeComboBox">
22132213
<property name="enabled">
2214-
<bool>false</bool>
2214+
<bool>true</bool>
22152215
</property>
22162216
</widget>
22172217
</item>
@@ -2417,7 +2417,7 @@
24172417
<connection>
24182418
<sender>mBufferTranspSlider</sender>
24192419
<signal>valueChanged(int)</signal>
2420-
<receiver>spinBox_2</receiver>
2420+
<receiver>mBufferTranspSpinBox</receiver>
24212421
<slot>setValue(int)</slot>
24222422
<hints>
24232423
<hint type="sourcelabel">
@@ -2431,7 +2431,7 @@
24312431
</hints>
24322432
</connection>
24332433
<connection>
2434-
<sender>spinBox_2</sender>
2434+
<sender>mBufferTranspSpinBox</sender>
24352435
<signal>valueChanged(int)</signal>
24362436
<receiver>mBufferTranspSlider</receiver>
24372437
<slot>setValue(int)</slot>

0 commit comments

Comments
 (0)
Please sign in to comment.