Skip to content
Permalink
Browse files

Port orientation decoding code from composer

  • Loading branch information
nyalldawson committed Dec 15, 2017
1 parent 447a949 commit 65f4c4acef97d1a9c298e1cac8228ff54b728885
@@ -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

};

@@ -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 )
@@ -18,6 +18,7 @@
#define QGSLAYOUTUTILS_H

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

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

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

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

0 comments on commit 65f4c4a

Please sign in to comment.
You can’t perform that action at this time.