Skip to content
Permalink
Browse files
Merge pull request #413 from olivierdalang/labelCanDisplayHtml
[Feature] add HTML capabilities to composer labels
  • Loading branch information
NathanW2 committed Feb 6, 2013
2 parents 81d865a + ec634a2 commit 526ab02b0b50a2933599562d43305fa08ab2a7a3
@@ -32,13 +32,43 @@ QgsComposerLabelWidget::QgsComposerLabelWidget( QgsComposerLabel* label ): QWidg
QgsComposerItemWidget* itemPropertiesWidget = new QgsComposerItemWidget( this, label );
toolBox->addItem( itemPropertiesWidget, tr( "General options" ) );


if ( mComposerLabel )
{
setGuiElementValues();
connect( mComposerLabel, SIGNAL( itemChanged() ), this, SLOT( setGuiElementValues() ) );
}
}

void QgsComposerLabelWidget::on_mHtmlCheckBox_stateChanged( int state )
{
if ( mComposerLabel )
{
if (state)
{
mFontButton->setEnabled( false );
mFontColorButton->setEnabled( false );
mHorizontalAlignementGroup->setEnabled( false );
mVerticalAlignementGroup->setEnabled( false );
}
else
{
mFontButton->setEnabled( true );
mFontColorButton->setEnabled( true );
mHorizontalAlignementGroup->setEnabled( true );
mVerticalAlignementGroup->setEnabled( true );
}

mComposerLabel->beginCommand( tr( "Label text HTML state changed" ), QgsComposerMergeCommand::ComposerLabelSetText );
mComposerLabel->blockSignals( true );
mComposerLabel->setHtmlSate(state);
mComposerLabel->setText( mTextEdit->toPlainText() );
mComposerLabel->update();
mComposerLabel->blockSignals( false );
mComposerLabel->endCommand();
}
}

void QgsComposerLabelWidget::on_mTextEdit_textChanged()
{
if ( mComposerLabel )
@@ -223,9 +253,10 @@ void QgsComposerLabelWidget::on_mRotationSpinBox_valueChanged( double v )
void QgsComposerLabelWidget::setGuiElementValues()
{
blockAllSignals( true );
mTextEdit->setText( mComposerLabel->text() );
mTextEdit->setPlainText( mComposerLabel->text() );
mTextEdit->moveCursor( QTextCursor::End, QTextCursor::MoveAnchor );
mMarginDoubleSpinBox->setValue( mComposerLabel->margin() );
mHtmlCheckBox->setChecked( mComposerLabel->htmlSate() );
mTopRadioButton->setChecked( mComposerLabel->vAlign() == Qt::AlignTop );
mMiddleRadioButton->setChecked( mComposerLabel->vAlign() == Qt::AlignVCenter );
mBottomRadioButton->setChecked( mComposerLabel->vAlign() == Qt::AlignBottom );
@@ -239,6 +270,7 @@ void QgsComposerLabelWidget::setGuiElementValues()
void QgsComposerLabelWidget::blockAllSignals( bool block )
{
mTextEdit->blockSignals( block );
mHtmlCheckBox->blockSignals( block );
mMarginDoubleSpinBox->blockSignals( block );
mTopRadioButton->blockSignals( block );
mMiddleRadioButton->blockSignals( block );
@@ -32,6 +32,7 @@ class QgsComposerLabelWidget: public QWidget, private Ui::QgsComposerLabelWidget
QgsComposerLabelWidget( QgsComposerLabel* label );

public slots:
void on_mHtmlCheckBox_stateChanged( int i );
void on_mTextEdit_textChanged();
void on_mFontButton_clicked();
void on_mInsertExpressionButton_clicked();
@@ -103,7 +103,7 @@ double QgsComposerHtml::htmlUnitsToMM()
return 1.0;
}

return ( mComposition->printResolution() / 96.0 ); //webkit seems to assume a standard dpi of 96
return ( mComposition->printResolution() / 72.0 ); //webkit seems to assume a standard dpi of 96
}

void QgsComposerHtml::addFrame( QgsComposerFrame* frame, bool recalcFrameSizes )
@@ -17,15 +17,19 @@

#include "qgscomposerlabel.h"
#include "qgsexpression.h"
#include <QCoreApplication>
#include <QDate>
#include <QDomElement>
#include <QPainter>
#include <QWebFrame>
#include <QWebPage>

QgsComposerLabel::QgsComposerLabel( QgsComposition *composition ):
QgsComposerItem( composition ), mMargin( 1.0 ), mFontColor( QColor( 0, 0, 0 ) ),
QgsComposerItem( composition ), mMargin( 1.0 ), mHtmlState( 0 ), mFontColor( QColor( 0, 0, 0 ) ),
mHAlignment( Qt::AlignLeft ), mVAlignment( Qt::AlignTop ),
mExpressionFeature( 0 ), mExpressionLayer( 0 )
mExpressionFeature( 0 ), mExpressionLayer( 0 ), mHtmlUnitsToMM( 1.0 )
{
mHtmlUnitsToMM = htmlUnitsToMM();
//default font size is 10 point
mFont.setPointSizeF( 10 );
}
@@ -45,22 +49,46 @@ void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem*

drawBackground( painter );
painter->save();
painter->setPen( QPen( QColor( mFontColor ) ) ); //draw all text black
painter->setFont( mFont );

QFontMetricsF fontSize( mFont );

//support multiline labels
double penWidth = pen().widthF();
QRectF painterRect( penWidth + mMargin, penWidth + mMargin, mTextBoxWidth - 2 * penWidth - 2 * mMargin, mTextBoxHeight - 2 * penWidth - 2 * mMargin );
painter->translate( rect().width() / 2.0, rect().height() / 2.0 );
painter->rotate( mRotation );
painter->translate( -mTextBoxWidth / 2.0, -mTextBoxHeight / 2.0 );

//debug
//painter->setPen( QColor( Qt::red ) );
//painter->drawRect( painterRect );
drawText( painter, painterRect, displayText(), mFont, mHAlignment, mVAlignment );
if( mHtmlState )
{
painter->scale( 1.0 / mHtmlUnitsToMM / 10.0, 1.0 / mHtmlUnitsToMM / 10.0 );

QWebPage* webPage = new QWebPage();

//This makes the background transparent. Found on http://blog.qt.digia.com/blog/2009/06/30/transparent-qwebview-or-qwebpage/
QPalette palette = webPage->palette();
palette.setBrush(QPalette::Base, Qt::transparent);
webPage->setPalette(palette);
//webPage->setAttribute(Qt::WA_OpaquePaintEvent, false); //this does not compile, why ?

webPage->setViewportSize( QSize(painterRect.width() * mHtmlUnitsToMM * 10.0, painterRect.height() * mHtmlUnitsToMM * 10.0) );
webPage->mainFrame()->setZoomFactor( 10.0 );
webPage->mainFrame()->setScrollBarPolicy( Qt::Horizontal, Qt::ScrollBarAlwaysOff );
webPage->mainFrame()->setScrollBarPolicy( Qt::Vertical, Qt::ScrollBarAlwaysOff );
webPage->mainFrame()->setHtml( displayText() );
webPage->mainFrame()->render( painter );//DELETE WEBPAGE ?
}
else
{
painter->setPen( QPen( QColor( mFontColor ) ) );
painter->setFont( mFont );

QFontMetricsF fontSize( mFont );

//debug
//painter->setPen( QColor( Qt::red ) );
//painter->drawRect( painterRect );
drawText( painter, painterRect, displayText(), mFont, mHAlignment, mVAlignment );
}




painter->restore();
@@ -72,6 +100,17 @@ void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem*
}
}

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

