QGIS API Documentation 3.99.0-Master (e9821da5c6b)
Loading...
Searching...
No Matches
qgslayoutvaliditychecks.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayoutvaliditychecks.cpp
3 ---------------------------
4 begin : November 2018
5 copyright : (C) 2018 Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8/***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
18
19#include "qgslayout.h"
20#include "qgslayoutitemmap.h"
23#include "qgslayoutmultiframe.h"
24#include "qgssettings.h"
26
27#include <QString>
28#include <QUrl>
29
30using namespace Qt::StringLiterals;
31
32//
33// QgsLayoutScaleBarValidityCheck
34//
35
40
42{
43 return u"layout_scalebar_check"_s;
44}
45
50
52{
54 return false;
55
56 const QgsLayoutValidityCheckContext *layoutContext = static_cast<const QgsLayoutValidityCheckContext *>( context );
57 if ( !layoutContext )
58 return false;
59
60 QList<QgsLayoutItemScaleBar *> barItems;
61 layoutContext->layout->layoutItems( barItems );
62 for ( QgsLayoutItemScaleBar *bar : std::as_const( barItems ) )
63 {
64 if ( !bar->linkedMap() )
65 {
68 res.title = QObject::tr( "Scalebar is not linked to a map" );
69 const QString name = bar->displayName().toHtmlEscaped();
70 res.detailedDescription = QObject::tr( "The scalebar “%1” is not linked to a map item. This scale will be misleading." ).arg( name );
71 mResults.append( res );
72 }
73 }
74
75 return true;
76}
77
79{
80 return mResults;
81}
82
83
84//
85// QgsLayoutNorthArrowValidityCheck
86//
87
92
94{
95 return u"layout_northarrow_check"_s;
96}
97
102
104{
106 return false;
107
108 const QgsLayoutValidityCheckContext *layoutContext = static_cast<const QgsLayoutValidityCheckContext *>( context );
109 if ( !layoutContext )
110 return false;
111
112 QgsSettings settings;
113 const QString defaultPath = settings.value( u"LayoutDesigner/defaultNorthArrow"_s, u":/images/north_arrows/layout_default_north_arrow.svg"_s, QgsSettings::Gui ).toString();
114
115 QList<QgsLayoutItemPicture *> pictureItems;
116 layoutContext->layout->layoutItems( pictureItems );
117 for ( QgsLayoutItemPicture *picture : std::as_const( pictureItems ) )
118 {
119 // look for pictures which use the default north arrow svg, but aren't actually linked to maps.
120 // alternatively identify them by looking for the default "North Arrow" string in their id
121 if ( !picture->linkedMap() && ( picture->picturePath() == defaultPath || picture->id().contains( QObject::tr( "North Arrow" ), Qt::CaseInsensitive ) ) )
122 {
125 res.title = QObject::tr( "North arrow is not linked to a map" );
126 const QString name = picture->displayName().toHtmlEscaped();
127 res.detailedDescription = QObject::tr( "The north arrow “%1” is not linked to a map item. The arrow orientation may be misleading." ).arg( name );
128 mResults.append( res );
129 }
130 }
131
132 return true;
133}
134
136{
137 return mResults;
138}
139
140
141//
142// QgsLayoutOverviewValidityCheck
143//
144
149
151{
152 return u"layout_overview_check"_s;
153}
154
159
161{
163 return false;
164
165 const QgsLayoutValidityCheckContext *layoutContext = static_cast<const QgsLayoutValidityCheckContext *>( context );
166 if ( !layoutContext )
167 return false;
168
169 QList<QgsLayoutItemMap *> mapItems;
170 layoutContext->layout->layoutItems( mapItems );
171 for ( QgsLayoutItemMap *map : std::as_const( mapItems ) )
172 {
173 for ( int i = 0; i < map->overviews()->size(); ++i )
174 {
175 QgsLayoutItemMapOverview *overview = map->overviews()->overview( i );
176 if ( overview && overview->enabled() && !overview->linkedMap() )
177 {
180 res.title = QObject::tr( "Overview is not linked to a map" );
181 const QString name = map->displayName().toHtmlEscaped();
182 res.detailedDescription = QObject::tr( "The map “%1” includes an overview (“%2”) which is not linked to a map item." ).arg( name, overview->name() );
183 mResults.append( res );
184 }
185 }
186 }
187
188 return true;
189}
190
192{
193 return mResults;
194}
195
196
197//
198// QgsLayoutPictureSourceValidityCheck
199//
200
205
207{
208 return u"layout_picture_source_check"_s;
209}
210
215
217{
219 return false;
220
221 const QgsLayoutValidityCheckContext *layoutContext = static_cast<const QgsLayoutValidityCheckContext *>( context );
222 if ( !layoutContext )
223 return false;
224
225 QList<QgsLayoutItemPicture *> pictureItems;
226 layoutContext->layout->layoutItems( pictureItems );
227 for ( QgsLayoutItemPicture *picture : std::as_const( pictureItems ) )
228 {
229 if ( picture->isMissingImage() )
230 {
233 res.title = QObject::tr( "Picture source is missing or corrupt" );
234 const QString name = picture->displayName().toHtmlEscaped();
235
236 const QUrl picUrl = QUrl::fromUserInput( picture->evaluatedPath() );
237 const bool isLocalFile = picUrl.isLocalFile();
238
239 res.detailedDescription = QObject::tr( "The source for picture “%1” could not be loaded or is corrupt:<p>%2" ).arg( name, isLocalFile ? QDir::toNativeSeparators( picture->evaluatedPath() ) : picture->evaluatedPath() );
240 mResults.append( res );
241 }
242 }
243
244 return true;
245}
246
248{
249 return mResults;
250}
251
252//
253// QgsLayoutHtmlItemValidityCheck
254//
255
260
262{
263 return u"layout_html_item_check"_s;
264}
265
270
272{
274 return false;
275
276 const QgsLayoutValidityCheckContext *layoutContext = static_cast<const QgsLayoutValidityCheckContext *>( context );
277 if ( !layoutContext )
278 return false;
279
280 const QList<QgsLayoutMultiFrame *> multiFrames = layoutContext->layout->multiFrames();
281 for ( QgsLayoutMultiFrame *multiFrame : std::as_const( multiFrames ) )
282 {
283 if ( multiFrame->type() == QgsLayoutItemRegistry::LayoutHtml && multiFrame->frameCount() > 0 )
284 {
287 res.title = QObject::tr( "HTML item cannot be rendered" );
288 res.detailedDescription = QObject::tr( "HTML items cannot be rendered because this QGIS install was built without WebKit support. These items will be missing from the export." );
289 mResults.append( res );
290 break;
291 }
292 }
293
294 return true;
295}
296
298{
299 return mResults;
300}
@ LayoutCheck
Print layout validity check, triggered on exporting a print layout.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
Layout HTML item validity check.
QList< QgsValidityCheckResult > runCheck(const QgsValidityCheckContext *context, QgsFeedback *feedback) override
Runs the check and returns a list of results.
QString id() const override
Returns the unique ID of the check.
QgsLayoutHtmlItemValidityCheck * create() const override
constructor
bool prepareCheck(const QgsValidityCheckContext *context, QgsFeedback *feedback) override
Prepares the check for execution, and returns true if the check can be run.
int checkType() const override
Returns the type of the check.
QString name() const
Returns the friendly display name for the item.
bool enabled() const
Returns whether the item will be drawn.
An individual overview which is drawn above the map content in a QgsLayoutItemMap,...
QgsLayoutItemMap * linkedMap()
Returns the source map to show the overview extent of.
Layout graphical items for displaying a map.
A layout item subclass that displays SVG files or raster format images (jpg, png, ....
@ LayoutHtml
Html multiframe item.
A layout item subclass for scale bars.
Abstract base class for layout items with the ability to distribute the content to several frames (Qg...
Layout north arrow validity check.
int checkType() const override
Returns the type of the check.
bool prepareCheck(const QgsValidityCheckContext *context, QgsFeedback *feedback) override
Prepares the check for execution, and returns true if the check can be run.
QString id() const override
Returns the unique ID of the check.
QgsLayoutNorthArrowValidityCheck * create() const override
constructor
QList< QgsValidityCheckResult > runCheck(const QgsValidityCheckContext *context, QgsFeedback *feedback) override
Runs the check and returns a list of results.
Layout overview validity check.
QList< QgsValidityCheckResult > runCheck(const QgsValidityCheckContext *context, QgsFeedback *feedback) override
Runs the check and returns a list of results.
int checkType() const override
Returns the type of the check.
QString id() const override
Returns the unique ID of the check.
QgsLayoutOverviewValidityCheck * create() const override
constructor
bool prepareCheck(const QgsValidityCheckContext *context, QgsFeedback *feedback) override
Prepares the check for execution, and returns true if the check can be run.
Layout picture source validity check.
bool prepareCheck(const QgsValidityCheckContext *context, QgsFeedback *feedback) override
Prepares the check for execution, and returns true if the check can be run.
QString id() const override
Returns the unique ID of the check.
QList< QgsValidityCheckResult > runCheck(const QgsValidityCheckContext *context, QgsFeedback *feedback) override
Runs the check and returns a list of results.
QgsLayoutPictureSourceValidityCheck * create() const override
constructor
int checkType() const override
Returns the type of the check.
Layout scalebar validity check.
QString id() const override
Returns the unique ID of the check.
bool prepareCheck(const QgsValidityCheckContext *context, QgsFeedback *feedback) override
Prepares the check for execution, and returns true if the check can be run.
QList< QgsValidityCheckResult > runCheck(const QgsValidityCheckContext *context, QgsFeedback *feedback) override
Runs the check and returns a list of results.
int checkType() const override
Returns the type of the check.
QgsLayoutScaleBarValidityCheck * create() const override
constructor
Validity check context for print layout validation.
QgsLayout * layout
Pointer to the layout which the check is being run against.
void layoutItems(QList< T * > &itemList) const
Returns a list of layout items of a specific type.
Definition qgslayout.h:121
QList< QgsLayoutMultiFrame * > multiFrames() const
Returns a list of multi frames contained in the layout.
Stores settings for use within QGIS.
Definition qgssettings.h:68
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Base class for validity check contexts.
virtual int type() const =0
Returns the context type.
@ TypeLayoutContext
Layout context, see QgsLayoutValidityCheckContext.
Represents an individual result from a validity check run by a QgsAbstractValidityCheck subclass.
QgsValidityCheckResult::Type type
Result type.
@ Warning
Warning only, allow operation to proceed but notify user of result.
QString detailedDescription
Detailed description of the result (translated), giving users enough detail for them to resolve the e...
QString title
A short, translated string summarising the result.