QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
19 #include "qgslayoutitemscalebar.h"
20 #include "qgslayoutitemmap.h"
21 #include "qgslayoutitempicture.h"
22 #include "qgslayout.h"
23 #include "qgssettings.h"
24 #include <QUrl>
25 
26 //
27 // QgsLayoutScaleBarValidityCheck
28 //
29 
31 {
32  return new QgsLayoutScaleBarValidityCheck();
33 }
34 
36 {
37  return QStringLiteral( "layout_scalebar_check" );
38 }
39 
41 {
43 }
44 
46 {
48  return false;
49 
50  const QgsLayoutValidityCheckContext *layoutContext = static_cast< const QgsLayoutValidityCheckContext * >( context );
51  if ( !layoutContext )
52  return false;
53 
54  QList< QgsLayoutItemScaleBar * > barItems;
55  layoutContext->layout->layoutItems( barItems );
56  for ( QgsLayoutItemScaleBar *bar : std::as_const( barItems ) )
57  {
58  if ( !bar->linkedMap() )
59  {
62  res.title = QObject::tr( "Scalebar is not linked to a map" );
63  const QString name = bar->displayName().toHtmlEscaped();
64  res.detailedDescription = QObject::tr( "The scalebar “%1” is not linked to a map item. This scale will be misleading." ).arg( name );
65  mResults.append( res );
66  }
67  }
68 
69  return true;
70 }
71 
73 {
74  return mResults;
75 }
76 
77 
78 //
79 // QgsLayoutNorthArrowValidityCheck
80 //
81 
83 {
85 }
86 
88 {
89  return QStringLiteral( "layout_northarrow_check" );
90 }
91 
93 {
95 }
96 
98 {
100  return false;
101 
102  const QgsLayoutValidityCheckContext *layoutContext = static_cast< const QgsLayoutValidityCheckContext * >( context );
103  if ( !layoutContext )
104  return false;
105 
106  QgsSettings settings;
107  const QString defaultPath = settings.value( QStringLiteral( "LayoutDesigner/defaultNorthArrow" ), QStringLiteral( ":/images/north_arrows/layout_default_north_arrow.svg" ), QgsSettings::Gui ).toString();
108 
109  QList< QgsLayoutItemPicture * > pictureItems;
110  layoutContext->layout->layoutItems( pictureItems );
111  for ( QgsLayoutItemPicture *picture : std::as_const( pictureItems ) )
112  {
113  // look for pictures which use the default north arrow svg, but aren't actually linked to maps.
114  // alternatively identify them by looking for the default "North Arrow" string in their id
115  if ( !picture->linkedMap() && ( picture->picturePath() == defaultPath || picture->id().contains( QObject::tr( "North Arrow" ), Qt::CaseInsensitive ) ) )
116  {
119  res.title = QObject::tr( "North arrow is not linked to a map" );
120  const QString name = picture->displayName().toHtmlEscaped();
121  res.detailedDescription = QObject::tr( "The north arrow “%1” is not linked to a map item. The arrow orientation may be misleading." ).arg( name );
122  mResults.append( res );
123  }
124  }
125 
126  return true;
127 }
128 
130 {
131  return mResults;
132 }
133 
134 
135 
136 //
137 // QgsLayoutOverviewValidityCheck
138 //
139 
141 {
142  return new QgsLayoutOverviewValidityCheck();
143 }
144 
146 {
147  return QStringLiteral( "layout_overview_check" );
148 }
149 
151 {
153 }
154 
156 {
158  return false;
159 
160  const QgsLayoutValidityCheckContext *layoutContext = static_cast< const QgsLayoutValidityCheckContext * >( context );
161  if ( !layoutContext )
162  return false;
163 
164  QList< QgsLayoutItemMap * > mapItems;
165  layoutContext->layout->layoutItems( mapItems );
166  for ( QgsLayoutItemMap *map : std::as_const( mapItems ) )
167  {
168  for ( int i = 0; i < map->overviews()->size(); ++i )
169  {
170  QgsLayoutItemMapOverview *overview = map->overviews()->overview( i );
171  if ( overview && overview->enabled() && !overview->linkedMap() )
172  {
175  res.title = QObject::tr( "Overview is not linked to a map" );
176  const QString name = map->displayName().toHtmlEscaped();
177  res.detailedDescription = QObject::tr( "The map “%1” includes an overview (“%2”) which is not linked to a map item." ).arg( name, overview->name() );
178  mResults.append( res );
179  }
180  }
181  }
182 
183  return true;
184 }
185 
187 {
188  return mResults;
189 }
190 
191 
192 
193 //
194 // QgsLayoutPictureSourceValidityCheck
195 //
196 
198 {
200 }
201 
203 {
204  return QStringLiteral( "layout_picture_source_check" );
205 }
206 
208 {
210 }
211 
213 {
215  return false;
216 
217  const QgsLayoutValidityCheckContext *layoutContext = static_cast< const QgsLayoutValidityCheckContext * >( context );
218  if ( !layoutContext )
219  return false;
220 
221  QList< QgsLayoutItemPicture * > pictureItems;
222  layoutContext->layout->layoutItems( pictureItems );
223  for ( QgsLayoutItemPicture *picture : std::as_const( pictureItems ) )
224  {
225  if ( picture->isMissingImage() )
226  {
229  res.title = QObject::tr( "Picture source is missing or corrupt" );
230  const QString name = picture->displayName().toHtmlEscaped();
231 
232  const QUrl picUrl = QUrl::fromUserInput( picture->evaluatedPath() );
233  const bool isLocalFile = picUrl.isLocalFile();
234 
235  res.detailedDescription = QObject::tr( "The source for picture “%1” could not be loaded or is corrupt:<p>%2" ).arg( name,
236  isLocalFile ? QDir::toNativeSeparators( picture->evaluatedPath() ) : picture->evaluatedPath() );
237  mResults.append( res );
238  }
239  }
240 
241  return true;
242 }
243 
245 {
246  return mResults;
247 }
QgsLayoutPictureSourceValidityCheck
Layout picture source validity check.
Definition: qgslayoutvaliditychecks.h:99
QgsLayoutNorthArrowValidityCheck::checkType
int checkType() const override
Returns the type of the check.
Definition: qgslayoutvaliditychecks.cpp:92
QgsSettings::value
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Definition: qgssettings.cpp:161
QgsLayoutScaleBarValidityCheck::runCheck
QList< QgsValidityCheckResult > runCheck(const QgsValidityCheckContext *context, QgsFeedback *feedback) override
Runs the check and returns a list of results.
Definition: qgslayoutvaliditychecks.cpp:72
QgsLayout::layoutItems
void layoutItems(QList< T * > &itemList) const
Returns a list of layout items of a specific type.
Definition: qgslayout.h:122
QgsLayoutOverviewValidityCheck::id
QString id() const override
Returns the unique ID of the check.
Definition: qgslayoutvaliditychecks.cpp:145
QgsLayoutOverviewValidityCheck::prepareCheck
bool prepareCheck(const QgsValidityCheckContext *context, QgsFeedback *feedback) override
Prepares the check for execution, and returns true if the check can be run.
Definition: qgslayoutvaliditychecks.cpp:155
QgsLayoutNorthArrowValidityCheck::create
QgsLayoutNorthArrowValidityCheck * create() const override
constructor
Definition: qgslayoutvaliditychecks.cpp:82
QgsLayoutPictureSourceValidityCheck::create
QgsLayoutPictureSourceValidityCheck * create() const override
constructor
Definition: qgslayoutvaliditychecks.cpp:197
qgslayoutvaliditychecks.h
QgsSettings
This class is a composition of two QSettings instances:
Definition: qgssettings.h:61
QgsLayoutScaleBarValidityCheck::id
QString id() const override
Returns the unique ID of the check.
Definition: qgslayoutvaliditychecks.cpp:35
QgsLayoutPictureSourceValidityCheck::prepareCheck
bool prepareCheck(const QgsValidityCheckContext *context, QgsFeedback *feedback) override
Prepares the check for execution, and returns true if the check can be run.
Definition: qgslayoutvaliditychecks.cpp:212
QgsLayoutOverviewValidityCheck
Layout overview validity check.
Definition: qgslayoutvaliditychecks.h:77
QgsValidityCheckContext::type
virtual int type() const =0
Returns the context type.
QgsLayoutValidityCheckContext::layout
QgsLayout * layout
Pointer to the layout which the check is being run against.
Definition: qgsvaliditycheckcontext.h:91
QgsLayoutPictureSourceValidityCheck::runCheck
QList< QgsValidityCheckResult > runCheck(const QgsValidityCheckContext *context, QgsFeedback *feedback) override
Runs the check and returns a list of results.
Definition: qgslayoutvaliditychecks.cpp:244
QgsLayoutItemMapOverview::linkedMap
QgsLayoutItemMap * linkedMap()
Returns the source map to show the overview extent of.
Definition: qgslayoutitemmapoverview.cpp:238
QgsValidityCheckContext
Base class for validity check contexts.
Definition: qgsvaliditycheckcontext.h:35
QgsLayoutScaleBarValidityCheck::prepareCheck
bool prepareCheck(const QgsValidityCheckContext *context, QgsFeedback *feedback) override
Prepares the check for execution, and returns true if the check can be run.
Definition: qgslayoutvaliditychecks.cpp:45
QgsAbstractValidityCheck::TypeLayoutCheck
@ TypeLayoutCheck
Print layout validity check, triggered on exporting a print layout.
Definition: qgsabstractvaliditycheck.h:100
QgsLayoutNorthArrowValidityCheck::id
QString id() const override
Returns the unique ID of the check.
Definition: qgslayoutvaliditychecks.cpp:87
QgsFeedback
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:44
QgsLayoutOverviewValidityCheck::runCheck
QList< QgsValidityCheckResult > runCheck(const QgsValidityCheckContext *context, QgsFeedback *feedback) override
Runs the check and returns a list of results.
Definition: qgslayoutvaliditychecks.cpp:186
QgsLayoutNorthArrowValidityCheck
Layout north arrow validity check.
Definition: qgslayoutvaliditychecks.h:55
QgsLayoutNorthArrowValidityCheck::prepareCheck
bool prepareCheck(const QgsValidityCheckContext *context, QgsFeedback *feedback) override
Prepares the check for execution, and returns true if the check can be run.
Definition: qgslayoutvaliditychecks.cpp:97
qgslayout.h
QgsLayoutOverviewValidityCheck::checkType
int checkType() const override
Returns the type of the check.
Definition: qgslayoutvaliditychecks.cpp:150
QgsLayoutItemPicture
A layout item subclass that displays SVG files or raster format images (jpg, png, ....
Definition: qgslayoutitempicture.h:35
QgsLayoutItemMapOverview
An individual overview which is drawn above the map content in a QgsLayoutItemMap,...
Definition: qgslayoutitemmapoverview.h:126
QgsValidityCheckResult
Represents an individual result from a validity check run by a QgsAbstractValidityCheck subclass.
Definition: qgsabstractvaliditycheck.h:37
QgsLayoutItemMap
Layout graphical items for displaying a map.
Definition: qgslayoutitemmap.h:317
QgsLayoutValidityCheckContext
Validity check context for print layout validation.
Definition: qgsvaliditycheckcontext.h:75
QgsValidityCheckResult::title
QString title
A short, translated string summarising the result.
Definition: qgsabstractvaliditycheck.h:54
QgsValidityCheckContext::TypeLayoutContext
@ TypeLayoutContext
Layout context, see QgsLayoutValidityCheckContext.
Definition: qgsvaliditycheckcontext.h:52
qgssettings.h
qgsvaliditycheckcontext.h
qgslayoutitempicture.h
QgsLayoutScaleBarValidityCheck::create
QgsLayoutScaleBarValidityCheck * create() const override
constructor
Definition: qgslayoutvaliditychecks.cpp:30
QgsValidityCheckResult::detailedDescription
QString detailedDescription
Detailed description of the result (translated), giving users enough detail for them to resolve the e...
Definition: qgsabstractvaliditycheck.h:60
QgsLayoutPictureSourceValidityCheck::id
QString id() const override
Returns the unique ID of the check.
Definition: qgslayoutvaliditychecks.cpp:202
QgsLayoutItemMapItem::name
QString name() const
Returns the friendly display name for the item.
Definition: qgslayoutitemmapitem.cpp:94
QgsLayoutScaleBarValidityCheck
Layout scalebar validity check.
Definition: qgslayoutvaliditychecks.h:33
QgsLayoutItemScaleBar
A layout item subclass for scale bars.
Definition: qgslayoutitemscalebar.h:35
QgsValidityCheckResult::Warning
@ Warning
Warning only, allow operation to proceed but notify user of result.
Definition: qgsabstractvaliditycheck.h:44
QgsLayoutPictureSourceValidityCheck::checkType
int checkType() const override
Returns the type of the check.
Definition: qgslayoutvaliditychecks.cpp:207
QgsValidityCheckResult::type
Type type
Result type.
Definition: qgsabstractvaliditycheck.h:49
QgsLayoutOverviewValidityCheck::create
QgsLayoutOverviewValidityCheck * create() const override
constructor
Definition: qgslayoutvaliditychecks.cpp:140
qgslayoutitemscalebar.h
QgsLayoutNorthArrowValidityCheck::runCheck
QList< QgsValidityCheckResult > runCheck(const QgsValidityCheckContext *context, QgsFeedback *feedback) override
Runs the check and returns a list of results.
Definition: qgslayoutvaliditychecks.cpp:129
QgsLayoutScaleBarValidityCheck::checkType
int checkType() const override
Returns the type of the check.
Definition: qgslayoutvaliditychecks.cpp:40
QgsLayoutItemMapItem::enabled
bool enabled() const
Returns whether the item will be drawn.
Definition: qgslayoutitemmapitem.cpp:104
QgsSettings::Gui
@ Gui
Definition: qgssettings.h:71
qgslayoutitemmap.h