Skip to content

Commit f7732d3

Browse files
committedApr 9, 2013
Fix html labels rendering before they are fully loaded. Prevents missing images in html labels when composer layout is output for the first time.
1 parent 1436056 commit f7732d3

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed
 

‎src/core/composer/qgscomposerlabel.cpp

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@
2424
#include <QPainter>
2525
#include <QWebFrame>
2626
#include <QWebPage>
27+
#include <QEventLoop>
2728

2829
QgsComposerLabel::QgsComposerLabel( QgsComposition *composition ):
2930
QgsComposerItem( composition ), mHtmlState( 0 ), mHtmlUnitsToMM( 1.0 ),
30-
mMargin( 1.0 ), mFontColor( QColor( 0, 0, 0 ) ),
31+
mHtmlLoaded( false ), mMargin( 1.0 ), mFontColor( QColor( 0, 0, 0 ) ),
3132
mHAlignment( Qt::AlignLeft ), mVAlignment( Qt::AlignTop ),
3233
mExpressionFeature( 0 ), mExpressionLayer( 0 )
3334
{
@@ -64,6 +65,11 @@ void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem*
6465

6566
QWebPage* webPage = new QWebPage();
6667

68+
//Setup event loop and timeout for rendering html
69+
QEventLoop loop;
70+
QTimer timeoutTimer;
71+
timeoutTimer.setSingleShot( true );
72+
6773
//This makes the background transparent. Found on http://blog.qt.digia.com/blog/2009/06/30/transparent-qwebview-or-qwebpage/
6874
QPalette palette = webPage->palette();
6975
palette.setBrush( QPalette::Base, Qt::transparent );
@@ -74,7 +80,30 @@ void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem*
7480
webPage->mainFrame()->setZoomFactor( 10.0 );
7581
webPage->mainFrame()->setScrollBarPolicy( Qt::Horizontal, Qt::ScrollBarAlwaysOff );
7682
webPage->mainFrame()->setScrollBarPolicy( Qt::Vertical, Qt::ScrollBarAlwaysOff );
83+
84+
//Connect timeout and webpage loadFinished signals to loop
85+
connect( &timeoutTimer, SIGNAL( timeout() ), &loop, SLOT( quit() ) );
86+
connect( webPage, SIGNAL( loadFinished( bool ) ), &loop, SLOT( quit() ) );
87+
88+
//mHtmlLoaded tracks whether the QWebPage has completed loading
89+
//its html contents, set it initially to false. The loadingHtmlFinished slot will
90+
//set this to true after html is loaded.
91+
mHtmlLoaded = false;
92+
connect( webPage, SIGNAL( loadFinished( bool ) ), SLOT( loadingHtmlFinished( bool ) ) );
93+
7794
webPage->mainFrame()->setHtml( displayText() );
95+
96+
//For very basic html labels with no external assets, the html load will already be
97+
//complete before we even get a chance to start the QEventLoop. Make sure we check
98+
//this before starting the loop
99+
if ( !mHtmlLoaded )
100+
{
101+
// Start a 20 second timeout in case html loading will never complete
102+
timeoutTimer.start( 20000 );
103+
// Pause until html is loaded
104+
loop.exec();
105+
}
106+
78107
webPage->mainFrame()->render( painter );//DELETE WEBPAGE ?
79108
}
80109
else
@@ -90,9 +119,6 @@ void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem*
90119
drawText( painter, painterRect, displayText(), mFont, mHAlignment, mVAlignment );
91120
}
92121

93-
94-
95-
96122
painter->restore();
97123

98124
drawFrame( painter );
@@ -102,6 +128,13 @@ void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem*
102128
}
103129
}
104130

131+
/*Track when QWebPage has finished loading its html contents*/
132+
void QgsComposerLabel::loadingHtmlFinished( bool result )
133+
{
134+
Q_UNUSED (result);
135+
mHtmlLoaded = true;
136+
}
137+
105138
double QgsComposerLabel::htmlUnitsToMM()
106139
{
107140
if ( !mComposition )

‎src/core/composer/qgscomposerlabel.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
104104
public slots:
105105
virtual void setRotation( double r );
106106

107+
private slots:
108+
void loadingHtmlFinished( bool );
109+
107110
private:
108111
// Text
109112
QString mText;
@@ -112,6 +115,7 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
112115
int mHtmlState;
113116
double mHtmlUnitsToMM;
114117
double htmlUnitsToMM(); //calculate scale factor
118+
bool mHtmlLoaded;
115119

116120
/**Helper function to calculate x/y shift for adjustSizeToText() depending on rotation, current size and alignment*/
117121
void itemShiftAdjustSize( double newWidth, double newHeight, double& xShift, double& yShift ) const;
@@ -142,6 +146,8 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
142146
QgsFeature* mExpressionFeature;
143147
QgsVectorLayer* mExpressionLayer;
144148
QMap<QString, QVariant> mSubstitutions;
149+
150+
145151
};
146152

147153
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.