QGIS API Documentation  3.20.0-Odense (decaadbb31)
qgslayertreeembeddedwidgetsimpl.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgslayertreeembeddedwidgetsimpl.h
3  --------------------------------------
4  Date : May 2016
5  Copyright : (C) 2016 by Martin Dobias
6  Email : wonder dot sk at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
17 
18 #include <QFontMetrics>
19 #include <QHBoxLayout>
20 #include <QLabel>
21 #include <QSlider>
22 #include <QTimer>
23 #include "qgsmaplayer.h"
24 
26 
27 QgsLayerTreeOpacityWidget::QgsLayerTreeOpacityWidget( QgsMapLayer *layer )
28  : mLayer( layer )
29 {
30  setAutoFillBackground( true ); // override the content from model
31  QLabel *l = new QLabel( tr( "Opacity" ), this );
32  mSlider = new QSlider( Qt::Horizontal, this );
33  mSlider->setRange( 0, 1000 );
34  int sliderW = static_cast< int >( QFontMetricsF( font() ).horizontalAdvance( 'X' ) * 16 * Qgis::UI_SCALE_FACTOR );
35  mSlider->setMinimumWidth( sliderW / 2 );
36  mSlider->setMaximumWidth( sliderW );
37  QHBoxLayout *lay = new QHBoxLayout();
38  QSpacerItem *spacerItem = new QSpacerItem( 1, 0, QSizePolicy::MinimumExpanding, QSizePolicy::Minimum );
39  lay->addWidget( l );
40  lay->addWidget( mSlider );
41  lay->addItem( spacerItem );
42  setLayout( lay );
43 
44  // timer for delayed transparency update - for more responsive GUI
45  mTimer = new QTimer( this );
46  mTimer->setSingleShot( true );
47  mTimer->setInterval( 100 );
48  connect( mTimer, &QTimer::timeout, this, &QgsLayerTreeOpacityWidget::updateOpacityFromSlider );
49 
50  connect( mSlider, &QAbstractSlider::valueChanged, this, &QgsLayerTreeOpacityWidget::sliderValueChanged );
51 
52  // init from layer
53  mSlider->setValue( mLayer->opacity() * 1000.0 );
54  connect( mLayer, &QgsMapLayer::opacityChanged, this, &QgsLayerTreeOpacityWidget::layerTrChanged );
55 }
56 
57 QSize QgsLayerTreeOpacityWidget::sizeHint() const
58 {
59  return QWidget::sizeHint();
60  //return QSize(200,200); // horizontal seems ignored, vertical is used for spacing
61 }
62 
63 void QgsLayerTreeOpacityWidget::sliderValueChanged( int value )
64 {
65  Q_UNUSED( value )
66 
67  if ( mTimer->isActive() )
68  return;
69  mTimer->start();
70 }
71 
72 void QgsLayerTreeOpacityWidget::updateOpacityFromSlider()
73 {
74  int value = mSlider->value();
75  mLayer->setOpacity( value / 1000.0 );
76  mLayer->triggerRepaint();
77 }
78 
79 void QgsLayerTreeOpacityWidget::layerTrChanged()
80 {
81  mSlider->blockSignals( true );
82  mSlider->setValue( mLayer->opacity() * 1000.0 );
83  mSlider->blockSignals( false );
84 }
85 
86 //
87 
88 QString QgsLayerTreeOpacityWidget::Provider::id() const
89 {
90  return QStringLiteral( "transparency" );
91 }
92 
93 QString QgsLayerTreeOpacityWidget::Provider::name() const
94 {
95  return tr( "Opacity slider" );
96 }
97 
98 QgsLayerTreeOpacityWidget *QgsLayerTreeOpacityWidget::Provider::createWidget( QgsMapLayer *layer, int widgetIndex )
99 {
100  Q_UNUSED( widgetIndex )
101  return new QgsLayerTreeOpacityWidget( layer );
102 }
103 
104 bool QgsLayerTreeOpacityWidget::Provider::supportsLayer( QgsMapLayer * )
105 {
106  return true;
107 }
108 
static const double UI_SCALE_FACTOR
UI scaling factor.
Definition: qgis.h:416
Base class for all map layer types.
Definition: qgsmaplayer.h:70
void opacityChanged(double opacity)
Emitted when the layer's opacity is changed, where opacity is a value between 0 (transparent) and 1 (...