Skip to content

Commit ab92dee

Browse files
nyalldawsonnirvn
authored andcommittedJun 14, 2019
[layouts] Warn when exporting a layout with a north arrow not
linked to a map item
1 parent 0ccc8f1 commit ab92dee

File tree

4 files changed

+102
-0
lines changed

4 files changed

+102
-0
lines changed
 

‎src/app/layout/qgslayoutvaliditychecks.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,60 @@ QList<QgsValidityCheckResult> QgsLayoutScaleBarValidityCheck::runCheck( const Qg
7373
}
7474

7575

76+
//
77+
// QgsLayoutNorthArrowValidityCheck
78+
//
79+
80+
QgsLayoutNorthArrowValidityCheck *QgsLayoutNorthArrowValidityCheck::create() const
81+
{
82+
return new QgsLayoutNorthArrowValidityCheck();
83+
}
84+
85+
QString QgsLayoutNorthArrowValidityCheck::id() const
86+
{
87+
return QStringLiteral( "layout_northarrow_check" );
88+
}
89+
90+
int QgsLayoutNorthArrowValidityCheck::checkType() const
91+
{
92+
return QgsAbstractValidityCheck::TypeLayoutCheck;
93+
}
94+
95+
bool QgsLayoutNorthArrowValidityCheck::prepareCheck( const QgsValidityCheckContext *context, QgsFeedback * )
96+
{
97+
if ( context->type() != QgsValidityCheckContext::TypeLayoutContext )
98+
return false;
99+
100+
const QgsLayoutValidityCheckContext *layoutContext = static_cast< const QgsLayoutValidityCheckContext * >( context );
101+
if ( !layoutContext )
102+
return false;
103+
104+
QList< QgsLayoutItemPicture * > pictureItems;
105+
layoutContext->layout->layoutItems( pictureItems );
106+
for ( QgsLayoutItemPicture *picture : qgis::as_const( pictureItems ) )
107+
{
108+
// look for pictures which use the default north arrow svg, but aren't actually linked to maps
109+
if ( !picture->linkedMap() && picture->picturePath() == QStringLiteral( ":/images/north_arrows/layout_default_north_arrow.svg" ) )
110+
{
111+
QgsValidityCheckResult res;
112+
res.type = QgsValidityCheckResult::Warning;
113+
res.title = QObject::tr( "North arrow is not linked to a map" );
114+
const QString name = picture->displayName().toHtmlEscaped();
115+
res.detailedDescription = QObject::tr( "The north arrow “%1” is not linked to a map item. The arrow orientation may be misleading." ).arg( name );
116+
mResults.append( res );
117+
}
118+
}
119+
120+
return true;
121+
}
122+
123+
QList<QgsValidityCheckResult> QgsLayoutNorthArrowValidityCheck::runCheck( const QgsValidityCheckContext *, QgsFeedback * )
124+
{
125+
return mResults;
126+
}
127+
128+
129+
76130
//
77131
// QgsLayoutOverviewValidityCheck
78132
//

‎src/app/layout/qgslayoutvaliditychecks.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ class APP_EXPORT QgsLayoutScaleBarValidityCheck : public QgsAbstractValidityChec
3131
QList<QgsValidityCheckResult> mResults;
3232
};
3333

34+
class APP_EXPORT QgsLayoutNorthArrowValidityCheck : public QgsAbstractValidityCheck
35+
{
36+
public:
37+
38+
QgsLayoutNorthArrowValidityCheck *create() const override;
39+
QString id() const override;
40+
int checkType() const override;
41+
bool prepareCheck( const QgsValidityCheckContext *context, QgsFeedback *feedback ) override;
42+
QList< QgsValidityCheckResult > runCheck( const QgsValidityCheckContext *context, QgsFeedback *feedback ) override;
43+
44+
private:
45+
QList<QgsValidityCheckResult> mResults;
46+
};
47+
3448
class APP_EXPORT QgsLayoutOverviewValidityCheck : public QgsAbstractValidityCheck
3549
{
3650
public:

‎src/app/qgisapp.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,6 +1253,7 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh
12531253
}
12541254

12551255
QgsApplication::validityCheckRegistry()->addCheck( new QgsLayoutScaleBarValidityCheck() );
1256+
QgsApplication::validityCheckRegistry()->addCheck( new QgsLayoutNorthArrowValidityCheck() );
12561257
QgsApplication::validityCheckRegistry()->addCheck( new QgsLayoutOverviewValidityCheck() );
12571258
QgsApplication::validityCheckRegistry()->addCheck( new QgsLayoutPictureSourceValidityCheck() );
12581259

‎tests/src/app/testqgsapplayoutvaliditychecks.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class TestQgsLayoutValidityChecks : public QObject
4646
void cleanup() {} // will be called after every testfunction.
4747

4848
void testScaleBarValidity();
49+
void testNorthArrowValidity();
4950
void testOverviewValidity();
5051
void testPictureValidity();
5152

@@ -103,6 +104,38 @@ void TestQgsLayoutValidityChecks::testScaleBarValidity()
103104
QCOMPARE( res.size(), 0 );
104105
}
105106

107+
void TestQgsLayoutValidityChecks::testNorthArrowValidity()
108+
{
109+
QgsProject p;
110+
QgsLayout l( &p );
111+
112+
QgsLayoutItemPicture *picture = new QgsLayoutItemPicture( &l );
113+
// we identify this as a north arrow based on the default picture path pointing to the default north arrow
114+
picture->setPicturePath( QStringLiteral( ":/images/north_arrows/layout_default_north_arrow.svg" ) );
115+
116+
l.addItem( picture );
117+
118+
QgsLayoutValidityCheckContext context( &l );
119+
QgsFeedback f;
120+
121+
// scalebar not linked to map
122+
QgsLayoutNorthArrowValidityCheck check;
123+
QVERIFY( check.prepareCheck( &context, &f ) );
124+
QList< QgsValidityCheckResult > res = check.runCheck( &context, &f );
125+
QCOMPARE( res.size(), 1 );
126+
QCOMPARE( res.at( 0 ).type, QgsValidityCheckResult::Warning );
127+
128+
// now link a map
129+
QgsLayoutItemMap *map = new QgsLayoutItemMap( &l );
130+
l.addItem( map );
131+
picture->setLinkedMap( map );
132+
133+
QgsLayoutNorthArrowValidityCheck check2;
134+
QVERIFY( check2.prepareCheck( &context, &f ) );
135+
res = check2.runCheck( &context, &f );
136+
QCOMPARE( res.size(), 0 );
137+
}
138+
106139
void TestQgsLayoutValidityChecks::testOverviewValidity()
107140
{
108141
QgsProject p;

0 commit comments

Comments
 (0)
Please sign in to comment.