QGIS API Documentation 3.41.0-Master (cea29feecf2)
Loading...
Searching...
No Matches
qgsexternalstoragefilewidget.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsexternalstoragefilewidget.cpp
3 --------------------------------------
4 Date : August 2021
5 Copyright : (C) 2021 by Julien Cabieces
6 Email : julien dot cabieces at oslandia 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#include "moc_qgsexternalstoragefilewidget.cpp"
18
19#include <QLineEdit>
20#include <QToolButton>
21#include <QLabel>
22#include <QGridLayout>
23#include <QUrl>
24#include <QDropEvent>
25#include <QRegularExpression>
26#include <QProgressBar>
27
28#include "qgslogger.h"
29#include "qgsapplication.h"
30#include "qgsexternalstorage.h"
32#include "qgsmessagebar.h"
33#include "qgsexpression.h"
34
35#define FILEPATH_VARIABLE "selected_file_path"
36
38 : QgsFileWidget( parent )
39{
40 mProgressLabel = new QLabel( this );
41 mLayout->addWidget( mProgressLabel );
42 mProgressLabel->hide();
43
44 mProgressBar = new QProgressBar( this );
45 mLayout->addWidget( mProgressBar );
46 mProgressBar->hide();
47
48 mCancelButton = new QToolButton( this );
49 mLayout->addWidget( mCancelButton );
50 mCancelButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mTaskCancel.svg" ) ) );
51 mCancelButton->hide();
52
53 updateAcceptDrops();
54}
55
56void QgsExternalStorageFileWidget::setStorageType( const QString &storageType )
57{
58 if ( storageType.isEmpty() )
59 mExternalStorage = nullptr;
60
61 else
62 {
64 if ( !mExternalStorage )
65 {
66 QgsDebugError( QStringLiteral( "Invalid storage type: %1" ).arg( storageType ) );
67 }
68 else
69 {
71 }
72 }
73
74 updateAcceptDrops();
75}
76
78{
80 updateAcceptDrops();
81}
82
83void QgsExternalStorageFileWidget::updateAcceptDrops()
84{
85 setAcceptDrops( !mReadOnly && mExternalStorage );
86}
87
89{
90 return mExternalStorage ? mExternalStorage->type() : QString();
91}
92
94{
95 return mExternalStorage;
96}
97
99{
100 mAuthCfg = authCfg;
101}
102
104{
105 return mAuthCfg;
106}
107
109{
110 mStorageUrlExpression.reset( new QgsExpression( urlExpression ) );
111}
112
114{
115 return mStorageUrlExpression.get();
116}
117
119{
120 return mStorageUrlExpression ? mStorageUrlExpression->expression() : QString();
121}
122
123
125{
126 mScope = nullptr; // deleted by old context when we override it with the new one
127 mExpressionContext = context;
129}
130
132{
133 if ( !mExternalStorage || mScope )
134 return;
135
136 mScope = createFileWidgetScope();
137 mExpressionContext << mScope;
138}
139
141{
142 QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "FileWidget" ) );
144 QStringLiteral( FILEPATH_VARIABLE ),
145 QString(), true, false, tr( "User selected absolute filepath" )
146 ) );
147 return scope;
148}
149
151{
152 return mExpressionContext;
153}
154
155
157{
158 mMessageBar = messageBar;
159}
160
162{
163 return mMessageBar;
164}
165
167{
168 mProgressLabel->setVisible( mStoreInProgress );
169 mProgressBar->setVisible( mStoreInProgress );
170 mCancelButton->setVisible( mStoreInProgress );
171
172 const bool linkVisible = mUseLink && !mIsLinkEdited;
173
174 mLineEdit->setVisible( !mStoreInProgress && !linkVisible );
175 mLinkLabel->setVisible( !mStoreInProgress && linkVisible );
176 mLinkEditButton->setVisible( !mStoreInProgress && mUseLink && !mReadOnly );
177
178 mFileWidgetButton->setVisible( mButtonVisible && !mStoreInProgress );
179 mFileWidgetButton->setEnabled( !mReadOnly );
180 mLineEdit->setEnabled( !mReadOnly );
181
182 mLinkEditButton->setIcon( linkVisible && !mReadOnly ? QgsApplication::getThemeIcon( QStringLiteral( "/mActionToggleEditing.svg" ) ) : QgsApplication::getThemeIcon( QStringLiteral( "/mActionSaveEdits.svg" ) ) );
183}
184
186{
187 Q_ASSERT( fileNames.count() );
188
189 // store files first, update filePath later
190 if ( mExternalStorage )
191 {
192 if ( !mStorageUrlExpression->prepare( &mExpressionContext ) )
193 {
194 if ( messageBar() )
195 {
196 messageBar()->pushWarning( tr( "Storing External resource" ), tr( "Storage URL expression is invalid : %1" ).arg( mStorageUrlExpression->evalErrorString() ) );
197 }
198
199 QgsDebugError( QStringLiteral( "Storage URL expression is invalid : %1" ).arg( mStorageUrlExpression->evalErrorString() ) );
200 return;
201 }
202
203 storeExternalFiles( fileNames );
204 }
205 else
206 {
208 }
209}
210
211void QgsExternalStorageFileWidget::storeExternalFiles( QStringList fileNames, QStringList storedUrls )
212{
213 if ( fileNames.isEmpty() )
214 return;
215
216 const QString filePath = fileNames.takeFirst();
217
218 mProgressLabel->setText( tr( "Storing file %1 ..." ).arg( QFileInfo( filePath ).fileName() ) );
219 mStoreInProgress = true;
220 mProgressBar->setValue( 0 );
221 updateLayout();
222
223 Q_ASSERT( mScope );
224 mScope->setVariable( QStringLiteral( FILEPATH_VARIABLE ), filePath );
225
226 QVariant url = mStorageUrlExpression->evaluate( &mExpressionContext );
227 if ( !url.isValid() )
228 {
229 if ( messageBar() )
230 {
231 messageBar()->pushWarning( tr( "Storing External resource" ), tr( "Storage URL expression is invalid : %1" ).arg( mStorageUrlExpression->evalErrorString() ) );
232 }
233
234 mStoreInProgress = false;
235 updateLayout();
236
237 return;
238 }
239
240 QgsExternalStorageStoredContent *storedContent = mExternalStorage->store( filePath, url.toString(), mAuthCfg );
241
242 connect( storedContent, &QgsExternalStorageStoredContent::progressChanged, mProgressBar, &QProgressBar::setValue );
243 connect( mCancelButton, &QToolButton::clicked, storedContent, &QgsExternalStorageStoredContent::cancel );
244
245 auto onStoreFinished = [=] {
246 mStoreInProgress = false;
247 updateLayout();
248 storedContent->deleteLater();
249
250 if ( storedContent->status() == Qgis::ContentStatus::Failed && messageBar() )
251 {
252 messageBar()->pushWarning( tr( "Storing External resource" ), tr( "Storing file '%1' to url '%2' has failed : %3" ).arg( filePath, url.toString(), storedContent->errorString() ) );
253 }
254
255 if ( storedContent->status() != Qgis::ContentStatus::Finished )
256 return;
257
258 QStringList newStoredUrls = storedUrls;
259 newStoredUrls << storedContent->url();
260
261 // every thing has been stored, we update filepath
262 if ( fileNames.isEmpty() )
263 {
264 setFilePaths( newStoredUrls );
265 }
266 else
267 storeExternalFiles( fileNames, newStoredUrls );
268 };
269
270 connect( storedContent, &QgsExternalStorageStoredContent::stored, onStoreFinished );
271 connect( storedContent, &QgsExternalStorageStoredContent::canceled, onStoreFinished );
272 connect( storedContent, &QgsExternalStorageStoredContent::errorOccurred, onStoreFinished );
273
274 storedContent->store();
275}
276
278{
279 const QStringList filePaths = mLineEdit->acceptableFilePaths( event );
280 if ( !filePaths.isEmpty() )
281 {
282 event->acceptProposedAction();
283 }
284 else
285 {
286 event->ignore();
287 }
288}
289
291{
292 storeExternalFiles( mLineEdit->acceptableFilePaths( event ) );
293 event->acceptProposedAction();
294}
@ Finished
Content fetching/storing is finished and successful.
@ Failed
Content fetching/storing has failed.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static QgsExternalStorageRegistry * externalStorageRegistry()
Returns registry of available external storage implementations.
Single scope for storing variables and functions for use within a QgsExpressionContext.
void addVariable(const QgsExpressionContextScope::StaticVariable &variable)
Adds a variable into the context scope.
void setVariable(const QString &name, const QVariant &value, bool isStatic=false)
Convenience method for setting a variable in the context scope by name name and value.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Class for parsing and evaluation of expressions (formerly called "search strings").
void canceled()
The signal is emitted when content fetching/storing has been canceled.
void progressChanged(double progress)
The signal is emitted whenever content fetching/storing estimated progression value progress has chan...
void errorOccurred(const QString &errorString)
The signal is emitted when an error occurred.
Qgis::ContentStatus status() const
Returns content status.
virtual void cancel()
Cancels content fetching/storing.
const QString & errorString() const
Returns error textual description if an error occurred and status() returns Failed.
void updateLayout() override
Update buttons visibility.
void setSelectedFileNames(QStringList fileNames) override
Called whenever user select fileNames from dialog.
void setMessageBar(QgsMessageBar *messageBar)
Set messageBar to report messages.
const QString & storageAuthConfigId() const
Returns the authentication configuration ID used for the current external storage (if defined)
void setStorageType(const QString &storageType)
Set storageType storage type unique identifier as defined in QgsExternalStorageRegistry or null QStri...
void setExpressionContext(const QgsExpressionContext &context)
Set expression context to be used when for storage URL expression evaluation.
QString storageUrlExpressionString() const
Returns the original, unmodified expression string, which once evaluated, provide the URL used to sto...
void setStorageUrlExpression(const QString &urlExpression)
Set urlExpression expression, which once evaluated, provide the URL used to store selected documents.
static QgsExpressionContextScope * createFileWidgetScope()
Creates and Returns an expression context scope specific to QgsExternalStorageFileWidget It defines t...
QgsExternalStorageFileWidget(QWidget *parent=nullptr)
QgsExternalStorageFileWidget creates a widget for selecting a file or a folder.
void setReadOnly(bool readOnly) override
Sets whether the widget should be read only.
QgsExternalStorage * externalStorage() const
Returns external storage used to store selected file names, nullptr if none have been defined.
void setStorageAuthConfigId(const QString &authCfg)
Sets the authentication configuration ID to be used for the current external storage (if defined)
QgsMessageBar * messageBar() const
Returns message bar used to report messages.
void addFileWidgetScope()
Add file widget specific scope to expression context.
void dragEnterEvent(QDragEnterEvent *event) override
const QgsExpressionContext & expressionContext() const
Returns expression context used for storage url expression evaluation.
void dropEvent(QDropEvent *event) override
QgsExternalStorage * externalStorageFromType(const QString &type) const
Returns external storage implementation if the storage type matches one.
Class for QgsExternalStorage stored content.
void stored()
The signal is emitted when the resource has successfully been stored.
virtual QString url() const =0
Returns stored resource URL.
virtual void store()=0
Starts storing.
Abstract interface for external storage - to be implemented by various backends and registered in Qgs...
virtual QString type() const =0
Unique identifier of the external storage type.
QgsExternalStorageStoredContent * store(const QString &filePath, const QString &url, const QString &authCfg=QString(), Qgis::ActionStart storingMode=Qgis::ActionStart::Deferred) const
Stores file filePath to the url for this project external storage.
The QgsFileWidget class creates a widget for selecting a file or a folder.
QString filePath()
Returns the current file path(s).
void setFilePaths(const QStringList &filePaths)
Update filePath according to filePaths list.
QHBoxLayout * mLayout
virtual void setSelectedFileNames(QStringList fileNames)
Called whenever user select fileNames from dialog.
QgsFileDropEdit * mLineEdit
QToolButton * mLinkEditButton
QToolButton * mFileWidgetButton
QLabel * mLinkLabel
virtual void setReadOnly(bool readOnly)
Sets whether the widget should be read only.
A bar for displaying non-blocking messages to the user.
void pushWarning(const QString &title, const QString &message)
Pushes a warning message that must be manually dismissed by the user.
#define FILEPATH_VARIABLE
#define QgsDebugError(str)
Definition qgslogger.h:38
Single variable definition for use within a QgsExpressionContextScope.