Skip to content

Commit 30b7fd1

Browse files
committedMay 9, 2018
use QgsNetworkContentFetcherRegistry
1 parent c04ca96 commit 30b7fd1

11 files changed

+25
-107
lines changed
 

‎python/core/qgseditformconfig.sip.in

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,6 @@ Constructor for TabData
6464
CodeSourceEnvironment
6565
};
6666

67-
enum FormPath
68-
{
69-
Original,
70-
LocalCopy
71-
};
72-
7367
QgsEditFormConfig( const QgsEditFormConfig &o );
7468
%Docstring
7569
Copy constructor
@@ -115,11 +109,9 @@ Get the active layout style for the attribute editor for this layer
115109
Set the active layout style for the attribute editor for this layer
116110
%End
117111

118-
QString uiForm( FormPath path = LocalCopy ) const;
112+
QString uiForm() const;
119113
%Docstring
120-
Get path to the .ui form. Only meaningful with EditorLayout.UiFileLayout
121-
If the form is from a URL and ``path`` is Original, the original URL
122-
of the UI form is returned instead of the local copy.
114+
Get path or URL to the .ui form. Only meaningful with EditorLayout.UiFileLayout
123115
%End
124116

125117
bool setUiForm( const QString &ui, QString *errMsg /Out/ = 0 );

‎python/core/qgsproject.sip.in

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -930,16 +930,6 @@ provider.
930930
Returns the current auxiliary storage.
931931

932932
.. versionadded:: 3.0
933-
%End
934-
935-
void addUiFormLocalCopy( QTemporaryFile *file );
936-
%Docstring
937-
Save a pointer to temporary file to keep local copy
938-
available of downloaded UI forms during project's life
939-
940-
:param file: the pointer to the temporary file
941-
942-
.. versionadded:: 3.2
943933
%End
944934

945935
const QgsProjectMetadata &metadata() const;

‎src/app/qgsattributesformproperties.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ void QgsAttributesFormProperties::initLayoutConfig()
169169
mEditorLayoutComboBox_currentIndexChanged( mEditorLayoutComboBox->currentIndex() );
170170

171171
QgsEditFormConfig cfg = mLayer->editFormConfig();
172-
mEditFormLineEdit->setText( cfg.uiForm( QgsEditFormConfig::Original ) );
172+
mEditFormLineEdit->setText( cfg.uiForm() );
173173
}
174174

175175
void QgsAttributesFormProperties::initInitPython()

‎src/core/qgseditformconfig.cpp

Lines changed: 10 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
***************************************************************************/
1515
#include "qgseditformconfig_p.h"
1616
#include "qgseditformconfig.h"
17-
#include "qgsnetworkcontentfetcher.h"
17+
#include "qgsnetworkcontentfetcherregistry.h"
1818
#include "qgspathresolver.h"
1919
#include "qgsproject.h"
2020
#include "qgsreadwritecontext.h"
@@ -147,75 +147,31 @@ void QgsEditFormConfig::setLayout( QgsEditFormConfig::EditorLayout editorLayout
147147
d->mConfiguredRootContainer = true;
148148
}
149149

150-
QString QgsEditFormConfig::uiForm( FormPath path ) const
150+
QString QgsEditFormConfig::uiForm() const
151151
{
152-
if ( path == Original && !d->mUiFormUrl.isEmpty() )
153-
return d->mUiFormUrl;
154-
else
155-
return d->mUiFormPath;
152+
return d->mUiFormPath;
156153
}
157154

