QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
qgsopacitywidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsopacitywidget.cpp
3  -------------------
4  Date : May 2017
5  Copyright : (C) 2017 Nyall Dawson
6  Email : [email protected]
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 
16 #include "qgsopacitywidget.h"
17 #include "qgsdoublespinbox.h"
18 #include "qgis.h"
19 #include <QHBoxLayout>
20 #include <QSlider>
21 
23  : QWidget( parent )
24 {
25  QHBoxLayout *layout = new QHBoxLayout();
26  layout->setContentsMargins( 0, 0, 0, 0 );
27  layout->setSpacing( 3 );
28  setLayout( layout );
29 
30  mSlider = new QSlider();
31  mSlider->setMinimum( 0 );
32  mSlider->setMaximum( 1000 );
33  mSlider->setSingleStep( 10 );
34  mSlider->setPageStep( 100 );
35  mSlider->setValue( 1000 );
36  mSlider->setOrientation( Qt::Horizontal );
37  layout->addWidget( mSlider, 1 );
38 
39  mSpinBox = new QgsDoubleSpinBox();
40  mSpinBox->setMinimum( 0.0 );
41  mSpinBox->setMaximum( 100.0 );
42  mSpinBox->setValue( 100.0 );
43  mSpinBox->setClearValue( 100.0 );
44  mSpinBox->setMinimumSize( QSize( 100, 0 ) );
45  mSpinBox->setDecimals( 1 );
46  mSpinBox->setSuffix( tr( " %" ) );
47  layout->addWidget( mSpinBox, 0 );
48 
49  setFocusProxy( mSpinBox );
50 
51  connect( mSlider, &QSlider::valueChanged, this, [ = ]( int value ) { mSpinBox->setValue( value / 10.0 ); } );
52  connect( mSpinBox, static_cast < void ( QgsDoubleSpinBox::* )( double ) > ( &QgsDoubleSpinBox::valueChanged ), this, [ = ]( double value ) { whileBlocking( mSlider )->setValue( value * 10 ); } );
53  connect( mSpinBox, static_cast < void ( QgsDoubleSpinBox::* )( double ) > ( &QgsDoubleSpinBox::valueChanged ), this, &QgsOpacityWidget::spinChanged );
54 }
55 
57 {
58  return mSpinBox->value() / 100.0;
59 }
60 
61 void QgsOpacityWidget::setOpacity( double opacity )
62 {
63  mSpinBox->setValue( opacity * 100.0 );
64 }
65 
66 void QgsOpacityWidget::spinChanged( double value )
67 {
68  emit opacityChanged( value / 100.0 );
69 }
70 
The QgsSpinBox is a spin box with a clear button that will set the value to the defined clear value.
void setClearValue(double customValue, const QString &clearValueText=QString())
Defines the clear value as a custom value and will automatically set the clear value mode to CustomVa...
void opacityChanged(double opacity)
Emitted when the opacity is changed in the widget, where opacity ranges from 0.0 (transparent) to 1....
QgsOpacityWidget(QWidget *parent=nullptr)
Constructor for QgsOpacityWidget.
void setOpacity(double opacity)
Sets the current opacity to show in the widget, where opacity ranges from 0.0 (transparent) to 1....
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
Definition: qgis.h:263