25
25
#include " qgsvectorlayer.h"
26
26
#include " qgsmessagelog.h"
27
27
#include " qgsdatadefined.h"
28
+ #include " qgsnetworkcontentfetcher.h"
28
29
#include < QDomDocument>
29
30
#include < QDomElement>
30
31
#include < QFileInfo>
31
32
#include < QImageReader>
32
33
#include < QPainter>
33
34
#include < QSvgRenderer>
34
-
35
+ #include < QNetworkRequest>
36
+ #include < QNetworkReply>
37
+ #include < QEventLoop>
38
+ #include < QCoreApplication>
35
39
36
40
QgsComposerPicture::QgsComposerPicture ( QgsComposition *composition ) :
37
41
QgsComposerItem( composition ),
@@ -268,20 +272,17 @@ QRect QgsComposerPicture::clippedImageRect( double &boundRectWidthMM, double &bo
268
272
break ;
269
273
}
270
274
271
-
272
275
return QRect ( leftClip, topClip, boundRectWidthPixels, boundRectHeightPixels );
273
-
274
276
}
275
277
276
278
void QgsComposerPicture::setPictureFile ( const QString& path )
277
279
{
278
- mSourceFile .setFileName ( path );
279
- refreshPicture ();
280
+ setPicturePath ( path );
280
281
}
281
282
282
283
void QgsComposerPicture::refreshPicture ()
283
284
{
284
- QString source = mSourceFile . fileName () ;
285
+ QString source = mSourcePath ;
285
286
286
287
// data defined source set?
287
288
mHasExpressionError = false ;
@@ -301,25 +302,55 @@ void QgsComposerPicture::refreshPicture()
301
302
}
302
303
}
303
304
304
- QFile path;
305
- path.setFileName ( source );
306
- loadPicture ( path );
305
+ loadPicture ( source );
307
306
}
308
307
309
- void QgsComposerPicture::loadPicture ( const QFile& file )
308
+ void QgsComposerPicture::loadRemotePicture ( const QString &url )
310
309
{
311
- if ( !file.exists () )
310
+ // remote location
311
+
312
+ QgsNetworkContentFetcher fetcher;
313
+ // pause until HTML fetch
314
+ mLoaded = false ;
315
+ fetcher.fetchContent ( QUrl ( url ) );
316
+ connect ( &fetcher, SIGNAL ( finished () ), this , SLOT ( remotePictureLoaded () ) );
317
+
318
+ while ( !mLoaded )
319
+ {
320
+ qApp->processEvents ();
321
+ }
322
+
323
+ QNetworkReply* reply = fetcher.reply ();
324
+ if ( reply )
325
+ {
326
+ QImageReader imageReader ( reply );
327
+ mImage = imageReader.read ();
328
+ mMode = RASTER;
329
+ reply->deleteLater ();
330
+ }
331
+ else
332
+ {
333
+ mMode = Unknown;
334
+ }
335
+ }
336
+
337
+ void QgsComposerPicture::loadLocalPicture ( const QString &path )
338
+ {
339
+ QFile pic;
340
+ pic.setFileName ( path );
341
+
342
+ if ( !pic.exists () )
312
343
{
313
344
mMode = Unknown;
314
345
}
315
346
else
316
347
{
317
- QFileInfo sourceFileInfo ( file );
348
+ QFileInfo sourceFileInfo ( pic );
318
349
QString sourceFileSuffix = sourceFileInfo.suffix ();
319
350
if ( sourceFileSuffix.compare ( " svg" , Qt::CaseInsensitive ) == 0 )
320
351
{
321
352
// try to open svg
322
- mSVG .load ( file .fileName () );
353
+ mSVG .load ( pic .fileName () );
323
354
if ( mSVG .isValid () )
324
355
{
325
356
mMode = SVG;
@@ -335,7 +366,7 @@ void QgsComposerPicture::loadPicture( const QFile& file )
335
366
else
336
367
{
337
368
// try to open raster with QImageReader
338
- QImageReader imageReader ( file .fileName () );
369
+ QImageReader imageReader ( pic .fileName () );
339
370
if ( imageReader.read ( &mImage ) )
340
371
{
341
372
mMode = RASTER;
@@ -347,11 +378,30 @@ void QgsComposerPicture::loadPicture( const QFile& file )
347
378
}
348
379
}
349
380
381
+ }
382
+
383
+ void QgsComposerPicture::remotePictureLoaded ()
384
+ {
385
+ mLoaded = true ;
386
+ }
387
+
388
+ void QgsComposerPicture::loadPicture ( const QString &path )
389
+ {
390
+ if ( path.startsWith ( " http" ) )
391
+ {
392
+ // remote location
393
+ loadRemotePicture ( path );
394
+ }
395
+ else
396
+ {
397
+ // local location
398
+ loadLocalPicture ( path );
399
+ }
350
400
if ( mMode != Unknown ) // make sure we start with a new QImage
351
401
{
352
402
recalculateSize ();
353
403
}
354
- else if ( mHasExpressionError || !( file. fileName () .isEmpty () ) )
404
+ else if ( mHasExpressionError || !( path .isEmpty () ) )
355
405
{
356
406
// trying to load an invalid file or bad expression, show cross picture
357
407
mMode = SVG;
@@ -589,7 +639,18 @@ void QgsComposerPicture::setPictureExpression( QString expression )
589
639
590
640
QString QgsComposerPicture::pictureFile () const
591
641
{
592
- return mSourceFile .fileName ();
642
+ return picturePath ();
643
+ }
644
+
645
+ void QgsComposerPicture::setPicturePath ( const QString &path )
646
+ {
647
+ mSourcePath = path;
648
+ refreshPicture ();
649
+ }
650
+
651
+ QString QgsComposerPicture::picturePath () const
652
+ {
653
+ return mSourcePath ;
593
654
}
594
655
595
656
bool QgsComposerPicture::writeXML ( QDomElement& elem, QDomDocument & doc ) const
@@ -599,7 +660,7 @@ bool QgsComposerPicture::writeXML( QDomElement& elem, QDomDocument & doc ) const
599
660
return false ;
600
661
}
601
662
QDomElement composerPictureElem = doc.createElement ( " ComposerPicture" );
602
- composerPictureElem.setAttribute ( " file" , QgsProject::instance ()->writePath ( mSourceFile . fileName () ) );
663
+ composerPictureElem.setAttribute ( " file" , QgsProject::instance ()->writePath ( mSourcePath ) );
603
664
composerPictureElem.setAttribute ( " pictureWidth" , QString::number ( mPictureWidth ) );
604
665
composerPictureElem.setAttribute ( " pictureHeight" , QString::number ( mPictureHeight ) );
605
666
composerPictureElem.setAttribute ( " resizeMode" , QString::number (( int )mResizeMode ) );
@@ -668,8 +729,7 @@ bool QgsComposerPicture::readXML( const QDomElement& itemElem, const QDomDocumen
668
729
setDataDefinedProperty ( QgsComposerObject::PictureSource, expressionActive, true , sourceExpression, QString () );
669
730
}
670
731
671
- QString fileName = QgsProject::instance ()->readPath ( itemElem.attribute ( " file" ) );
672
- mSourceFile .setFileName ( fileName );
732
+ mSourcePath = QgsProject::instance ()->readPath ( itemElem.attribute ( " file" ) );
673
733
674
734
// picture rotation
675
735
if ( itemElem.attribute ( " pictureRotation" , " 0" ).toDouble () != 0 )
0 commit comments