158155
bool QgsEditFormConfig::setUiForm( const QString &ui, QString *errMsg )
159156
{
160-
bool success = false;
161-
162-
if ( !ui.isEmpty() && ui == d->mUiFormUrl && !d->mUiFormPath.isEmpty() )
163-
{
164-
// do not download again if URL did not change and was correctly loaded before
165-
return success;
166-
}
167-
168-
// if the ui points to a URL make a local copy
169-
QString formPath = ui;
170-
QString formUrl = QString();
157+
Q_UNUSED( errMsg );
171158
if ( !ui.isEmpty() && !QUrl::fromUserInput( ui ).isLocalFile() )
172159
{
173-
formPath = QString();
174-
formUrl = ui;
175-
176-
QgsNetworkContentFetcher fetcher;
177-
QEventLoop loop;
178-
QObject::connect( &fetcher, &QgsNetworkContentFetcher::finished, &loop, &QEventLoop::quit );
179-
fetcher.fetchContent( QUrl( ui ) );
180-
181-
//wait until form is fetched
182-
loop.exec( QEventLoop::ExcludeUserInputEvents );
183-
184-
QNetworkReply *reply = fetcher.reply();
185-
if ( reply && reply->error() == QNetworkReply::NoError )
186-
{
187-
QTemporaryFile *localFile = new QTemporaryFile( QStringLiteral( "XXXXXX.ui" ) );
188-
if ( localFile->open() )
189-
{
190-
localFile->write( reply->readAll() );
191-
localFile->close();
192-
success = true;
193-
QgsProject::instance()->addUiFormLocalCopy( localFile );
194-
formPath = localFile->fileName();
195-
}
196-
}
197-
if ( !success && errMsg )
198-
{
199-
*errMsg = QStringLiteral( "Could not load UI from %1 (%2)" ).arg( ui ).arg( reply->errorString() );
200-
}
201-
}
202-
else
203-
{
204-
success = true;
160+
// any existing download will not be restarted!
161+
QgsApplication::instance()->networkContentFetcherRegistry()->fetch( ui, QgsNetworkContentFetcherRegistry::DownloadImmediately );
205162
}
206163

207-
if ( formPath.isEmpty() )
164+
if ( ui.isEmpty() )
208165
{
209166
setLayout( GeneratedLayout );
210167
}
211168
else
212169
{
213170
setLayout( UiFileLayout );
214171
}
215-
d->mUiFormPath = formPath;
216-
d->mUiFormUrl = formUrl;
172+
d->mUiFormPath = ui;
217173

218-
return success;
174+
return true;
219175
}
220176

221177
bool QgsEditFormConfig::readOnly( int idx ) const
@@ -452,7 +408,7 @@ void QgsEditFormConfig::writeXml( QDomNode &node, const QgsReadWriteContext &con
452408
QDomDocument doc( node.ownerDocument() );
453409

454410
QDomElement efField = doc.createElement( QStringLiteral( "editform" ) );
455-
QDomText efText = doc.createTextNode( context.pathResolver().writePath( uiForm( Original ) ) );
411+
QDomText efText = doc.createTextNode( context.pathResolver().writePath( uiForm() ) );
456412
efField.appendChild( efText );
457413
node.appendChild( efField );
458414

‎src/core/qgseditformconfig.h

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,6 @@ class CORE_EXPORT QgsEditFormConfig
9393
CodeSourceEnvironment = 3 //!< Use the Python code available in the Python environment
9494
};
9595

96-
/**
97-
* The FormPath enum determines the path of the custom UI form
98-
*/
99-
enum FormPath
100-
{
101-
Original, //!< User entered directory or URL
102-
LocalCopy //!< If the Original is an URL, this is for the local copy of the file
103-
};
104-
10596
/**
10697
* Copy constructor
10798
*
@@ -145,11 +136,9 @@ class CORE_EXPORT QgsEditFormConfig
145136
void setLayout( EditorLayout editorLayout );
146137

147138
/**
148-
* \brief Get path to the .ui form. Only meaningful with EditorLayout::UiFileLayout
149-
* If the form is from a URL and \a path is Original, the original URL
150-
* of the UI form is returned instead of the local copy.
139+
* \brief Get path or URL to the .ui form. Only meaningful with EditorLayout::UiFileLayout
151140
*/
152-
QString uiForm( FormPath path = LocalCopy ) const;
141+
QString uiForm() const;
153142

