Skip to content
Permalink
Browse files

Add QComboBox subclass widget for selecting layout units

  • Loading branch information
nyalldawson committed Jul 18, 2017
1 parent cdec70b commit d14f3b9c137ab3e760b4c005adb7f4c4074249a8
@@ -280,6 +280,7 @@
%Include layout/qgslayoutitemguiregistry.sip
%Include layout/qgslayoutnewitempropertiesdialog.sip
%Include layout/qgslayoutruler.sip
%Include layout/qgslayoutunitscombobox.sip
%Include layout/qgslayoutview.sip
%Include layout/qgslayoutviewtool.sip
%Include layout/qgslayoutviewtooladditem.sip
@@ -0,0 +1,56 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/layout/qgslayoutunitscombobox.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/


class QgsLayoutUnitsComboBox : QComboBox
{
%Docstring
A custom combo box for selecting units for layout settings.

.. versionadded:: 3.0
%End

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

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

QgsUnitTypes::LayoutUnit unit() const;
%Docstring
Returns the unit currently selected in the combo box.
.. seealso:: setUnit()
:rtype: QgsUnitTypes.LayoutUnit
%End

void setUnit( QgsUnitTypes::LayoutUnit unit );
%Docstring
Sets the ``unit`` currently selected in the combo box.
.. seealso:: unit()
%End

signals:

void changed( QgsUnitTypes::LayoutUnit unit );
%Docstring
Emitted when the ``unit`` is changed.
%End

};

/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/layout/qgslayoutunitscombobox.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
@@ -38,6 +38,7 @@
#include "qgsmessagelog.h"
#include "qgsannotationregistry.h"
#include "qgssettings.h"
#include "qgsunittypes.h"

#include "gps/qgsgpsconnectionregistry.h"
#include "processing/qgsprocessingregistry.h"
@@ -135,6 +136,7 @@ void QgsApplication::init( QString customConfigPath )
qRegisterMetaType<QgsGeometry::Error>( "QgsGeometry::Error" );
qRegisterMetaType<QgsProcessingFeatureSourceDefinition>( "QgsProcessingFeatureSourceDefinition" );
qRegisterMetaType<QgsProcessingOutputLayerDefinition>( "QgsProcessingOutputLayerDefinition" );
qRegisterMetaType<QgsUnitTypes::LayoutUnit>( "QgsUnitTypes::LayoutUnit" );

