QGIS API Documentation  master-3f58142
src/core/composer/qgscomposerhtml.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                               qgscomposerhtml.cpp
00003     ------------------------------------------------------------
00004     begin                : July 2012
00005     copyright            : (C) 2012 by Marco Hugentobler
00006     email                : marco dot hugentobler at sourcepole dot ch
00007  ***************************************************************************
00008  *                                                                         *
00009  *   This program is free software; you can redistribute it and/or modify  *
00010  *   it under the terms of the GNU General Public License as published by  *
00011  *   the Free Software Foundation; either version 2 of the License, or     *
00012  *   (at your option) any later version.                                   *
00013  *                                                                         *
00014  ***************************************************************************/
00015 
00016 #include "qgscomposerhtml.h"
00017 #include "qgscomposerframe.h"
00018 #include "qgscomposition.h"
00019 #include "qgsaddremovemultiframecommand.h"
00020 #include <QCoreApplication>
00021 #include <QPainter>
00022 #include <QWebFrame>
00023 #include <QWebPage>
00024 
00025 QgsComposerHtml::QgsComposerHtml( QgsComposition* c, bool createUndoCommands ): QgsComposerMultiFrame( c, createUndoCommands ),
00026     mWebPage( 0 ), mLoaded( false ), mHtmlUnitsToMM( 1.0 )
00027 {
00028   mHtmlUnitsToMM = htmlUnitsToMM();
00029   mWebPage = new QWebPage();
00030   QObject::connect( mWebPage, SIGNAL( loadFinished( bool ) ), this, SLOT( frameLoaded( bool ) ) );
00031   if ( mComposition )
00032   {
00033     QObject::connect( mComposition, SIGNAL( itemRemoved( QgsComposerItem* ) ), this, SLOT( handleFrameRemoval( QgsComposerItem* ) ) );
00034   }
00035 }
00036 
00037 QgsComposerHtml::QgsComposerHtml(): QgsComposerMultiFrame( 0, false ), mWebPage( 0 ), mLoaded( false ), mHtmlUnitsToMM( 1.0 )
00038 {
00039 }
00040 
00041 QgsComposerHtml::~QgsComposerHtml()
00042 {
00043   delete mWebPage;
00044 }
00045 
00046 void QgsComposerHtml::setUrl( const QUrl& url )
00047 {
00048   if ( !mWebPage )
00049   {
00050     return;
00051   }
00052   mLoaded = false;
00053 
00054   mUrl = url;
00055   mWebPage->mainFrame()->load( mUrl );
00056   while ( !mLoaded )
00057   {
00058     qApp->processEvents();
00059   }
00060 
00061   if ( frameCount() < 1 )  return;
00062 
00063   QSize contentsSize = mWebPage->mainFrame()->contentsSize();
00064   contentsSize.setWidth( mFrameItems.at( 0 )->boundingRect().width() * mHtmlUnitsToMM );
00065   mWebPage->setViewportSize( contentsSize );
00066   mWebPage->mainFrame()->setScrollBarPolicy( Qt::Horizontal, Qt::ScrollBarAlwaysOff );
00067   mWebPage->mainFrame()->setScrollBarPolicy( Qt::Vertical, Qt::ScrollBarAlwaysOff );
00068   mSize.setWidth( contentsSize.width() / mHtmlUnitsToMM );
00069   mSize.setHeight( contentsSize.height() / mHtmlUnitsToMM );
00070   recalculateFrameSizes();
00071   emit changed();
00072 }
00073 
00074 void QgsComposerHtml::frameLoaded( bool ok )
00075 {
00076   Q_UNUSED( ok );
00077   mLoaded = true;
00078 }
00079 
00080 QSizeF QgsComposerHtml::totalSize() const
00081 {
00082   return mSize;
00083 }
00084 
00085 void QgsComposerHtml::render( QPainter* p, const QRectF& renderExtent )
00086 {
00087   if ( !mWebPage )
00088   {
00089     return;
00090   }
00091 
00092   p->save();
00093   p->scale( 1.0 / mHtmlUnitsToMM, 1.0 / mHtmlUnitsToMM );
00094   p->translate( 0.0, -renderExtent.top() * mHtmlUnitsToMM );
00095   mWebPage->mainFrame()->render( p, QRegion( renderExtent.left(), renderExtent.top() * mHtmlUnitsToMM, renderExtent.width() * mHtmlUnitsToMM, renderExtent.height() * mHtmlUnitsToMM ) );
00096   p->restore();
00097 }
00098 
00099 double QgsComposerHtml::htmlUnitsToMM()
00100 {
00101   if ( !mComposition )
00102   {
00103     return 1.0;
00104   }
00105 
00106   return ( mComposition->printResolution() / 72.0 ); //webkit seems to assume a standard dpi of 96
00107 }
00108 
00109 void QgsComposerHtml::addFrame( QgsComposerFrame* frame, bool recalcFrameSizes )
00110 {
00111   mFrameItems.push_back( frame );
00112   QObject::connect( frame, SIGNAL( sizeChanged() ), this, SLOT( recalculateFrameSizes() ) );
00113   if ( mComposition )
00114   {
00115     mComposition->addComposerHtmlFrame( this, frame );
00116   }
00117 
00118   if ( recalcFrameSizes )
00119   {
00120     recalculateFrameSizes();
00121   }
00122 }
00123 
00124 bool QgsComposerHtml::writeXML( QDomElement& elem, QDomDocument & doc, bool ignoreFrames ) const
00125 {
00126   QDomElement htmlElem = doc.createElement( "ComposerHtml" );
00127   htmlElem.setAttribute( "url", mUrl.toString() );
00128   bool state = _writeXML( htmlElem, doc, ignoreFrames );
00129   elem.appendChild( htmlElem );
00130   return state;
00131 }
00132 
00133 bool QgsComposerHtml::readXML( const QDomElement& itemElem, const QDomDocument& doc, bool ignoreFrames )
00134 {
00135   deleteFrames();
00136   QString urlString = itemElem.attribute( "url" );
00137   if ( !urlString.isEmpty() )
00138   {
00139     setUrl( QUrl( urlString ) );
00140   }
00141   return _readXML( itemElem, doc, ignoreFrames );
00142 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines