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 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()
74 int value = mSlider->value();
75 mLayer->setOpacity( value / 1000.0 );
76 mLayer->triggerRepaint();
79 void QgsLayerTreeOpacityWidget::layerTrChanged()
81 mSlider->blockSignals(
true );
82 mSlider->setValue( mLayer->opacity() * 1000.0 );
83 mSlider->blockSignals(
false );
88 QString QgsLayerTreeOpacityWidget::Provider::id()
const
90 return QStringLiteral(
"transparency" );
93 QString QgsLayerTreeOpacityWidget::Provider::name()
const
95 return tr(
"Opacity slider" );
98 QgsLayerTreeOpacityWidget *QgsLayerTreeOpacityWidget::Provider::createWidget(
QgsMapLayer *layer,
int widgetIndex )
100 Q_UNUSED( widgetIndex )
101 return new QgsLayerTreeOpacityWidget( layer );
104 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 (...