Skip to content

Commit

Permalink
Show correct unit type in status bar
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 25, 2017
1 parent 0ab3b8e commit 8044353
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 19 deletions.
20 changes: 20 additions & 0 deletions python/core/qgsunittypes.sip
Expand Up @@ -396,6 +396,7 @@ class QgsUnitTypes
\param unit unit to encode
:return: encoded string
.. seealso:: decodeLayoutUnit()
.. versionadded:: 3.0
:rtype: str
%End

Expand All @@ -406,15 +407,34 @@ class QgsUnitTypes
\param ok optional boolean, will be set to true if string was converted successfully
:return: decoded units
.. seealso:: encodeUnit()
.. versionadded:: 3.0
:rtype: LayoutUnit
%End

static LayoutUnitType unitType( const LayoutUnit units );
%Docstring
Returns the type for a unit of measurement.

.. versionadded:: 3.0
:rtype: LayoutUnitType
%End

static QString toAbbreviatedString( LayoutUnit unit );
%Docstring
Returns a translated abbreviation representing a layout ``unit`` (e.g. "mm").

.. versionadded:: 3.0
:rtype: str
%End

static QString toString( LayoutUnit unit );
%Docstring
Returns a translated string representing a layout ``unit``.

.. versionadded:: 3.0
:rtype: str
%End

};

/************************************************************************
Expand Down
7 changes: 4 additions & 3 deletions src/app/layout/qgslayoutdesignerdialog.cpp
Expand Up @@ -431,9 +431,10 @@ void QgsLayoutDesignerDialog::updateStatusCursorPos( QPointF position )
QPointF pagePosition = mLayout->pageCollection()->positionOnPage( position );
int currentPage = mLayout->pageCollection()->pageNumberForPoint( position );

mStatusCursorXLabel->setText( QString( tr( "x: %1 mm" ) ).arg( pagePosition.x() ) );
mStatusCursorYLabel->setText( QString( tr( "y: %1 mm" ) ).arg( pagePosition.y() ) );
mStatusCursorPageLabel->setText( QString( tr( "page: %1" ) ).arg( currentPage + 1 ) );
QString unit = QgsUnitTypes::toAbbreviatedString( mLayout->units() );
mStatusCursorXLabel->setText( tr( "x: %1 %2" ).arg( pagePosition.x() ).arg( unit ) );
mStatusCursorYLabel->setText( tr( "y: %1 %2" ).arg( pagePosition.y() ).arg( unit ) );
mStatusCursorPageLabel->setText( tr( "page: %1" ).arg( currentPage + 1 ) );
}

void QgsLayoutDesignerDialog::toggleFullScreen( bool enabled )
Expand Down
48 changes: 48 additions & 0 deletions src/core/qgsunittypes.cpp
Expand Up @@ -1859,3 +1859,51 @@ QgsUnitTypes::LayoutUnitType QgsUnitTypes::unitType( const QgsUnitTypes::LayoutU
// avoid warnings
return LayoutPaperUnits;
}

QString QgsUnitTypes::toAbbreviatedString( QgsUnitTypes::LayoutUnit unit )
{
switch ( unit )
{
case LayoutPixels:
return QObject::tr( "px" );
case LayoutMillimeters:
return QObject::tr( "mm" );
case LayoutCentimeters:
return QObject::tr( "cm" );
case LayoutMeters:
return QObject::tr( "m" );
case LayoutInches:
return QObject::tr( "in" );
case LayoutFeet:
return QObject::tr( "ft" );
case LayoutPoints:
return QObject::tr( "pt" );
case LayoutPicas:
return QObject::tr( "pica" );
}
return QString(); // no warnings
}

QString QgsUnitTypes::toString( QgsUnitTypes::LayoutUnit unit )
{
switch ( unit )
{
case LayoutPixels:
return QObject::tr( "pixels" );
case LayoutMillimeters:
return QObject::tr( "millimeters" );
case LayoutCentimeters:
return QObject::tr( "centimeters" );
case LayoutMeters:
return QObject::tr( "meters" );
case LayoutInches:
return QObject::tr( "inches" );
case LayoutFeet:
return QObject::tr( "feet" );
case LayoutPoints:
return QObject::tr( "points" );
case LayoutPicas:
return QObject::tr( "picas" );
}
return QString(); // no warnings
}
18 changes: 18 additions & 0 deletions src/core/qgsunittypes.h
Expand Up @@ -384,6 +384,7 @@ class CORE_EXPORT QgsUnitTypes
* \param unit unit to encode
* \returns encoded string
* \see decodeLayoutUnit()
* \since QGIS 3.0
*/
Q_INVOKABLE static QString encodeUnit( LayoutUnit unit );

