QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 
76  break;
77 
78  }
79 }
80 
81 QSize QgsLayerTreeOpacityWidget::sizeHint() const
82 {
83  return QWidget::sizeHint();
84  //return QSize(200,200); // horizontal seems ignored, vertical is used for spacing
85 }
86 
87 void QgsLayerTreeOpacityWidget::sliderValueChanged( int value )
88 {
89  Q_UNUSED( value )
90 
91  if ( mTimer->isActive() )
92  return;
93  mTimer->start();
94 }
95 
96 void QgsLayerTreeOpacityWidget::updateOpacityFromSlider()
97 {
98  int value = mSlider->value();
99 
100  switch ( mLayer->type() )
101  {
103  {
104  qobject_cast<QgsVectorLayer *>( mLayer )->setOpacity( value / 1000.0 );
105  break;
106  }
108  {
109  qobject_cast<QgsRasterLayer *>( mLayer )->renderer()->setOpacity( value / 1000.0 );
110  break;
111  }
112 
115  break;
116  }
117 
118  mLayer->triggerRepaint();
119 }
120 
121 void QgsLayerTreeOpacityWidget::layerTrChanged()
122 {
123  mSlider->blockSignals( true );
124  mSlider->setValue( qobject_cast<QgsVectorLayer *>( mLayer )->opacity() * 1000.0 );
125  mSlider->blockSignals( false );
126 }
127 
128 //
129 
130 QString QgsLayerTreeOpacityWidget::Provider::id() const
131 {
132  return QStringLiteral( "transparency" );
133 }
134 
135 QString QgsLayerTreeOpacityWidget::Provider::name() const
136 {
137  return tr( "Opacity slider" );
138 }
139 
140 QgsLayerTreeOpacityWidget *QgsLayerTreeOpacityWidget::Provider::createWidget( QgsMapLayer *layer, int widgetIndex )
141 {
142  Q_UNUSED( widgetIndex )
143  return new QgsLayerTreeOpacityWidget( layer );
144 }
145 
146 bool QgsLayerTreeOpacityWidget::Provider::supportsLayer( QgsMapLayer *layer )
147 {
148  switch ( layer->type() )
149  {
152  return true;
153 
156  return false;
157  }
158  return false;
159 }
160 
void opacityChanged(double opacity)
Emitted when the layer&#39;s opacity is changed, where opacity is a value between 0 (transparent) and 1 (...
Base class for all map layer types.
Definition: qgsmaplayer.h:79
QgsMapLayerType type() const
Returns the type of the layer.
static const double UI_SCALE_FACTOR
UI scaling factor.
Definition: qgis.h:154
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.