QGIS API Documentation 3.99.0-Master (21b3aa880ba)
Loading...
Searching...
No Matches
qgsvaliditycheckregistry.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsvaliditycheckregistry.cpp
3 ----------------------------
4 begin : November 2018
5 copyright : (C) 2018 by 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
17
18#include "qgsfeedback.h"
19
23
25{
26 qDeleteAll( mChecks );
27}
28
29QList<const QgsAbstractValidityCheck *> QgsValidityCheckRegistry::checks() const
30{
31 QList<const QgsAbstractValidityCheck *> results;
32 for ( const QgsAbstractValidityCheck *check : mChecks )
33 {
34 if ( check )
35 results.append( check );
36 }
37 return results;
38}
39
40QList<const QgsAbstractValidityCheck *> QgsValidityCheckRegistry::checks( int type ) const
41{
42 QList< const QgsAbstractValidityCheck * > results;
43 for ( const QgsAbstractValidityCheck *check : mChecks )
44 {
45 if ( check && check->checkType() == type )
46 results << check;
47 }
48 return results;
49}
50
52{
53 mChecks.append( check );
54}
55
57{
58 const int index = mChecks.indexOf( check );
59 if ( index >= 0 )
60 delete mChecks.takeAt( index );
61}
62
63QList<QgsValidityCheckResult> QgsValidityCheckRegistry::runChecks( int type, const QgsValidityCheckContext *context, QgsFeedback *feedback ) const
64{
65 QList<QgsValidityCheckResult> result;
66 const std::vector<std::unique_ptr<QgsAbstractValidityCheck> > toCheck = createChecks( type );
67 int i = 0;
68 for ( const std::unique_ptr< QgsAbstractValidityCheck > &check : toCheck )
69 {
70 if ( feedback && feedback->isCanceled() )
71 break;
72
73 if ( !check->prepareCheck( context, feedback ) )
74 {
75 if ( feedback )
76 {
77 feedback->setProgress( static_cast< double >( i ) / toCheck.size() * 100 );
78 }
79 continue;
80 }
81
82 const QList< QgsValidityCheckResult > checkResults = check->runCheck( context, feedback );
83 for ( QgsValidityCheckResult checkResult : checkResults )
84 {
85 checkResult.checkId = check->id();
86 result << checkResult;
87 }
88 i++;
89 if ( feedback )
90 {
91 feedback->setProgress( static_cast< double >( i ) / toCheck.size() * 100 );
92 }
93 }
94 return result;
95}
96
97std::vector<std::unique_ptr<QgsAbstractValidityCheck> > QgsValidityCheckRegistry::createChecks( int type ) const
98{
99 const QList< const QgsAbstractValidityCheck *> toCheck = checks( type );
100 std::vector<std::unique_ptr<QgsAbstractValidityCheck> > results;
101 results.reserve( toCheck.size() );
102 for ( const QgsAbstractValidityCheck *check : toCheck )
103 {
104 results.emplace_back( std::unique_ptr< QgsAbstractValidityCheck >( check->create() ) );
105 }
106 return results;
107}
Abstract base class for individual validity checks.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:53
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:61
Base class for validity check contexts.
QList< QgsValidityCheckResult > runChecks(int type, const QgsValidityCheckContext *context, QgsFeedback *feedback) const
Runs all checks of the specified type and returns a list of results.
QList< const QgsAbstractValidityCheck * > checks() const
Returns the list of available checks.
void removeCheck(QgsAbstractValidityCheck *check)
Removes a check from the registry.
void addCheck(QgsAbstractValidityCheck *check)
Adds a check to the registry.
Represents an individual result from a validity check run by a QgsAbstractValidityCheck subclass.