24
24
#include < QPainter>
25
25
#include < QWebFrame>
26
26
#include < QWebPage>
27
+ #include < QEventLoop>
27
28
28
29
QgsComposerLabel::QgsComposerLabel ( QgsComposition *composition ):
29
30
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 ) ),
31
32
mHAlignment( Qt::AlignLeft ), mVAlignment( Qt::AlignTop ),
32
33
mExpressionFeature( 0 ), mExpressionLayer( 0 )
33
34
{
@@ -64,6 +65,11 @@ void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem*
64
65
65
66
QWebPage* webPage = new QWebPage ();
66
67
68
+ // Setup event loop and timeout for rendering html
69
+ QEventLoop loop;
70
+ QTimer timeoutTimer;
71
+ timeoutTimer.setSingleShot ( true );
72
+
67
73
// This makes the background transparent. Found on http://blog.qt.digia.com/blog/2009/06/30/transparent-qwebview-or-qwebpage/
68
74
QPalette palette = webPage->palette ();
69
75
palette.setBrush ( QPalette::Base, Qt::transparent );
@@ -74,7 +80,30 @@ void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem*
74
80
webPage->mainFrame ()->setZoomFactor ( 10.0 );
75
81
webPage->mainFrame ()->setScrollBarPolicy ( Qt::Horizontal, Qt::ScrollBarAlwaysOff );
76
82
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
+
77
94
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
+
78
107
webPage->mainFrame ()->render ( painter );// DELETE WEBPAGE ?
79
108
}
80
109
else
@@ -90,9 +119,6 @@ void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem*
90
119
drawText ( painter, painterRect, displayText (), mFont , mHAlignment , mVAlignment );
91
120
}
92
121
93
-
94
-
95
-
96
122
painter->restore ();
97
123
98
124
drawFrame ( painter );
@@ -102,6 +128,13 @@ void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem*
102
128
}
103
129
}
104
130
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
+
105
138
double QgsComposerLabel::htmlUnitsToMM ()
106
139
{
107
140
if ( !mComposition )
0 commit comments