Skip to content

Commit 9104704

Browse files
committed
[composer] Set base stylesheet of composer labels to match the
label font and margins when rendering as HTML. This allows interactive choice of font, margins and colors and avoids the need to manually set these with CSS within the label HTML code.
1 parent 6526cf5 commit 9104704

File tree

9 files changed

+73
-13
lines changed

9 files changed

+73
-13
lines changed

ci/travis/linux/qt5/blacklist.txt

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ PyQgsZonalStatistics
7474
qgis_alignrastertest
7575
qgis_atlascompositiontest
7676
qgis_composereffectstest
77+
qgis_composerlabeltest
7778
qgis_composermapgridtest
7879
qgis_composermapoverviewtest
7980
qgis_composermaptest

src/app/composer/qgscomposerlabelwidget.cpp

+11-12
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,10 @@ void QgsComposerLabelWidget::on_mHtmlCheckBox_stateChanged( int state )
5050
{
5151
if ( mComposerLabel )
5252
{
53-
if ( state )
54-
{
55-
mFontButton->setEnabled( false );
56-
mFontColorButton->setEnabled( false );
57-
mAppearanceGroup->setEnabled( false );
58-
}
59-
else
60-
{
61-
mFontButton->setEnabled( true );
62-
mFontColorButton->setEnabled( true );
63-
mAppearanceGroup->setEnabled( true );
64-
}
53+
mVerticalAlignementLabel->setDisabled( state );
54+
mTopRadioButton->setDisabled( state );
55+
mMiddleRadioButton->setDisabled( state );
56+
mBottomRadioButton->setDisabled( state );
6557

6658
mComposerLabel->beginCommand( tr( "Label text HTML state changed" ), QgsComposerMergeCommand::ComposerLabelSetText );
6759
mComposerLabel->blockSignals( true );
@@ -249,6 +241,13 @@ void QgsComposerLabelWidget::setGuiElementValues()
249241
mCenterRadioButton->setChecked( mComposerLabel->hAlign() == Qt::AlignHCenter );
250242
mRightRadioButton->setChecked( mComposerLabel->hAlign() == Qt::AlignRight );
251243
mFontColorButton->setColor( mComposerLabel->fontColor() );
244+
245+
246+
mVerticalAlignementLabel->setDisabled( mComposerLabel->htmlState() );
247+
mTopRadioButton->setDisabled( mComposerLabel->htmlState() );
248+
mMiddleRadioButton->setDisabled( mComposerLabel->htmlState() );
249+
mBottomRadioButton->setDisabled( mComposerLabel->htmlState() );
250+
252251
blockAllSignals( false );
253252
}
254253

src/core/composer/qgscomposerlabel.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem*
133133
webPage->mainFrame()->setZoomFactor( 10.0 );
134134
webPage->mainFrame()->setScrollBarPolicy( Qt::Horizontal, Qt::ScrollBarAlwaysOff );
135135
webPage->mainFrame()->setScrollBarPolicy( Qt::Vertical, Qt::ScrollBarAlwaysOff );
136+
webPage->settings()->setUserStyleSheetUrl( createStylesheetUrl() );
136137

137138
// QGIS segfaults when rendering web page while in composer if html
138139
// contains images. So if we are not printing the composition, then
@@ -633,3 +634,18 @@ void QgsComposerLabel::itemShiftAdjustSize( double newWidth, double newHeight, d
633634
}
634635
}
635636
}
637+
638+
QUrl QgsComposerLabel::createStylesheetUrl() const
639+
{
640+
QString stylesheet;
641+
stylesheet += QString( "body { margin: %1 %2;" ).arg( qMax( mMarginX * mHtmlUnitsToMM, 0.0 ) ).arg( qMax( mMarginY * mHtmlUnitsToMM, 0.0 ) );
642+
stylesheet += QgsFontUtils::asCSS( mFont, 0.352778 * mHtmlUnitsToMM );
643+
stylesheet += QString( "color: %1;" ).arg( mFontColor.name() );
644+
stylesheet += QString( "text-align: %1; }" ).arg( mHAlignment == Qt::AlignLeft ? "left" : mHAlignment == Qt::AlignRight ? "right" : "center" );
645+
646+
QByteArray ba;
647+
ba.append( stylesheet.toUtf8() );
648+
QUrl cssFileURL = QUrl( "data:text/css;charset=utf-8;base64," + ba.toBase64() );
649+
650+
return cssFileURL;
651+
}

