QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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#include <QString>
27
28#include "moc_qgsvaliditycheckresultswidget.cpp"
29
30using namespace Qt::StringLiterals;
31
32//
33// QgsValidityCheckResultsModel
34//
35
36QgsValidityCheckResultsModel::QgsValidityCheckResultsModel( const QList<QgsValidityCheckResult> &results, QObject *parent )
37 : QAbstractItemModel( parent )
38 , mResults( results )
39{}
40
41QModelIndex QgsValidityCheckResultsModel::index( int row, int column, const QModelIndex &parent ) const
42{
43 Q_UNUSED( parent )
44 return createIndex( row, column );
45}
46
47QModelIndex QgsValidityCheckResultsModel::parent( const QModelIndex &child ) const
48{
49 Q_UNUSED( child )
50 return QModelIndex();
51}
52
53int QgsValidityCheckResultsModel::rowCount( const QModelIndex &parent ) const
54{
55 Q_UNUSED( parent )
56 return mResults.count();
57}
58
60{
61 Q_UNUSED( parent )
62 return 1;
63}
64
65QVariant QgsValidityCheckResultsModel::data( const QModelIndex &index, int role ) const
66{
67 if ( index.row() >= mResults.count() || index.row() < 0 )
68 return QVariant();
69
70 const QgsValidityCheckResult &res = mResults.at( index.row() );
71 switch ( role )
72 {
73 case Qt::DisplayRole:
74 case Qt::ToolTipRole:
75 return res.title;
76
78 return res.detailedDescription;
79
80 case Qt::DecorationRole:
81 switch ( res.type )
82 {
84 return QgsApplication::getThemeIcon( u"/mIconCritical.svg"_s );
85
87 return QgsApplication::getThemeIcon( u"/mIconWarning.svg"_s );
88 }
89 break;
90
91 default:
92 return QVariant();
93 }
94 return QVariant();
95}
96
97//
98// QgsValidityCheckResultsWidget
99//
100
102 : QWidget( parent )
103{
104 setupUi( this );
105}
106
107void QgsValidityCheckResultsWidget::setResults( const QList<QgsValidityCheckResult> &results )
108{
109 if ( mResultsModel )
110 mResultsModel->deleteLater();
111
112 mResultsModel = new QgsValidityCheckResultsModel( results, this );
113 mResultsListView->setModel( mResultsModel );
114
115 connect( mResultsListView->selectionModel(), &QItemSelectionModel::currentChanged, this, &QgsValidityCheckResultsWidget::selectionChanged );
116
117 if ( mResultsModel->rowCount() > 0 )
118 {
119 // auto select first result in list
120 const QModelIndex firstResult( mResultsModel->index( 0, 0, QModelIndex() ) );
121 mResultsListView->selectionModel()->select( firstResult, QItemSelectionModel::ClearAndSelect );
122 selectionChanged( firstResult, QModelIndex() );
123 }
124
125 mDescriptionLabel->hide();
126}
127
128void QgsValidityCheckResultsWidget::setDescription( const QString &description )
129{
130 mDescriptionLabel->setText( description );
131 mDescriptionLabel->setVisible( !description.isEmpty() );
132}
133
134bool QgsValidityCheckResultsWidget::runChecks( int type, const QgsValidityCheckContext *context, const QString &title, const QString &description, QWidget *parent )
135{
136 auto feedback = std::make_unique<QgsFeedback>();
137 auto progressDialog = std::make_unique<QProgressDialog>( tr( "Running Checks…" ), tr( "Abort" ), 0, 100, parent );
138 progressDialog->setWindowTitle( title );
139
140 QgsProxyProgressTask *proxyTask = new QgsProxyProgressTask( tr( "Running Checks" ) );
141
142 connect( feedback.get(), &QgsFeedback::progressChanged, progressDialog.get(), [&]( double progress ) {
143 progressDialog->setValue( static_cast<int>( progress ) );
144 progressDialog->setLabelText( feedback->property( "progress" ).toString() );
145
146 proxyTask->setProxyProgress( progress );
147
148#ifdef Q_OS_LINUX
149 // One iteration is actually enough on Windows to get good interactivity
150 // whereas on Linux we must allow for far more iterations.
151 int nIters = 0;
152 while ( ++nIters < 100 )
153#endif
154 {
155 QCoreApplication::processEvents();
156 }
157 } );
158 connect( progressDialog.get(), &QProgressDialog::canceled, progressDialog.get(), [&] { feedback->cancel(); } );
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.