Skip to content

Commit

Permalink
Create new QgsOpacityWidget widget
Browse files Browse the repository at this point in the history
Allows consistent behavior and appearance across all opacity controls
  • Loading branch information
nyalldawson committed May 30, 2017
1 parent a326224 commit e58f25d
Show file tree
Hide file tree
Showing 11 changed files with 433 additions and 0 deletions.
1 change: 1 addition & 0 deletions python/gui/gui.sip
Expand Up @@ -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
Expand Down
66 changes: 66 additions & 0 deletions python/gui/qgsopacitywidget.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 *
************************************************************************/
3 changes: 3 additions & 0 deletions src/customwidgets/CMakeLists.txt
Expand Up @@ -26,6 +26,7 @@ SET (QGIS_CUSTOMWIDGETS_SRCS
qgsfilewidgetplugin.cpp
qgsfilterlineeditplugin.cpp
qgsmaplayercomboboxplugin.cpp
qgsopacitywidgetplugin.cpp
qgspasswordlineeditplugin.cpp
qgsprojectionselectionwidgetplugin.cpp
qgspropertyoverridebuttonplugin.cpp
Expand Down Expand Up @@ -54,6 +55,7 @@ SET (QGIS_CUSTOMWIDGETS_MOC_HDRS
qgsfilewidgetplugin.h
qgsfilterlineeditplugin.h
qgsmaplayercomboboxplugin.h
qgsopacitywidgetplugin.h
qgspasswordlineeditplugin.h
qgsprojectionselectionwidgetplugin.h
qgspropertyoverridebuttonplugin.h
Expand Down Expand Up @@ -87,6 +89,7 @@ SET(QGIS_CUSTOMWIDGETS_HDRS
qgsfilewidgetplugin.h
qgsfilterlineeditplugin.h
qgsmaplayercomboboxplugin.h
qgsopacitywidgetplugin.h
qgsprojectionselectionwidgetplugin.h
qgspropertyoverridebuttonplugin.h
qgsrasterbandcomboboxplugin.h
Expand Down
2 changes: 2 additions & 0 deletions src/customwidgets/qgiscustomwidgets.cpp
Expand Up @@ -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"
Expand All @@ -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 ) );
Expand Down
97 changes: 97 additions & 0 deletions src/customwidgets/qgsopacitywidgetplugin.cpp
@@ -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() );
}
51 changes: 51 additions & 0 deletions src/customwidgets/qgsopacitywidgetplugin.h
@@ -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
2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Expand Up @@ -273,6 +273,7 @@ SET(QGIS_GUI_SRCS
qgsnewnamedialog.cpp
qgsnewvectorlayerdialog.cpp
qgsnewgeopackagelayerdialog.cpp
qgsopacitywidget.cpp
qgsoptionsdialogbase.cpp
qgsorderbydialog.cpp
qgsowssourceselect.cpp
Expand Down Expand Up @@ -421,6 +422,7 @@ SET(QGIS_GUI_MOC_HDRS
qgsnewnamedialog.h
qgsnewvectorlayerdialog.h
qgsnewgeopackagelayerdialog.h
qgsopacitywidget.h
qgsoptionsdialogbase.h
qgsoptionswidgetfactory.h
qgsorderbydialog.h
Expand Down
69 changes: 69 additions & 0 deletions src/gui/qgsopacitywidget.cpp
@@ -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.