src/core/composer/qgscomposerlabel.h

+4
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,14 @@ class CORE_EXPORT QgsComposerLabel: public QgsComposerItem
204204
/** Replaces replace '$CURRENT_DATE<(FORMAT)>' with the current date (e.g. $CURRENT_DATE(d 'June' yyyy)*/
205205
void replaceDateText( QString& text ) const;
206206

207+
//! Creates an encoded stylesheet url using the current font and label appearance settings
208+
QUrl createStylesheetUrl() const;
209+
207210
QScopedPointer<QgsFeature> mExpressionFeature;
208211
QgsVectorLayer* mExpressionLayer;
209212
QMap<QString, QVariant> mSubstitutions;
210213
QgsDistanceArea* mDistanceArea;
214+
211215
};
212216

213217
#endif

tests/src/core/testqgscomposerlabel.cpp

+41-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include "qgsmaprenderer.h"
2323
#include "qgsvectorlayer.h"
2424
#include "qgsvectordataprovider.h"
25+
#include "qgsmultirenderchecker.h"
26+
#include "qgsfontutils.h"
2527

2628
#include <QObject>
2729
#include <QtTest/QtTest>
@@ -50,14 +52,16 @@ class TestQgsComposerLabel : public QObject
5052
void feature_evaluation();
5153
// test page expressions
5254
void page_evaluation();
53-
5455
void marginMethods(); //tests getting/setting margins
56+
void render();
57+
void renderAsHtml();
5558

5659
private:
5760
QgsComposition* mComposition;
5861
QgsComposerLabel* mComposerLabel;
5962
QgsMapSettings *mMapSettings;
6063
QgsVectorLayer* mVectorLayer;
64+
QString mReport;
6165
};
6266

6367
void TestQgsComposerLabel::initTestCase()
@@ -89,6 +93,15 @@ void TestQgsComposerLabel::initTestCase()
8993

9094
void TestQgsComposerLabel::cleanupTestCase()
9195
{
96+
QString myReportFile = QDir::tempPath() + "/qgistest.html";
97+
QFile myFile( myReportFile );
98+
if ( myFile.open( QIODevice::WriteOnly | QIODevice::Append ) )
99+
{
100+
QTextStream myQTextStream( &myFile );
101+
myQTextStream << mReport;
102+
myFile.close();
103+
}
104+
92105
delete mComposition;
93106
delete mMapSettings;
94107

@@ -225,5 +238,32 @@ void TestQgsComposerLabel::marginMethods()
225238
QCOMPARE( label3.marginY(), 12.0 );
226239
}
227240

241+
void TestQgsComposerLabel::render()
242+
{
243+
mComposerLabel->setText( "test label" );
244+
mComposerLabel->setFont( QgsFontUtils::getStandardTestFont( "Bold", 48 ) );
245+
mComposerLabel->setPos( 70, 70 );
246+
mComposerLabel->adjustSizeToText();
247+
248+
QgsCompositionChecker checker( "composerlabel_render", mComposition );
249+
checker.setControlPathPrefix( "composer_label" );
250+
QVERIFY( checker.testComposition( mReport, 0, 0 ) );
251+
}
252+
253+
void TestQgsComposerLabel::renderAsHtml()
254+
{
255+
mComposerLabel->setFontColor( QColor( 200, 40, 60 ) );
256+
mComposerLabel->setText( "test <i>html</i>" );
257+
mComposerLabel->setFont( QgsFontUtils::getStandardTestFont( "Bold", 48 ) );
258+
mComposerLabel->setPos( 70, 70 );
259+
mComposerLabel->adjustSizeToText();
260+
mComposerLabel->setHtmlState( 1 );
261+
mComposerLabel->update();
262+
263+
QgsCompositionChecker checker( "composerlabel_renderhtml", mComposition );
264+
checker.setControlPathPrefix( "composer_label" );
265+
QVERIFY( checker.testComposition( mReport, 0, 0 ) );
266+
}
267+
228268
QTEST_MAIN( TestQgsComposerLabel )
229269
#include "testqgscomposerlabel.moc"

0 commit comments

Comments
 (0)