18 #include <QFontMetrics> 19 #include <QHBoxLayout> 31 QgsLayerTreeOpacityWidget::QgsLayerTreeOpacityWidget(
QgsMapLayer *layer )
34 setAutoFillBackground(
true );
35 QLabel *l =
new QLabel( tr(
"Opacity" ),
this );
36 mSlider =
new QSlider( Qt::Horizontal,
this );
37 mSlider->setRange( 0, 1000 );
38 int sliderW =
static_cast< int >( QFontMetricsF( font() ).width(
'X' ) * 16 *
Qgis::UI_SCALE_FACTOR );
39 mSlider->setMinimumWidth( sliderW / 2 );
40 mSlider->setMaximumWidth( sliderW );
41 QHBoxLayout *lay =
new QHBoxLayout();
42 QSpacerItem *spacerItem =
new QSpacerItem( 1, 0, QSizePolicy::MinimumExpanding, QSizePolicy::Minimum );
44 lay->addWidget( mSlider );
45 lay->addItem( spacerItem );
49 mTimer =
new QTimer(
this );
50 mTimer->setSingleShot(
true );
51 mTimer->setInterval( 100 );
52 connect( mTimer, &QTimer::timeout,
this, &QgsLayerTreeOpacityWidget::updateOpacityFromSlider );
54 connect( mSlider, &QAbstractSlider::valueChanged,
this, &QgsLayerTreeOpacityWidget::sliderValueChanged );
57 switch ( mLayer->type() )
62 mSlider->setValue( vl->
opacity() * 1000.0 );
69 mSlider->setValue( qobject_cast<QgsRasterLayer *>( mLayer )->renderer()->opacity() * 1000 );
81 QSize QgsLayerTreeOpacityWidget::sizeHint()
const 83 return QWidget::sizeHint();
87 void QgsLayerTreeOpacityWidget::sliderValueChanged(
int value )
91 if ( mTimer->isActive() )
96 void QgsLayerTreeOpacityWidget::updateOpacityFromSlider()
98 int value = mSlider->value();
100 switch ( mLayer->type() )
104 qobject_cast<
QgsVectorLayer *>( mLayer )->setOpacity( value / 1000.0 );
109 qobject_cast<
QgsRasterLayer *>( mLayer )->renderer()->setOpacity( value / 1000.0 );
118 mLayer->triggerRepaint();
121 void QgsLayerTreeOpacityWidget::layerTrChanged()
123 mSlider->blockSignals(
true );
124 mSlider->setValue( qobject_cast<QgsVectorLayer *>( mLayer )->opacity() * 1000.0 );
125 mSlider->blockSignals(
false );
130 QString QgsLayerTreeOpacityWidget::Provider::id()
const 132 return QStringLiteral(
"transparency" );
135 QString QgsLayerTreeOpacityWidget::Provider::name()
const 137 return tr(
"Opacity slider" );
140 QgsLayerTreeOpacityWidget *QgsLayerTreeOpacityWidget::Provider::createWidget(
QgsMapLayer *layer,
int widgetIndex )
142 Q_UNUSED( widgetIndex )
143 return new QgsLayerTreeOpacityWidget( layer );
146 bool QgsLayerTreeOpacityWidget::Provider::supportsLayer(
QgsMapLayer *layer )
148 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.
static const double UI_SCALE_FACTOR
UI scaling factor.
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.