Skip to content
Permalink
Browse files

Move adding the first html frame out of QgsComposerHtml

  • Loading branch information
mhugent committed Aug 8, 2012
1 parent 4969d67 commit 9a3d6abd02419b76bd59d3120626ea99d29db391
@@ -22,19 +22,15 @@
#include <QWebFrame>
#include <QWebPage>

QgsComposerHtml::QgsComposerHtml( QgsComposition* c, qreal x, qreal y, qreal width, qreal height, bool createUndoCommands ): QgsComposerMultiFrame( c, createUndoCommands ),
QgsComposerHtml::QgsComposerHtml( QgsComposition* c, bool createUndoCommands ): QgsComposerMultiFrame( c, createUndoCommands ),
mWebPage( 0 ), mLoaded( false ), mHtmlUnitsToMM( 1.0 )
{
mHtmlUnitsToMM = htmlUnitsToMM();
mWebPage = new QWebPage();
QObject::connect( mWebPage, SIGNAL( loadFinished( bool ) ), this, SLOT( frameLoaded( bool ) ) );

if ( mComposition && width > 0 && height > 0 )
if ( mComposition )
{
QgsComposerFrame* frame = new QgsComposerFrame( c, this, x, y, width, height );
addFrame( frame );
QObject::connect( mComposition, SIGNAL( itemRemoved( QgsComposerItem* ) ), this, SLOT( handleFrameRemoval( QgsComposerItem* ) ) );
recalculateFrameSizes();
}
}

@@ -105,14 +101,19 @@ double QgsComposerHtml::htmlUnitsToMM()
return ( mComposition->printResolution() / 96.0 ); //webkit seems to assume a standard dpi of 96
}

void QgsComposerHtml::addFrame( QgsComposerFrame* frame )
void QgsComposerHtml::addFrame( QgsComposerFrame* frame, bool recalcFrameSizes )
{
mFrameItems.push_back( frame );
QObject::connect( frame, SIGNAL( sizeChanged() ), this, SLOT( recalculateFrameSizes() ) );
if ( mComposition )
{
mComposition->addComposerHtmlFrame( this, frame );
}

if ( recalcFrameSizes )
{
recalculateFrameSizes();
}
}

bool QgsComposerHtml::writeXML( QDomElement& elem, QDomDocument & doc, bool ignoreFrames ) const
@@ -25,7 +25,7 @@ class QgsComposerHtml: public QgsComposerMultiFrame
{
Q_OBJECT
public:
QgsComposerHtml( QgsComposition* c, qreal x, qreal y, qreal width, qreal height, bool createUndoCommands );
QgsComposerHtml( QgsComposition* c, bool createUndoCommands );
QgsComposerHtml();
~QgsComposerHtml();

@@ -38,8 +38,7 @@ class QgsComposerHtml: public QgsComposerMultiFrame
bool writeXML( QDomElement& elem, QDomDocument & doc, bool ignoreFrames = false ) const;
bool readXML( const QDomElement& itemElem, const QDomDocument& doc, bool ignoreFrames = false );

protected:
void addFrame( QgsComposerFrame* frame );
void addFrame( QgsComposerFrame* frame, bool recalcFrameSizes = true );

private slots:
void frameLoaded( bool ok );
@@ -15,7 +15,6 @@

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

QgsComposerMultiFrame::QgsComposerMultiFrame( QgsComposition* c, bool createUndoCommands ): mComposition( c ), mResizeMode( UseExistingFrames ), mCreateUndoCommands( createUndoCommands )
{
@@ -107,7 +106,7 @@ void QgsComposerMultiFrame::recalculateFrameSizes()
newFrame->setContentSection( QRectF( 0, currentY, newFrame->rect().width(), newFrame->rect().height() ) );
currentY += newFrame->rect().height();
currentItem = newFrame;
addFrame( newFrame );
addFrame( newFrame, false );
}
}
}
@@ -125,16 +124,7 @@ void QgsComposerMultiFrame::handleFrameRemoval( QgsComposerItem* item )
return;
}
mFrameItems.removeAt( index );

if ( mFrameItems.size() < 1 )
{
if ( mComposition )
{
//schedule this composer multi frame for deletion
mComposition->removeMultiFrame( this );
}
}
else
if ( mFrameItems.size() > 0 )
{
recalculateFrameSizes();
}
@@ -43,6 +43,9 @@ class QgsComposerMultiFrame: public QObject
virtual ~QgsComposerMultiFrame();
virtual QSizeF totalSize() const = 0;
virtual void render( QPainter* p, const QRectF& renderExtent ) = 0;

virtual void addFrame( QgsComposerFrame* frame, bool recalcFrameSizes = true ) = 0;

void removeFrame( int i );

void update();
@@ -64,15 +67,15 @@ class QgsComposerMultiFrame: public QObject
/**Removes and deletes all frames from mComposition*/
void deleteFrames();

int nFrames() const { return mFrameItems.size(); }

protected:
QgsComposition* mComposition;
QList<QgsComposerFrame*> mFrameItems;
ResizeMode mResizeMode;
/**True: creates QgsMultiFrameCommands on internal changes (e.g. changing frames )*/
bool mCreateUndoCommands;

virtual void addFrame( QgsComposerFrame* frame ) = 0;

