Skip to content
Permalink
Browse files

Create new QgsOpacityWidget widget

Allows consistent behavior and appearance across all opacity controls
  • Loading branch information
nyalldawson committed May 30, 2017
1 parent a326224 commit e58f25dbb9a7351e3b8113b97f9caff8581da3ba
@@ -128,6 +128,7 @@
%Include qgsnewnamedialog.sip
%Include qgsnewvectorlayerdialog.sip
%Include qgsnewgeopackagelayerdialog.sip
%Include qgsopacitywidget.sip
%Include qgsoptionsdialogbase.sip
%Include qgsoptionswidgetfactory.sip
%Include qgsorderbydialog.sip
@@ -0,0 +1,66 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/qgsopacitywidget.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/




class QgsOpacityWidget : QWidget
{
%Docstring
A widget for setting an opacity value.
.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgsopacitywidget.h"
%End
public:

explicit QgsOpacityWidget( QWidget *parent /TransferThis/ = 0 );
%Docstring
Constructor for QgsOpacityWidget.
%End

double opacity() const;
%Docstring
Returns the current opacity selected in the widget, where opacity ranges from 0.0 (transparent)
to 1.0 (opaque).
.. seealso:: setOpacity()
.. seealso:: opacityChanged()
:rtype: float
%End

public slots:

void setOpacity( double opacity );
%Docstring
Sets the current ``opacity`` to show in the widget, where ``opacity`` ranges from 0.0 (transparent)
to 1.0 (opaque).
.. seealso:: opacity()
.. seealso:: opacityChanged()
%End

signals:

void opacityChanged( double opacity );
%Docstring
Emitted when the ``opacity`` is changed in the widget, where ``opacity`` ranges from 0.0 (transparent)
to 1.0 (opaque).
.. seealso:: setOpacity()
.. seealso:: opacity()
%End

};

