QGIS API Documentation 3.41.0-Master (cea29feecf2)
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
20#include "qgslayoutitemmap.h"
22#ifndef WITH_QTWEBKIT
23#include "qgslayoutmultiframe.h"
24#endif
25#include "qgslayout.h"
26#include "qgssettings.h"
27#include <QUrl>
28
29//
30// QgsLayoutScaleBarValidityCheck
31//
32
37
39{
40 return QStringLiteral( "layout_scalebar_check" );
41}
42
47
49{
51 return false;
52
53 const QgsLayoutValidityCheckContext *layoutContext = static_cast<const QgsLayoutValidityCheckContext *>( context );
54 if ( !layoutContext )
55 return false;
56
57 QList<QgsLayoutItemScaleBar *> barItems;
58 layoutContext->layout->layoutItems( barItems );
59 for ( QgsLayoutItemScaleBar *bar : std::as_const( barItems ) )
60 {
61 if ( !bar->linkedMap() )
62 {
65 res.title = QObject::tr( "Scalebar is not linked to a map" );
66 const QString name = bar->displayName().toHtmlEscaped();
67 res.detailedDescription = QObject::tr( "The scalebar “%1” is not linked to a map item. This scale will be misleading." ).arg( name );
68 mResults.append( res );
69 }
70 }
71
72 return true;
73}
74
76{
77 return mResults;
78}
79
80
81//
82// QgsLayoutNorthArrowValidityCheck
83//
84
89
91{
92 return QStringLiteral( "layout_northarrow_check" );
93}
94
99
101{
103 return false;
104
105 const QgsLayoutValidityCheckContext *layoutContext = static_cast<const QgsLayoutValidityCheckContext *>( context );
106 if ( !layoutContext )
107 return false;
108
109 QgsSettings settings;
110 const QString defaultPath = settings.value( QStringLiteral( "LayoutDesigner/defaultNorthArrow" ), QStringLiteral( ":/images/north_arrows/layout_default_north_arrow.svg" ), QgsSettings::Gui ).toString();
111
112 QList<QgsLayoutItemPicture *> pictureItems;
113 layoutContext->layout->layoutItems( pictureItems );
114 for ( QgsLayoutItemPicture *picture : std::as_const( pictureItems ) )
115 {
116 // look for pictures which use the default north arrow svg, but aren't actually linked to maps.
117 // alternatively identify them by looking for the default "North Arrow" string in their id
118 if ( !picture->linkedMap() && ( picture->picturePath() == defaultPath || picture->id().contains( QObject::tr( "North Arrow" ), Qt::CaseInsensitive ) ) )
119 {
122 res.title = QObject::tr( "North arrow is not linked to a map" );
123 const QString name = picture->displayName().toHtmlEscaped();
124 res.detailedDescription = QObject::tr( "The north arrow “%1” is not linked to a map item. The arrow orientation may be misleading." ).arg( name );
125 mResults.append( res );
126 }
127 }
128
129 return true;
130}
131
133{
134 return mResults;
135}
136
137
138//
139// QgsLayoutOverviewValidityCheck
140//
141
146
148{
149 return QStringLiteral( "layout_overview_check" );
150}
151
156
158{
160 return false;
161
162 const QgsLayoutValidityCheckContext *layoutContext = static_cast<const QgsLayoutValidityCheckContext *>( context );
163 if ( !layoutContext )
164 return false;
165
166 QList<QgsLayoutItemMap *> mapItems;
167 layoutContext->layout->layoutItems( mapItems );
168 for ( QgsLayoutItemMap *map : std::as_const( mapItems ) )
169 {
170 for ( int i = 0; i < map->overviews()->size(); ++i )
171 {
172 QgsLayoutItemMapOverview *overview = map->overviews()->overview( i );
173 if ( overview && overview->enabled() && !overview->linkedMap() )
174 {
177 res.title = QObject::tr( "Overview is not linked to a map" );
178 const QString name = map->displayName().toHtmlEscaped();
179 res.detailedDescription = QObject::tr( "The map “%1” includes an overview (“%2”) which is not linked to a map item." ).arg( name, overview->name() );
180 mResults.append( res );
181 }
182 }
183 }
184
185 return true;
186}
187
189{
190 return mResults;
191}
192
193
194//
195// QgsLayoutPictureSourceValidityCheck
196//
197
202
204{
205 return QStringLiteral( "layout_picture_source_check" );
206}
207
212
214{
216 return false;
217
218 const QgsLayoutValidityCheckContext *layoutContext = static_cast<const QgsLayoutValidityCheckContext *>( context );
219 if ( !layoutContext )
220 return false;
221
222 QList<QgsLayoutItemPicture *> pictureItems;
223 layoutContext->layout->layoutItems( pictureItems );
224 for ( QgsLayoutItemPicture *picture : std::as_const( pictureItems ) )
225 {
226 if ( picture->isMissingImage() )
227 {
230 res.title = QObject::tr( "Picture source is missing or corrupt" );
231 const QString name = picture->displayName().toHtmlEscaped();
232
233 const QUrl picUrl = QUrl::fromUserInput( picture->evaluatedPath() );
234 const bool isLocalFile = picUrl.isLocalFile();
235
236 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() );
237 mResults.append( res );
238 }
239 }
240
241 return true;
242}
243
245{
246 return mResults;
247}
248
249#ifndef WITH_QTWEBKIT
250//
251// QgsLayoutHtmlItemValidityCheck
252//
253
258
260{
261 return QStringLiteral( "layout_html_item_check" );
262}
263
268
270{
272 return false;
273
274 const QgsLayoutValidityCheckContext *layoutContext = static_cast<const QgsLayoutValidityCheckContext *>( context );
275 if ( !layoutContext )
276 return false;
277
278 const QList<QgsLayoutMultiFrame *> multiFrames = layoutContext->layout->multiFrames();
279 for ( QgsLayoutMultiFrame *multiFrame : std::as_const( multiFrames ) )
280 {
281 if ( multiFrame->type() == QgsLayoutItemRegistry::LayoutHtml && multiFrame->frameCount() > 0 )
282 {
285 res.title = QObject::tr( "HTML item cannot be rendered" );
286 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." );
287 mResults.append( res );
288 break;
289 }
290 }
291
292 return true;
293}
294
296{
297 return mResults;
298}
299#endif
@ 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:120
QList< QgsLayoutMultiFrame * > multiFrames() const
Returns a list of multi frames contained in the layout.
This class is a composition of two QSettings instances:
Definition qgssettings.h:64
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.
@ 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.