protected slots:
void recalculateFrameSizes();
/**Called before a frame is going to be removed (update frame list)*/
@@ -27,6 +27,7 @@
#include "qgscomposerscalebar.h"
#include "qgscomposershape.h"
#include "qgscomposerattributetable.h"
#include "qgsaddremovemultiframecommand.h"
#include "qgscomposermultiframecommand.h"
#include "qgslogger.h"
#include "qgspaintenginehack.h"
@@ -462,7 +463,7 @@ void QgsComposition::addItemsFromXML( const QDomElement& elem, const QDomDocumen
for ( int i = 0; i < composerHtmlList.size(); ++i )
{
QDomElement currentHtmlElem = composerHtmlList.at( i ).toElement();
QgsComposerHtml* newHtml = new QgsComposerHtml( this, 0, 0, 0, 0, true );
QgsComposerHtml* newHtml = new QgsComposerHtml( this, true );
newHtml->readXML( currentHtmlElem, doc );
this->addMultiFrame( newHtml );
}
@@ -1069,7 +1070,6 @@ void QgsComposition::addMultiFrame( QgsComposerMultiFrame* multiFrame )
void QgsComposition::removeMultiFrame( QgsComposerMultiFrame* multiFrame )
{
mMultiFrames.remove( multiFrame );
//delete multiFrame; //e.v. use deleteLater() in case of stability problems
}

void QgsComposition::addComposerArrow( QgsComposerArrow* arrow )
@@ -1203,10 +1203,13 @@ void QgsComposition::removeComposerItem( QgsComposerItem* item, bool createComma
}
else
{
bool frameItem = ( item->type() == QgsComposerItem::ComposerFrame );
QgsComposerMultiFrame* multiFrame = 0;
if ( createCommand )
{
if ( item->type() == QgsComposerItem::ComposerFrame ) //multiframe tracks item changes
if ( frameItem ) //multiframe tracks item changes
{
multiFrame = static_cast<QgsComposerFrame*>( item )->multiFrame();
item->beginItemCommand( tr( "Frame deleted" ) );
emit itemRemoved( item );
item->endItemCommand();
@@ -1221,6 +1224,25 @@ void QgsComposition::removeComposerItem( QgsComposerItem* item, bool createComma
{
emit itemRemoved( item );
}

//check if there are frames left. If not, remove the multi frame
if ( frameItem && multiFrame )
{
if ( multiFrame->nFrames() < 1 )
{
removeMultiFrame( multiFrame );
if ( createCommand )
{
QgsAddRemoveMultiFrameCommand* command = new QgsAddRemoveMultiFrameCommand( QgsAddRemoveMultiFrameCommand::Removed,
multiFrame, this, tr( "Multiframe removed" ) );
undoStack()->push( command );
}
else
{
delete multiFrame;
}
}
}
}
}
}
@@ -24,6 +24,7 @@

#include "qgscomposerview.h"
#include "qgscomposerarrow.h"
#include "qgscomposerframe.h"
#include "qgscomposerhtml.h"
#include "qgscomposerlabel.h"
#include "qgscomposerlegend.h"
@@ -323,9 +324,16 @@ void QgsComposerView::mouseReleaseEvent( QMouseEvent* e )
case AddHtml:
if ( composition() )
{
QgsComposerHtml* composerHtml = new QgsComposerHtml( composition(), mRubberBandItem->transform().dx(), mRubberBandItem->transform().dy(),
mRubberBandItem->rect().width(), mRubberBandItem->rect().height(), true );
Q_UNUSED( composerHtml );
QgsComposerHtml* composerHtml = new QgsComposerHtml( composition(), true );
QgsAddRemoveMultiFrameCommand* command = new QgsAddRemoveMultiFrameCommand( QgsAddRemoveMultiFrameCommand::Added,
composerHtml, composition(), tr( "Html item added" ) );
composition()->undoStack()->push( command );
QgsComposerFrame* frame = new QgsComposerFrame( composition(), composerHtml, mRubberBandItem->transform().dx(),
mRubberBandItem->transform().dy(), mRubberBandItem->rect().width(),
mRubberBandItem->rect().height() );
composition()->beginMultiFrameCommand( composerHtml, tr( "Html frame added" ) );
composerHtml->addFrame( frame );
composition()->endMultiFrameCommand();
scene()->removeItem( mRubberBandItem );
delete mRubberBandItem;
mRubberBandItem = 0;
@@ -16,6 +16,7 @@
***************************************************************************/

#include "qgscomposerhtml.h"
#include "qgscomposerframe.h"
#include "qgscomposition.h"
#include "qgscompositionchecker.h"
#include <QObject>
@@ -58,7 +59,9 @@ void TestQgsComposerHtml::cleanup()

void TestQgsComposerHtml::table()
{
QgsComposerHtml* htmlItem = new QgsComposerHtml( mComposition, 0, 0, 100, 200, false );
QgsComposerHtml* htmlItem = new QgsComposerHtml( mComposition, false );
QgsComposerFrame* htmlFrame = new QgsComposerFrame( mComposition, htmlItem, 0, 0, 100, 200 );
htmlItem->addFrame( htmlFrame );
htmlItem->setUrl( QUrl( QString( "file:///%1" ).arg( QString( TEST_DATA_DIR ) + QDir::separator() + "html_table.html" ) ) );
QgsCompositionChecker checker( "Composer html table", mComposition, QString( QString( TEST_DATA_DIR ) + QDir::separator() +
"control_images" + QDir::separator() + "expected_composerhtml" + QDir::separator() + "composerhtml_table.png" ) );
@@ -70,7 +73,9 @@ void TestQgsComposerHtml::table()

void TestQgsComposerHtml::tableMultiFrame()
{
QgsComposerHtml* htmlItem = new QgsComposerHtml( mComposition, 10, 10, 100, 50, false );
QgsComposerHtml* htmlItem = new QgsComposerHtml( mComposition, false );
QgsComposerFrame* htmlFrame = new QgsComposerFrame( mComposition, htmlItem, 0, 0, 100, 200 );
htmlItem->addFrame( htmlFrame );
htmlItem->setResizeMode( QgsComposerMultiFrame::ExtendToNextPage );
bool result = true;
//page1

0 comments on commit 9a3d6ab

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