QGIS API Documentation 3.99.0-Master (21b3aa880ba)
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 <QSvgRenderer>
32
33#include "moc_qgsprojecttrustdialog.cpp"
34
35QgsProjectTrustDialog::QgsProjectTrustDialog( QgsProject *project, QWidget *parent, Qt::WindowFlags fl )
36 : QDialog( parent, fl )
37{
38 setupUi( this );
40
41 mButtonBox->button( QDialogButtonBox::StandardButton::YesToAll )->setText( tr( "Always Trust" ) );
42 mButtonBox->button( QDialogButtonBox::StandardButton::Yes )->setText( tr( "Trust" ) );
43 mButtonBox->button( QDialogButtonBox::StandardButton::No )->setText( tr( "Deny" ) );
44 mButtonBox->button( QDialogButtonBox::StandardButton::NoToAll )->setText( tr( "Always Deny" ) );
45
46 connect( mButtonBox, &QDialogButtonBox::clicked, this, &QgsProjectTrustDialog::buttonBoxClicked );
47
48 connect( mScriptPreviewList, &QListWidget::itemDoubleClicked, this, [this]( QListWidgetItem *item ) {
49 const int row = mScriptPreviewList->row( item );
50 if ( row >= 0 && row < mEmbeddedScriptsVisitor.embeddedScripts().size() )
51 {
52 mScriptPreviewEditor->setText( mEmbeddedScriptsVisitor.embeddedScripts()[row].script() );
53 mScriptPreviewStackedWidget->setCurrentIndex( 1 );
54 }
55 } );
56
57 connect( mScriptPreviewBackButton, &QAbstractButton::clicked, this, [this] {
58 mScriptPreviewStackedWidget->setCurrentIndex( 0 );
59 } );
60
61 mScriptPreviewEditor->setReadOnly( true );
62 mScriptPreviewEditor->setLineNumbersVisible( false );
63
64 QSvgRenderer svg( QStringLiteral( ":/images/themes/default/mIconPythonFile.svg" ) );
65 if ( svg.isValid() )
66 {
67 const double maxLength = 64.0;
68 QSizeF size( maxLength, maxLength );
69 const QRectF viewBox = svg.viewBoxF();
70 if ( viewBox.height() > viewBox.width() )
71 {
72 size.setWidth( maxLength * viewBox.width() / viewBox.height() );
73 }
74 else
75 {
76 size.setHeight( maxLength * viewBox.height() / viewBox.width() );
77 }
78
79 QPixmap pixmap( static_cast<int>( maxLength ), static_cast<int>( maxLength ) );
80 pixmap.fill( Qt::transparent );
81
82 QPainter painter;
83 painter.begin( &pixmap );
84 painter.setRenderHint( QPainter::SmoothPixmapTransform );
85 painter.translate( ( maxLength - size.width() ) / 2, ( maxLength - size.height() ) / 2 );
86 svg.render( &painter, QRectF( 0, 0, size.width(), size.height() ) );
87 painter.end();
88
89 mIconLabel->setPixmap( pixmap );
90 }
91
92 if ( project )
93 {
95 if ( storage )
96 {
97 if ( !storage->filePath( project->fileName() ).isEmpty() )
98 {
99 QFileInfo projectFileInfo( storage->filePath( project->fileName() ) );
100 mProjectAbsoluteFilePath = projectFileInfo.absoluteFilePath();
101 mProjectAbsolutePath = projectFileInfo.absolutePath();
102 mProjectIsFile = true;
103 }
104 else
105 {
106 mProjectAbsolutePath = project->fileName();
107 mProjectIsFile = false;
108 }
109 }
110 else
111 {
112 QFileInfo projectFileInfo( project->fileName() );
113 mProjectAbsoluteFilePath = projectFileInfo.absoluteFilePath();
114 mProjectAbsolutePath = projectFileInfo.absolutePath();
115 mProjectIsFile = true;
116 }
117
118 if ( mProjectIsFile )
119 {
120 mProjectDetailsLabel->setText( tr( "The current project file path is ’%1’." ).arg( QStringLiteral( "<b>%1</b>" ).arg( mProjectAbsoluteFilePath ) ) );
121 QDir dir( mProjectAbsolutePath );
122 mTrustProjectFolderCheckBox->setText( tr( "Apply decision to all projects in folder ’%1’" ).arg( QStringLiteral( "%1" ).arg( dir.dirName() ) ) );
123 }
124 else
125 {
126 mProjectDetailsLabel->setText( tr( "The current project URI is ’%1’." ).arg( QStringLiteral( "<b>%1</b>" ).arg( mProjectAbsoluteFilePath ) ) );
127 mTrustProjectFolderCheckBox->setVisible( false );
128 }
129
130 project->accept( &mEmbeddedScriptsVisitor, QgsObjectVisitorContext() );
131 }
132
133 for ( const QgsEmbeddedScriptEntity &scriptDetails : mEmbeddedScriptsVisitor.embeddedScripts() )
134 {
135 QListWidgetItem *newItem = new QListWidgetItem( mScriptPreviewList );
136 switch ( scriptDetails.type() )
137 {
139 newItem->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mIconPythonFile.svg" ) ) );
140 break;
141
143 newItem->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mIconExpression.svg" ) ) );
144 break;
145
147 newItem->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mAction.svg" ) ) );
148 break;
149
151 newItem->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionFormView.svg" ) ) );
152 break;
153 }
154 newItem->setText( scriptDetails.name() );
155 newItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
156 mScriptPreviewList->addItem( newItem );
157 }
158}
159
160void QgsProjectTrustDialog::buttonBoxClicked( QAbstractButton *button )
161{
162 QDialogButtonBox::StandardButton buttonType = mButtonBox->standardButton( button );
163 if ( buttonType == QDialogButtonBox::StandardButton::Help )
164 {
165 showHelp();
166 return;
167 }
168
169 bool accepted = false;
170 QString path = !mProjectIsFile || mTrustProjectFolderCheckBox->isChecked() ? mProjectAbsolutePath : mProjectAbsoluteFilePath;
171 if ( !path.isEmpty() )
172 {
174 trustedProjectsFolders.removeAll( path );
176 untrustedProjectsFolders.removeAll( path );
177
178 QStringList temporarilyTrustedProjectsFolders = QgsApplication::temporarilyTrustedProjectsFolders();
179 temporarilyTrustedProjectsFolders.removeAll( path );
180 QStringList temporarilyUntrustedProjectsFolders = QgsApplication::temporarilyUntrustedProjectsFolders();
181 temporarilyUntrustedProjectsFolders.removeAll( path );
182
183 if ( buttonType == QDialogButtonBox::StandardButton::YesToAll )
184 {
185 trustedProjectsFolders << path;
186 accepted = true;
187 }
188 else if ( buttonType == QDialogButtonBox::StandardButton::Yes )
189 {
190 temporarilyTrustedProjectsFolders << path;
191 accepted = true;
192 }
193 else if ( buttonType == QDialogButtonBox::StandardButton::NoToAll )
194 {
195 untrustedProjectsFolders << path;
196 accepted = false;
197 }
198 else if ( buttonType == QDialogButtonBox::StandardButton::No )
199 {
200 temporarilyUntrustedProjectsFolders << path;
201 accepted = false;
202 }
203
204 trustedProjectsFolders.sort();
205 untrustedProjectsFolders.sort();
206 temporarilyTrustedProjectsFolders.sort();
207 temporarilyUntrustedProjectsFolders.sort();
208
211
212 QgsApplication::setTemporarilyTrustedProjectsFolders( temporarilyTrustedProjectsFolders );
213 QgsApplication::setTemporarilyUntrustedProjectsFolders( temporarilyUntrustedProjectsFolders );
214 }
215
216 done( accepted ? QDialog::Accepted : QDialog::Rejected );
217}
218
219void QgsProjectTrustDialog::showHelp()
220{
221 QgsHelp::openHelp( QStringLiteral( "introduction/getting_started.html" ) );
222}
@ Action
Expression functions.
Definition qgis.h:442
@ FormInitCode
Map layers' action.
Definition qgis.h:443
@ ExpressionFunction
Project macros.
Definition qgis.h:441
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:221
static void openHelp(const QString &key)
Opens help topic for the given help key using default system web browser.
Definition qgshelp.cpp:38
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:109
QString fileName
Definition qgsproject.h:113
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...