QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
24 #include "qgsrasterlayer.h"
25 #include "qgsrasterrenderer.h"
26 #include "qgsvectorlayer.h"
27 
28 
30 
31 QgsLayerTreeOpacityWidget::QgsLayerTreeOpacityWidget( QgsMapLayer *layer )
32  : mLayer( layer )
33 {
34  setAutoFillBackground( true ); // override the content from model
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 );
43  lay->addWidget( l );
44  lay->addWidget( mSlider );
45  lay->addItem( spacerItem );
46  setLayout( lay );
47 
48  // timer for delayed transparency update - for more responsive GUI
49  mTimer = new QTimer( this );
50  mTimer->setSingleShot( true );
51  mTimer->setInterval( 100 );
52  connect( mTimer, &QTimer::timeout, this, &QgsLayerTreeOpacityWidget::updateOpacityFromSlider );
53 
54  connect( mSlider, &QAbstractSlider::valueChanged, this, &QgsLayerTreeOpacityWidget::sliderValueChanged );
55 
56  // init from layer
57  switch ( mLayer->type() )
58  {
60  {
61  QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( mLayer );
62  mSlider->setValue( vl->opacity() * 1000.0 );
63  connect( vl, &QgsVectorLayer::opacityChanged, this, &QgsLayerTreeOpacityWidget::layerTrChanged );
64  break;
65  }
66 
68  {
69  mSlider->setValue( qobject_cast<QgsRasterLayer *>( mLayer )->renderer()->opacity() * 1000 );
70  // TODO: there is no signal for raster layers
71  break;
72  }
73 
77  break;
78 
79  }
80 }
81 
82 QSize QgsLayerTreeOpacityWidget::sizeHint() const
83 {
84  return QWidget::sizeHint();
85  //return QSize(200,200); // horizontal seems ignored, vertical is used for spacing
86 }
87 
88 void QgsLayerTreeOpacityWidget::sliderValueChanged( int value )
89 {
90  Q_UNUSED( value )
91 
92  if ( mTimer->isActive() )
93  return;
94  mTimer->start();
95 }
96 
97 void QgsLayerTreeOpacityWidget::updateOpacityFromSlider()
98 {
99  int value = mSlider->value();
100 
101  switch ( mLayer->type() )
102  {
104  {
105  qobject_cast<QgsVectorLayer *>( mLayer )->setOpacity( value / 1000.0 );
106  break;
107  }
109  {
110  qobject_cast<QgsRasterLayer *>( mLayer )->renderer()->setOpacity( value / 1000.0 );
111  break;
112  }
113 
117  break;
118  }
119 
120  mLayer->triggerRepaint();
121 }
122 
123 void QgsLayerTreeOpacityWidget::layerTrChanged()
124 {
125  mSlider->blockSignals( true );
126  mSlider->setValue( qobject_cast<QgsVectorLayer *>( mLayer )->opacity() * 1000.0 );
127  mSlider->blockSignals( false );
128 }
129 
130 //
131 
132 QString QgsLayerTreeOpacityWidget::Provider::id() const
133 {
134  return QStringLiteral( "transparency" );
135 }
136 
137 QString QgsLayerTreeOpacityWidget::Provider::name() const
138 {
139  return tr( "Opacity slider" );
140 }
141 
142 QgsLayerTreeOpacityWidget *QgsLayerTreeOpacityWidget::Provider::createWidget( QgsMapLayer *layer, int widgetIndex )
143 {
144  Q_UNUSED( widgetIndex )
145  return new QgsLayerTreeOpacityWidget( layer );
146 }
147 
148 bool QgsLayerTreeOpacityWidget::Provider::supportsLayer( QgsMapLayer *layer )
149 {
150  switch ( layer->type() )
151  {
154  return true;
155 
159  return false;
160  }
161  return false;
162 }
163 
qgsrasterlayer.h
QgsMapLayerType::MeshLayer
@ MeshLayer
Added in 3.2.
QgsMapLayerType::VectorLayer
@ VectorLayer
QgsVectorLayer::opacityChanged
void opacityChanged(double opacity)
Emitted when the layer's opacity is changed, where opacity is a value between 0 (transparent) and 1 (...
qgsrasterrenderer.h
QgsMapLayerType::RasterLayer
@ RasterLayer
Qgis::UI_SCALE_FACTOR
static const double UI_SCALE_FACTOR
UI scaling factor.
Definition: qgis.h:195
qgsvectorlayer.h
QgsVectorLayer
Definition: qgsvectorlayer.h:385
QgsMapLayer
Definition: qgsmaplayer.h:81
QgsMapLayerType::VectorTileLayer
@ VectorTileLayer
Added in 3.14.
QgsVectorLayer::opacity
double opacity
Definition: qgsvectorlayer.h:394
qgslayertreeembeddedwidgetsimpl.h
QgsMapLayer::type
QgsMapLayerType type() const
Returns the type of the layer.
Definition: qgsmaplayer.cpp:129
QgsMapLayerType::PluginLayer
@ PluginLayer