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