18 #include <QFontMetrics>
19 #include <QHBoxLayout>
27 QgsLayerTreeOpacityWidget::QgsLayerTreeOpacityWidget(
QgsMapLayer *layer )
30 setAutoFillBackground(
true );
31 QLabel *l =
new QLabel( tr(
"Opacity" ),
this );
32 mSlider =
new QSlider( Qt::Horizontal,
this );
33 mSlider->setRange( 0, 1000 );
34 const int sliderW =
static_cast< int >( QFontMetricsF( font() ).horizontalAdvance(
'X' ) * 16 *
Qgis::UI_SCALE_FACTOR );
35 mSlider->setMinimumWidth( sliderW / 2 );
36 mSlider->setMaximumWidth( sliderW );
37 QHBoxLayout *lay =
new QHBoxLayout();
38 QSpacerItem *spacerItem =
new QSpacerItem( 1, 0, QSizePolicy::MinimumExpanding, QSizePolicy::Minimum );
40 lay->addWidget( mSlider );
41 lay->addItem( spacerItem );
45 mTimer =
new QTimer(
this );
46 mTimer->setSingleShot(
true );
47 mTimer->setInterval( 100 );
48 connect( mTimer, &QTimer::timeout,
this, &QgsLayerTreeOpacityWidget::updateOpacityFromSlider );
50 connect( mSlider, &QAbstractSlider::valueChanged,
this, &QgsLayerTreeOpacityWidget::sliderValueChanged );
53 mSlider->setValue( mLayer->opacity() * 1000.0 );
57 QSize QgsLayerTreeOpacityWidget::sizeHint()
const
59 return QWidget::sizeHint();
63 void QgsLayerTreeOpacityWidget::sliderValueChanged(
int value )
67 if ( mTimer->isActive() )
72 void QgsLayerTreeOpacityWidget::updateOpacityFromSlider()
76 const int value = mSlider->value();
77 mLayer->setOpacity( value / 1000.0 );
78 mLayer->triggerRepaint();
81 void QgsLayerTreeOpacityWidget::layerTrChanged()
85 mSlider->blockSignals(
true );
86 mSlider->setValue( mLayer->opacity() * 1000.0 );
87 mSlider->blockSignals(
false );
92 QString QgsLayerTreeOpacityWidget::Provider::id()
const
94 return QStringLiteral(
"transparency" );
97 QString QgsLayerTreeOpacityWidget::Provider::name()
const
99 return tr(
"Opacity slider" );
102 QgsLayerTreeOpacityWidget *QgsLayerTreeOpacityWidget::Provider::createWidget(
QgsMapLayer *layer,
int widgetIndex )
104 Q_UNUSED( widgetIndex )
105 return new QgsLayerTreeOpacityWidget( layer );
108 bool QgsLayerTreeOpacityWidget::Provider::supportsLayer(
QgsMapLayer * )
static const double UI_SCALE_FACTOR
UI scaling factor.
Base class for all map layer types.
void opacityChanged(double opacity)
Emitted when the layer's opacity is changed, where opacity is a value between 0 (transparent) and 1 (...