QString prefixPath( getenv( "QGIS_PREFIX_PATH" ) ? getenv( "QGIS_PREFIX_PATH" ) : applicationDirPath() );
// QgsDebugMsg( QString( "prefixPath(): %1" ).arg( prefixPath ) );
@@ -161,6 +161,7 @@ SET(QGIS_GUI_SRCS
layout/qgslayoutitemguiregistry.cpp
layout/qgslayoutnewitempropertiesdialog.cpp
layout/qgslayoutruler.cpp
layout/qgslayoutunitscombobox.cpp
layout/qgslayoutview.cpp
layout/qgslayoutviewmouseevent.cpp
layout/qgslayoutviewrubberband.cpp
@@ -650,6 +651,7 @@ SET(QGIS_GUI_MOC_HDRS
layout/qgslayoutitemguiregistry.h
layout/qgslayoutnewitempropertiesdialog.h
layout/qgslayoutruler.h
layout/qgslayoutunitscombobox.h
layout/qgslayoutview.h
layout/qgslayoutviewtool.h
layout/qgslayoutviewtooladditem.h
@@ -0,0 +1,53 @@
/***************************************************************************
qgslayoutunitscombobox.cpp
--------------------------
Date : July 2017
Copyright : (C) 2017 Nyall Dawson
Email : nyall dot dawson at gmail dot 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 "qgslayoutunitscombobox.h"

QgsLayoutUnitsComboBox::QgsLayoutUnitsComboBox( QWidget *parent )
: QComboBox( parent )
{
addItem( tr( "mm" ), QgsUnitTypes::LayoutMillimeters );
setItemData( 0, tr( "Millimeters" ), Qt::ToolTipRole );
addItem( tr( "cm" ), QgsUnitTypes::LayoutCentimeters );
setItemData( 1, tr( "Centimeters" ), Qt::ToolTipRole );
addItem( tr( "m" ), QgsUnitTypes::LayoutMeters );
setItemData( 2, tr( "Meters" ), Qt::ToolTipRole );
addItem( tr( "in" ), QgsUnitTypes::LayoutInches );
setItemData( 3, tr( "Inches" ), Qt::ToolTipRole );
addItem( tr( "ft" ), QgsUnitTypes::LayoutFeet );
setItemData( 4, tr( "Feet" ), Qt::ToolTipRole );
addItem( tr( "pt" ), QgsUnitTypes::LayoutPoints );
setItemData( 5, tr( "Points" ), Qt::ToolTipRole );
addItem( tr( "pica" ), QgsUnitTypes::LayoutPicas );
setItemData( 6, tr( "Picas" ), Qt::ToolTipRole );
addItem( tr( "px" ), QgsUnitTypes::LayoutPixels );
setItemData( 7, tr( "Pixels" ), Qt::ToolTipRole );
connect( this, static_cast<void ( QgsLayoutUnitsComboBox::* )( int )>( &QgsLayoutUnitsComboBox::currentIndexChanged ), this, [ = ]( int )
{
emit changed( unit() );
} );
}

QgsUnitTypes::LayoutUnit QgsLayoutUnitsComboBox::unit() const
{
return static_cast< QgsUnitTypes::LayoutUnit >( currentData().toInt() );
}

void QgsLayoutUnitsComboBox::setUnit( QgsUnitTypes::LayoutUnit unit )
{
setCurrentIndex( findData( unit ) );
}

#include "qgslayoutunitscombobox.h"
@@ -0,0 +1,62 @@
/***************************************************************************
qgslayoutunitscombobox.h
------------------------
Date : July 2017
Copyright : (C) 2017 Nyall Dawson
Email : nyall dot dawson at gmail dot 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 QGSLAYOUTUNITSCOMBOBOX_H
#define QGSLAYOUTUNITSCOMBOBOX_H

#include <QComboBox>
#include "qgis_gui.h"
#include "qgis_sip.h"
#include "qgsunittypes.h"

/**
* \ingroup gui
* A custom combo box for selecting units for layout settings.
*
* \since QGIS 3.0
*/
class GUI_EXPORT QgsLayoutUnitsComboBox : public QComboBox
{
Q_OBJECT
Q_PROPERTY( QgsUnitTypes::LayoutUnit unit READ unit WRITE setUnit NOTIFY changed )

public:

/**
* Constructor for QgsLayoutUnitsComboBox.
*/
QgsLayoutUnitsComboBox( QWidget *parent SIP_TRANSFERTHIS = nullptr );

/**
* Returns the unit currently selected in the combo box.
* \see setUnit()
*/
QgsUnitTypes::LayoutUnit unit() const;

/**
* Sets the \a unit currently selected in the combo box.
* \see unit()
*/
void setUnit( QgsUnitTypes::LayoutUnit unit );

signals:

/**
* Emitted when the \a unit is changed.
*/
void changed( QgsUnitTypes::LayoutUnit unit );

};

#endif // QGSLAYOUTUNITSCOMBOBOX_H
@@ -70,12 +70,13 @@ ADD_PYTHON_TEST(PyQgsGeometryValidator test_qgsgeometryvalidator.py)
ADD_PYTHON_TEST(PyQgsGraduatedSymbolRenderer test_qgsgraduatedsymbolrenderer.py)
ADD_PYTHON_TEST(PyQgsInterval test_qgsinterval.py)
ADD_PYTHON_TEST(PyQgsJsonUtils test_qgsjsonutils.py)
ADD_PYTHON_TEST(PyQgsLayerMetadata test_qgslayermetadata.py)
ADD_PYTHON_TEST(PyQgsLayerTreeMapCanvasBridge test_qgslayertreemapcanvasbridge.py)
ADD_PYTHON_TEST(PyQgsLayerTree test_qgslayertree.py)
ADD_PYTHON_TEST(PyQgsLayoutManager test_qgslayoutmanager.py)
ADD_PYTHON_TEST(PyQgsLayoutView test_qgslayoutview.py)
ADD_PYTHON_TEST(PyQgsLayoutUnitsComboBox test_qgslayoutunitscombobox.py)
ADD_PYTHON_TEST(PyQgsLineSymbolLayers test_qgslinesymbollayers.py)
ADD_PYTHON_TEST(PyQgsLayerMetadata test_qgslayermetadata.py)
ADD_PYTHON_TEST(PyQgsLocator test_qgslocator.py)
ADD_PYTHON_TEST(PyQgsMapCanvas test_qgsmapcanvas.py)
ADD_PYTHON_TEST(PyQgsMapCanvasAnnotationItem test_qgsmapcanvasannotationitem.py)
@@ -0,0 +1,47 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsLayoutUnitsComboBox
.. note:: 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.
"""
__author__ = 'Nyall Dawson'
__date__ = '18/07/2017'
__copyright__ = 'Copyright 2017, The QGIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import qgis # NOQA

from qgis.core import QgsUnitTypes
from qgis.gui import QgsLayoutUnitsComboBox

from qgis.PyQt.QtTest import QSignalSpy
from qgis.testing import start_app, unittest

start_app()


class TestQgsLayoutUnitsComboBox(unittest.TestCase):

def testGettersSetters(self):
""" test widget getters/setters """
w = qgis.gui.QgsLayoutUnitsComboBox()

w.setUnit(QgsUnitTypes.LayoutPixels)
self.assertEqual(w.unit(), QgsUnitTypes.LayoutPixels)

def test_ChangedSignals(self):
""" test that signals are correctly emitted when setting unit"""
w = qgis.gui.QgsLayoutUnitsComboBox()

spy = QSignalSpy(w.changed)
w.setUnit(QgsUnitTypes.LayoutPixels)

self.assertEqual(len(spy), 1)
self.assertEqual(spy[0][0], QgsUnitTypes.LayoutPixels)


if __name__ == '__main__':
unittest.main()

0 comments on commit d14f3b9

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