QGIS API Documentation  2.0.1-Dufour
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgscomposerhtml.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscomposerhtml.cpp
3  ------------------------------------------------------------
4  begin : July 2012
5  copyright : (C) 2012 by Marco Hugentobler
6  email : marco dot hugentobler at sourcepole dot ch
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgscomposerhtml.h"
17 #include "qgscomposerframe.h"
18 #include "qgscomposition.h"
20 #include <QCoreApplication>
21 #include <QPainter>
22 #include <QWebFrame>
23 #include <QWebPage>
24 
25 QgsComposerHtml::QgsComposerHtml( QgsComposition* c, bool createUndoCommands ): QgsComposerMultiFrame( c, createUndoCommands ),
26  mWebPage( 0 ), mLoaded( false ), mHtmlUnitsToMM( 1.0 )
27 {
29  mWebPage = new QWebPage();
30  QObject::connect( mWebPage, SIGNAL( loadFinished( bool ) ), this, SLOT( frameLoaded( bool ) ) );
31  if ( mComposition )
32  {
33  QObject::connect( mComposition, SIGNAL( itemRemoved( QgsComposerItem* ) ), this, SLOT( handleFrameRemoval( QgsComposerItem* ) ) );
34  }
35 }
36 
37 QgsComposerHtml::QgsComposerHtml(): QgsComposerMultiFrame( 0, false ), mWebPage( 0 ), mLoaded( false ), mHtmlUnitsToMM( 1.0 )
38 {
39 }
40 
42 {
43  delete mWebPage;
44 }
45 
46 void QgsComposerHtml::setUrl( const QUrl& url )
47 {
48  if ( !mWebPage )
49  {
50  return;
51  }
52  mLoaded = false;
53 
54  mUrl = url;
55  mWebPage->mainFrame()->load( mUrl );
56  while ( !mLoaded )
57  {
58  qApp->processEvents();
59  }
60 
61  if ( frameCount() < 1 ) return;
62 
63  QSize contentsSize = mWebPage->mainFrame()->contentsSize();
64  contentsSize.setWidth( mFrameItems.at( 0 )->boundingRect().width() * mHtmlUnitsToMM );
65  mWebPage->setViewportSize( contentsSize );
66  mWebPage->mainFrame()->setScrollBarPolicy( Qt::Horizontal, Qt::ScrollBarAlwaysOff );
67  mWebPage->mainFrame()->setScrollBarPolicy( Qt::Vertical, Qt::ScrollBarAlwaysOff );
68  mSize.setWidth( contentsSize.width() / mHtmlUnitsToMM );
69  mSize.setHeight( contentsSize.height() / mHtmlUnitsToMM );
71  emit changed();
72 }
73 
75 {
76  Q_UNUSED( ok );
77  mLoaded = true;
78 }
79 
81 {
82  return mSize;
83 }
84 
85 void QgsComposerHtml::render( QPainter* p, const QRectF& renderExtent )
86 {
87  if ( !mWebPage )
88  {
89  return;
90  }
91 
92  p->save();
93  p->scale( 1.0 / mHtmlUnitsToMM, 1.0 / mHtmlUnitsToMM );
94  p->translate( 0.0, -renderExtent.top() * mHtmlUnitsToMM );
95  mWebPage->mainFrame()->render( p, QRegion( renderExtent.left(), renderExtent.top() * mHtmlUnitsToMM, renderExtent.width() * mHtmlUnitsToMM, renderExtent.height() * mHtmlUnitsToMM ) );
96  p->restore();
97 }
98 
100 {
101  if ( !mComposition )
102  {
103  return 1.0;
104  }
105 
106  return ( mComposition->printResolution() / 72.0 ); //webkit seems to assume a standard dpi of 96
107 }
108 
109 void QgsComposerHtml::addFrame( QgsComposerFrame* frame, bool recalcFrameSizes )
110 {
111  mFrameItems.push_back( frame );
112  QObject::connect( frame, SIGNAL( sizeChanged() ), this, SLOT( recalculateFrameSizes() ) );
113  if ( mComposition )
114  {
115  mComposition->addComposerHtmlFrame( this, frame );
116  }
117 
118  if ( recalcFrameSizes )
119  {
121  }
122 }
123 
124 bool QgsComposerHtml::writeXML( QDomElement& elem, QDomDocument & doc, bool ignoreFrames ) const
125 {
126  QDomElement htmlElem = doc.createElement( "ComposerHtml" );
127  htmlElem.setAttribute( "url", mUrl.toString() );
128  bool state = _writeXML( htmlElem, doc, ignoreFrames );
129  elem.appendChild( htmlElem );
130  return state;
131 }
132 
133 bool QgsComposerHtml::readXML( const QDomElement& itemElem, const QDomDocument& doc, bool ignoreFrames )
134 {
135  deleteFrames();
136  QString urlString = itemElem.attribute( "url" );
137  if ( !urlString.isEmpty() )
138  {
139  setUrl( QUrl( urlString ) );
140  }
141  return _readXML( itemElem, doc, ignoreFrames );
142 }