//TODO : fix this more precisely so that the label's default text size is the same with or without "display as html"
return ( mComposition->printResolution() / 72.0 ); //webkit seems to assume a standard dpi of 72
}

void QgsComposerLabel::setText( const QString& text )
{
mText = text;
@@ -181,6 +220,8 @@ bool QgsComposerLabel::writeXML( QDomElement& elem, QDomDocument & doc ) const

QDomElement composerLabelElem = doc.createElement( "ComposerLabel" );

composerLabelElem.setAttribute( "htmlState", mHtmlState );

composerLabelElem.setAttribute( "labelText", mText );
composerLabelElem.setAttribute( "margin", QString::number( mMargin ) );

@@ -217,6 +258,9 @@ bool QgsComposerLabel::readXML( const QDomElement& itemElem, const QDomDocument&
//text
mText = itemElem.attribute( "labelText" );

//html state
mHtmlState = itemElem.attribute( "htmlState" ).toInt();

//margin
mMargin = itemElem.attribute( "margin" ).toDouble();

@@ -44,6 +44,9 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
QString text() { return mText; }
void setText( const QString& text );

int htmlSate() { return mHtmlState; }
void setHtmlSate( int state ) {mHtmlState = state;}

/**Returns the text as it appears on screen (with replaced data field)
@note this function was added in version 1.2*/
QString displayText() const;
@@ -104,6 +107,11 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
// Text
QString mText;

// Html state
int mHtmlState;
double mHtmlUnitsToMM;
double htmlUnitsToMM(); //calculate scale factor

// Font
QFont mFont;

@@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>243</width>
<height>116</height>
<height>153</height>
</rect>
</property>
<property name="windowTitle">
@@ -24,41 +24,54 @@
<rect>
<x>0</x>
<y>0</y>
<width>225</width>
<height>72</height>
<width>221</width>
<height>100</height>
</rect>
</property>
<attribute name="label">
<string>HTML</string>
</attribute>
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="2">
<item row="2" column="2">
<widget class="QToolButton" name="mFileToolButton">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item row="1" column="1">
<item row="2" column="1">
<widget class="QLineEdit" name="mUrlLineEdit"/>
</item>
<item row="1" column="0">
<item row="2" column="0">
<widget class="QLabel" name="mUrlLabel">
<property name="text">
<string>URL</string>
</property>
</widget>
</item>
<item row="0" column="1" colspan="2">
<widget class="QComboBox" name="mResizeModeComboBox"/>
</item>
<item row="3" column="1">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="0">
<widget class="QLabel" name="mResizeModeLabel">
<property name="text">
<string>Resize mode</string>
</property>
</widget>
</item>
<item row="0" column="1" colspan="2">
<widget class="QComboBox" name="mResizeModeComboBox"/>
</item>
</layout>
</widget>
</widget>
Loading

0 comments on commit 526ab02

Please sign in to comment.