Skip to content

Commit d14f3b9

Browse files
committedJul 18, 2017
Add QComboBox subclass widget for selecting layout units
1 parent cdec70b commit d14f3b9

File tree

8 files changed

+225
-1
lines changed

8 files changed

+225
-1
lines changed
 

‎python/gui/gui_auto.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@
280280
%Include layout/qgslayoutitemguiregistry.sip
281281
%Include layout/qgslayoutnewitempropertiesdialog.sip
282282
%Include layout/qgslayoutruler.sip
283+
%Include layout/qgslayoutunitscombobox.sip
283284
%Include layout/qgslayoutview.sip
284285
%Include layout/qgslayoutviewtool.sip
285286
%Include layout/qgslayoutviewtooladditem.sip
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/************************************************************************
2+
* This file has been generated automatically from *
3+
* *
4+
* src/gui/layout/qgslayoutunitscombobox.h *
5+
* *
6+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
7+
************************************************************************/
8+
9+
10+
class QgsLayoutUnitsComboBox : QComboBox
11+
{
12+
%Docstring
13+
A custom combo box for selecting units for layout settings.
14+
15+
.. versionadded:: 3.0
16+
%End
17+
18+
%TypeHeaderCode
19+
#include "qgslayoutunitscombobox.h"
20+
%End
21+
public:
22+
23+
QgsLayoutUnitsComboBox( QWidget *parent /TransferThis/ = 0 );
24+
%Docstring
25+
Constructor for QgsLayoutUnitsComboBox.
26+
%End
27+
28+
QgsUnitTypes::LayoutUnit unit() const;
29+
%Docstring
30+
Returns the unit currently selected in the combo box.
31+
.. seealso:: setUnit()
32+
:rtype: QgsUnitTypes.LayoutUnit
33+
%End
34+
35+
void setUnit( QgsUnitTypes::LayoutUnit unit );
36+
%Docstring
37+
Sets the ``unit`` currently selected in the combo box.
38+
.. seealso:: unit()
39+
%End
40+
41+
signals:
42+
43+
void changed( QgsUnitTypes::LayoutUnit unit );
44+
%Docstring
45+
Emitted when the ``unit`` is changed.
46+
%End
47+
48+
};
49+
50+
/************************************************************************
51+
* This file has been generated automatically from *
52+
* *
53+
* src/gui/layout/qgslayoutunitscombobox.h *
54+
* *
55+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
56+
************************************************************************/

‎src/core/qgsapplication.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "qgsmessagelog.h"
3939
#include "qgsannotationregistry.h"
4040
#include "qgssettings.h"
41+
#include "qgsunittypes.h"
4142

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

139141
QString prefixPath( getenv( "QGIS_PREFIX_PATH" ) ? getenv( "QGIS_PREFIX_PATH" ) : applicationDirPath() );
140142
// QgsDebugMsg( QString( "prefixPath(): %1" ).arg( prefixPath ) );

