Branch data Line data Source code
1 : : /*************************************************************************** 2 : : qgsabstractvaliditycheck.h 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 : : #ifndef QGSABSTRACTVALIDITYCHECK_H 16 : : #define QGSABSTRACTVALIDITYCHECK_H 17 : : 18 : : #include "qgis_core.h" 19 : : #include "qgis_sip.h" 20 : : #include <QString> 21 : : #include <QObject> 22 : : 23 : : class QgsValidityCheckContext; 24 : : class QgsFeedback; 25 : : 26 : : /** 27 : : * \class QgsValidityCheckResult 28 : : * \ingroup core 29 : : * \brief Represents an individual result from a validity check run by a QgsAbstractValidityCheck subclass. 30 : : * 31 : : * Results can either be warnings or critical errors, as dictated by the type member. Critical error 32 : : * are errors which are serious enough to prevent an operation from proceeding, while a warning 33 : : * result will be communicated to users, but not prevent them from proceeding. 34 : : * 35 : : * \since QGIS 3.6 36 : : */ 37 : 0 : class CORE_EXPORT QgsValidityCheckResult 38 : : { 39 : : public: 40 : : 41 : : //! Result types 42 : : enum Type 43 : : { 44 : : Warning, //!< Warning only, allow operation to proceed but notify user of result 45 : : Critical, //!< Critical error - notify user of result and prevent operation from proceeding 46 : : }; 47 : : 48 : : //! Result type 49 : : Type type; 50 : : 51 : : /** 52 : : * A short, translated string summarising the result. Ideally a single sentence. 53 : : */ 54 : : QString title; 55 : : 56 : : /** 57 : : * Detailed description of the result (translated), giving users enough detail for them to resolve 58 : : * the error. 59 : : */ 60 : : QString detailedDescription; 61 : : 62 : : /** 63 : : * ID of the check which generated the result. This is usually automatically populated. 64 : : */ 65 : : QString checkId; 66 : : 67 : : }; 68 : : 69 : : /** 70 : : * \class QgsAbstractValidityCheck 71 : : * \ingroup core 72 : : * \brief Abstract base class for individual validity checks. 73 : : * 74 : : * Validity checks represent objects which can run a test using a QgsValidityCheckContext, and return 75 : : * the results of the check via QgsValidityCheckResult objects. 76 : : * 77 : : * Checks can be used for many different use cases, e.g. validating a layout's contents before allowing 78 : : * an export to occur, or validating the contents of a Processing model (and warning if required plugin based 79 : : * providers are not available or if compulsory algorithm parameters have not been populated). 80 : : * 81 : : * Subclasses must indicate the type of check they represent via the checkType() method. When checks are performed, 82 : : * all the registered checks with a matching check type are performed sequentially. The check type also 83 : : * dictates the subclass of the QgsValidityCheckContext which is given to the subclass' runCheck method. 84 : : * 85 : : * Checks must be registered in the application's QgsValidityCheckRegistry, which is accessible via 86 : : * QgsApplication::validityCheckRegistry(). 87 : : * 88 : : * \see QgsValidityCheckRegistry 89 : : * 90 : : * \since QGIS 3.6 91 : : */ 92 : : class CORE_EXPORT QgsAbstractValidityCheck 93 : : { 94 : : 95 : : public: 96 : : 97 : : //! Check types 98 : : enum Type 99 : : { 100 : : TypeLayoutCheck = 0, //!< Print layout validity check, triggered on exporting a print layout 101 : : TypeUserCheck = 10000, //!< Starting point for custom user types 102 : : }; 103 : : 104 : : virtual ~QgsAbstractValidityCheck() = default; 105 : : 106 : : /** 107 : : * Creates a new instance of the check and returns it. 108 : : */ 109 : : virtual QgsAbstractValidityCheck *create() const = 0 SIP_FACTORY; 110 : : 111 : : /** 112 : : * Returns the unique ID of the check. 113 : : * 114 : : * This is a non-translated, non-user visible string identifying the check. 115 : : */ 116 : : virtual QString id() const = 0; 117 : : 118 : : /** 119 : : * Returns the type of the check. 120 : : */ 121 : : virtual int checkType() const = 0; 122 : : 123 : : /** 124 : : * Prepares the check for execution, and returns TRUE if the check can be run. 125 : : * 126 : : * This method is always called from the main thread, and subclasses can implement 127 : : * it to do preparatory steps which are not thread safe (e.g. obtaining feature 128 : : * sources from vector layers). It is followed by a call to runCheck(), which 129 : : * may be performed in a background thread. 130 : : * 131 : : * Individual calls to prepareCheck()/runCheck() are run on a new instance of the 132 : : * check (see create()), so subclasses can safely store state from the prepareCheck() method 133 : : * ready for the subsequent runCheck() method. 134 : : * 135 : : * The \a context argument gives the wider in which the check is being run. 136 : : */ 137 : : virtual bool prepareCheck( const QgsValidityCheckContext *context, QgsFeedback *feedback ) 138 : : { 139 : : Q_UNUSED( context ) 140 : : Q_UNUSED( feedback ) 141 : : return true; 142 : : } 143 : : 144 : : /** 145 : : * Runs the check and returns a list of results. If the check is "passed" and no warnings or errors are generated, 146 : : * then an empty list should be returned. 147 : : * 148 : : * This method may be called in a background thread, so subclasses should take care to ensure that 149 : : * only thread-safe methods are used. It is always preceded by a call to prepareCheck(). 150 : : * 151 : : * If a check needs to perform non-thread-safe tests, these should be implemented within prepareCheck() 152 : : * and stored in the subclass instance to be returned by this method. 153 : : * 154 : : * The \a context argument gives the wider in which the check is being run. 155 : : */ 156 : : virtual QList< QgsValidityCheckResult > runCheck( const QgsValidityCheckContext *context, QgsFeedback *feedback ) = 0; 157 : : 158 : : }; 159 : : 160 : : #endif // QGSABSTRACTVALIDITYCHECK_H