Skip to content

Commit

Permalink
use QgsNetworkContentFetcherRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed May 9, 2018
1 parent c04ca96 commit 30b7fd1
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 107 deletions.
12 changes: 2 additions & 10 deletions python/core/qgseditformconfig.sip.in
Expand Up @@ -64,12 +64,6 @@ Constructor for TabData
CodeSourceEnvironment
};

enum FormPath
{
Original,
LocalCopy
};

QgsEditFormConfig( const QgsEditFormConfig &o );
%Docstring
Copy constructor
Expand Down Expand Up @@ -115,11 +109,9 @@ Get the active layout style for the attribute editor for this layer
Set the active layout style for the attribute editor for this layer
%End

QString uiForm( FormPath path = LocalCopy ) const;
QString uiForm() const;
%Docstring
Get path to the .ui form. Only meaningful with EditorLayout.UiFileLayout
If the form is from a URL and ``path`` is Original, the original URL
of the UI form is returned instead of the local copy.
Get path or URL to the .ui form. Only meaningful with EditorLayout.UiFileLayout
%End

bool setUiForm( const QString &ui, QString *errMsg /Out/ = 0 );
Expand Down
10 changes: 0 additions & 10 deletions python/core/qgsproject.sip.in
Expand Up @@ -930,16 +930,6 @@ provider.
Returns the current auxiliary storage.

.. versionadded:: 3.0
%End

void addUiFormLocalCopy( QTemporaryFile *file );
%Docstring
Save a pointer to temporary file to keep local copy
available of downloaded UI forms during project's life

:param file: the pointer to the temporary file

.. versionadded:: 3.2
%End

const QgsProjectMetadata &metadata() const;
Expand Down
2 changes: 1 addition & 1 deletion src/app/qgsattributesformproperties.cpp
Expand Up @@ -169,7 +169,7 @@ void QgsAttributesFormProperties::initLayoutConfig()
mEditorLayoutComboBox_currentIndexChanged( mEditorLayoutComboBox->currentIndex() );

QgsEditFormConfig cfg = mLayer->editFormConfig();
mEditFormLineEdit->setText( cfg.uiForm( QgsEditFormConfig::Original ) );
mEditFormLineEdit->setText( cfg.uiForm() );
}

void QgsAttributesFormProperties::initInitPython()
Expand Down
64 changes: 10 additions & 54 deletions src/core/qgseditformconfig.cpp
Expand Up @@ -14,7 +14,7 @@
***************************************************************************/
#include "qgseditformconfig_p.h"
#include "qgseditformconfig.h"
#include "qgsnetworkcontentfetcher.h"
#include "qgsnetworkcontentfetcherregistry.h"
#include "qgspathresolver.h"
#include "qgsproject.h"
#include "qgsreadwritecontext.h"
Expand Down Expand Up @@ -147,75 +147,31 @@ void QgsEditFormConfig::setLayout( QgsEditFormConfig::EditorLayout editorLayout
d->mConfiguredRootContainer = true;
}

QString QgsEditFormConfig::uiForm( FormPath path ) const
QString QgsEditFormConfig::uiForm() const
{
if ( path == Original && !d->mUiFormUrl.isEmpty() )
return d->mUiFormUrl;
else
return d->mUiFormPath;
return d->mUiFormPath;
}

bool QgsEditFormConfig::setUiForm( const QString &ui, QString *errMsg )
{
bool success = false;

if ( !ui.isEmpty() && ui == d->mUiFormUrl && !d->mUiFormPath.isEmpty() )
{
// do not download again if URL did not change and was correctly loaded before
return success;
}

// if the ui points to a URL make a local copy
QString formPath = ui;
QString formUrl = QString();
Q_UNUSED( errMsg );
if ( !ui.isEmpty() && !QUrl::fromUserInput( ui ).isLocalFile() )
{
formPath = QString();
formUrl = ui;

QgsNetworkContentFetcher fetcher;
QEventLoop loop;
QObject::connect( &fetcher, &QgsNetworkContentFetcher::finished, &loop, &QEventLoop::quit );
fetcher.fetchContent( QUrl( ui ) );

//wait until form is fetched
loop.exec( QEventLoop::ExcludeUserInputEvents );

QNetworkReply *reply = fetcher.reply();
if ( reply && reply->error() == QNetworkReply::NoError )
{
QTemporaryFile *localFile = new QTemporaryFile( QStringLiteral( "XXXXXX.ui" ) );
if ( localFile->open() )
{
localFile->write( reply->readAll() );
localFile->close();
success = true;
QgsProject::instance()->addUiFormLocalCopy( localFile );
formPath = localFile->fileName();
}
}
if ( !success && errMsg )
{
*errMsg = QStringLiteral( "Could not load UI from %1 (%2)" ).arg( ui ).arg( reply->errorString() );
}
}
else
{
success = true;
// any existing download will not be restarted!
QgsApplication::instance()->networkContentFetcherRegistry()->fetch( ui, QgsNetworkContentFetcherRegistry::DownloadImmediately );
}

if ( formPath.isEmpty() )
if ( ui.isEmpty() )
{
setLayout( GeneratedLayout );
}
else
{
setLayout( UiFileLayout );
}
d->mUiFormPath = formPath;
d->mUiFormUrl = formUrl;
d->mUiFormPath = ui;

return success;
return true;
}

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

QDomElement efField = doc.createElement( QStringLiteral( "editform" ) );
QDomText efText = doc.createTextNode( context.pathResolver().writePath( uiForm( Original ) ) );
QDomText efText = doc.createTextNode( context.pathResolver().writePath( uiForm() ) );
efField.appendChild( efText );
node.appendChild( efField );

