QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgsvaliditycheckresultswidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsvaliditycheckresultswidget.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 "qgsapplication.h"
19#include "qgsfeedback.h"
22
23#include <QDialogButtonBox>
24#include <QProgressDialog>
25#include <QPushButton>
26
27#include "moc_qgsvaliditycheckresultswidget.cpp"
28
29//
30// QgsValidityCheckResultsModel
31//
32
33QgsValidityCheckResultsModel::QgsValidityCheckResultsModel( const QList<QgsValidityCheckResult> &results, QObject *parent )
34 : QAbstractItemModel( parent )
35 , mResults( results )
36{
37}
38
39QModelIndex QgsValidityCheckResultsModel::index( int row, int column, const QModelIndex &parent ) const
40{
41 Q_UNUSED( parent )
42 return createIndex( row, column );
43}
44
45QModelIndex QgsValidityCheckResultsModel::parent( const QModelIndex &child ) const
46{
47 Q_UNUSED( child )
48 return QModelIndex();
49}
50
51int QgsValidityCheckResultsModel::rowCount( const QModelIndex &parent ) const
52{
53 Q_UNUSED( parent )
54 return mResults.count();
55}
56
58{
59 Q_UNUSED( parent )
60 return 1;
61}
62
63QVariant QgsValidityCheckResultsModel::data( const QModelIndex &index, int role ) const
64{
65 if ( index.row() >= mResults.count() || index.row() < 0 )
66 return QVariant();
67
68 const QgsValidityCheckResult &res = mResults.at( index.row() );
69 switch ( role )
70 {
71 case Qt::DisplayRole:
72 case Qt::ToolTipRole:
73 return res.title;
74
76 return res.detailedDescription;
77
78 case Qt::DecorationRole:
79 switch ( res.type )
80 {
82 return QgsApplication::getThemeIcon( QStringLiteral( "/mIconCritical.svg" ) );
83
85 return QgsApplication::getThemeIcon( QStringLiteral( "/mIconWarning.svg" ) );
86 }
87 break;
88
89 default:
90 return QVariant();
91 }
92 return QVariant();
93}
94
95//
96// QgsValidityCheckResultsWidget
97//
98
100 : QWidget( parent )
101{
102 setupUi( this );
103}
104
105void QgsValidityCheckResultsWidget::setResults( const QList<QgsValidityCheckResult> &results )
106{
107 if ( mResultsModel )
108 mResultsModel->deleteLater();
109
110 mResultsModel = new QgsValidityCheckResultsModel( results, this );
111 mResultsListView->setModel( mResultsModel );
112
113 connect( mResultsListView->selectionModel(), &QItemSelectionModel::currentChanged, this, &QgsValidityCheckResultsWidget::selectionChanged );
114
115 if ( mResultsModel->rowCount() > 0 )
116 {
117 // auto select first result in list
118 const QModelIndex firstResult( mResultsModel->index( 0, 0, QModelIndex() ) );
119 mResultsListView->selectionModel()->select( firstResult, QItemSelectionModel::ClearAndSelect );
120 selectionChanged( firstResult, QModelIndex() );
121 }
122
123 mDescriptionLabel->hide();
124}
125
126void QgsValidityCheckResultsWidget::setDescription( const QString &description )
127{
128 mDescriptionLabel->setText( description );
129 mDescriptionLabel->setVisible( !description.isEmpty() );
130}
131
132bool QgsValidityCheckResultsWidget::runChecks( int type, const QgsValidityCheckContext *context, const QString &title, const QString &description, QWidget *parent )
133{
134 auto feedback = std::make_unique<QgsFeedback>();
135 auto progressDialog = std::make_unique<QProgressDialog>( tr( "Running Checks…" ), tr( "Abort" ), 0, 100, parent );
136 progressDialog->setWindowTitle( title );
137
138 QgsProxyProgressTask *proxyTask = new QgsProxyProgressTask( tr( "Running Checks" ) );
139
140 connect( feedback.get(), &QgsFeedback::progressChanged, progressDialog.get(), [&]( double progress ) {
141 progressDialog->setValue( static_cast<int>( progress ) );
142 progressDialog->setLabelText( feedback->property( "progress" ).toString() );
143
144 proxyTask->setProxyProgress( progress );
145
146#ifdef Q_OS_LINUX
147 // One iteration is actually enough on Windows to get good interactivity
148 // whereas on Linux we must allow for far more iterations.
149 int nIters = 0;
150 while ( ++nIters < 100 )
151#endif
152 {
153 QCoreApplication::processEvents();
154 }
155 } );
156 connect( progressDialog.get(), &QProgressDialog::canceled, progressDialog.get(), [&] {
157 feedback->cancel();
158 } );
159
160 QgsApplication::taskManager()->addTask( proxyTask );
161
162 const QList<QgsValidityCheckResult> results = QgsApplication::validityCheckRegistry()->runChecks( type, context, feedback.get() );
163
164 proxyTask->finalize( true );
165
166 if ( feedback->isCanceled() )
167 return false;
168
169 if ( results.empty() )
170 return true;
171
173 w->setResults( results );
174 w->setDescription( description );
175
176 bool hasCritical = false;
177 for ( const QgsValidityCheckResult &res : results )
178 {
179 if ( res.type == QgsValidityCheckResult::Critical )
180 {
181 hasCritical = true;
182 break;
183 }
184 }
185
186 QVBoxLayout *l = new QVBoxLayout();
187 l->addWidget( w );
188
189 QDialog dlg( parent );
190 dlg.setWindowTitle( title );
191
192 QDialogButtonBox *buttons = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, &dlg );
193 connect( buttons, &QDialogButtonBox::accepted, &dlg, &QDialog::accept );
194 connect( buttons, &QDialogButtonBox::rejected, &dlg, &QDialog::reject );
195 if ( hasCritical )
196 {
197 buttons->button( QDialogButtonBox::Ok )->setEnabled( false );
198 buttons->button( QDialogButtonBox::Ok )->setToolTip( tr( "Critical errors prevent this task from proceeding. Please address these issues and then retry." ) );
199 }
200
201 l->addWidget( buttons );
202
203 dlg.setLayout( l );
204
205 return dlg.exec();
206}
207
208void QgsValidityCheckResultsWidget::selectionChanged( const QModelIndex &current, const QModelIndex & )
209{
210 const QString desc = mResultsModel->data( current, static_cast<int>( QgsValidityCheckResultsModel::CustomRole::Description ) ).toString();
211 mDetailedDescriptionTextBrowser->setHtml( desc );
212}
static QgsValidityCheckRegistry * validityCheckRegistry()
Returns the application's validity check registry, used for managing validity checks.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static QgsTaskManager * taskManager()
Returns the application's task manager, used for managing application wide background task handling.
void progressChanged(double progress)
Emitted when the feedback object reports a progress change.
A QgsTask shell which proxies progress reports.
long addTask(QgsTask *task, int priority=0)
Adds a task to the manager.
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.
Represents an individual result from a validity check run by a QgsAbstractValidityCheck subclass.
QgsValidityCheckResult::Type type
Result type.
@ Critical
Critical error - notify user of result and prevent operation from proceeding.
@ 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.
A QAbstractItemModel subclass for displaying the results from a QgsAbstractValidityCheck.
QgsValidityCheckResultsModel(const QList< QgsValidityCheckResult > &results, QObject *parent=nullptr)
Constructor for QgsValidityCheckResultsModel, showing the specified list of checks results.
QModelIndex parent(const QModelIndex &child) const override
QModelIndex index(int row, int column, const QModelIndex &parent) const override
int columnCount(const QModelIndex &parent) const override
QVariant data(const QModelIndex &index, int role) const override
int rowCount(const QModelIndex &parent=QModelIndex()) const override
A reusable widget which displays a summary of the results from a QgsAbstractValidityCheck (or checks)...
QgsValidityCheckResultsWidget(QWidget *parent)
Constructor for QgsValidityCheckResultsWidget, with the specified parent widget.
void setDescription(const QString &description)
Sets a description label to show at the top of the widget, e.g.
static bool runChecks(int type, const QgsValidityCheckContext *context, const QString &title, const QString &description, QWidget *parent=nullptr)
Runs all registered validity checks of the given type, and if any warnings or critical errors are enc...
void setResults(const QList< QgsValidityCheckResult > &results)
Sets the list of check results to show in the dialog.