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 );
54 mSlider->setValue( vl->
opacity() * 1000.0 );
59 mSlider->setValue( 1000 - qobject_cast<QgsRasterLayer *>( mLayer )->renderer()->opacity() * 1000 );
64 QSize QgsLayerTreeOpacityWidget::sizeHint()
const 66 return QWidget::sizeHint();
70 void QgsLayerTreeOpacityWidget::sliderValueChanged(
int value )
74 if ( mTimer->isActive() )
79 void QgsLayerTreeOpacityWidget::updateOpacityFromSlider()
81 int value = mSlider->value();
85 qobject_cast<
QgsVectorLayer *>( mLayer )->setOpacity( value / 1000.0 );
89 qobject_cast<
QgsRasterLayer *>( mLayer )->renderer()->setOpacity( 1 - value / 1000.0 );
95 void QgsLayerTreeOpacityWidget::layerTrChanged()
97 mSlider->blockSignals(
true );
98 mSlider->setValue( qobject_cast<QgsVectorLayer *>( mLayer )->opacity() * 1000.0 );
99 mSlider->blockSignals(
false );
104 QString QgsLayerTreeOpacityWidget::Provider::id()
const 106 return QStringLiteral(
"transparency" );
109 QString QgsLayerTreeOpacityWidget::Provider::name()
const 111 return tr(
"Opacity slider" );
114 QgsLayerTreeOpacityWidget *QgsLayerTreeOpacityWidget::Provider::createWidget(
QgsMapLayer *layer,
int widgetIndex )
116 Q_UNUSED( widgetIndex );
117 return new QgsLayerTreeOpacityWidget( layer );
120 bool QgsLayerTreeOpacityWidget::Provider::supportsLayer(
QgsMapLayer *layer )
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.
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
QgsMapLayer::LayerType type() const
Returns the type of the layer.
void triggerRepaint(bool deferredUpdate=false)
Will advise the map canvas (and any other interested party) that this layer requires to be repainted...
Represents a vector layer which manages a vector based data sets.