Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add QComboBox subclass widget for selecting layout units
- Loading branch information
1 parent
cdec70b
commit d14f3b9
Showing
8 changed files
with
225 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 * | ||
************************************************************************/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |