Skip to content

Commit d14a5bd

Browse files
committed
[layouts] Add inbuilt validity check to warn when scale bars are
not linked to a map item (and remove test Web mercator projection warning check)
1 parent ddd522b commit d14a5bd

File tree

5 files changed

+132
-19
lines changed

5 files changed

+132
-19
lines changed

src/app/layout/qgslayoutvaliditychecks.cpp

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,25 @@
1616

1717
#include "qgslayoutvaliditychecks.h"
1818
#include "qgsvaliditycheckcontext.h"
19-
#include "qgslayoutitemmap.h"
19+
#include "qgslayoutitemscalebar.h"
2020
#include "qgslayout.h"
2121

22-
QgsLayoutMapCrsValidityCheck *QgsLayoutMapCrsValidityCheck::create() const
22+
QgsLayoutScaleBarValidityCheck *QgsLayoutScaleBarValidityCheck::create() const
2323
{
24-
return new QgsLayoutMapCrsValidityCheck();
24+
return new QgsLayoutScaleBarValidityCheck();
2525
}
2626

27-
QString QgsLayoutMapCrsValidityCheck::id() const { return QStringLiteral( "map_crs_check" ); }
27+
QString QgsLayoutScaleBarValidityCheck::id() const
28+
{
29+
return QStringLiteral( "layout_scalebar_check" );
30+
}
2831

29-
int QgsLayoutMapCrsValidityCheck::checkType() const { return QgsAbstractValidityCheck::TypeLayoutCheck; }
32+
int QgsLayoutScaleBarValidityCheck::checkType() const
33+
{
34+
return QgsAbstractValidityCheck::TypeLayoutCheck;
35+
}
3036

