18 #include <QHBoxLayout> 30 QgsLayerTreeOpacityWidget::QgsLayerTreeOpacityWidget(
QgsMapLayer *layer )
33 setAutoFillBackground(
true );
34 QLabel *l =
new QLabel( QStringLiteral(
"Opacity" ),
this );
35 mSlider =
new QSlider( Qt::Horizontal,
this );
36 mSlider->setRange( 0, 1000 );
37 QHBoxLayout *lay =
new QHBoxLayout();
39 lay->addWidget( mSlider );
43 mTimer =
new QTimer(
this );
44 mTimer->setSingleShot(
true );
45 mTimer->setInterval( 100 );
46 connect( mTimer, &QTimer::timeout,
this, &QgsLayerTreeOpacityWidget::updateOpacityFromSlider );
48 connect( mSlider, &QAbstractSlider::valueChanged,
this, &QgsLayerTreeOpacityWidget::sliderValueChanged );
51 switch ( mLayer->type() )
56 mSlider->setValue( vl->
opacity() * 1000.0 );
63 mSlider->setValue( 1000 - qobject_cast<QgsRasterLayer *>( mLayer )->renderer()->opacity() * 1000 );
75 QSize QgsLayerTreeOpacityWidget::sizeHint()
const 77 return QWidget::sizeHint();
81 void QgsLayerTreeOpacityWidget::sliderValueChanged(
int value )
85 if ( mTimer->isActive() )
90 void QgsLayerTreeOpacityWidget::updateOpacityFromSlider()
92 int value = mSlider->value();
94 switch ( mLayer->type() )
98 qobject_cast<
QgsVectorLayer *>( mLayer )->setOpacity( value / 1000.0 );
103 qobject_cast<
QgsRasterLayer *>( mLayer )->renderer()->setOpacity( 1 - value / 1000.0 );
112 mLayer->triggerRepaint();
115 void QgsLayerTreeOpacityWidget::layerTrChanged()
117 mSlider->blockSignals(
true );
118 mSlider->setValue( qobject_cast<QgsVectorLayer *>( mLayer )->opacity() * 1000.0 );
119 mSlider->blockSignals(
false );
124 QString QgsLayerTreeOpacityWidget::Provider::id()
const 126 return QStringLiteral(
"transparency" );
129 QString QgsLayerTreeOpacityWidget::Provider::name()
const 131 return tr(
"Opacity slider" );
134 QgsLayerTreeOpacityWidget *QgsLayerTreeOpacityWidget::Provider::createWidget(
QgsMapLayer *layer,
int widgetIndex )
136 Q_UNUSED( widgetIndex )
137 return new QgsLayerTreeOpacityWidget( layer );
140 bool QgsLayerTreeOpacityWidget::Provider::supportsLayer(
QgsMapLayer *layer )
142 switch ( layer->
type() )
void opacityChanged(double opacity)
Emitted when the layer's opacity is changed, where opacity is a value between 0 (transparent) and 1 (...
Base class for all map layer types.
QgsMapLayerType type() const
Returns the type of the layer.
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
Represents a vector layer which manages a vector based data sets.