Skip to content

Commit

Permalink
begins to work
Browse files Browse the repository at this point in the history
  • Loading branch information
olivierdalang committed Jan 29, 2013
1 parent f150fb8 commit 820ff2f
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 46 deletions.
30 changes: 11 additions & 19 deletions src/app/composer/qgscomposerlabelwidget.cpp
Expand Up @@ -50,32 +50,22 @@ void QgsComposerLabelWidget::on_mHtmlCheckBox_stateChanged( int state )
mFontColorButton->setEnabled( false );
mHorizontalAlignementGroup->setEnabled( false );
mVerticalAlignementGroup->setEnabled( false );
mMarginDoubleSpinBox->setEnabled( false );
mRotationSpinBox->setEnabled( false );
mComposerLabel->beginCommand( tr( "Label text HTML state changed" ), QgsComposerMergeCommand::ComposerLabelSetText );
mComposerLabel->blockSignals( true );
//mComposerLabel->setHtml(state);
mComposerLabel->setText( mTextEdit->toPlainText() );
mComposerLabel->update();
mComposerLabel->blockSignals( false );
mComposerLabel->endCommand();
}
else
{
mFontButton->setEnabled( true );
mFontColorButton->setEnabled( true );
mHorizontalAlignementGroup->setEnabled( true );
mVerticalAlignementGroup->setEnabled( true );
mMarginDoubleSpinBox->setEnabled( true );
mRotationSpinBox->setEnabled( true );
mComposerLabel->beginCommand( tr( "Label text HTML state changed" ), QgsComposerMergeCommand::ComposerLabelSetText );
mComposerLabel->blockSignals( true );
//mComposerLabel->setHtml(state);
mComposerLabel->setText( mTextEdit->toPlainText() );
mComposerLabel->update();
mComposerLabel->blockSignals( false );
mComposerLabel->endCommand();
}

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();
}
}

Expand Down Expand Up @@ -263,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 );
Expand All @@ -279,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 );
Expand Down
2 changes: 1 addition & 1 deletion src/core/composer/qgscomposerhtml.cpp
Expand Up @@ -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 )
Expand Down
64 changes: 53 additions & 11 deletions src/core/composer/qgscomposerlabel.cpp
Expand Up @@ -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 );
}
Expand All @@ -45,22 +49,45 @@ 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, 1.0 / mHtmlUnitsToMM );
//painter->translate( 0.0, -renderExtent.top() * mHtmlUnitsToMM );
QWebPage* webPage = new QWebPage();
/*
TODO : http://blog.qt.digia.com/blog/2009/06/30/transparent-qwebview-or-qwebpage/
is it possible ?
*/
webPage->setViewportSize( QSize(painterRect.width() * mHtmlUnitsToMM, painterRect.height() * mHtmlUnitsToMM) );
webPage->mainFrame()->setScrollBarPolicy( Qt::Horizontal, Qt::ScrollBarAlwaysOff );
webPage->mainFrame()->setScrollBarPolicy( Qt::Vertical, Qt::ScrollBarAlwaysOff );
webPage->mainFrame()->setHtml( displayText() );
webPage->mainFrame()->render( painter );
//webPage->mainFrame()->render( painter, QRegion( painterRect.left(), painterRect.top() * mHtmlUnitsToMM, painterRect.width(), painterRect.height() ) );
//webPage->mainFrame()->render( painter, QRegion( painterRect.left(), painterRect.top() * mHtmlUnitsToMM, painterRect.width() * mHtmlUnitsToMM, painterRect.height() * mHtmlUnitsToMM ) );
//DELETE WEBPAGE ?
}
else
{
painter->setPen( QPen( QColor( mFontColor ) ) ); //draw all text black
painter->setFont( mFont );

QFontMetricsF fontSize( mFont );

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




painter->restore();
Expand All @@ -72,6 +99,16 @@ void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem*
}
}

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

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

void QgsComposerLabel::setText( const QString& text )
{
mText = text;
Expand Down Expand Up @@ -181,6 +218,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 ) );

Expand Down Expand Up @@ -217,6 +256,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();

Expand Down
8 changes: 8 additions & 0 deletions src/core/composer/qgscomposerlabel.h
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
18 changes: 3 additions & 15 deletions src/ui/qgscomposerlabelwidgetbase.ui
Expand Up @@ -38,20 +38,6 @@
<string>Label</string>
</attribute>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0" colspan="2">
<widget class="QTextEdit" name="mTextEdit">
<property name="lineWrapMode">
<enum>QTextEdit::WidgetWidth</enum>
</property>
<property name="html">
<string>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt;
&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
p, li { white-space: pre-wrap; }
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'MS Shell Dlg 2'; font-size:7.8pt; font-weight:400; font-style:normal;&quot;&gt;
&lt;p style=&quot;-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:8pt;&quot;&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
<item row="7" column="1">
<widget class="QDoubleSpinBox" name="mMarginDoubleSpinBox">
<property name="prefix">
Expand Down Expand Up @@ -247,6 +233,9 @@ p, li { white-space: pre-wrap; }
</property>
</widget>
</item>
<item row="0" column="0" colspan="2">
<widget class="QPlainTextEdit" name="mTextEdit"/>
</item>
</layout>
</widget>
</widget>
Expand All @@ -255,7 +244,6 @@ p, li { white-space: pre-wrap; }
</widget>
<layoutdefault spacing="6" margin="11"/>
<tabstops>
<tabstop>mTextEdit</tabstop>
<tabstop>mFontButton</tabstop>
<tabstop>mMarginDoubleSpinBox</tabstop>
</tabstops>
Expand Down

0 comments on commit 820ff2f

Please sign in to comment.