Expand All @@ -392,14 +393,31 @@ class CORE_EXPORT QgsUnitTypes
* \param ok optional boolean, will be set to true if string was converted successfully
* \returns decoded units
* \see encodeUnit()
* \since QGIS 3.0
*/
Q_INVOKABLE static LayoutUnit decodeLayoutUnit( const QString &string, bool *ok SIP_OUT = 0 );

/**
* Returns the type for a unit of measurement.
*
* \since QGIS 3.0
*/
Q_INVOKABLE static LayoutUnitType unitType( const LayoutUnit units );

/**
* Returns a translated abbreviation representing a layout \a unit (e.g. "mm").
*
* \since QGIS 3.0
*/
Q_INVOKABLE static QString toAbbreviatedString( LayoutUnit unit );

/**
* Returns a translated string representing a layout \a unit.
*
* \since QGIS 3.0
*/
Q_INVOKABLE static QString toString( LayoutUnit unit );

};

#endif // QGSUNITTYPES_H
31 changes: 15 additions & 16 deletions src/gui/layout/qgslayoutunitscombobox.cpp
Expand Up @@ -19,22 +19,21 @@
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 );
QList< QgsUnitTypes::LayoutUnit > units;
units << QgsUnitTypes::LayoutMillimeters
<< QgsUnitTypes::LayoutCentimeters
<< QgsUnitTypes::LayoutMeters
<< QgsUnitTypes::LayoutInches
<< QgsUnitTypes::LayoutFeet
<< QgsUnitTypes::LayoutPoints
<< QgsUnitTypes::LayoutPicas
<< QgsUnitTypes::LayoutPixels;

Q_FOREACH ( QgsUnitTypes::LayoutUnit u, units )
{
addItem( QgsUnitTypes::toAbbreviatedString( u ), u );
setItemData( count() - 1, QgsUnitTypes::toString( u ), Qt::ToolTipRole );
}
connect( this, static_cast<void ( QgsLayoutUnitsComboBox::* )( int )>( &QgsLayoutUnitsComboBox::currentIndexChanged ), this, &QgsLayoutUnitsComboBox::indexChanged );
}

Expand Down
18 changes: 18 additions & 0 deletions tests/src/python/test_qgsunittypes.py
Expand Up @@ -660,6 +660,24 @@ def testEncodeDecodeLayoutUnits(self):
assert ok
self.assertEqual(res, QgsUnitTypes.LayoutPixels)

def testAbbreviateLayoutUnits(self):
"""Test abbreviating layout units"""
units = [QgsUnitTypes.LayoutMillimeters,
QgsUnitTypes.LayoutCentimeters,
QgsUnitTypes.LayoutMeters,
QgsUnitTypes.LayoutInches,
QgsUnitTypes.LayoutFeet,
QgsUnitTypes.LayoutPoints,
QgsUnitTypes.LayoutPicas,
QgsUnitTypes.LayoutPixels]

used = set()
for u in units:
self.assertTrue(QgsUnitTypes.toString(u))
self.assertTrue(QgsUnitTypes.toAbbreviatedString(u))
self.assertFalse(QgsUnitTypes.toAbbreviatedString(u) in used)
used.add(QgsUnitTypes.toAbbreviatedString(u))


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

0 comments on commit 8044353

Please sign in to comment.