154143
/**
155144
* Set path to the .ui form.

‎src/core/qgseditformconfig_p.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,8 @@ class QgsEditFormConfigPrivate : public QSharedData
6565
//! Defines the default layout to use for the attribute editor (Drag and drop, UI File, Generated)
6666
QgsEditFormConfig::EditorLayout mEditorLayout = QgsEditFormConfig::GeneratedLayout;
6767

68-
//! Path to the UI form
68+
//! Path or URL to the UI form
6969
QString mUiFormPath;
70-
//! URL of the UI form if taken from the web
71-
QString mUiFormUrl;
7270
//! Name of the Python form init function
7371
QString mInitFunction;
7472
//! Path of the Python external file to be loaded

‎src/core/qgsnetworkcontentfetcherregistry.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ const QgsFetchedContent *QgsNetworkContentFetcherRegistry::fetch( const QUrl &ur
128128
return content;
129129
}
130130

131-
const QFile *QgsNetworkContentFetcherRegistry::localFile( const QString &filePathOrUrl )
131+
QFile *QgsNetworkContentFetcherRegistry::localFile( const QString &filePathOrUrl )
132132
{
133133
QFile *file = nullptr;
134134
QString path = filePathOrUrl;

‎src/core/qgsnetworkcontentfetcherregistry.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class CORE_EXPORT QgsNetworkContentFetcherRegistry : public QObject
159159
* \brief Returns a QFile from a local file or to a temporary file previously fetched by the registry
160160
* \param filePathOrUrl can either be a local file path or a remote content which has previously been fetched
161161
*/
162-
const QFile *localFile( const QString &filePathOrUrl );
162+
QFile *localFile( const QString &filePathOrUrl );
163163
#endif
164164

165165
/**

‎src/core/qgsproject.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2687,8 +2687,3 @@ void QgsProject::setRequiredLayers( const QSet<QgsMapLayer *> &layers )
26872687
}
26882688
writeEntry( QStringLiteral( "RequiredLayers" ), QStringLiteral( "Layers" ), layerIds );
26892689
}
2690-
2691-
void QgsProject::addUiFormLocalCopy( QTemporaryFile *file )
2692-
{
2693-
mUiFormLocalCopies.append( file );
2694-
}

‎src/core/qgsproject.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,9 +1351,6 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera
13511351

13521352
QgsCoordinateTransformContext mTransformContext;
13531353

1354-
// local temporary copies of downloaded UI form files
1355-
QList<QPointer<QTemporaryFile>> mUiFormLocalCopies;
1356-
13571354
QgsProjectMetadata mMetadata;
13581355

13591356
friend class QgsProjectDirtyBlocker;

‎src/gui/qgsattributeform.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "qgsattributeformeditorwidget.h"
2828
#include "qgsmessagebar.h"
2929
#include "qgsmessagebaritem.h"
30+
#include "qgsnetworkcontentfetcherregistry.h"
3031
#include "qgseditorwidgetwrapper.h"
3132
#include "qgsrelationmanager.h"
3233
#include "qgslogger.h"
@@ -1118,21 +1119,21 @@ void QgsAttributeForm::init()
11181119
!mLayer->editFormConfig().uiForm().isEmpty() )
11191120
{
11201121
QgsDebugMsg( QString( "loading form: %1" ).arg( mLayer->editFormConfig().uiForm() ) );
1121-
QFile file( mLayer->editFormConfig().uiForm( QgsEditFormConfig::LocalCopy ) );
1122-
1123-
if ( file.open( QFile::ReadOnly ) )
1122+
const QString path = mLayer->editFormConfig().uiForm();
1123+
QFile *file = QgsApplication::instance()->networkContentFetcherRegistry()->localFile( path );
1124+
if ( file->isReadable() && file->open( QFile::ReadOnly ) )
11241125
{
11251126
QUiLoader loader;
11261127

1127-
QFileInfo fi( file );
1128+
QFileInfo fi( file->fileName() );
11281129
loader.setWorkingDirectory( fi.dir() );
1129-
formWidget = loader.load( &file, this );
1130+
formWidget = loader.load( file, this );
11301131
if ( formWidget )
11311132
{
11321133
formWidget->setWindowFlags( Qt::Widget );
11331134
layout->addWidget( formWidget );
11341135
formWidget->show();
1135-
file.close();
1136+
file->close();
11361137
mButtonBox = findChild<QDialogButtonBox *>();
11371138
createWrappers();
11381139

0 commit comments

Comments
 (0)
Please sign in to comment.