Skip to content
Permalink
Browse files

use QgsNetworkContentFetcherRegistry

  • Loading branch information
3nids committed Apr 27, 2018
1 parent c04ca96 commit 30b7fd183cc1d041224a2bda2593c6ab62745aed
@@ -64,12 +64,6 @@ Constructor for TabData
CodeSourceEnvironment
};

enum FormPath
{
Original,
LocalCopy
};

QgsEditFormConfig( const QgsEditFormConfig &o );
%Docstring
Copy constructor
@@ -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 );
@@ -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;
@@ -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()
@@ -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"
@@ -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
@@ -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 );

@@ -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
*
@@ -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.
@@ -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
@@ -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;
@@ -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

/**
@@ -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 );
}
@@ -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;
@@ -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"
@@ -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();

0 comments on commit 30b7fd1

Please sign in to comment.
You can’t perform that action at this time.