Skip to content
Permalink
Browse files

Scaling for html multi frame

  • Loading branch information
mhugent committed Jul 25, 2012
1 parent b3b9674 commit c01edd4e252059a95c4723df8ddb19ecd8acfbbb
@@ -136,6 +136,7 @@ QgsComposer::QgsComposer( QgisApp *qgis, const QString& title )
toggleActionGroup->addAction( mActionAddEllipse );
toggleActionGroup->addAction( mActionAddArrow );
toggleActionGroup->addAction( mActionAddTable );
toggleActionGroup->addAction( mActionAddHtml );
toggleActionGroup->setExclusive( true );


@@ -821,6 +822,14 @@ void QgsComposer::on_mActionAddTable_triggered()
}
}

void QgsComposer::on_mActionAddHtml_triggered()
{
if ( mView )
{
mView->setCurrentTool( QgsComposerView::AddHtml );
}
}

void QgsComposer::on_mActionAddArrow_triggered()
{
if ( mView )
@@ -167,6 +167,8 @@ class QgsComposer: public QMainWindow, private Ui::QgsComposerBase
//! Add attribute table
void on_mActionAddTable_triggered();

void on_mActionAddHtml_triggered();

//! Save composer as template
void on_mActionSaveAsTemplate_triggered();

@@ -16,7 +16,8 @@
#include "qgscomposerframe.h"
#include "qgscomposermultiframe.h"

QgsComposerFrame::QgsComposerFrame( QgsComposition* c, QgsComposerMultiFrame* mf ): QgsComposerItem( c ), mMultiFrame( mf )
QgsComposerFrame::QgsComposerFrame( QgsComposition* c, QgsComposerMultiFrame* mf, double x, double y, double width, double height ):
QgsComposerItem( x, y, width, height, c ), mMultiFrame( mf )
{
}

@@ -36,8 +37,20 @@ bool QgsComposerFrame::readXML( const QDomElement& itemElem, const QDomDocument&

void QgsComposerFrame::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
{
if ( mMultiFrame && painter )
if ( !painter )
{
return;
}

drawBackground( painter );
if ( mMultiFrame )
{
mMultiFrame->render( painter, mSection );
}

drawFrame( painter );
if ( isSelected() )
{
drawSelectionBoxes( painter );
}
}
@@ -25,7 +25,7 @@ class QgsComposerMultiFrame;
class QgsComposerFrame: public QgsComposerItem
{
public:
QgsComposerFrame( QgsComposition* c, QgsComposerMultiFrame* mf );
QgsComposerFrame( QgsComposition* c, QgsComposerMultiFrame* mf, qreal x, qreal y, qreal width, qreal height );
~QgsComposerFrame();

/**Sets the part of this frame (relative to the total multiframe extent in mm)*/
@@ -14,20 +14,27 @@
***************************************************************************/

#include "qgscomposerhtml.h"
#include "qgscomposerframe.h"
#include "qgscomposition.h"
#include <QCoreApplication>
#include <QImage>
#include <QPainter>
#include <QWebFrame>
#include <QWebPage>

QgsComposerHtml::QgsComposerHtml( QgsComposition* c ): QgsComposerMultiFrame( c ), mWebPage( 0 ), mLoaded( false )
QgsComposerHtml::QgsComposerHtml( QgsComposition* c, qreal x, qreal y, qreal width, qreal height ): QgsComposerMultiFrame( c ), mWebPage( 0 ), mLoaded( false ), mHtmlUnitsToMM( 1.0 )
{
mHtmlUnitsToMM = htmlUnitsToMM();
mWebPage = new QWebPage();
QObject::connect( mWebPage, SIGNAL( loadFinished( bool ) ), this, SLOT( frameLoaded( bool ) ) );
setUrl( QUrl( "http://www.qgis.org" ) );//test
QgsComposerFrame* frame = new QgsComposerFrame( c, this, x, y, width, height );
addFrame( frame );
mComposition->addItem( frame );
recalculateFrameSizes();
}

QgsComposerHtml::QgsComposerHtml(): QgsComposerMultiFrame( 0 ), mWebPage( 0 ), mLoaded( false )
QgsComposerHtml::QgsComposerHtml(): QgsComposerMultiFrame( 0 ), mWebPage( 0 ), mLoaded( false ), mHtmlUnitsToMM( 1.0 )
{
}

@@ -49,7 +56,12 @@ void QgsComposerHtml::setUrl( const QUrl& url )
{
qApp->processEvents();
}
mWebPage->setViewportSize( mWebPage->mainFrame()->contentsSize() );
QSize contentsSize = mWebPage->mainFrame()->contentsSize();
mWebPage->setViewportSize( contentsSize );

double pixelPerMM = mComposition->printResolution() / 25.4;
mSize.setWidth( contentsSize.width() / pixelPerMM );
mSize.setHeight( contentsSize.height() / pixelPerMM );
}

void QgsComposerHtml::frameLoaded( bool ok )
@@ -59,7 +71,7 @@ void QgsComposerHtml::frameLoaded( bool ok )

QSizeF QgsComposerHtml::totalSize() const
{
return QSizeF(); //soon...
return mSize;
}

void QgsComposerHtml::render( QPainter* p, const QRectF& renderExtent )
@@ -69,12 +81,21 @@ void QgsComposerHtml::render( QPainter* p, const QRectF& renderExtent )
return;
}

QImage img; //dummy image to find out assumed screen dpi

double pixelPerMM = mComposition->printResolution() / 25.4;
double painterScale = 1.0 / ( pixelPerMM / (( double )img.dotsPerMeterX() / 1000.0 ) );
p->save();
p->scale( painterScale, painterScale );
mWebPage->mainFrame()->render( p, QRegion( renderExtent.left(), renderExtent.top(), renderExtent.width(), renderExtent.height() ) );
p->scale( 1.0 / mHtmlUnitsToMM, 1.0 / mHtmlUnitsToMM );
mWebPage->mainFrame()->render( p, QRegion( renderExtent.left(), renderExtent.top(), renderExtent.width() * mHtmlUnitsToMM, renderExtent.height() * mHtmlUnitsToMM ) );
p->restore();
}

