void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget ) { if ( !painter ) { return; } drawBackground( painter ); 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, rect().width() - 2 * penWidth - 2 * mMargin, rect().height() - 2 * penWidth - 2 * mMargin ); drawText( painter, painterRect, displayText(), mFont, mHAlignment, mVAlignment ); drawFrame( painter ); if ( isSelected() ) { drawSelectionBoxes( painter ); } }
void QgsComposerLabel::paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) { Q_UNUSED( itemStyle ); Q_UNUSED( pWidget ); if ( !painter ) { return; } if ( !shouldDrawItem() ) { return; } drawBackground( painter ); painter->save(); //antialiasing on painter->setRenderHint( QPainter::Antialiasing, true ); double penWidth = hasFrame() ? ( pen().widthF() / 2.0 ) : 0; double xPenAdjust = mMarginX < 0 ? -penWidth : penWidth; double yPenAdjust = mMarginY < 0 ? -penWidth : penWidth; QRectF painterRect( xPenAdjust + mMarginX, yPenAdjust + mMarginY, rect().width() - 2 * xPenAdjust - 2 * mMarginX, rect().height() - 2 * yPenAdjust - 2 * mMarginY ); if ( mHtmlState ) { if ( mFirstRender ) { contentChanged(); mFirstRender = false; } painter->scale( 1.0 / mHtmlUnitsToMM / 10.0, 1.0 / mHtmlUnitsToMM / 10.0 ); mWebPage->setViewportSize( QSize( painterRect.width() * mHtmlUnitsToMM * 10.0, painterRect.height() * mHtmlUnitsToMM * 10.0 ) ); mWebPage->settings()->setUserStyleSheetUrl( createStylesheetUrl() ); mWebPage->mainFrame()->render( painter ); } else { const QString textToDraw = displayText(); painter->setFont( mFont ); //debug //painter->setPen( QColor( Qt::red ) ); //painter->drawRect( painterRect ); QgsComposerUtils::drawText( painter, painterRect, textToDraw, mFont, mFontColor, mHAlignment, mVAlignment, Qt::TextWordWrap ); } painter->restore(); drawFrame( painter ); if ( isSelected() ) { drawSelectionBoxes( painter ); } }
void QgsNumericScaleBarStyle::draw( QPainter *p, double xOffset ) const { Q_UNUSED( xOffset ); if ( !p || !mScaleBar ) { return; } p->save(); //antialiasing on p->setRenderHint( QPainter::Antialiasing, true ); p->setFont( mScaleBar->font() ); //call QgsComposerItem's pen() function, since that refers to the frame pen //and QgsComposerScalebar's pen() function refers to the scale bar line width, //which is not used for numeric scale bars. Divide the pen width by 2 since //half the width of the frame is drawn outside the item. double penWidth = mScaleBar->QgsComposerItem::pen().widthF() / 2.0; double margin = mScaleBar->boxContentSpace(); //map scalebar alignment to Qt::AlignmentFlag type Qt::AlignmentFlag hAlign; switch ( mScaleBar->alignment() ) { case QgsComposerScaleBar::Left: hAlign = Qt::AlignLeft; break; case QgsComposerScaleBar::Middle: hAlign = Qt::AlignHCenter; break; case QgsComposerScaleBar::Right: hAlign = Qt::AlignRight; break; default: hAlign = Qt::AlignLeft; break; } //text destination is item's rect, excluding the margin and frame QRectF painterRect( penWidth + margin, penWidth + margin, mScaleBar->rect().width() - 2 * penWidth - 2 * margin, mScaleBar->rect().height() - 2 * penWidth - 2 * margin ); QgsComposerUtils::drawText( p, painterRect, scaleText(), mScaleBar->font(), mScaleBar->fontColor(), hAlign, Qt::AlignTop ); p->restore(); }
void QgsLayoutItemLabel::draw( QgsLayoutItemRenderContext &context ) { QPainter *painter = context.renderContext().painter(); painter->save(); // painter is scaled to dots, so scale back to layout units painter->scale( context.renderContext().scaleFactor(), context.renderContext().scaleFactor() ); double penWidth = frameEnabled() ? ( pen().widthF() / 2.0 ) : 0; double xPenAdjust = mMarginX < 0 ? -penWidth : penWidth; double yPenAdjust = mMarginY < 0 ? -penWidth : penWidth; QRectF painterRect( xPenAdjust + mMarginX, yPenAdjust + mMarginY, rect().width() - 2 * xPenAdjust - 2 * mMarginX, rect().height() - 2 * yPenAdjust - 2 * mMarginY ); switch ( mMode ) { case ModeHtml: { if ( mFirstRender ) { contentChanged(); mFirstRender = false; } painter->scale( 1.0 / mHtmlUnitsToLayoutUnits / 10.0, 1.0 / mHtmlUnitsToLayoutUnits / 10.0 ); mWebPage->setViewportSize( QSize( painterRect.width() * mHtmlUnitsToLayoutUnits * 10.0, painterRect.height() * mHtmlUnitsToLayoutUnits * 10.0 ) ); mWebPage->settings()->setUserStyleSheetUrl( createStylesheetUrl() ); mWebPage->mainFrame()->render( painter ); break; } case ModeFont: { const QString textToDraw = currentText(); painter->setFont( mFont ); QgsLayoutUtils::drawText( painter, painterRect, textToDraw, mFont, mFontColor, mHAlignment, mVAlignment, Qt::TextWordWrap ); break; } } painter->restore(); }
void QgsNumericScaleBarRenderer::draw( QgsRenderContext &context, const QgsScaleBarSettings &settings, const ScaleBarContext &scaleContext ) const { if ( !context.painter() ) { return; } QPainter *painter = context.painter(); painter->save(); if ( context.flags() & QgsRenderContext::Antialiasing ) painter->setRenderHint( QPainter::Antialiasing, true ); double margin = context.convertToPainterUnits( settings.boxContentSpace(), QgsUnitTypes::RenderMillimeters ); //map scalebar alignment to Qt::AlignmentFlag type QgsTextRenderer::HAlignment hAlign = QgsTextRenderer::AlignLeft; switch ( settings.alignment() ) { case QgsScaleBarSettings::AlignLeft: hAlign = QgsTextRenderer::AlignLeft; break; case QgsScaleBarSettings::AlignMiddle: hAlign = QgsTextRenderer::AlignCenter; break; case QgsScaleBarSettings::AlignRight: hAlign = QgsTextRenderer::AlignRight; break; } //text destination is item's rect, excluding the margin QRectF painterRect( margin, margin, context.convertToPainterUnits( scaleContext.size.width(), QgsUnitTypes::RenderMillimeters ) - 2 * margin, context.convertToPainterUnits( scaleContext.size.height(), QgsUnitTypes::RenderMillimeters ) - 2 * margin ); QgsTextRenderer::drawText( painterRect, 0, hAlign, QStringList() << scaleText( scaleContext.scale ), context, settings.textFormat() ); painter->restore(); }
void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget ) { Q_UNUSED( itemStyle ); Q_UNUSED( pWidget ); if ( !painter ) { return; } if ( !shouldDrawItem() ) { return; } drawBackground( painter ); painter->save(); //antialiasing on painter->setRenderHint( QPainter::Antialiasing, true ); double penWidth = hasFrame() ? pen().widthF() : 0; QRectF painterRect( penWidth + mMargin, penWidth + mMargin, rect().width() - 2 * penWidth - 2 * mMargin, rect().height() - 2 * penWidth - 2 * mMargin ); QString textToDraw = displayText(); if ( mHtmlState ) { painter->scale( 1.0 / mHtmlUnitsToMM / 10.0, 1.0 / mHtmlUnitsToMM / 10.0 ); QWebPage *webPage = new QWebPage(); webPage->setNetworkAccessManager( QgsNetworkAccessManager::instance() ); //Setup event loop and timeout for rendering html QEventLoop loop; QTimer timeoutTimer; timeoutTimer.setSingleShot( true ); //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 ); // QGIS segfaults when rendering web page while in composer if html // contains images. So if we are not printing the composition, then // disable image loading if ( mComposition->plotStyle() != QgsComposition::Print && mComposition->plotStyle() != QgsComposition::Postscript ) { webPage->settings()->setAttribute( QWebSettings::AutoLoadImages, false ); } //Connect timeout and webpage loadFinished signals to loop connect( &timeoutTimer, SIGNAL( timeout() ), &loop, SLOT( quit() ) ); connect( webPage, SIGNAL( loadFinished( bool ) ), &loop, SLOT( quit() ) ); //mHtmlLoaded tracks whether the QWebPage has completed loading //its html contents, set it initially to false. The loadingHtmlFinished slot will //set this to true after html is loaded. mHtmlLoaded = false; connect( webPage, SIGNAL( loadFinished( bool ) ), SLOT( loadingHtmlFinished( bool ) ) ); webPage->mainFrame()->setHtml( textToDraw ); //For very basic html labels with no external assets, the html load will already be //complete before we even get a chance to start the QEventLoop. Make sure we check //this before starting the loop if ( !mHtmlLoaded ) { // Start a 20 second timeout in case html loading will never complete timeoutTimer.start( 20000 ); // Pause until html is loaded loop.exec(); } webPage->mainFrame()->render( painter );//DELETE WEBPAGE ? } else {