@@ -79,11 +79,27 @@ QgsComposerLabel::QgsComposerLabel( QgsComposition *composition )
79
79
// to update the expression context
80
80
connect ( &mComposition ->atlasComposition (), SIGNAL ( featureChanged ( QgsFeature* ) ), this , SLOT ( refreshExpressionContext () ) );
81
81
}
82
+
83
+ mWebPage = new QWebPage ( this );
84
+ mWebPage ->setNetworkAccessManager ( QgsNetworkAccessManager::instance () );
85
+
86
+ // This makes the background transparent. Found on http://blog.qt.digia.com/blog/2009/06/30/transparent-qwebview-or-qwebpage/
87
+ QPalette palette = mWebPage ->palette ();
88
+ palette.setBrush ( QPalette::Base, Qt::transparent );
89
+ mWebPage ->setPalette ( palette );
90
+ // webPage->setAttribute(Qt::WA_OpaquePaintEvent, false); //this does not compile, why ?
91
+
92
+ mWebPage ->mainFrame ()->setZoomFactor ( 10.0 );
93
+ mWebPage ->mainFrame ()->setScrollBarPolicy ( Qt::Horizontal, Qt::ScrollBarAlwaysOff );
94
+ mWebPage ->mainFrame ()->setScrollBarPolicy ( Qt::Vertical, Qt::ScrollBarAlwaysOff );
95
+
96
+ connect ( mWebPage , SIGNAL ( loadFinished ( bool ) ), SLOT ( loadingHtmlFinished ( bool ) ) );
82
97
}
83
98
84
99
QgsComposerLabel::~QgsComposerLabel ()
85
100
{
86
101
delete mDistanceArea ;
102
+ delete mWebPage ;
87
103
}
88
104
89
105
void QgsComposerLabel::paint ( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
@@ -110,79 +126,66 @@ void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem*
110
126
double yPenAdjust = mMarginY < 0 ? -penWidth : penWidth;
111
127
QRectF painterRect ( xPenAdjust + mMarginX , yPenAdjust + mMarginY , rect ().width () - 2 * xPenAdjust - 2 * mMarginX , rect ().height () - 2 * yPenAdjust - 2 * mMarginY );
112
128
113
- QString textToDraw = displayText ();
114
-
115
129
if ( mHtmlState )
116
130
{
117
131
painter->scale ( 1.0 / mHtmlUnitsToMM / 10.0 , 1.0 / mHtmlUnitsToMM / 10.0 );
118
- QWebPage *webPage = new QWebPage ();
119
- webPage->setNetworkAccessManager ( QgsNetworkAccessManager::instance () );
120
-
121
- // Setup event loop and timeout for rendering html
122
- QEventLoop loop;
123
- QTimer timeoutTimer;
124
- timeoutTimer.setSingleShot ( true );
125
-
126
- // This makes the background transparent. Found on http://blog.qt.digia.com/blog/2009/06/30/transparent-qwebview-or-qwebpage/
127
- QPalette palette = webPage->palette ();
128
- palette.setBrush ( QPalette::Base, Qt::transparent );
129
- webPage->setPalette ( palette );
130
- // webPage->setAttribute(Qt::WA_OpaquePaintEvent, false); //this does not compile, why ?
131
-
132
- webPage->setViewportSize ( QSize ( painterRect.width () * mHtmlUnitsToMM * 10.0 , painterRect.height () * mHtmlUnitsToMM * 10.0 ) );
133
- webPage->mainFrame ()->setZoomFactor ( 10.0 );
134
- webPage->mainFrame ()->setScrollBarPolicy ( Qt::Horizontal, Qt::ScrollBarAlwaysOff );
135
- webPage->mainFrame ()->setScrollBarPolicy ( Qt::Vertical, Qt::ScrollBarAlwaysOff );
136
- webPage->settings ()->setUserStyleSheetUrl ( createStylesheetUrl () );
137
-
138
- // QGIS segfaults when rendering web page while in composer if html
139
- // contains images. So if we are not printing the composition, then
140
- // disable image loading
141
- if ( mComposition ->plotStyle () != QgsComposition::Print &&
142
- mComposition ->plotStyle () != QgsComposition::Postscript )
143
- {
144
- webPage->settings ()->setAttribute ( QWebSettings::AutoLoadImages, false );
145
- }
132
+ mWebPage ->setViewportSize ( QSize ( painterRect.width () * mHtmlUnitsToMM * 10.0 , painterRect.height () * mHtmlUnitsToMM * 10.0 ) );
133
+ mWebPage ->settings ()->setUserStyleSheetUrl ( createStylesheetUrl () );
134
+ mWebPage ->mainFrame ()->render ( painter );
135
+ }
136
+ else
137
+ {
138
+ const QString textToDraw = displayText ();
139
+ painter->setFont ( mFont );
140
+ // debug
141
+ // painter->setPen( QColor( Qt::red ) );
142
+ // painter->drawRect( painterRect );
143
+ QgsComposerUtils::drawText ( painter, painterRect, textToDraw, mFont , mFontColor , mHAlignment , mVAlignment , Qt::TextWordWrap );
144
+ }
145
+
146
+ painter->restore ();
147
+
148
+ drawFrame ( painter );
149
+ if ( isSelected () )
150
+ {
151
+ drawSelectionBoxes ( painter );
152
+ }
153
+ }
146
154
147
- // Connect timeout and webpage loadFinished signals to loop
148
- connect ( &timeoutTimer, SIGNAL ( timeout () ), &loop, SLOT ( quit () ) );
149
- connect ( webPage, SIGNAL ( loadFinished ( bool ) ), &loop, SLOT ( quit () ) );
155
+ void QgsComposerLabel::contentChanged ()
156
+ {
157
+ if ( mHtmlState )
158
+ {
159
+ const QString textToDraw = displayText ();
150
160
151
161
// mHtmlLoaded tracks whether the QWebPage has completed loading
152
162
// its html contents, set it initially to false. The loadingHtmlFinished slot will
153
163
// set this to true after html is loaded.
154
164
mHtmlLoaded = false ;
155
- connect ( webPage, SIGNAL ( loadFinished ( bool ) ), SLOT ( loadingHtmlFinished ( bool ) ) );
156
165
157
- webPage->mainFrame ()->setHtml ( textToDraw );
166
+ const QUrl baseUrl = QUrl::fromLocalFile ( QgsProject::instance ()->fileInfo ().absoluteFilePath () );
167
+ mWebPage ->mainFrame ()->setHtml ( textToDraw, baseUrl );
158
168
159
169
// For very basic html labels with no external assets, the html load will already be
160
170
// complete before we even get a chance to start the QEventLoop. Make sure we check
161
171
// this before starting the loop
162
172
if ( !mHtmlLoaded )
163
173
{
174
+ // Setup event loop and timeout for rendering html
175
+ QEventLoop loop;
176
+
177
+ // Connect timeout and webpage loadFinished signals to loop
178
+ connect ( mWebPage , SIGNAL ( loadFinished ( bool ) ), &loop, SLOT ( quit () ) );
179
+
164
180
// Start a 20 second timeout in case html loading will never complete
181
+ QTimer timeoutTimer;
182
+ timeoutTimer.setSingleShot ( true );
183
+ connect ( &timeoutTimer, SIGNAL ( timeout () ), &loop, SLOT ( quit () ) );
165
184
timeoutTimer.start ( 20000 );
185
+
166
186
// Pause until html is loaded
167
187
loop.exec ();
168
188
}
169
- webPage->mainFrame ()->render ( painter );// DELETE WEBPAGE ?
170
- }
171
- else
172
- {
173
- painter->setFont ( mFont );
174
- // debug
175
- // painter->setPen( QColor( Qt::red ) );
176
- // painter->drawRect( painterRect );
177
- QgsComposerUtils::drawText ( painter, painterRect, textToDraw, mFont , mFontColor , mHAlignment , mVAlignment , Qt::TextWordWrap );
178
- }
179
-
180
- painter->restore ();
181
-
182
- drawFrame ( painter );
183
- if ( isSelected () )
184
- {
185
- drawSelectionBoxes ( painter );
186
189
}
187
190
}
188
191
@@ -209,6 +212,8 @@ void QgsComposerLabel::setText( const QString& text )
209
212
mText = text;
210
213
emit itemChanged ();
211
214
215
+ contentChanged ();
216
+
212
217
if ( mComposition && id ().isEmpty () && !mHtmlState )
213
218
{
214
219
// notify the model that the display name has changed
@@ -224,6 +229,7 @@ void QgsComposerLabel::setHtmlState( int state )
224
229
}
225
230
226
231
mHtmlState = state;
232
+ contentChanged ();
227
233
228
234
if ( mComposition && id ().isEmpty () )
229
235
{
@@ -253,6 +259,7 @@ void QgsComposerLabel::setExpressionContext( QgsFeature *feature, QgsVectorLayer
253
259
mDistanceArea ->setEllipsoidalMode ( mComposition ->mapSettings ().hasCrsTransformEnabled () );
254
260
}
255
261
mDistanceArea ->setEllipsoid ( QgsProject::instance ()->readEntry ( " Measure" , " /Ellipsoid" , GEO_NONE ) );
262
+ contentChanged ();
256
263
257
264
// Force label to redraw -- fixes label printing for labels with blend modes when used with atlas
258
265
update ();
@@ -289,6 +296,7 @@ void QgsComposerLabel::refreshExpressionContext()
289
296
}
290
297
mDistanceArea ->setEllipsoidalMode ( mComposition ->mapSettings ().hasCrsTransformEnabled () );
291
298
mDistanceArea ->setEllipsoid ( QgsProject::instance ()->readEntry ( " Measure" , " /Ellipsoid" , GEO_NONE ) );
299
+ contentChanged ();
292
300
293
301
update ();
294
302
}
@@ -488,6 +496,7 @@ bool QgsComposerLabel::readXML( const QDomElement& itemElem, const QDomDocument&
488
496
_readXML ( composerItemElem, doc );
489
497
}
490
498
emit itemChanged ();
499
+ contentChanged ();
491
500
return true ;
492
501
}
493
502
0 commit comments