‎src/gui/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ SET(QGIS_GUI_SRCS
161161
layout/qgslayoutitemguiregistry.cpp
162162
layout/qgslayoutnewitempropertiesdialog.cpp
163163
layout/qgslayoutruler.cpp
164+
layout/qgslayoutunitscombobox.cpp
164165
layout/qgslayoutview.cpp
165166
layout/qgslayoutviewmouseevent.cpp
166167
layout/qgslayoutviewrubberband.cpp
@@ -650,6 +651,7 @@ SET(QGIS_GUI_MOC_HDRS
650651
layout/qgslayoutitemguiregistry.h
651652
layout/qgslayoutnewitempropertiesdialog.h
652653
layout/qgslayoutruler.h
654+
layout/qgslayoutunitscombobox.h
653655
layout/qgslayoutview.h
654656
layout/qgslayoutviewtool.h
655657
layout/qgslayoutviewtooladditem.h
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/***************************************************************************
2+
qgslayoutunitscombobox.cpp
3+
--------------------------
4+
Date : July 2017
5+
Copyright : (C) 2017 Nyall Dawson
6+
Email : nyall dot dawson at gmail dot com
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 "qgslayoutunitscombobox.h"
17+
18+
QgsLayoutUnitsComboBox::QgsLayoutUnitsComboBox( QWidget *parent )
19+
: QComboBox( parent )
20+
{
21+
addItem( tr( "mm" ), QgsUnitTypes::LayoutMillimeters );
22+
setItemData( 0, tr( "Millimeters" ), Qt::ToolTipRole );
23+
addItem( tr( "cm" ), QgsUnitTypes::LayoutCentimeters );
24+
setItemData( 1, tr( "Centimeters" ), Qt::ToolTipRole );
25+
addItem( tr( "m" ), QgsUnitTypes::LayoutMeters );
26+
setItemData( 2, tr( "Meters" ), Qt::ToolTipRole );
27+
addItem( tr( "in" ), QgsUnitTypes::LayoutInches );
28+
setItemData( 3, tr( "Inches" ), Qt::ToolTipRole );
29+
addItem( tr( "ft" ), QgsUnitTypes::LayoutFeet );
30+
setItemData( 4, tr( "Feet" ), Qt::ToolTipRole );
31+
addItem( tr( "pt" ), QgsUnitTypes::LayoutPoints );
32+
setItemData( 5, tr( "Points" ), Qt::ToolTipRole );
33+
addItem( tr( "pica" ), QgsUnitTypes::LayoutPicas );
34+
setItemData( 6, tr( "Picas" ), Qt::ToolTipRole );
35+
addItem( tr( "px" ), QgsUnitTypes::LayoutPixels );
36+
setItemData( 7, tr( "Pixels" ), Qt::ToolTipRole );
37+
connect( this, static_cast<void ( QgsLayoutUnitsComboBox::* )( int )>( &QgsLayoutUnitsComboBox::currentIndexChanged ), this, [ = ]( int )
38+
{
39+
emit changed( unit() );
40+
} );
41+
}
42+
43+
QgsUnitTypes::LayoutUnit QgsLayoutUnitsComboBox::unit() const
44+
{
45+
return static_cast< QgsUnitTypes::LayoutUnit >( currentData().toInt() );
46+
}
47+
48+
void QgsLayoutUnitsComboBox::setUnit( QgsUnitTypes::LayoutUnit unit )
49+
{
50+
setCurrentIndex( findData( unit ) );
51+
}
52+
53+
#include "qgslayoutunitscombobox.h"
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/***************************************************************************
2+
qgslayoutunitscombobox.h
3+
------------------------
4+
Date : July 2017
5+
Copyright : (C) 2017 Nyall Dawson
6+
Email : nyall dot dawson at gmail dot com
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+
#ifndef QGSLAYOUTUNITSCOMBOBOX_H
16+
#define QGSLAYOUTUNITSCOMBOBOX_H
17+
18+
#include <QComboBox>
19+
#include "qgis_gui.h"
20+
#include "qgis_sip.h"
21+
#include "qgsunittypes.h"
22+
23+
/**
24+
* \ingroup gui
25+
* A custom combo box for selecting units for layout settings.
26+
*
27+
* \since QGIS 3.0
28+
*/
29+
class GUI_EXPORT QgsLayoutUnitsComboBox : public QComboBox
30+
{
31+
Q_OBJECT
32+
Q_PROPERTY( QgsUnitTypes::LayoutUnit unit READ unit WRITE setUnit NOTIFY changed )
33+
34+
public:
35+
36+
/**
37+
* Constructor for QgsLayoutUnitsComboBox.
38+
*/
39+
QgsLayoutUnitsComboBox( QWidget *parent SIP_TRANSFERTHIS = nullptr );
40+
41+
/**
42+
* Returns the unit currently selected in the combo box.
43+
* \see setUnit()
44+
*/
45+
QgsUnitTypes::LayoutUnit unit() const;
46+
47+
/**
48+
* Sets the \a unit currently selected in the combo box.
49+
* \see unit()
50+
*/
51+
void setUnit( QgsUnitTypes::LayoutUnit unit );
52+
53+
signals:
54+
55+
/**
56+
* Emitted when the \a unit is changed.
57+
*/
58+
void changed( QgsUnitTypes::LayoutUnit unit );
59+
60+
};
61+
62+
#endif // QGSLAYOUTUNITSCOMBOBOX_H

‎tests/src/python/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,13 @@ ADD_PYTHON_TEST(PyQgsGeometryValidator test_qgsgeometryvalidator.py)
7070
ADD_PYTHON_TEST(PyQgsGraduatedSymbolRenderer test_qgsgraduatedsymbolrenderer.py)
7171
ADD_PYTHON_TEST(PyQgsInterval test_qgsinterval.py)
7272
ADD_PYTHON_TEST(PyQgsJsonUtils test_qgsjsonutils.py)
73+
ADD_PYTHON_TEST(PyQgsLayerMetadata test_qgslayermetadata.py)
7374
ADD_PYTHON_TEST(PyQgsLayerTreeMapCanvasBridge test_qgslayertreemapcanvasbridge.py)
7475
ADD_PYTHON_TEST(PyQgsLayerTree test_qgslayertree.py)
7576
ADD_PYTHON_TEST(PyQgsLayoutManager test_qgslayoutmanager.py)
7677
ADD_PYTHON_TEST(PyQgsLayoutView test_qgslayoutview.py)
78+
ADD_PYTHON_TEST(PyQgsLayoutUnitsComboBox test_qgslayoutunitscombobox.py)
7779
ADD_PYTHON_TEST(PyQgsLineSymbolLayers test_qgslinesymbollayers.py)
78-
ADD_PYTHON_TEST(PyQgsLayerMetadata test_qgslayermetadata.py)
7980
ADD_PYTHON_TEST(PyQgsLocator test_qgslocator.py)
8081
ADD_PYTHON_TEST(PyQgsMapCanvas test_qgsmapcanvas.py)
8182
ADD_PYTHON_TEST(PyQgsMapCanvasAnnotationItem test_qgsmapcanvasannotationitem.py)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# -*- coding: utf-8 -*-
2+
"""QGIS Unit tests for QgsLayoutUnitsComboBox
3+
4+
.. note:: This program is free software; you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation; either version 2 of the License, or
7+
(at your option) any later version.
8+
"""
9+
__author__ = 'Nyall Dawson'
10+
__date__ = '18/07/2017'
11+
__copyright__ = 'Copyright 2017, The QGIS Project'
12+
# This will get replaced with a git SHA1 when you do a git archive
13+
__revision__ = '$Format:%H$'
14+
15+
import qgis # NOQA
16+
17+
from qgis.core import QgsUnitTypes
18+
from qgis.gui import QgsLayoutUnitsComboBox
19+
20+
from qgis.PyQt.QtTest import QSignalSpy
21+
from qgis.testing import start_app, unittest
22+
23+
start_app()
24+
25+
26+
class TestQgsLayoutUnitsComboBox(unittest.TestCase):
27+
28+
def testGettersSetters(self):
29+
""" test widget getters/setters """
30+
w = qgis.gui.QgsLayoutUnitsComboBox()
31+
32+
w.setUnit(QgsUnitTypes.LayoutPixels)
33+
self.assertEqual(w.unit(), QgsUnitTypes.LayoutPixels)
34+
35+
def test_ChangedSignals(self):
36+
""" test that signals are correctly emitted when setting unit"""
37+
w = qgis.gui.QgsLayoutUnitsComboBox()
38+
39+
spy = QSignalSpy(w.changed)
40+
w.setUnit(QgsUnitTypes.LayoutPixels)
41+
42+
self.assertEqual(len(spy), 1)
43+
self.assertEqual(spy[0][0], QgsUnitTypes.LayoutPixels)
44+
45+
46+
if __name__ == '__main__':
47+
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.