QGIS API Documentation  3.0.2-Girona (307d082)
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 <QHBoxLayout>
19 #include <QSlider>
20 
22  : QWidget( parent )
23 {
24  QHBoxLayout *layout = new QHBoxLayout();
25  layout->setContentsMargins( 0, 0, 0, 0 );
26  layout->setSpacing( 3 );
27  setLayout( layout );
28 
29  mSlider = new QSlider();
30  mSlider->setMinimum( 0 );
31  mSlider->setMaximum( 1000 );
32  mSlider->setSingleStep( 10 );
33  mSlider->setPageStep( 100 );
34  mSlider->setValue( 1000 );
35  mSlider->setOrientation( Qt::Horizontal );
36  layout->addWidget( mSlider, 1 );
37 
38  mSpinBox = new QgsDoubleSpinBox();
39  mSpinBox->setMinimum( 0.0 );
40  mSpinBox->setMaximum( 100.0 );
41  mSpinBox->setValue( 100.0 );
42  mSpinBox->setClearValue( 100.0 );
43  mSpinBox->setMinimumSize( QSize( 100, 0 ) );
44  mSpinBox->setDecimals( 1 );
45  mSpinBox->setSuffix( tr( " %" ) );
46  layout->addWidget( mSpinBox, 0 );
47 
48  setFocusProxy( mSpinBox );
49 
50  connect( mSlider, &QSlider::valueChanged, this, [ = ]( int value ) { mSpinBox->setValue( value / 10.0 ); } );
51  connect( mSpinBox, static_cast < void ( QgsDoubleSpinBox::* )( double ) > ( &QgsDoubleSpinBox::valueChanged ), this, [ = ]( double value ) { whileBlocking( mSlider )->setValue( value * 10 ); } );
52  connect( mSpinBox, static_cast < void ( QgsDoubleSpinBox::* )( double ) > ( &QgsDoubleSpinBox::valueChanged ), this, &QgsOpacityWidget::spinChanged );
53 }
54 
55 double QgsOpacityWidget::opacity() const
56 {
57  return mSpinBox->value() / 100.0;
58 }
59 
61 {
62  mSpinBox->setValue( opacity * 100.0 );
63 }
64 
65 void QgsOpacityWidget::spinChanged( double value )
66 {
67  emit opacityChanged( value / 100.0 );
68 }
69 
The QgsSpinBox is a spin box with a clear button that will set the value to the defined clear value...
QgsOpacityWidget(QWidget *parent=nullptr)
Constructor for QgsOpacityWidget.
void opacityChanged(double opacity)
Emitted when the opacity is changed in the widget, where opacity ranges from 0.0 (transparent) to 1...
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 setOpacity(double opacity)
Sets the current opacity to show in the widget, where opacity ranges from 0.0 (transparent) to 1...
double opacity() const
Returns the current opacity selected 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:224