31-
bool QgsLayoutMapCrsValidityCheck::prepareCheck( const QgsValidityCheckContext *context, QgsFeedback * )
37+
bool QgsLayoutScaleBarValidityCheck::prepareCheck( const QgsValidityCheckContext *context, QgsFeedback * )
3238
{
3339
if ( context->type() != QgsValidityCheckContext::TypeLayoutContext )
3440
return false;
@@ -37,24 +43,25 @@ bool QgsLayoutMapCrsValidityCheck::prepareCheck( const QgsValidityCheckContext *
3743
if ( !layoutContext )
3844
return false;
3945

40-
QList< QgsLayoutItemMap * > mapItems;
41-
layoutContext->layout->layoutItems( mapItems );
42-
for ( QgsLayoutItemMap *map : qgis::as_const( mapItems ) )
46+
QList< QgsLayoutItemScaleBar * > barItems;
47+
layoutContext->layout->layoutItems( barItems );
48+
for ( QgsLayoutItemScaleBar *bar : qgis::as_const( barItems ) )
4349
{
44-
if ( map->crs().authid() == QStringLiteral( "EPSG:3857" ) )
50+
if ( !bar->linkedMap() )
4551
{
4652
QgsValidityCheckResult res;
4753
res.type = QgsValidityCheckResult::Warning;
48-
res.title = tr( "Map projection is misleading" );
49-
res.detailedDescription = tr( "The projection for the map item %1 is set to <i>Web Mercator (EPSG:3857)</i> which misrepresents areas and shapes. Consider using an appropriate local projection instead." ).arg( map->displayName() );
54+
res.title = tr( "Scalebar is not linked to a map" );
55+
const QString name = bar->displayName().toHtmlEscaped();
56+
res.detailedDescription = tr( "The scalebar “%1” is not linked to a map item. This scale will be misleading." ).arg( name );
5057
mResults.append( res );
5158
}
5259
}
5360

5461
return true;
5562
}
5663

57-
QList<QgsValidityCheckResult> QgsLayoutMapCrsValidityCheck::runCheck( const QgsValidityCheckContext *, QgsFeedback * )
64+
QList<QgsValidityCheckResult> QgsLayoutScaleBarValidityCheck::runCheck( const QgsValidityCheckContext *, QgsFeedback * )
5865
{
5966
return mResults;
6067
}

src/app/layout/qgslayoutvaliditychecks.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@
1515
***************************************************************************/
1616

1717
#include "qgsabstractvaliditycheck.h"
18+
#include "qgis_app.h"
1819

19-
class QgsLayoutMapCrsValidityCheck : public QgsAbstractValidityCheck
20+
class APP_EXPORT QgsLayoutScaleBarValidityCheck : public QgsAbstractValidityCheck
2021
{
21-
Q_OBJECT
22-
2322
public:
2423

25-
QgsLayoutMapCrsValidityCheck *create() const override;
24+
QgsLayoutScaleBarValidityCheck *create() const override;
2625
QString id() const override;
2726
int checkType() const override;
2827
bool prepareCheck( const QgsValidityCheckContext *context, QgsFeedback *feedback ) override;

src/app/qgisapp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1233,7 +1233,7 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh
12331233
setIconSizes( size );
12341234
}
12351235

1236-
QgsApplication::validityCheckRegistry()->addCheck( new QgsLayoutMapCrsValidityCheck() );
1236+
QgsApplication::validityCheckRegistry()->addCheck( new QgsLayoutScaleBarValidityCheck() );
12371237

12381238
mSplash->showMessage( tr( "Initializing file filters" ), Qt::AlignHCenter | Qt::AlignBottom );
12391239
qApp->processEvents();

tests/src/app/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ INCLUDE_DIRECTORIES(
1212
${CMAKE_SOURCE_DIR}/src/core/metadata
1313
${CMAKE_SOURCE_DIR}/src/core/raster
1414
${CMAKE_SOURCE_DIR}/src/core/symbology
15+
${CMAKE_SOURCE_DIR}/src/core/validity
1516
${CMAKE_SOURCE_DIR}/src/ui
1617
${CMAKE_SOURCE_DIR}/src/gui
1718
${CMAKE_SOURCE_DIR}/src/gui/editorwidgets
@@ -113,4 +114,4 @@ ADD_QGIS_TEST(vectorlayersaveasdialogtest testqgsvectorlayersaveasdialog.cpp)
113114
ADD_QGIS_TEST(maptoolreverselinetest testqgsmaptoolreverseline.cpp)
114115
ADD_QGIS_TEST(maptooltrimextendfeaturetest testqgsmaptooltrimextendfeature.cpp)
115116
ADD_QGIS_TEST(projectproperties testqgsprojectproperties.cpp)
116-
117+
ADD_QGIS_TEST(layoutvaliditychecks testqgsapplayoutvaliditychecks.cpp)
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/***************************************************************************
2+
testqgsapplayoutvaliditychecks.cpp
3+
--------------------------------------
4+
Date : January 2019
5+
Copyright : (C) 2019 Nyall Dawson
6+
Email : nyall dot dawson at gmail dot com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
#include "qgstest.h"
16+
17+
#include <QObject>
18+
#include <QString>
19+
#include <QStringList>
20+
21+
//qgis includes...
22+
#include "qgsdataitem.h"
23+
#include "qgsapplication.h"
24+
#include "qgslogger.h"
25+
#include "qgsproject.h"
26+
#include "qgslayout.h"
27+
#include "qgslayoutitemscalebar.h"
28+
#include "qgslayoutitemmap.h"
29+
#include "qgsabstractvaliditycheck.h"
30+
#include "qgsvaliditycheckcontext.h"
31+
#include "layout/qgslayoutvaliditychecks.h"
32+
#include "qgsfeedback.h"
33+
34+
class TestQgsLayoutValidityChecks : public QObject
35+
{
36+
Q_OBJECT
37+
38+
public:
39+
TestQgsLayoutValidityChecks();
40+
41+
private slots:
42+
void initTestCase();// will be called before the first testfunction is executed.
43+
void cleanupTestCase();// will be called after the last testfunction was executed.
44+
void init() {} // will be called before each testfunction is executed.
45+
void cleanup() {} // will be called after every testfunction.
46+
47+
void testScaleBarValidity();
48+
49+
private:
50+
QString mTestDataDir;
51+
};
52+
53+
TestQgsLayoutValidityChecks::TestQgsLayoutValidityChecks() = default;
54+
55+
void TestQgsLayoutValidityChecks::initTestCase()
56+
{
57+
//
58+
// Runs once before any tests are run
59+
//
60+
// init QGIS's paths - true means that all path will be inited from prefix
61+
QgsApplication::init();
62+
QgsApplication::initQgis();
63+
QgsApplication::showSettings();
64+
65+
QString dataDir( TEST_DATA_DIR ); //defined in CmakeLists.txt
66+
mTestDataDir = dataDir + '/';
67+
}
68+
69+
void TestQgsLayoutValidityChecks::cleanupTestCase()
70+
{
71+
QgsApplication::exitQgis();
72+
}
73+
74+
void TestQgsLayoutValidityChecks::testScaleBarValidity()
75+
{
76+
QgsProject p;
77+
QgsLayout l( &p );
78+
79+
QgsLayoutItemScaleBar *scale = new QgsLayoutItemScaleBar( &l );
80+
l.addItem( scale );
81+
82+
QgsLayoutValidityCheckContext context( &l );
83+
QgsFeedback f;
84+
85+
// scalebar not linked to map
86+
QgsLayoutScaleBarValidityCheck check;
87+
QVERIFY( check.prepareCheck( &context, &f ) );
88+
QList< QgsValidityCheckResult > res = check.runCheck( &context, &f );
89+
QCOMPARE( res.size(), 1 );
90+
QCOMPARE( res.at( 0 ).type, QgsValidityCheckResult::Warning );
91+
92+
// now link a map
93+
QgsLayoutItemMap *map = new QgsLayoutItemMap( &l );
94+
l.addItem( map );
95+
scale->setLinkedMap( map );
96+
97+
QgsLayoutScaleBarValidityCheck check2;
98+
QVERIFY( check2.prepareCheck( &context, &f ) );
99+
res = check2.runCheck( &context, &f );
100+
QCOMPARE( res.size(), 0 );
101+
}
102+
103+
104+
105+
QGSTEST_MAIN( TestQgsLayoutValidityChecks )
106+
#include "testqgsapplayoutvaliditychecks.moc"

0 commit comments

Comments
 (0)