QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 QgsLayerTreeTransparencyWidget::QgsLayerTreeTransparencyWidget( QgsMapLayer* layer )
31  : mLayer( layer )
32 {
33  setAutoFillBackground( true ); // override the content from model
34  QLabel* l = new QLabel( "Transparency", this );
35  mSlider = new QSlider( Qt::Horizontal, this );
36  mSlider->setRange( 0, 100 );
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, SIGNAL( timeout() ), this, SLOT( updateTransparencyFromSlider() ) );
47 
48  connect( mSlider, SIGNAL( valueChanged( int ) ), this, SLOT( sliderValueChanged( int ) ) );
49 
50  // init from layer
51  if ( mLayer->type() == QgsMapLayer::VectorLayer )
52  {
53  mSlider->setValue( qobject_cast<QgsVectorLayer*>( mLayer )->layerTransparency() );
54  connect( mLayer, SIGNAL( layerTransparencyChanged( int ) ), this, SLOT( layerTrChanged() ) );
55  }
56  else if ( mLayer->type() == QgsMapLayer::RasterLayer )
57  {
58  mSlider->setValue( 100 - qobject_cast<QgsRasterLayer*>( mLayer )->renderer()->opacity() * 100 );
59  // TODO: there is no signal for raster layers
60  }
61 }
62 
63 QSize QgsLayerTreeTransparencyWidget::sizeHint() const
64 {
65  return QWidget::sizeHint();
66  //return QSize(200,200); // horizontal seems ignored, vertical is used for spacing
67 }
68 
69 void QgsLayerTreeTransparencyWidget::sliderValueChanged( int value )
70 {
71  Q_UNUSED( value );
72 
73  if ( mTimer->isActive() )
74  return;
75  mTimer->start();
76 }
77 
78 void QgsLayerTreeTransparencyWidget::updateTransparencyFromSlider()
79 {
80  int value = mSlider->value();
81 
82  if ( mLayer->type() == QgsMapLayer::VectorLayer )
83  {
84  qobject_cast<QgsVectorLayer*>( mLayer )->setLayerTransparency( value );
85  }
86  else if ( mLayer->type() == QgsMapLayer::RasterLayer )
87  {
88  qobject_cast<QgsRasterLayer*>( mLayer )->renderer()->setOpacity( 1 - value / 100. );
89  }
90 
91  mLayer->triggerRepaint();
92 }
93 
94 void QgsLayerTreeTransparencyWidget::layerTrChanged()
95 {
96  mSlider->blockSignals( true );
97  mSlider->setValue( qobject_cast<QgsVectorLayer*>( mLayer )->layerTransparency() );
98  mSlider->blockSignals( false );
99 }
100 
101 //
102 
103 QString QgsLayerTreeTransparencyWidget::Provider::id() const
104 {
105  return "transparency";
106 }
107 
108 QString QgsLayerTreeTransparencyWidget::Provider::name() const
109 {
110  return tr( "Transparency slider" );
111 }
112 
113 QgsLayerTreeTransparencyWidget* QgsLayerTreeTransparencyWidget::Provider::createWidget( QgsMapLayer* layer, int widgetIndex )
114 {
115  Q_UNUSED( widgetIndex );
116  return new QgsLayerTreeTransparencyWidget( layer );
117 }
118 
119 bool QgsLayerTreeTransparencyWidget::Provider::supportsLayer( QgsMapLayer* layer )
120 {
121  return layer->type() == QgsMapLayer::VectorLayer || layer->type() == QgsMapLayer::RasterLayer;
122 }
123 
Base class for all map layer types.
Definition: qgsmaplayer.h:49
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
QgsMapLayer::LayerType type() const
Get the type of the layer.
Definition: qgsmaplayer.cpp:99
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
void triggerRepaint()
Will advice the map canvas (and any other interested party) that this layer requires to be repainted...
virtual QSize sizeHint() const
Represents a vector layer which manages a vector based data sets.