QGIS API Documentation  3.0.2-Girona (307d082)
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  if ( mLayer->type() == QgsMapLayer::VectorLayer )
52  {
53  QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( mLayer );
54  mSlider->setValue( vl->opacity() * 1000.0 );
55  connect( vl, &QgsVectorLayer::opacityChanged, this, &QgsLayerTreeOpacityWidget::layerTrChanged );
56  }
57  else if ( mLayer->type() == QgsMapLayer::RasterLayer )
58  {
59  mSlider->setValue( 1000 - qobject_cast<QgsRasterLayer *>( mLayer )->renderer()->opacity() * 1000 );
60  // TODO: there is no signal for raster layers
61  }
62 }
63 
64 QSize QgsLayerTreeOpacityWidget::sizeHint() const
65 {
66  return QWidget::sizeHint();
67  //return QSize(200,200); // horizontal seems ignored, vertical is used for spacing
68 }
69 
70 void QgsLayerTreeOpacityWidget::sliderValueChanged( int value )
71 {
72  Q_UNUSED( value );
73 
74  if ( mTimer->isActive() )
75  return;
76  mTimer->start();
77 }
78 
79 void QgsLayerTreeOpacityWidget::updateOpacityFromSlider()
80 {
81  int value = mSlider->value();
82 
83  if ( mLayer->type() == QgsMapLayer::VectorLayer )
84  {
85  qobject_cast<QgsVectorLayer *>( mLayer )->setOpacity( value / 1000.0 );
86  }
87  else if ( mLayer->type() == QgsMapLayer::RasterLayer )
88  {
89  qobject_cast<QgsRasterLayer *>( mLayer )->renderer()->setOpacity( 1 - value / 1000.0 );
90  }
91 
92  mLayer->triggerRepaint();
93 }
94 
95 void QgsLayerTreeOpacityWidget::layerTrChanged()
96 {
97  mSlider->blockSignals( true );
98  mSlider->setValue( qobject_cast<QgsVectorLayer *>( mLayer )->opacity() * 1000.0 );
99  mSlider->blockSignals( false );
100 }
101 
102 //
103 
104 QString QgsLayerTreeOpacityWidget::Provider::id() const
105 {
106  return QStringLiteral( "transparency" );
107 }
108 
109 QString QgsLayerTreeOpacityWidget::Provider::name() const
110 {
111  return tr( "Opacity slider" );
112 }
113 
114 QgsLayerTreeOpacityWidget *QgsLayerTreeOpacityWidget::Provider::createWidget( QgsMapLayer *layer, int widgetIndex )
115 {
116  Q_UNUSED( widgetIndex );
117  return new QgsLayerTreeOpacityWidget( layer );
118 }
119 
120 bool QgsLayerTreeOpacityWidget::Provider::supportsLayer( QgsMapLayer *layer )
121 {
122  return layer->type() == QgsMapLayer::VectorLayer || layer->type() == QgsMapLayer::RasterLayer;
123 }
124 
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:56
This class provides qgis with the ability to render raster datasets onto the mapcanvas.
QgsMapLayer::LayerType type() const
Returns the type of the layer.
void triggerRepaint(bool deferredUpdate=false)
Will advise the map canvas (and any other interested party) that this layer requires to be repainted...
Represents a vector layer which manages a vector based data sets.