Expand Down
15 changes: 2 additions & 13 deletions src/core/qgseditformconfig.h
Expand Up @@ -93,15 +93,6 @@ class CORE_EXPORT QgsEditFormConfig
CodeSourceEnvironment = 3 //!< Use the Python code available in the Python environment
};

/**
* The FormPath enum determines the path of the custom UI form
*/
enum FormPath
{
Original, //!< User entered directory or URL
LocalCopy //!< If the Original is an URL, this is for the local copy of the file
};

/**
* Copy constructor
*
Expand Down Expand Up @@ -145,11 +136,9 @@ class CORE_EXPORT QgsEditFormConfig
void setLayout( EditorLayout editorLayout );

/**
* \brief Get path to the .ui form. Only meaningful with EditorLayout::UiFileLayout
* If the form is from a URL and \a path is Original, the original URL
* of the UI form is returned instead of the local copy.
* \brief Get path or URL to the .ui form. Only meaningful with EditorLayout::UiFileLayout
*/
QString uiForm( FormPath path = LocalCopy ) const;
QString uiForm() const;

/**
* Set path to the .ui form.
Expand Down
4 changes: 1 addition & 3 deletions src/core/qgseditformconfig_p.h
Expand Up @@ -65,10 +65,8 @@ class QgsEditFormConfigPrivate : public QSharedData
//! Defines the default layout to use for the attribute editor (Drag and drop, UI File, Generated)
QgsEditFormConfig::EditorLayout mEditorLayout = QgsEditFormConfig::GeneratedLayout;

//! Path to the UI form
//! Path or URL to the UI form
QString mUiFormPath;
//! URL of the UI form if taken from the web
QString mUiFormUrl;
//! Name of the Python form init function
QString mInitFunction;
//! Path of the Python external file to be loaded
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsnetworkcontentfetcherregistry.cpp
Expand Up @@ -128,7 +128,7 @@ const QgsFetchedContent *QgsNetworkContentFetcherRegistry::fetch( const QUrl &ur
return content;
}

const QFile *QgsNetworkContentFetcherRegistry::localFile( const QString &filePathOrUrl )
QFile *QgsNetworkContentFetcherRegistry::localFile( const QString &filePathOrUrl )
{
QFile *file = nullptr;
QString path = filePathOrUrl;
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsnetworkcontentfetcherregistry.h
Expand Up @@ -159,7 +159,7 @@ class CORE_EXPORT QgsNetworkContentFetcherRegistry : public QObject
* \brief Returns a QFile from a local file or to a temporary file previously fetched by the registry
* \param filePathOrUrl can either be a local file path or a remote content which has previously been fetched
*/
const QFile *localFile( const QString &filePathOrUrl );
QFile *localFile( const QString &filePathOrUrl );
#endif

/**
Expand Down
5 changes: 0 additions & 5 deletions src/core/qgsproject.cpp
Expand Up @@ -2687,8 +2687,3 @@ void QgsProject::setRequiredLayers( const QSet<QgsMapLayer *> &layers )
}
writeEntry( QStringLiteral( "RequiredLayers" ), QStringLiteral( "Layers" ), layerIds );
}

void QgsProject::addUiFormLocalCopy( QTemporaryFile *file )
{
mUiFormLocalCopies.append( file );
}
3 changes: 0 additions & 3 deletions src/core/qgsproject.h
Expand Up @@ -1351,9 +1351,6 @@ class CORE_EXPORT QgsProject : public QObject, public QgsExpressionContextGenera

QgsCoordinateTransformContext mTransformContext;

// local temporary copies of downloaded UI form files
QList<QPointer<QTemporaryFile>> mUiFormLocalCopies;

QgsProjectMetadata mMetadata;

friend class QgsProjectDirtyBlocker;
Expand Down
13 changes: 7 additions & 6 deletions src/gui/qgsattributeform.cpp
Expand Up @@ -27,6 +27,7 @@
#include "qgsattributeformeditorwidget.h"
#include "qgsmessagebar.h"
#include "qgsmessagebaritem.h"
#include "qgsnetworkcontentfetcherregistry.h"
#include "qgseditorwidgetwrapper.h"
#include "qgsrelationmanager.h"
#include "qgslogger.h"
Expand Down Expand Up @@ -1118,21 +1119,21 @@ void QgsAttributeForm::init()
!mLayer->editFormConfig().uiForm().isEmpty() )
{
QgsDebugMsg( QString( "loading form: %1" ).arg( mLayer->editFormConfig().uiForm() ) );
QFile file( mLayer->editFormConfig().uiForm( QgsEditFormConfig::LocalCopy ) );

if ( file.open( QFile::ReadOnly ) )
const QString path = mLayer->editFormConfig().uiForm();
QFile *file = QgsApplication::instance()->networkContentFetcherRegistry()->localFile( path );
if ( file->isReadable() && file->open( QFile::ReadOnly ) )
{
QUiLoader loader;

QFileInfo fi( file );
QFileInfo fi( file->fileName() );
loader.setWorkingDirectory( fi.dir() );
formWidget = loader.load( &file, this );
formWidget = loader.load( file, this );
if ( formWidget )
{
formWidget->setWindowFlags( Qt::Widget );
layout->addWidget( formWidget );
formWidget->show();
file.close();
file->close();
mButtonBox = findChild<QDialogButtonBox *>();
createWrappers();

Expand Down

0 comments on commit 30b7fd1

Please sign in to comment.