Skip to content

Commit

Permalink
Port orientation decoding code from composer
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Dec 17, 2017
1 parent 447a949 commit 65f4c4a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
7 changes: 7 additions & 0 deletions python/core/layout/qgslayoututils.sip
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,13 @@ the a specified ``rotation`` amount.
:return: largest scaled version of the rectangle possible
%End

static QgsLayoutItemPage::Orientation decodePaperOrientation( const QString &string, bool &ok );
%Docstring
Decodes a ``string`` representing a paper orientation and returns the
decoded orientation.
If the string was correctly decoded, ``ok`` will be set to true.
:rtype: QgsLayoutItemPage.Orientation
%End

};

Expand Down
16 changes: 16 additions & 0 deletions src/core/layout/qgslayoututils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,23 @@ QRectF QgsLayoutUtils::largestRotatedRectWithinBounds( const QRectF &originalRec
offsetY += std::fabs( minY );

return QRectF( offsetX, offsetY, rectScaledWidth, rectScaledHeight );
}

QgsLayoutItemPage::Orientation QgsLayoutUtils::decodePaperOrientation( const QString &string, bool &ok )
{
QString s = string.trimmed();
if ( s.compare( QLatin1String( "Portrait" ), Qt::CaseInsensitive ) == 0 )
{
ok = true;
return QgsLayoutItemPage::Portrait;
}
else if ( s.compare( QLatin1String( "Landscape" ), Qt::CaseInsensitive ) == 0 )
{
ok = true;
return QgsLayoutItemPage::Landscape;
}
ok = false;
return QgsLayoutItemPage::Landscape; // default to landscape
}

double QgsLayoutUtils::pointsToMM( const double pointSize )
Expand Down
7 changes: 7 additions & 0 deletions src/core/layout/qgslayoututils.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#define QGSLAYOUTUTILS_H

#include "qgis_core.h"
#include "qgslayoutitempage.h"
#include <QFont>
#include <QColor>

Expand Down Expand Up @@ -198,6 +199,12 @@ class CORE_EXPORT QgsLayoutUtils
*/
static QRectF largestRotatedRectWithinBounds( const QRectF &originalRect, const QRectF &boundsRect, const double rotation );

/**
* Decodes a \a string representing a paper orientation and returns the
* decoded orientation.
* If the string was correctly decoded, \a ok will be set to true.
*/
static QgsLayoutItemPage::Orientation decodePaperOrientation( const QString &string, bool &ok );

private:

Expand Down
18 changes: 18 additions & 0 deletions tests/src/core/testqgslayoututils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class TestQgsLayoutUtils: public QObject
void drawTextPos(); //test drawing text at a pos
void drawTextRect(); //test drawing text in a rect
void largestRotatedRect(); //test largest rotated rect helper function
void decodePaperOrientation();

private:

Expand Down Expand Up @@ -609,6 +610,23 @@ void TestQgsLayoutUtils::largestRotatedRect()
}
}

void TestQgsLayoutUtils::decodePaperOrientation()
{
QgsLayoutItemPage::Orientation orientation;
bool ok = false;
orientation = QgsLayoutUtils::decodePaperOrientation( QStringLiteral( "bad string" ), ok );
QVERIFY( !ok );
QCOMPARE( orientation, QgsLayoutItemPage::Landscape ); //should default to landscape
ok = false;
orientation = QgsLayoutUtils::decodePaperOrientation( QStringLiteral( "portrait" ), ok );
QVERIFY( ok );
QCOMPARE( orientation, QgsLayoutItemPage::Portrait );
ok = false;
orientation = QgsLayoutUtils::decodePaperOrientation( QStringLiteral( " LANDSCAPE " ), ok );
QVERIFY( ok );
QCOMPARE( orientation, QgsLayoutItemPage::Landscape );
}

bool TestQgsLayoutUtils::renderCheck( const QString &testName, QImage &image, int mismatchCount )
{
mReport += "<h2>" + testName + "</h2>\n";
Expand Down

0 comments on commit 65f4c4a

Please sign in to comment.