QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
25 //
26 // QgsLayoutScaleBarValidityCheck
27 //
28 
30 {
31  return new QgsLayoutScaleBarValidityCheck();
32 }
33 
35 {
36  return QStringLiteral( "layout_scalebar_check" );
37 }
38 
40 {
42 }
43 
45 {
47  return false;
48 
49  const QgsLayoutValidityCheckContext *layoutContext = static_cast< const QgsLayoutValidityCheckContext * >( context );
50  if ( !layoutContext )
51  return false;
52 
53  QList< QgsLayoutItemScaleBar * > barItems;
54  layoutContext->layout->layoutItems( barItems );
55  for ( QgsLayoutItemScaleBar *bar : qgis::as_const( barItems ) )
56  {
57  if ( !bar->linkedMap() )
58  {
61  res.title = QObject::tr( "Scalebar is not linked to a map" );
62  const QString name = bar->displayName().toHtmlEscaped();
63  res.detailedDescription = QObject::tr( "The scalebar “%1” is not linked to a map item. This scale will be misleading." ).arg( name );
64  mResults.append( res );
65  }
66  }
67 
68  return true;
69 }
70 
72 {
73  return mResults;
74 }
75 
76 
77 //
78 // QgsLayoutNorthArrowValidityCheck
79 //
80 
82 {
84 }
85 
87 {
88  return QStringLiteral( "layout_northarrow_check" );
89 }
90 
92 {
94 }
95 
97 {
99  return false;
100 
101  const QgsLayoutValidityCheckContext *layoutContext = static_cast< const QgsLayoutValidityCheckContext * >( context );
102  if ( !layoutContext )
103  return false;
104 
105  QgsSettings settings;
106  const QString defaultPath = settings.value( QStringLiteral( "LayoutDesigner/defaultNorthArrow" ), QStringLiteral( ":/images/north_arrows/layout_default_north_arrow.svg" ), QgsSettings::Gui ).toString();
107 
108  QList< QgsLayoutItemPicture * > pictureItems;
109  layoutContext->layout->layoutItems( pictureItems );
110  for ( QgsLayoutItemPicture *picture : qgis::as_const( pictureItems ) )
111  {
112  // look for pictures which use the default north arrow svg, but aren't actually linked to maps.
113  // alternatively identify them by looking for the default "North Arrow" string in their id
114  if ( !picture->linkedMap() && ( picture->picturePath() == defaultPath || picture->id().contains( QObject::tr( "North Arrow" ), Qt::CaseInsensitive ) ) )
115  {
118  res.title = QObject::tr( "North arrow is not linked to a map" );
119  const QString name = picture->displayName().toHtmlEscaped();
120  res.detailedDescription = QObject::tr( "The north arrow “%1” is not linked to a map item. The arrow orientation may be misleading." ).arg( name );
121  mResults.append( res );
122  }
123  }
124 
125  return true;
126 }
127 
129 {
130  return mResults;
131 }
132 
133 
134 
135 //
136 // QgsLayoutOverviewValidityCheck
137 //
138 
140 {
141  return new QgsLayoutOverviewValidityCheck();
142 }
143 
145 {
146  return QStringLiteral( "layout_overview_check" );
147 }
148 
150 {
152 }
153 
155 {
157  return false;
158 
159  const QgsLayoutValidityCheckContext *layoutContext = static_cast< const QgsLayoutValidityCheckContext * >( context );
160  if ( !layoutContext )
161  return false;
162 
163  QList< QgsLayoutItemMap * > mapItems;
164  layoutContext->layout->layoutItems( mapItems );
165  for ( QgsLayoutItemMap *map : qgis::as_const( mapItems ) )
166  {
167  for ( int i = 0; i < map->overviews()->size(); ++i )
168  {
169  QgsLayoutItemMapOverview *overview = map->overviews()->overview( i );
170  if ( overview && overview->enabled() && !overview->linkedMap() )
171  {
174  res.title = QObject::tr( "Overview is not linked to a map" );
175  const QString name = map->displayName().toHtmlEscaped();
176  res.detailedDescription = QObject::tr( "The map “%1” includes an overview (“%2”) which is not linked to a map item." ).arg( name, overview->name() );
177  mResults.append( res );
178  }
179  }
180  }
181 
182  return true;
183 }
184 
186 {
187  return mResults;
188 }
189 
190 
191 
192 //
193 // QgsLayoutPictureSourceValidityCheck
194 //
195 
197 {
199 }
200 
202 {
203  return QStringLiteral( "layout_picture_source_check" );
204 }
205 
207 {
209 }
210 
212 {
214  return false;
215 
216  const QgsLayoutValidityCheckContext *layoutContext = static_cast< const QgsLayoutValidityCheckContext * >( context );
217  if ( !layoutContext )
218  return false;
219 
220  QList< QgsLayoutItemPicture * > pictureItems;
221  layoutContext->layout->layoutItems( pictureItems );
222  for ( QgsLayoutItemPicture *picture : qgis::as_const( pictureItems ) )
223  {
224  if ( picture->isMissingImage() )
225  {
228  res.title = QObject::tr( "Picture source is missing or corrupt" );
229  const QString name = picture->displayName().toHtmlEscaped();
230 
231  const QUrl picUrl = QUrl::fromUserInput( picture->evaluatedPath() );
232  const bool isLocalFile = picUrl.isLocalFile();
233 
234  res.detailedDescription = QObject::tr( "The source for picture “%1” could not be loaded or is corrupt:<p>%2" ).arg( name,
235  isLocalFile ? QDir::toNativeSeparators( picture->evaluatedPath() ) : picture->evaluatedPath() );
236  mResults.append( res );
237  }
238  }
239 
240  return true;
241 }
242 
244 {
245  return mResults;
246 }
QgsLayoutPictureSourceValidityCheck
Definition: qgslayoutvaliditychecks.h:99
QgsLayoutNorthArrowValidityCheck::checkType
int checkType() const override
Returns the type of the check.
Definition: qgslayoutvaliditychecks.cpp:91
QgsSettings::value
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
Definition: qgssettings.cpp:174
QgsLayoutScaleBarValidityCheck::runCheck
QList< QgsValidityCheckResult > runCheck(const QgsValidityCheckContext *context, QgsFeedback *feedback) override
Runs the check and returns a list of results.
Definition: qgslayoutvaliditychecks.cpp:71
QgsLayout::layoutItems
void layoutItems(QList< T * > &itemList) const
Returns a list of layout items of a specific type.
Definition: qgslayout.h:121
QgsLayoutOverviewValidityCheck::id
QString id() const override
Returns the unique ID of the check.
Definition: qgslayoutvaliditychecks.cpp:144
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:154
QgsLayoutNorthArrowValidityCheck::create
QgsLayoutNorthArrowValidityCheck * create() const override
constructor
Definition: qgslayoutvaliditychecks.cpp:81
QgsLayoutPictureSourceValidityCheck::create
QgsLayoutPictureSourceValidityCheck * create() const override
constructor
Definition: qgslayoutvaliditychecks.cpp:196
qgslayoutvaliditychecks.h
QgsSettings
Definition: qgssettings.h:61
QgsLayoutScaleBarValidityCheck::id
QString id() const override
Returns the unique ID of the check.
Definition: qgslayoutvaliditychecks.cpp:34
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:211
QgsLayoutOverviewValidityCheck
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:243
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:44
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:86
QgsFeedback
Definition: qgsfeedback.h:43
QgsLayoutOverviewValidityCheck::runCheck
QList< QgsValidityCheckResult > runCheck(const QgsValidityCheckContext *context, QgsFeedback *feedback) override
Runs the check and returns a list of results.
Definition: qgslayoutvaliditychecks.cpp:185
QgsLayoutNorthArrowValidityCheck
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:96
qgslayout.h
QgsLayoutOverviewValidityCheck::checkType
int checkType() const override
Returns the type of the check.
Definition: qgslayoutvaliditychecks.cpp:149
QgsLayoutItemPicture
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:39
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:29
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:201
QgsLayoutItemMapItem::name
QString name() const
Returns the friendly display name for the item.
Definition: qgslayoutitemmapitem.cpp:94
QgsLayoutScaleBarValidityCheck
Definition: qgslayoutvaliditychecks.h:33
QgsLayoutItemScaleBar
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:206
QgsValidityCheckResult::type
Type type
Result type.
Definition: qgsabstractvaliditycheck.h:49
QgsLayoutOverviewValidityCheck::create
QgsLayoutOverviewValidityCheck * create() const override
constructor
Definition: qgslayoutvaliditychecks.cpp:139
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:128
QgsLayoutScaleBarValidityCheck::checkType
int checkType() const override
Returns the type of the check.
Definition: qgslayoutvaliditychecks.cpp:39
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