Skip to content
Permalink
Browse files

Show correct unit type in status bar

  • Loading branch information
nyalldawson committed Jul 22, 2017
1 parent 0ab3b8e commit 8044353aa7f219e89306a7062152616389c16164
@@ -396,6 +396,7 @@ class QgsUnitTypes
\param unit unit to encode
:return: encoded string
.. seealso:: decodeLayoutUnit()
.. versionadded:: 3.0
:rtype: str
%End

@@ -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

};

/************************************************************************
@@ -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 )
@@ -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
}
@@ -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 );

@@ -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
@@ -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 );
}

@@ -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.
You can’t perform that action at this time.