QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 #include "qgsmaplayer.h"
24 
26 
27 QgsLayerTreeOpacityWidget::QgsLayerTreeOpacityWidget( QgsMapLayer *layer )
28  : mLayer( layer )
29 {
30  setAutoFillBackground( true ); // override the content from model
31  QLabel *l = new QLabel( tr( "Opacity" ), this );
32  mSlider = new QSlider( Qt::Horizontal, this );
33  mSlider->setRange( 0, 1000 );
34  const int sliderW = static_cast< int >( QFontMetricsF( font() ).horizontalAdvance( 'X' ) * 16 * Qgis::UI_SCALE_FACTOR );
35  mSlider->setMinimumWidth( sliderW / 2 );
36  mSlider->setMaximumWidth( sliderW );
37  QHBoxLayout *lay = new QHBoxLayout();
38  QSpacerItem *spacerItem = new QSpacerItem( 1, 0, QSizePolicy::MinimumExpanding, QSizePolicy::Minimum );
39  lay->addWidget( l );
40  lay->addWidget( mSlider );
41  lay->addItem( spacerItem );
42  setLayout( lay );
43 
44  // timer for delayed transparency update - for more responsive GUI
45  mTimer = new QTimer( this );
46  mTimer->setSingleShot( true );
47  mTimer->setInterval( 100 );
48  connect( mTimer, &QTimer::timeout, this, &QgsLayerTreeOpacityWidget::updateOpacityFromSlider );
49 
50  connect( mSlider, &QAbstractSlider::valueChanged, this, &QgsLayerTreeOpacityWidget::sliderValueChanged );
51 
52  // init from layer
53  mSlider->setValue( mLayer->opacity() * 1000.0 );
54  connect( mLayer, &QgsMapLayer::opacityChanged, this, &QgsLayerTreeOpacityWidget::layerTrChanged );
55 }
56 
57 QSize QgsLayerTreeOpacityWidget::sizeHint() const
58 {
59  return QWidget::sizeHint();
60  //return QSize(200,200); // horizontal seems ignored, vertical is used for spacing
61 }
62 
63 void QgsLayerTreeOpacityWidget::sliderValueChanged( int value )
64 {
65  Q_UNUSED( value )
66 
67  if ( mTimer->isActive() )
68  return;
69  mTimer->start();
70 }
71 
72 void QgsLayerTreeOpacityWidget::updateOpacityFromSlider()
73 {
74  if ( !mLayer )
75  return;
76  const int value = mSlider->value();
77  mLayer->setOpacity( value / 1000.0 );
78  mLayer->triggerRepaint();
79 }
80 
81 void QgsLayerTreeOpacityWidget::layerTrChanged()
82 {
83  if ( !mLayer )
84  return;
85  mSlider->blockSignals( true );
86  mSlider->setValue( mLayer->opacity() * 1000.0 );
87  mSlider->blockSignals( false );
88 }
89 
90 //
91 
92 QString QgsLayerTreeOpacityWidget::Provider::id() const
93 {
94  return QStringLiteral( "transparency" );
95 }
96 
97 QString QgsLayerTreeOpacityWidget::Provider::name() const
98 {
99  return tr( "Opacity slider" );
100 }
101 
102 QgsLayerTreeOpacityWidget *QgsLayerTreeOpacityWidget::Provider::createWidget( QgsMapLayer *layer, int widgetIndex )
103 {
104  Q_UNUSED( widgetIndex )
105  return new QgsLayerTreeOpacityWidget( layer );
106 }
107 
108 bool QgsLayerTreeOpacityWidget::Provider::supportsLayer( QgsMapLayer * )
109 {
110  return true;
111 }
112 
qgsmaplayer.h
Qgis::UI_SCALE_FACTOR
static const double UI_SCALE_FACTOR
UI scaling factor.
Definition: qgis.h:2043
QgsMapLayer::opacityChanged
void opacityChanged(double opacity)
Emitted when the layer's opacity is changed, where opacity is a value between 0 (transparent) and 1 (...
QgsMapLayer
Base class for all map layer types. This is the base class for all map layer types (vector,...
Definition: qgsmaplayer.h:72
qgslayertreeembeddedwidgetsimpl.h