|
14 | 14 | ***************************************************************************/ |
15 | 15 | #include "qgseditformconfig_p.h" |
16 | 16 | #include "qgseditformconfig.h" |
| 17 | +#include "qgsnetworkcontentfetcher.h" |
17 | 18 | #include "qgspathresolver.h" |
18 | 19 | #include "qgsproject.h" |
19 | 20 | #include "qgsreadwritecontext.h" |
@@ -146,22 +147,75 @@ void QgsEditFormConfig::setLayout( QgsEditFormConfig::EditorLayout editorLayout |
146 | 147 | d->mConfiguredRootContainer = true; |
147 | 148 | } |
148 | 149 |
|
149 | | -QString QgsEditFormConfig::uiForm() const |
| 150 | +QString QgsEditFormConfig::uiForm( FormPath path ) const |
150 | 151 | { |
151 | | - return d->mUiFormPath; |
| 152 | + if ( path == Original && !d->mUiFormUrl.isEmpty() ) |
| 153 | + return d->mUiFormUrl; |
| 154 | + else |
| 155 | + return d->mUiFormPath; |
152 | 156 | } |
153 | 157 |
|
154 | | -void QgsEditFormConfig::setUiForm( const QString &ui ) |
| 158 | +bool QgsEditFormConfig::setUiForm( const QString &ui, QString *errMsg ) |
155 | 159 | { |
156 | | - if ( ui.isEmpty() || ui.isNull() ) |
| 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(); |
| 171 | + if ( !ui.isEmpty() && !QUrl::fromUserInput( ui ).isLocalFile() ) |
| 172 | + { |
| 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 ) |
| 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 = QString( "Could not load UI from %1" ).arg( ui ); |
| 200 | + } |
| 201 | + } |
| 202 | + else |
| 203 | + { |
| 204 | + success = true; |
| 205 | + } |
| 206 | + |
| 207 | + if ( formPath.isEmpty() ) |
157 | 208 | { |
158 | 209 | setLayout( GeneratedLayout ); |
159 | 210 | } |
160 | 211 | else |
161 | 212 | { |
162 | 213 | setLayout( UiFileLayout ); |
163 | 214 | } |
164 | | - d->mUiFormPath = ui; |
| 215 | + d->mUiFormPath = formPath; |
| 216 | + d->mUiFormUrl = formUrl; |
| 217 | + |
| 218 | + return success; |
165 | 219 | } |
166 | 220 |
|
167 | 221 | bool QgsEditFormConfig::readOnly( int idx ) const |
@@ -268,7 +322,9 @@ void QgsEditFormConfig::readXml( const QDomNode &node, QgsReadWriteContext &cont |
268 | 322 | if ( !editFormNode.isNull() ) |
269 | 323 | { |
270 | 324 | QDomElement e = editFormNode.toElement(); |
271 | | - d->mUiFormPath = context.pathResolver().readPath( e.text() ); |
| 325 | + QString *errMsg = new QString(); |
| 326 | + if ( !setUiForm( context.pathResolver().readPath( e.text() ), errMsg ) ) |
| 327 | + context.pushMessage( *errMsg, Qgis::Warning ); |
272 | 328 | } |
273 | 329 |
|
274 | 330 | QDomNode editFormInitNode = node.namedItem( QStringLiteral( "editforminit" ) ); |
@@ -396,7 +452,7 @@ void QgsEditFormConfig::writeXml( QDomNode &node, const QgsReadWriteContext &con |
396 | 452 | QDomDocument doc( node.ownerDocument() ); |
397 | 453 |
|
398 | 454 | QDomElement efField = doc.createElement( QStringLiteral( "editform" ) ); |
399 | | - QDomText efText = doc.createTextNode( context.pathResolver().writePath( uiForm() ) ); |
| 455 | + QDomText efText = doc.createTextNode( context.pathResolver().writePath( uiForm( Original ) ) ); |
400 | 456 | efField.appendChild( efText ); |
401 | 457 | node.appendChild( efField ); |
402 | 458 |
|
|
0 commit comments