QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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 <QHBoxLayout>
19 #include <QLabel>
20 #include <QSlider>
21 #include <QTimer>
22 
23 #include "qgsrasterlayer.h"
24 #include "qgsrasterrenderer.h"
25 #include "qgsvectorlayer.h"
26 
27 
29 
30 QgsLayerTreeOpacityWidget::QgsLayerTreeOpacityWidget( QgsMapLayer *layer )
31  : mLayer( layer )
32 {
33  setAutoFillBackground( true ); // override the content from model
34  QLabel *l = new QLabel( QStringLiteral( "Opacity" ), this );
35  mSlider = new QSlider( Qt::Horizontal, this );
36  mSlider->setRange( 0, 1000 );
37  QHBoxLayout *lay = new QHBoxLayout();
38  lay->addWidget( l );
39  lay->addWidget( mSlider );
40  setLayout( lay );
41 
42  // timer for delayed transparency update - for more responsive GUI
43  mTimer = new QTimer( this );
44  mTimer->setSingleShot( true );
45  mTimer->setInterval( 100 );
46  connect( mTimer, &QTimer::timeout, this, &QgsLayerTreeOpacityWidget::updateOpacityFromSlider );
47 
48  connect( mSlider, &QAbstractSlider::valueChanged, this, &QgsLayerTreeOpacityWidget::sliderValueChanged );
49 
50  // init from layer
51  switch ( mLayer->type() )
52  {
54  {
55  QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( mLayer );
56  mSlider->setValue( vl->opacity() * 1000.0 );
57  connect( vl, &QgsVectorLayer::opacityChanged, this, &QgsLayerTreeOpacityWidget::layerTrChanged );
58  break;
59  }
60 
62  {
63  mSlider->setValue( 1000 - qobject_cast<QgsRasterLayer *>( mLayer )->renderer()->opacity() * 1000 );
64  // TODO: there is no signal for raster layers
65  break;
66  }
67 
70  break;
71 
72  }
73 }
74 
75 QSize QgsLayerTreeOpacityWidget::sizeHint() const
76 {
77  return QWidget::sizeHint();
78  //return QSize(200,200); // horizontal seems ignored, vertical is used for spacing
79 }
80 
81 void QgsLayerTreeOpacityWidget::sliderValueChanged( int value )
82 {
83  Q_UNUSED( value )
84 
85  if ( mTimer->isActive() )
86  return;
87  mTimer->start();
88 }
89 
90 void QgsLayerTreeOpacityWidget::updateOpacityFromSlider()
91 {
92  int value = mSlider->value();
93 
94  switch ( mLayer->type() )
95  {
97  {
98  qobject_cast<QgsVectorLayer *>( mLayer )->setOpacity( value / 1000.0 );
99  break;
100  }
102  {
103  qobject_cast<QgsRasterLayer *>( mLayer )->renderer()->setOpacity( 1 - value / 1000.0 );
104  break;
105  }
106 
109  break;
110  }
111 
112  mLayer->triggerRepaint();
113 }
114 
115 void QgsLayerTreeOpacityWidget::layerTrChanged()
116 {
117  mSlider->blockSignals( true );
118  mSlider->setValue( qobject_cast<QgsVectorLayer *>( mLayer )->opacity() * 1000.0 );
119  mSlider->blockSignals( false );
120 }
121 
122 //
123 
124 QString QgsLayerTreeOpacityWidget::Provider::id() const
125 {
126  return QStringLiteral( "transparency" );
127 }
128 
129 QString QgsLayerTreeOpacityWidget::Provider::name() const
130 {
131  return tr( "Opacity slider" );
132 }
133 
134 QgsLayerTreeOpacityWidget *QgsLayerTreeOpacityWidget::Provider::createWidget( QgsMapLayer *layer, int widgetIndex )
135 {
136  Q_UNUSED( widgetIndex )
137  return new QgsLayerTreeOpacityWidget( layer );
138 }
139 
140 bool QgsLayerTreeOpacityWidget::Provider::supportsLayer( QgsMapLayer *layer )
141 {
142  switch ( layer->type() )
143  {
146  return true;
147 
150  return false;
151  }
152  return false;
153 }
154 
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:78
QgsMapLayerType type() const
Returns the type of the layer.
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.