QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgsprojecttrustdialog.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsprojecttrustdialog.cpp
3 -------------------
4 begin : October 2025
5 copyright : (C) 2025 by Mathieu Pellerin
6 email : mathieu at opengis dot ch
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
20#include "qgsapplication.h"
21#include "qgsgui.h"
22#include "qgshelp.h"
23#include "qgsprojectstorage.h"
25#include "qgssettings.h"
28
29#include <QFileInfo>
30#include <QPushButton>
31#include <QString>
32#include <QSvgRenderer>
33
34#include "moc_qgsprojecttrustdialog.cpp"
35
36using namespace Qt::StringLiterals;
37
38QgsProjectTrustDialog::QgsProjectTrustDialog( QgsProject *project, QWidget *parent, Qt::WindowFlags fl )
39 : QDialog( parent, fl )
40{
41 setupUi( this );
43
44 mButtonBox->button( QDialogButtonBox::StandardButton::YesToAll )->setText( tr( "Always Trust" ) );
45 mButtonBox->button( QDialogButtonBox::StandardButton::Yes )->setText( tr( "Trust" ) );
46 mButtonBox->button( QDialogButtonBox::StandardButton::No )->setText( tr( "Deny" ) );
47 mButtonBox->button( QDialogButtonBox::StandardButton::NoToAll )->setText( tr( "Always Deny" ) );
48
49 connect( mButtonBox, &QDialogButtonBox::clicked, this, &QgsProjectTrustDialog::buttonBoxClicked );
50
51 connect( mScriptPreviewList, &QListWidget::itemDoubleClicked, this, [this]( QListWidgetItem *item ) {
52 const int row = mScriptPreviewList->row( item );
53 if ( row >= 0 && row < mEmbeddedScriptsVisitor.embeddedScripts().size() )
54 {
55 mScriptPreviewEditor->setText( mEmbeddedScriptsVisitor.embeddedScripts()[row].script() );
56 mScriptPreviewStackedWidget->setCurrentIndex( 1 );
57 }
58 } );
59
60 connect( mScriptPreviewBackButton, &QAbstractButton::clicked, this, [this] {
61 mScriptPreviewStackedWidget->setCurrentIndex( 0 );
62 } );
63
64 mScriptPreviewEditor->setReadOnly( true );
65 mScriptPreviewEditor->setLineNumbersVisible( false );
66
67 QSvgRenderer svg( u":/images/themes/default/mIconPythonFile.svg"_s );
68 if ( svg.isValid() )
69 {
70 const double maxLength = 64.0;
71 QSizeF size( maxLength, maxLength );
72 const QRectF viewBox = svg.viewBoxF();
73 if ( viewBox.height() > viewBox.width() )
74 {
75 size.setWidth( maxLength * viewBox.width() / viewBox.height() );
76 }
77 else
78 {
79 size.setHeight( maxLength * viewBox.height() / viewBox.width() );
80 }
81
82 QPixmap pixmap( static_cast<int>( maxLength ), static_cast<int>( maxLength ) );
83 pixmap.fill( Qt::transparent );
84
85 QPainter painter;
86 painter.begin( &pixmap );
87 painter.setRenderHint( QPainter::SmoothPixmapTransform );
88 painter.translate( ( maxLength - size.width() ) / 2, ( maxLength - size.height() ) / 2 );
89 svg.render( &painter, QRectF( 0, 0, size.width(), size.height() ) );
90 painter.end();
91
92 mIconLabel->setPixmap( pixmap );
93 }
94
95 if ( project )
96 {
98 if ( storage )
99 {
100 if ( !storage->filePath( project->fileName() ).isEmpty() )
101 {
102 QFileInfo projectFileInfo( storage->filePath( project->fileName() ) );
103 mProjectAbsoluteFilePath = projectFileInfo.absoluteFilePath();
104 mProjectAbsolutePath = projectFileInfo.absolutePath();
105 mProjectIsFile = true;
106 }
107 else
108 {
109 mProjectAbsolutePath = project->fileName();
110 mProjectIsFile = false;
111 }
112 }
113 else
114 {
115 QFileInfo projectFileInfo( project->fileName() );
116 mProjectAbsoluteFilePath = projectFileInfo.absoluteFilePath();
117 mProjectAbsolutePath = projectFileInfo.absolutePath();
118 mProjectIsFile = true;
119 }
120
121 if ( mProjectIsFile )
122 {
123 mProjectDetailsLabel->setText( tr( "The current project file path is ’%1’." ).arg( u"<b>%1</b>"_s.arg( mProjectAbsoluteFilePath ) ) );
124 QDir dir( mProjectAbsolutePath );
125 mTrustProjectFolderCheckBox->setText( tr( "Apply decision to all projects in folder ’%1’" ).arg( u"%1"_s.arg( dir.dirName() ) ) );
126 }
127 else
128 {
129 mProjectDetailsLabel->setText( tr( "The current project URI is ’%1’." ).arg( u"<b>%1</b>"_s.arg( mProjectAbsoluteFilePath ) ) );
130 mTrustProjectFolderCheckBox->setVisible( false );
131 }
132
133 project->accept( &mEmbeddedScriptsVisitor, QgsObjectVisitorContext() );
134 }
135
136 for ( const QgsEmbeddedScriptEntity &scriptDetails : mEmbeddedScriptsVisitor.embeddedScripts() )
137 {
138 QListWidgetItem *newItem = new QListWidgetItem( mScriptPreviewList );
139 switch ( scriptDetails.type() )
140 {
142 newItem->setIcon( QgsApplication::getThemeIcon( u"/mIconPythonFile.svg"_s ) );
143 break;
144
146 newItem->setIcon( QgsApplication::getThemeIcon( u"/mIconExpression.svg"_s ) );
147 break;
148
150 newItem->setIcon( QgsApplication::getThemeIcon( u"/mAction.svg"_s ) );
151 break;
152
154 newItem->setIcon( QgsApplication::getThemeIcon( u"/mActionFormView.svg"_s ) );
155 break;
156 }
157 newItem->setText( scriptDetails.name() );
158 newItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
159 mScriptPreviewList->addItem( newItem );
160 }
161}
162
163void QgsProjectTrustDialog::buttonBoxClicked( QAbstractButton *button )
164{
165 QDialogButtonBox::StandardButton buttonType = mButtonBox->standardButton( button );
166 if ( buttonType == QDialogButtonBox::StandardButton::Help )
167 {
168 showHelp();
169 return;
170 }
171
172 bool accepted = false;
173 QString path = !mProjectIsFile || mTrustProjectFolderCheckBox->isChecked() ? mProjectAbsolutePath : mProjectAbsoluteFilePath;
174 if ( !path.isEmpty() )
175 {
177 trustedProjectsFolders.removeAll( path );
179 untrustedProjectsFolders.removeAll( path );
180
181 QStringList temporarilyTrustedProjectsFolders = QgsApplication::temporarilyTrustedProjectsFolders();
182 temporarilyTrustedProjectsFolders.removeAll( path );
183 QStringList temporarilyUntrustedProjectsFolders = QgsApplication::temporarilyUntrustedProjectsFolders();
184 temporarilyUntrustedProjectsFolders.removeAll( path );
185
186 if ( buttonType == QDialogButtonBox::StandardButton::YesToAll )
187 {
188 trustedProjectsFolders << path;
189 accepted = true;
190 }
191 else if ( buttonType == QDialogButtonBox::StandardButton::Yes )
192 {
193 temporarilyTrustedProjectsFolders << path;
194 accepted = true;
195 }
196 else if ( buttonType == QDialogButtonBox::StandardButton::NoToAll )
197 {
198 untrustedProjectsFolders << path;
199 accepted = false;
200 }
201 else if ( buttonType == QDialogButtonBox::StandardButton::No )
202 {
203 temporarilyUntrustedProjectsFolders << path;
204 accepted = false;
205 }
206
207 trustedProjectsFolders.sort();
208 untrustedProjectsFolders.sort();
209 temporarilyTrustedProjectsFolders.sort();
210 temporarilyUntrustedProjectsFolders.sort();
211
214
215 QgsApplication::setTemporarilyTrustedProjectsFolders( temporarilyTrustedProjectsFolders );
216 QgsApplication::setTemporarilyUntrustedProjectsFolders( temporarilyUntrustedProjectsFolders );
217 }
218
219 done( accepted ? QDialog::Accepted : QDialog::Rejected );
220}
221
222void QgsProjectTrustDialog::showHelp()
223{
224 QgsHelp::openHelp( u"introduction/getting_started.html"_s );
225}
@ Action
Expression functions.
Definition qgis.h:461
@ FormInitCode
Map layers' action.
Definition qgis.h:462
@ ExpressionFunction
Project macros.
Definition qgis.h:460
static void setTemporarilyUntrustedProjectsFolders(const QStringList &untrustedProjectsFolders)
Sets the list of projects and folders that have been temporarily determined as untrusted by the user.
static QgsProjectStorageRegistry * projectStorageRegistry()
Returns registry of available project storage implementations.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static QStringList temporarilyTrustedProjectsFolders()
Returns the list of projects and folders that have been temporarily determined as trusted by the user...
static QStringList temporarilyUntrustedProjectsFolders()
Returns the list of projects and folders that have been temporarily determined as untrusted by the us...
static void setTemporarilyTrustedProjectsFolders(const QStringList &trustedProjectsFolders)
Sets the list of projects and folders that have been temporarily determined as trusted by the user.
A embedded script entity for QgsObjectEntityVisitorInterface.
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
Definition qgsgui.cpp:224
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition qgshelp.cpp:41
A QgsObjectEntityVisitorInterface context object.
QgsProjectStorage * projectStorageFromUri(const QString &uri)
Returns storage implementation if the URI matches one. Returns nullptr otherwise (it is a normal file...
Abstract interface for project storage - to be implemented by various backends and registered in QgsP...
virtual QString filePath(const QString &uri)
Extracts and returns the file path from a storage backend uri, filesystem-based storage backends shou...
QgsProjectTrustDialog(QgsProject *project, QWidget *parent=nullptr, Qt::WindowFlags fl=QgsGuiUtils::ModalDialogFlags)
Constructor for QgsProjectTrustDialog using the specified project instance.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:112
QString fileName
Definition qgsproject.h:116
bool accept(QgsStyleEntityVisitorInterface *visitor) const
Accepts the specified style entity visitor, causing it to visit all style entities associated with th...
T value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
bool setValue(const T &value, const QString &dynamicKeyPart=QString()) const
Set settings value.
static const QgsSettingsEntryStringList * settingsCodeExecutionTrustedProjectsFolders
Settings entry for projects and folders that are allowed execution of embedded scripts across session...
static const QgsSettingsEntryStringList * settingsCodeExecutionUntrustedProjectsFolders
Settings entry for projects and folders that are denied execution of embedded scripts across sessions...