17#include "moc_qgslayertreeembeddedwidgetsimpl.cpp"
19#include <QFontMetrics>
28QgsLayerTreeOpacityWidget::QgsLayerTreeOpacityWidget(
QgsMapLayer *layer )
31 setAutoFillBackground(
true );
32 QLabel *l =
new QLabel( tr(
"Opacity" ),
this );
33 mSlider =
new QSlider( Qt::Horizontal,
this );
34 mSlider->setRange( 0, 1000 );
35 const int sliderW =
static_cast<int>( QFontMetricsF( font() ).horizontalAdvance(
'X' ) * 16 *
Qgis::UI_SCALE_FACTOR );
36 mSlider->setMinimumWidth( sliderW / 2 );
37 mSlider->setMaximumWidth( sliderW );
38 QHBoxLayout *lay =
new QHBoxLayout();
39 QSpacerItem *spacerItem =
new QSpacerItem( 1, 0, QSizePolicy::MinimumExpanding, QSizePolicy::Minimum );
41 lay->addWidget( mSlider );
42 lay->addItem( spacerItem );
46 mTimer =
new QTimer(
this );
47 mTimer->setSingleShot(
true );
48 mTimer->setInterval( 100 );
49 connect( mTimer, &QTimer::timeout,
this, &QgsLayerTreeOpacityWidget::updateOpacityFromSlider );
51 connect( mSlider, &QAbstractSlider::valueChanged,
this, &QgsLayerTreeOpacityWidget::sliderValueChanged );
54 mSlider->setValue( mLayer->opacity() * 1000.0 );
58QSize QgsLayerTreeOpacityWidget::sizeHint()
const
60 return QWidget::sizeHint();
64void QgsLayerTreeOpacityWidget::sliderValueChanged(
int value )
68 if ( mTimer->isActive() )
73void QgsLayerTreeOpacityWidget::updateOpacityFromSlider()
77 const int value = mSlider->value();
78 mLayer->setOpacity( value / 1000.0 );
79 mLayer->triggerRepaint();
82void QgsLayerTreeOpacityWidget::layerTrChanged()
86 mSlider->blockSignals(
true );
87 mSlider->setValue( mLayer->opacity() * 1000.0 );
88 mSlider->blockSignals(
false );
93QString QgsLayerTreeOpacityWidget::Provider::id()
const
95 return QStringLiteral(
"transparency" );
98QString QgsLayerTreeOpacityWidget::Provider::name()
const
100 return tr(
"Opacity slider" );
103QgsLayerTreeOpacityWidget *QgsLayerTreeOpacityWidget::Provider::createWidget(
QgsMapLayer *layer,
int widgetIndex )
105 Q_UNUSED( widgetIndex )
106 return new QgsLayerTreeOpacityWidget( layer );
109bool 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 (...