double QgsComposerHtml::htmlUnitsToMM()
{
if ( !mComposition )
{
return 1.0;
}

QImage img( 1, 1, QImage::Format_ARGB32_Premultiplied );
double debug = img.dotsPerMeterX();
double pixelPerMM = mComposition->printResolution() / 25.4;
return ( pixelPerMM / ( img.dotsPerMeterX() / 1000.0 ) );
}
@@ -25,7 +25,7 @@ class QgsComposerHtml: public QgsComposerMultiFrame
{
Q_OBJECT
public:
QgsComposerHtml( QgsComposition* c );
QgsComposerHtml( QgsComposition* c, qreal x, qreal y, qreal width, qreal height );
QgsComposerHtml();
~QgsComposerHtml();

@@ -42,6 +42,10 @@ class QgsComposerHtml: public QgsComposerMultiFrame
QUrl mUrl;
QWebPage* mWebPage;
bool mLoaded;
QSizeF mSize; //total size in mm
double mHtmlUnitsToMM;

double htmlUnitsToMM(); //calculate scale factor
};

#endif // QGSCOMPOSERHTML_H
@@ -14,6 +14,7 @@
***************************************************************************/

#include "qgscomposermultiframe.h"
#include "qgscomposerframe.h"

QgsComposerMultiFrame::QgsComposerMultiFrame( QgsComposition* c ): mComposition( c )
{
@@ -29,5 +30,10 @@ QgsComposerMultiFrame::~QgsComposerMultiFrame()

void QgsComposerMultiFrame::recalculateFrameSizes()
{
//todo...
if ( mFrameItems.size() > 0 )
{
QSizeF size = totalSize();
QgsComposerFrame* item = mFrameItems[0];
item->setContentSection( QRectF( 0, 0, item->rect().width(), item->rect().height() ) );
}
}
@@ -19,7 +19,7 @@
#include <QObject>
#include <QSizeF>

class QgsComposerItem;
class QgsComposerFrame;
class QgsComposition;
class QRectF;
class QPainter;
@@ -41,9 +41,11 @@ class QgsComposerMultiFrame: public QObject
virtual QSizeF totalSize() const = 0;
virtual void render( QPainter* p, const QRectF& renderExtent ) = 0;

void addFrame( QgsComposerFrame* frame ) { mFrameItems.push_back( frame ); }

protected:
QgsComposition* mComposition;
QList<QgsComposerItem*> mFrameItems;
QList<QgsComposerFrame*> mFrameItems;

void recalculateFrameSizes();

@@ -24,6 +24,7 @@

#include "qgscomposerview.h"
#include "qgscomposerarrow.h"
#include "qgscomposerhtml.h"
#include "qgscomposerlabel.h"
#include "qgscomposerlegend.h"
#include "qgscomposermap.h"
@@ -126,6 +127,7 @@ void QgsComposerView::mousePressEvent( QMouseEvent* e )
case AddRectangle:
case AddTriangle:
case AddEllipse:
case AddHtml:
{
QTransform t;
mRubberBandItem = new QGraphicsRectItem( 0, 0, 0, 0 );
@@ -317,6 +319,17 @@ void QgsComposerView::mouseReleaseEvent( QMouseEvent* e )
}
break;

case AddHtml:
if ( composition() )
{
QgsComposerHtml* composerHtml = new QgsComposerHtml( composition(), mRubberBandItem->transform().dx(), mRubberBandItem->transform().dy(),
mRubberBandItem->rect().width(), mRubberBandItem->rect().height() );
scene()->removeItem( mRubberBandItem );
delete mRubberBandItem;
mRubberBandItem = 0;
emit actionFinished();
//composition()->pushAddRemoveCommand...
}
default:
break;
}
@@ -359,6 +372,7 @@ void QgsComposerView::mouseMoveEvent( QMouseEvent* e )
case AddRectangle:
case AddTriangle:
case AddEllipse:
case AddHtml:
//adjust rubber band item
{
double x = 0;
@@ -54,6 +54,7 @@ class GUI_EXPORT QgsComposerView: public QGraphicsView
{
Select = 0, // Select/Move item
AddArrow, //add arrow
AddHtml,
AddMap, // add new map
AddLegend, // add vector legend
AddLabel, // add label
@@ -80,6 +80,7 @@
<addaction name="mActionAddNewScalebar"/>
<addaction name="mActionAddArrow"/>
<addaction name="mActionAddTable"/>
<addaction name="mActionAddHtml"/>
<addaction name="separator"/>
<addaction name="mActionSelectMoveItem"/>
<addaction name="mActionMoveItemContent"/>
@@ -443,6 +444,17 @@
<string>Add Ellipse</string>
</property>
</action>
<action name="mActionAddHtml">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>Add html</string>
</property>
<property name="toolTip">
<string>Add html frame</string>
</property>
</action>
</widget>
<resources>
<include location="../../images/images.qrc"/>

0 comments on commit c01edd4

Please sign in to comment.
You can’t perform that action at this time.