Skip to content

Commit

Permalink
[layouts] Warn when exporting a layout with a north arrow not
Browse files Browse the repository at this point in the history
linked to a map item
  • Loading branch information
nyalldawson authored and nirvn committed Jun 14, 2019
1 parent 0ccc8f1 commit ab92dee
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/app/layout/qgslayoutvaliditychecks.cpp
Expand Up @@ -73,6 +73,60 @@ QList<QgsValidityCheckResult> QgsLayoutScaleBarValidityCheck::runCheck( const Qg
}


//
// QgsLayoutNorthArrowValidityCheck
//

QgsLayoutNorthArrowValidityCheck *QgsLayoutNorthArrowValidityCheck::create() const
{
return new QgsLayoutNorthArrowValidityCheck();
}

QString QgsLayoutNorthArrowValidityCheck::id() const
{
return QStringLiteral( "layout_northarrow_check" );
}

int QgsLayoutNorthArrowValidityCheck::checkType() const
{
return QgsAbstractValidityCheck::TypeLayoutCheck;
}

bool QgsLayoutNorthArrowValidityCheck::prepareCheck( const QgsValidityCheckContext *context, QgsFeedback * )
{
if ( context->type() != QgsValidityCheckContext::TypeLayoutContext )
return false;

const QgsLayoutValidityCheckContext *layoutContext = static_cast< const QgsLayoutValidityCheckContext * >( context );
if ( !layoutContext )
return false;

QList< QgsLayoutItemPicture * > pictureItems;
layoutContext->layout->layoutItems( pictureItems );
for ( QgsLayoutItemPicture *picture : qgis::as_const( pictureItems ) )
{
// look for pictures which use the default north arrow svg, but aren't actually linked to maps
if ( !picture->linkedMap() && picture->picturePath() == QStringLiteral( ":/images/north_arrows/layout_default_north_arrow.svg" ) )
{
QgsValidityCheckResult res;
res.type = QgsValidityCheckResult::Warning;
res.title = QObject::tr( "North arrow is not linked to a map" );
const QString name = picture->displayName().toHtmlEscaped();
res.detailedDescription = QObject::tr( "The north arrow “%1” is not linked to a map item. The arrow orientation may be misleading." ).arg( name );
mResults.append( res );
}
}

return true;
}

QList<QgsValidityCheckResult> QgsLayoutNorthArrowValidityCheck::runCheck( const QgsValidityCheckContext *, QgsFeedback * )
{
return mResults;
}



//
// QgsLayoutOverviewValidityCheck
//
Expand Down
14 changes: 14 additions & 0 deletions src/app/layout/qgslayoutvaliditychecks.h
Expand Up @@ -31,6 +31,20 @@ class APP_EXPORT QgsLayoutScaleBarValidityCheck : public QgsAbstractValidityChec
QList<QgsValidityCheckResult> mResults;
};

class APP_EXPORT QgsLayoutNorthArrowValidityCheck : public QgsAbstractValidityCheck
{
public:

QgsLayoutNorthArrowValidityCheck *create() const override;
QString id() const override;
int checkType() const override;
bool prepareCheck( const QgsValidityCheckContext *context, QgsFeedback *feedback ) override;
QList< QgsValidityCheckResult > runCheck( const QgsValidityCheckContext *context, QgsFeedback *feedback ) override;

private:
QList<QgsValidityCheckResult> mResults;
};

class APP_EXPORT QgsLayoutOverviewValidityCheck : public QgsAbstractValidityCheck
{
public:
Expand Down
1 change: 1 addition & 0 deletions src/app/qgisapp.cpp
Expand Up @@ -1253,6 +1253,7 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh
}

QgsApplication::validityCheckRegistry()->addCheck( new QgsLayoutScaleBarValidityCheck() );
QgsApplication::validityCheckRegistry()->addCheck( new QgsLayoutNorthArrowValidityCheck() );
QgsApplication::validityCheckRegistry()->addCheck( new QgsLayoutOverviewValidityCheck() );
QgsApplication::validityCheckRegistry()->addCheck( new QgsLayoutPictureSourceValidityCheck() );

Expand Down
33 changes: 33 additions & 0 deletions tests/src/app/testqgsapplayoutvaliditychecks.cpp
Expand Up @@ -46,6 +46,7 @@ class TestQgsLayoutValidityChecks : public QObject
void cleanup() {} // will be called after every testfunction.

void testScaleBarValidity();
void testNorthArrowValidity();
void testOverviewValidity();
void testPictureValidity();

Expand Down Expand Up @@ -103,6 +104,38 @@ void TestQgsLayoutValidityChecks::testScaleBarValidity()
QCOMPARE( res.size(), 0 );
}

void TestQgsLayoutValidityChecks::testNorthArrowValidity()
{
QgsProject p;
QgsLayout l( &p );

QgsLayoutItemPicture *picture = new QgsLayoutItemPicture( &l );
// we identify this as a north arrow based on the default picture path pointing to the default north arrow
picture->setPicturePath( QStringLiteral( ":/images/north_arrows/layout_default_north_arrow.svg" ) );

l.addItem( picture );

QgsLayoutValidityCheckContext context( &l );
QgsFeedback f;

// scalebar not linked to map
QgsLayoutNorthArrowValidityCheck check;
QVERIFY( check.prepareCheck( &context, &f ) );
QList< QgsValidityCheckResult > res = check.runCheck( &context, &f );
QCOMPARE( res.size(), 1 );
QCOMPARE( res.at( 0 ).type, QgsValidityCheckResult::Warning );

// now link a map
QgsLayoutItemMap *map = new QgsLayoutItemMap( &l );
l.addItem( map );
picture->setLinkedMap( map );

QgsLayoutNorthArrowValidityCheck check2;
QVERIFY( check2.prepareCheck( &context, &f ) );
res = check2.runCheck( &context, &f );
QCOMPARE( res.size(), 0 );
}

void TestQgsLayoutValidityChecks::testOverviewValidity()
{
QgsProject p;
Expand Down

0 comments on commit ab92dee

Please sign in to comment.