27 #include <QCoreApplication> 29 #include <QDomElement> 40 , mHtmlUnitsToMM( 1.0 )
41 , mHtmlLoaded( false )
44 , mFontColor( QColor( 0, 0, 0 ) )
45 , mHAlignment( Qt::AlignLeft )
46 , mVAlignment( Qt::AlignTop )
47 , mExpressionFeature( 0 )
48 , mExpressionLayer( 0 )
52 mHtmlUnitsToMM = htmlUnitsToMM();
56 QString defaultFontString = settings.value(
"/Composer/defaultFont" ).toString();
57 if ( !defaultFontString.isEmpty() )
59 mFont.setFamily( defaultFontString );
63 mFont.setPointSizeF( 10 );
90 Q_UNUSED( itemStyle );
105 painter->setRenderHint( QPainter::Antialiasing,
true );
107 double penWidth =
hasFrame() ? ( pen().widthF() / 2.0 ) : 0;
108 double xPenAdjust = mMarginX < 0 ? -penWidth : penWidth;
109 double yPenAdjust = mMarginY < 0 ? -penWidth : penWidth;
110 QRectF painterRect( xPenAdjust + mMarginX, yPenAdjust + mMarginY, rect().width() - 2 * xPenAdjust - 2 * mMarginX, rect().height() - 2 * yPenAdjust - 2 * mMarginY );
116 painter->scale( 1.0 / mHtmlUnitsToMM / 10.0, 1.0 / mHtmlUnitsToMM / 10.0 );
117 QWebPage *webPage =
new QWebPage();
123 timeoutTimer.setSingleShot(
true );
126 QPalette palette = webPage->palette();
127 palette.setBrush( QPalette::Base, Qt::transparent );
128 webPage->setPalette( palette );
131 webPage->setViewportSize( QSize( painterRect.width() * mHtmlUnitsToMM * 10.0, painterRect.height() * mHtmlUnitsToMM * 10.0 ) );
132 webPage->mainFrame()->setZoomFactor( 10.0 );
133 webPage->mainFrame()->setScrollBarPolicy( Qt::Horizontal, Qt::ScrollBarAlwaysOff );
134 webPage->mainFrame()->setScrollBarPolicy( Qt::Vertical, Qt::ScrollBarAlwaysOff );
142 webPage->settings()->setAttribute( QWebSettings::AutoLoadImages,
false );
146 connect( &timeoutTimer, SIGNAL( timeout() ), &loop, SLOT( quit() ) );
147 connect( webPage, SIGNAL( loadFinished(
bool ) ), &loop, SLOT( quit() ) );
153 connect( webPage, SIGNAL( loadFinished(
bool ) ), SLOT( loadingHtmlFinished(
bool ) ) );
155 webPage->mainFrame()->setHtml( textToDraw );
163 timeoutTimer.start( 20000 );
167 webPage->mainFrame()->render( painter );
171 painter->setFont( mFont );
175 QgsComposerUtils::drawText( painter, painterRect, textToDraw, mFont, mFontColor, mHAlignment, mVAlignment, Qt::TextWordWrap );
188 void QgsComposerLabel::loadingHtmlFinished(
bool result )
194 double QgsComposerLabel::htmlUnitsToMM()
219 if ( state == mHtmlState )
235 mExpressionFeature = feature;
236 mExpressionLayer = layer;
237 mSubstitutions = substitutions;
279 replaceDateText( displayText );
280 QMap<QString, QVariant> subs = mSubstitutions;
285 void QgsComposerLabel::replaceDateText( QString&
text )
const 287 QString constant =
"$CURRENT_DATE";
288 int currentDatePos = text.indexOf( constant );
289 if ( currentDatePos != -1 )
293 int openingBracketPos = text.indexOf(
"(", currentDatePos );
294 int closingBracketPos = text.indexOf(
")", openingBracketPos + 1 );
295 if ( openingBracketPos != -1 &&
296 closingBracketPos != -1 &&
297 ( closingBracketPos - openingBracketPos ) > 1 &&
298 openingBracketPos == currentDatePos + constant.size() )
300 formatText = text.mid( openingBracketPos + 1, closingBracketPos - openingBracketPos - 1 );
301 text.replace( currentDatePos, closingBracketPos - currentDatePos + 1, QDate::currentDate().toString( formatText ) );
305 text.replace(
"$CURRENT_DATE", QDate::currentDate().toString() );
319 prepareGeometryChange();
325 prepareGeometryChange();
331 prepareGeometryChange();
339 double penWidth =
hasFrame() ? ( pen().widthF() / 2.0 ) : 0;
341 double width = textWidth + 2 * mMarginX + 2 * penWidth + 1;
342 double height = fontHeight + 2 * mMarginY + 2 * penWidth;
347 itemShiftAdjustSize( width, height, xShift, yShift );
350 QRectF evaluatedRect =
evalItemRect( QRectF( pos().x() + xShift, pos().y() + yShift, width, height ) );
368 QDomElement composerLabelElem = doc.createElement(
"ComposerLabel" );
370 composerLabelElem.setAttribute(
"htmlState", mHtmlState );
372 composerLabelElem.setAttribute(
"labelText", mText );
373 composerLabelElem.setAttribute(
"marginX", QString::number( mMarginX ) );
374 composerLabelElem.setAttribute(
"marginY", QString::number( mMarginY ) );
375 composerLabelElem.setAttribute(
"halign", mHAlignment );
376 composerLabelElem.setAttribute(
"valign", mVAlignment );
379 QDomElement labelFontElem = doc.createElement(
"LabelFont" );
380 labelFontElem.setAttribute(
"description", mFont.toString() );
381 composerLabelElem.appendChild( labelFontElem );
384 QDomElement fontColorElem = doc.createElement(
"FontColor" );
385 fontColorElem.setAttribute(
"red", mFontColor.red() );
386 fontColorElem.setAttribute(
"green", mFontColor.green() );
387 fontColorElem.setAttribute(
"blue", mFontColor.blue() );
388 composerLabelElem.appendChild( fontColorElem );
390 elem.appendChild( composerLabelElem );
391 return _writeXML( composerLabelElem, doc );
398 if ( itemElem.isNull() )
406 mText = itemElem.attribute(
"labelText" );
409 mHtmlState = itemElem.attribute(
"htmlState" ).toInt();
412 bool marginXOk =
false;
413 bool marginYOk =
false;
414 mMarginX = itemElem.attribute(
"marginX" ).toDouble( &marginXOk );
415 mMarginY = itemElem.attribute(
"marginY" ).toDouble( &marginYOk );
416 if ( !marginXOk || !marginYOk )
419 double margin = itemElem.attribute(
"margin",
"1.0" ).toDouble();
425 mHAlignment = ( Qt::AlignmentFlag )( itemElem.attribute(
"halign" ).toInt() );
428 mVAlignment = ( Qt::AlignmentFlag )( itemElem.attribute(
"valign" ).toInt() );
431 QDomNodeList labelFontList = itemElem.elementsByTagName(
"LabelFont" );
432 if ( labelFontList.size() > 0 )
434 QDomElement labelFontElem = labelFontList.at( 0 ).toElement();
435 mFont.fromString( labelFontElem.attribute(
"description" ) );
439 QDomNodeList fontColorList = itemElem.elementsByTagName(
"FontColor" );
440 if ( fontColorList.size() > 0 )
442 QDomElement fontColorElem = fontColorList.at( 0 ).toElement();
443 int red = fontColorElem.attribute(
"red",
"0" ).toInt();
444 int green = fontColorElem.attribute(
"green",
"0" ).toInt();
445 int blue = fontColorElem.attribute(
"blue",
"0" ).toInt();
446 mFontColor = QColor( red, green, blue );
450 mFontColor = QColor( 0, 0, 0 );
454 QDomNodeList composerItemList = itemElem.elementsByTagName(
"ComposerItem" );
455 if ( composerItemList.size() > 0 )
457 QDomElement composerItemElem = composerItemList.at( 0 ).toElement();
460 if ( composerItemElem.attribute(
"rotation",
"0" ).toDouble() != 0 )
463 setItemRotation( composerItemElem.attribute(
"rotation",
"0" ).toDouble() );
474 if ( !
id().isEmpty() )
481 return tr(
"<HTML label>" );
485 QString
text = mText;
486 if ( text.isEmpty() )
488 return tr(
"<label>" );
490 if ( text.length() > 25 )
492 return QString(
tr(
"%1..." ) ).arg( text.left( 25 ).simplified() );
496 return text.simplified();
502 QRectF rectangle = rect();
503 double penWidth =
hasFrame() ? ( pen().widthF() / 2.0 ) : 0;
504 rectangle.adjust( -penWidth, -penWidth, penWidth, penWidth );
508 rectangle.adjust( mMarginX, 0, -mMarginX, 0 );
512 rectangle.adjust( 0, mMarginY, 0, -mMarginY );
521 prepareGeometryChange();
527 prepareGeometryChange();
530 void QgsComposerLabel::itemShiftAdjustSize(
double newWidth,
double newHeight,
double& xShift,
double& yShift )
const 533 double currentWidth = rect().width();
534 double currentHeight = rect().height();
540 if ( mHAlignment == Qt::AlignHCenter )
542 xShift = - ( newWidth - currentWidth ) / 2.0;
544 else if ( mHAlignment == Qt::AlignRight )
546 xShift = - ( newWidth - currentWidth );
548 if ( mVAlignment == Qt::AlignVCenter )
550 yShift = -( newHeight - currentHeight ) / 2.0;
552 else if ( mVAlignment == Qt::AlignBottom )
554 yShift = - ( newHeight - currentHeight );
559 if ( mHAlignment == Qt::AlignHCenter )
561 yShift = -( newHeight - currentHeight ) / 2.0;
563 else if ( mHAlignment == Qt::AlignRight )
565 yShift = -( newHeight - currentHeight );
567 if ( mVAlignment == Qt::AlignTop )
569 xShift = -( newWidth - currentWidth );
571 else if ( mVAlignment == Qt::AlignVCenter )
573 xShift = -( newWidth - currentWidth / 2.0 );
578 if ( mHAlignment == Qt::AlignHCenter )
580 xShift = -( newWidth - currentWidth ) / 2.0;
582 else if ( mHAlignment == Qt::AlignLeft )
584 xShift = -( newWidth - currentWidth );
586 if ( mVAlignment == Qt::AlignVCenter )
588 yShift = ( newHeight - currentHeight ) / 2.0;
590 else if ( mVAlignment == Qt::AlignTop )
592 yShift = ( newHeight - currentHeight );
597 if ( mHAlignment == Qt::AlignHCenter )
599 yShift = -( newHeight - currentHeight ) / 2.0;
601 else if ( mHAlignment == Qt::AlignLeft )
603 yShift = -( newHeight - currentHeight );
605 if ( mVAlignment == Qt::AlignBottom )
607 xShift = -( newWidth - currentWidth );
609 else if ( mVAlignment == Qt::AlignVCenter )
611 xShift = -( newWidth - currentWidth / 2.0 );
QgsComposition::AtlasMode atlasMode() const
Returns the current atlas mode of the composition.
void setHtmlState(int state)
bool readXML(const QDomElement &itemElem, const QDomDocument &doc) override
sets state from Dom document
QgsComposerModel * itemsModel()
Returns the items model attached to the composition.
void itemChanged()
Emitted when the item changes.
const QgsMapSettings & mapSettings() const
Return setting of QGIS map canvas.
void setSourceCrs(long srsid)
sets source spatial reference system (by QGIS CRS)
static QgsNetworkAccessManager * instance()
A item that forms part of a map composition.
static void drawText(QPainter *painter, const QPointF &pos, const QString &text, const QFont &font, const QColor &color=QColor())
Draws text on a painter at a specific position, taking care of composer specific issues (calculation ...
bool enabled() const
Returns whether the atlas generation is enabled.
bool hasCrsTransformEnabled() const
returns true if projections are enabled for this layer set
virtual void drawFrame(QPainter *p)
Draw black frame around item.
virtual void setFrameEnabled(const bool drawFrame)
Set whether this item has a frame drawn around it or not.
bool setEllipsoid(const QString &ellipsoid)
sets ellipsoid by its acronym
void setFont(const QFont &f)
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
void updateItemDisplayName(QgsComposerItem *item)
Must be called when an item's display name is modified.
bool writeXML(QDomElement &elem, QDomDocument &doc) const override
stores state in Dom element
bool _readXML(const QDomElement &itemElem, const QDomDocument &doc)
Reads parameter that are not subclass specific in document.
int itemPageNumber(const QgsComposerItem *) const
Returns on which page number (0-based) is displayed an item.
virtual void drawSelectionBoxes(QPainter *p)
Draws additional graphics on selected items.
int printResolution() const
const QgsCoordinateReferenceSystem & destinationCrs() const
returns CRS of destination coordinate reference system
QgsComposerLabel(QgsComposition *composition)
bool shouldDrawItem() const
Returns whether the item should be drawn in the current context.
void setBackgroundEnabled(const bool drawBackground)
Set whether this item has a Background drawn around it or not.
QRectF boundingRect() const override
In case of negative margins, the bounding rect may be larger than the label's frame.
void setMarginX(const double margin)
Sets the horizontal margin between the edge of the frame and the label contents.
Graphics scene for map printing.
QgsFeature * currentFeature()
Returns the current atlas feature.
void setMarginY(const double margin)
Sets the vertical margin between the edge of the frame and the label contents.
void paint(QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget) override
Reimplementation of QCanvasItem::paint.
virtual void setFrameOutlineWidth(const double outlineWidth)
Sets frame outline width.
General purpose distance and area calculator.
static double textWidthMM(const QFont &font, const QString &text)
Calculate font width in millimeters for a string, including workarounds for QT font rendering issues...
QgsComposition * mComposition
static double fontHeightMM(const QFont &font)
Calculate font height in millimeters, including workarounds for QT font rendering issues The font hei...
QRectF evalItemRect(const QRectF &newRect, const bool resizeOnly=false)
Evaluates an item's bounding rect to consider data defined position and size of item and reference po...
bool _writeXML(QDomElement &itemElem, QDomDocument &doc) const
Writes parameter that are not subclass specific in document.
virtual void setItemRotation(const double r, const bool adjustPosition=false)
Sets the item rotation.
virtual void drawBackground(QPainter *p)
Draw background.
bool hasFrame() const
Whether this item has a frame or not.
static QgsProject * instance()
access to canonical QgsProject instance
void setMargin(const double m)
Sets the margin between the edge of the frame and the label contents.
virtual void setSceneRect(const QRectF &rectangle)
Sets this items bound in scene coordinates such that 1 item size units corresponds to 1 scene size un...
const CORE_EXPORT QString GEO_NONE
Constant that holds the string representation for "No ellips/No CRS".
void setText(const QString &text)
const QgsCoordinateReferenceSystem & crs() const
Returns layer's spatial reference system.
Q_DECL_DEPRECATED double margin()
Returns the margin between the edge of the frame and the label contents.
void refreshExpressionContext()
QgsAtlasComposition & atlasComposition()
void setExpressionContext(QgsFeature *feature, QgsVectorLayer *layer, QMap< QString, QVariant > substitutions=(QMap< QString, QVariant >()))
Sets the current feature, the current layer and a list of local variable substitutions for evaluating...
QgsVectorLayer * coverageLayer() const
Returns the coverage layer used for the atlas features.
QgsComposition::PlotStyle plotStyle() const
Represents a vector layer which manages a vector based data sets.
QString displayText() const
Returns the text as it appears on screen (with replaced data field)
double mItemRotation
Item rotation in degrees, clockwise.
void adjustSizeToText()
resizes the widget such that the text fits to the item.
void setEllipsoidalMode(bool flag)
sets whether coordinates must be projected to ellipsoid before measuring
virtual void setFrameEnabled(const bool drawFrame) override
Reimplemented to call prepareGeometryChange after toggling frame.
virtual QString displayName() const override
Get item display name.
static QString replaceExpressionText(const QString &action, const QgsFeature *feat, QgsVectorLayer *layer, const QMap< QString, QVariant > *substitutionMap=0, const QgsDistanceArea *distanceArea=0)
This function currently replaces each expression between [% and %] in the string with the result of i...
virtual void setFrameOutlineWidth(const double outlineWidth) override
Reimplemented to call prepareGeometryChange after changing outline width.
QString id() const
Get item's id (which is not necessarly unique)