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 );
82 QSize QgsLayerTreeOpacityWidget::sizeHint()
const
84 return QWidget::sizeHint();
88 void QgsLayerTreeOpacityWidget::sliderValueChanged(
int value )
92 if ( mTimer->isActive() )
97 void QgsLayerTreeOpacityWidget::updateOpacityFromSlider()
99 int value = mSlider->value();
101 switch ( mLayer->type() )
105 qobject_cast<QgsVectorLayer *>( mLayer )->setOpacity( value / 1000.0 );
110 qobject_cast<QgsRasterLayer *>( mLayer )->renderer()->setOpacity( value / 1000.0 );
120 mLayer->triggerRepaint();
123 void QgsLayerTreeOpacityWidget::layerTrChanged()
125 mSlider->blockSignals(
true );
126 mSlider->setValue( qobject_cast<QgsVectorLayer *>( mLayer )->opacity() * 1000.0 );
127 mSlider->blockSignals(
false );
132 QString QgsLayerTreeOpacityWidget::Provider::id()
const
134 return QStringLiteral(
"transparency" );
137 QString QgsLayerTreeOpacityWidget::Provider::name()
const
139 return tr(
"Opacity slider" );
142 QgsLayerTreeOpacityWidget *QgsLayerTreeOpacityWidget::Provider::createWidget(
QgsMapLayer *layer,
int widgetIndex )
144 Q_UNUSED( widgetIndex )
145 return new QgsLayerTreeOpacityWidget( layer );
148 bool QgsLayerTreeOpacityWidget::Provider::supportsLayer(
QgsMapLayer *layer )
150 switch ( layer->
type() )