/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/qgsopacitywidget.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
@@ -26,6 +26,7 @@ SET (QGIS_CUSTOMWIDGETS_SRCS
qgsfilewidgetplugin.cpp
qgsfilterlineeditplugin.cpp
qgsmaplayercomboboxplugin.cpp
qgsopacitywidgetplugin.cpp
qgspasswordlineeditplugin.cpp
qgsprojectionselectionwidgetplugin.cpp
qgspropertyoverridebuttonplugin.cpp
@@ -54,6 +55,7 @@ SET (QGIS_CUSTOMWIDGETS_MOC_HDRS
qgsfilewidgetplugin.h
qgsfilterlineeditplugin.h
qgsmaplayercomboboxplugin.h
qgsopacitywidgetplugin.h
qgspasswordlineeditplugin.h
qgsprojectionselectionwidgetplugin.h
qgspropertyoverridebuttonplugin.h
@@ -87,6 +89,7 @@ SET(QGIS_CUSTOMWIDGETS_HDRS
qgsfilewidgetplugin.h
qgsfilterlineeditplugin.h
qgsmaplayercomboboxplugin.h
qgsopacitywidgetplugin.h
qgsprojectionselectionwidgetplugin.h
qgspropertyoverridebuttonplugin.h
qgsrasterbandcomboboxplugin.h
@@ -28,6 +28,7 @@
#include "qgsfilewidgetplugin.h"
#include "qgsfilterlineeditplugin.h"
#include "qgsmaplayercomboboxplugin.h"
#include "qgsopacitywidgetplugin.h"
#include "qgsprojectionselectionwidgetplugin.h"
#include "qgspropertyoverridebuttonplugin.h"
#include "qgsrasterbandcomboboxplugin.h"
@@ -53,6 +54,7 @@ QgisCustomWidgets::QgisCustomWidgets( QObject *parent )
mWidgets.append( new QgsFileWidgetPlugin( this ) );
mWidgets.append( new QgsFilterLineEditPlugin( this ) );
mWidgets.append( new QgsMapLayerComboBoxPlugin( this ) );
mWidgets.append( new QgsOpacityWidgetPlugin( this ) );
mWidgets.append( new QgsProjectionSelectionWidgetPlugin( this ) );
mWidgets.append( new QgsPropertyOverrideButtonPlugin( this ) );
mWidgets.append( new QgsRasterBandComboBoxPlugin( this ) );
@@ -0,0 +1,97 @@
/***************************************************************************
qgsopacitywidgetplugin.cpp
-------------------------
Date : 30.05.2017
Copyright : (C) 2017 Nyall Dawson
Email : nyall.dawson@gmail.com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgiscustomwidgets.h"
#include "qgsopacitywidget.h"
#include "qgsopacitywidgetplugin.h"


QgsOpacityWidgetPlugin::QgsOpacityWidgetPlugin( QObject *parent )
: QObject( parent )
, mInitialized( false )
{
}


QString QgsOpacityWidgetPlugin::name() const
{
return "QgsOpacityWidget";
}

QString QgsOpacityWidgetPlugin::group() const
{
return QgisCustomWidgets::groupName();
}

QString QgsOpacityWidgetPlugin::includeFile() const
{
return "qgsopacitywidget.h";
}

QIcon QgsOpacityWidgetPlugin::icon() const
{
return QIcon( ":/images/icons/qgis-icon-60x60.png" );
}

bool QgsOpacityWidgetPlugin::isContainer() const
{
return false;
}

QWidget *QgsOpacityWidgetPlugin::createWidget( QWidget *parent )
{
return new QgsOpacityWidget( parent );
}

bool QgsOpacityWidgetPlugin::isInitialized() const
{
return mInitialized;
}

void QgsOpacityWidgetPlugin::initialize( QDesignerFormEditorInterface *core )
{
Q_UNUSED( core );
if ( mInitialized )
return;
mInitialized = true;
}


QString QgsOpacityWidgetPlugin::toolTip() const
{
return tr( "A widget for specifying an opacity value." );
}

QString QgsOpacityWidgetPlugin::whatsThis() const
{
return tr( "A widget for specifying an opacity value." );
}

QString QgsOpacityWidgetPlugin::domXml() const
{
return QString( "<ui language=\"c++\">\n"
" <widget class=\"%1\" name=\"mOpacityWidget\">\n"
" <property name=\"geometry\">\n"
" <rect>\n"
" <x>0</x>\n"
" <y>0</y>\n"
" <width>160</width>\n"
" <height>27</height>\n"
" </rect>\n"
" </property>\n"
" </widget>\n"
"</ui>\n" )
.arg( name() );
}
@@ -0,0 +1,51 @@
/***************************************************************************
qgsopacitywidgetplugin.h
-----------------------
Date : 30.05.2017
Copyright : (C) 2017 Nyall Dawson
Email : nyall.dawson@gmail.com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSOPACITYWIDGETPLUGIN_H
#define QGSOPACITYWIDGETPLUGIN_H


#include <QtGlobal>
#include <QtUiPlugin/QDesignerCustomWidgetInterface>
#include <QtUiPlugin/QDesignerExportWidget>
#include "qgis_customwidgets.h"


class CUSTOMWIDGETS_EXPORT QgsOpacityWidgetPlugin : public QObject, public QDesignerCustomWidgetInterface
{
Q_OBJECT
Q_INTERFACES( QDesignerCustomWidgetInterface )

public:
explicit QgsOpacityWidgetPlugin( QObject *parent = 0 );

private:
bool mInitialized;

// QDesignerCustomWidgetInterface interface
public:
QString name() const override;
QString group() const override;
QString includeFile() const override;
QIcon icon() const override;
bool isContainer() const override;
QWidget *createWidget( QWidget *parent ) override;
bool isInitialized() const override;
void initialize( QDesignerFormEditorInterface *core ) override;
QString toolTip() const override;
QString whatsThis() const override;
QString domXml() const override;
};
#endif // QGSOPACITYWIDGETPLUGIN_H
@@ -273,6 +273,7 @@ SET(QGIS_GUI_SRCS
qgsnewnamedialog.cpp
qgsnewvectorlayerdialog.cpp
qgsnewgeopackagelayerdialog.cpp
qgsopacitywidget.cpp
qgsoptionsdialogbase.cpp
qgsorderbydialog.cpp
qgsowssourceselect.cpp
@@ -421,6 +422,7 @@ SET(QGIS_GUI_MOC_HDRS
qgsnewnamedialog.h
qgsnewvectorlayerdialog.h
qgsnewgeopackagelayerdialog.h
qgsopacitywidget.h
qgsoptionsdialogbase.h
qgsoptionswidgetfactory.h
qgsorderbydialog.h
@@ -0,0 +1,69 @@
/***************************************************************************
qgsopacitywidget.cpp
-------------------
Date : May 2017
Copyright : (C) 2017 Nyall Dawson
Email : nyall.dawson@gmail.com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsopacitywidget.h"
#include "qgsdoublespinbox.h"
#include <QHBoxLayout>
#include <QSlider>

QgsOpacityWidget::QgsOpacityWidget( QWidget *parent )
: QWidget( parent )
{
QHBoxLayout *layout = new QHBoxLayout();
layout->setContentsMargins( 0, 0, 0, 0 );
layout->setSpacing( 0 );
setLayout( layout );

mSlider = new QSlider();
mSlider->setMinimum( 0 );
mSlider->setMaximum( 1000 );
mSlider->setSingleStep( 10 );
mSlider->setPageStep( 100 );
mSlider->setValue( 1000 );
mSlider->setOrientation( Qt::Horizontal );
layout->addWidget( mSlider, 1 );

mSpinBox = new QgsDoubleSpinBox();
mSpinBox->setMinimum( 0.0 );
mSpinBox->setMaximum( 100.0 );
mSpinBox->setValue( 100.0 );
mSpinBox->setClearValue( 100.0 );
mSpinBox->setMinimumSize( QSize( 100, 0 ) );
mSpinBox->setDecimals( 1 );
mSpinBox->setSuffix( tr( " %" ) );
layout->addWidget( mSpinBox, 0 );

setFocusProxy( mSpinBox );

connect( mSlider, &QSlider::valueChanged, this, [ = ]( int value ) { mSpinBox->setValue( value / 10.0 ); } );
connect( mSpinBox, static_cast < void ( QgsDoubleSpinBox::* )( double ) > ( &QgsDoubleSpinBox::valueChanged ), this, [ = ]( double value ) { whileBlocking( mSlider )->setValue( value * 10 ); } );
connect( mSpinBox, static_cast < void ( QgsDoubleSpinBox::* )( double ) > ( &QgsDoubleSpinBox::valueChanged ), this, &QgsOpacityWidget::spinChanged );
}

double QgsOpacityWidget::opacity() const
{
return mSpinBox->value() / 100.0;
}

void QgsOpacityWidget::setOpacity( double opacity )
{
mSpinBox->setValue( opacity * 100.0 );
}

void QgsOpacityWidget::spinChanged( double value )
{
emit opacityChanged( value / 100.0 );
}

0 comments on commit e58f25d

Please sign in to comment.
You can’t perform that action at this time.