14
14
***************************************************************************/
15
15
16
16
#include " qgscomposerhtml.h"
17
+ #include " qgscomposerframe.h"
17
18
#include " qgscomposition.h"
18
19
#include < QCoreApplication>
19
20
#include < QImage>
20
21
#include < QPainter>
21
22
#include < QWebFrame>
22
23
#include < QWebPage>
23
24
24
- QgsComposerHtml::QgsComposerHtml ( QgsComposition* c ): QgsComposerMultiFrame( c ), mWebPage( 0 ), mLoaded( false )
25
+ QgsComposerHtml::QgsComposerHtml ( QgsComposition* c, qreal x, qreal y, qreal width, qreal height ): QgsComposerMultiFrame( c ), mWebPage( 0 ), mLoaded( false ), mHtmlUnitsToMM( 1.0 )
25
26
{
27
+ mHtmlUnitsToMM = htmlUnitsToMM ();
26
28
mWebPage = new QWebPage ();
27
29
QObject::connect ( mWebPage , SIGNAL ( loadFinished ( bool ) ), this , SLOT ( frameLoaded ( bool ) ) );
30
+ setUrl ( QUrl ( " http://www.qgis.org" ) );// test
31
+ QgsComposerFrame* frame = new QgsComposerFrame ( c, this , x, y, width, height );
32
+ addFrame ( frame );
33
+ mComposition ->addItem ( frame );
34
+ recalculateFrameSizes ();
28
35
}
29
36
30
- QgsComposerHtml::QgsComposerHtml (): QgsComposerMultiFrame( 0 ), mWebPage( 0 ), mLoaded( false )
37
+ QgsComposerHtml::QgsComposerHtml (): QgsComposerMultiFrame( 0 ), mWebPage( 0 ), mLoaded( false ), mHtmlUnitsToMM( 1.0 )
31
38
{
32
39
}
33
40
@@ -49,7 +56,12 @@ void QgsComposerHtml::setUrl( const QUrl& url )
49
56
{
50
57
qApp->processEvents ();
51
58
}
52
- mWebPage ->setViewportSize ( mWebPage ->mainFrame ()->contentsSize () );
59
+ QSize contentsSize = mWebPage ->mainFrame ()->contentsSize ();
60
+ mWebPage ->setViewportSize ( contentsSize );
61
+
62
+ double pixelPerMM = mComposition ->printResolution () / 25.4 ;
63
+ mSize .setWidth ( contentsSize.width () / pixelPerMM );
64
+ mSize .setHeight ( contentsSize.height () / pixelPerMM );
53
65
}
54
66
55
67
void QgsComposerHtml::frameLoaded ( bool ok )
@@ -59,7 +71,7 @@ void QgsComposerHtml::frameLoaded( bool ok )
59
71
60
72
QSizeF QgsComposerHtml::totalSize () const
61
73
{
62
- return QSizeF (); // soon...
74
+ return mSize ;
63
75
}
64
76
65
77
void QgsComposerHtml::render ( QPainter* p, const QRectF& renderExtent )
@@ -69,12 +81,21 @@ void QgsComposerHtml::render( QPainter* p, const QRectF& renderExtent )
69
81
return ;
70
82
}
71
83
72
- QImage img; // dummy image to find out assumed screen dpi
73
-
74
- double pixelPerMM = mComposition ->printResolution () / 25.4 ;
75
- double painterScale = 1.0 / ( pixelPerMM / (( double )img.dotsPerMeterX () / 1000.0 ) );
76
84
p->save ();
77
- p->scale ( painterScale, painterScale );
78
- mWebPage ->mainFrame ()->render ( p, QRegion ( renderExtent.left (), renderExtent.top (), renderExtent.width (), renderExtent.height () ) );
85
+ p->scale ( 1.0 / mHtmlUnitsToMM , 1.0 / mHtmlUnitsToMM );
86
+ mWebPage ->mainFrame ()->render ( p, QRegion ( renderExtent.left (), renderExtent.top (), renderExtent.width () * mHtmlUnitsToMM , renderExtent.height () * mHtmlUnitsToMM ) );
79
87
p->restore ();
80
88
}
89
+
90
+ double QgsComposerHtml::htmlUnitsToMM ()
91
+ {
92
+ if ( !mComposition )
93
+ {
94
+ return 1.0 ;
95
+ }
96
+
97
+ QImage img ( 1 , 1 , QImage::Format_ARGB32_Premultiplied );
98
+ double debug = img.dotsPerMeterX ();
99
+ double pixelPerMM = mComposition ->printResolution () / 25.4 ;
100
+ return ( pixelPerMM / ( img.dotsPerMeterX () / 1000.0 ) );
101
+ }
0 commit comments