23 #include <QCoreApplication>
25 #include <QDomElement>
35 mHtmlLoaded( false ), mMargin( 1.0 ), mFontColor( QColor( 0, 0, 0 ) ),
36 mHAlignment( Qt::AlignLeft ), mVAlignment( Qt::AlignTop ),
37 mExpressionFeature( 0 ), mExpressionLayer( 0 )
43 QString defaultFontString = settings.value(
"/Composer/defaultFont" ).toString();
44 if ( !defaultFontString.isEmpty() )
46 mFont.setFamily( defaultFontString );
50 mFont.setPointSizeF( 10 );
71 Q_UNUSED( itemStyle );
81 double penWidth =
hasFrame() ? pen().widthF() : 0;
82 QRectF painterRect( penWidth +
mMargin, penWidth +
mMargin, rect().width() - 2 * penWidth - 2 *
mMargin, rect().height() - 2 * penWidth - 2 *
mMargin );
90 QWebPage *webPage =
new QWebPage();
96 timeoutTimer.setSingleShot(
true );
99 QPalette palette = webPage->palette();
100 palette.setBrush( QPalette::Base, Qt::transparent );
101 webPage->setPalette( palette );
105 webPage->mainFrame()->setZoomFactor( 10.0 );
106 webPage->mainFrame()->setScrollBarPolicy( Qt::Horizontal, Qt::ScrollBarAlwaysOff );
107 webPage->mainFrame()->setScrollBarPolicy( Qt::Vertical, Qt::ScrollBarAlwaysOff );
115 webPage->settings()->setAttribute( QWebSettings::AutoLoadImages,
false );
119 connect( &timeoutTimer, SIGNAL( timeout() ), &loop, SLOT( quit() ) );
120 connect( webPage, SIGNAL( loadFinished(
bool ) ), &loop, SLOT( quit() ) );
128 webPage->mainFrame()->setHtml( textToDraw );
136 timeoutTimer.start( 20000 );
141 webPage->mainFrame()->render( painter );
145 painter->setPen( QPen( QColor(
mFontColor ) ) );
146 painter->setFont(
mFont );
148 QFontMetricsF fontSize(
mFont );
226 QString constant =
"$CURRENT_DATE";
227 int currentDatePos = text.indexOf( constant );
228 if ( currentDatePos != -1 )
232 int openingBracketPos = text.indexOf(
"(", currentDatePos );
233 int closingBracketPos = text.indexOf(
")", openingBracketPos + 1 );
234 if ( openingBracketPos != -1 &&
235 closingBracketPos != -1 &&
236 ( closingBracketPos - openingBracketPos ) > 1 &&
237 openingBracketPos == currentDatePos + constant.size() )
239 formatText = text.mid( openingBracketPos + 1, closingBracketPos - openingBracketPos - 1 );
240 text.replace( currentDatePos, closingBracketPos - currentDatePos + 1, QDate::currentDate().toString( formatText ) );
244 text.replace(
"$CURRENT_DATE", QDate::currentDate().toString() );
259 double penWidth =
hasFrame() ? pen().widthF() : 0;
261 double width = textWidth + 2 *
mMargin + 2 * penWidth + 1;
262 double height = fontHeight + 2 *
mMargin + 2 * penWidth;
269 setSceneRect( QRectF( pos().x() + xShift, pos().y() + yShift, width, height ) );
286 QDomElement composerLabelElem = doc.createElement(
"ComposerLabel" );
288 composerLabelElem.setAttribute(
"htmlState",
mHtmlState );
290 composerLabelElem.setAttribute(
"labelText",
mText );
291 composerLabelElem.setAttribute(
"margin", QString::number(
mMargin ) );
293 composerLabelElem.setAttribute(
"halign",
mHAlignment );
294 composerLabelElem.setAttribute(
"valign",
mVAlignment );
297 QDomElement labelFontElem = doc.createElement(
"LabelFont" );
298 labelFontElem.setAttribute(
"description",
mFont.toString() );
299 composerLabelElem.appendChild( labelFontElem );
302 QDomElement fontColorElem = doc.createElement(
"FontColor" );
303 fontColorElem.setAttribute(
"red",
mFontColor.red() );
304 fontColorElem.setAttribute(
"green",
mFontColor.green() );
305 fontColorElem.setAttribute(
"blue",
mFontColor.blue() );
306 composerLabelElem.appendChild( fontColorElem );
308 elem.appendChild( composerLabelElem );
309 return _writeXML( composerLabelElem, doc );
316 if ( itemElem.isNull() )
324 mText = itemElem.attribute(
"labelText" );
327 mHtmlState = itemElem.attribute(
"htmlState" ).toInt();
330 mMargin = itemElem.attribute(
"margin" ).toDouble();
333 mHAlignment = ( Qt::AlignmentFlag )( itemElem.attribute(
"halign" ).toInt() );
336 mVAlignment = ( Qt::AlignmentFlag )( itemElem.attribute(
"valign" ).toInt() );
339 QDomNodeList labelFontList = itemElem.elementsByTagName(
"LabelFont" );
340 if ( labelFontList.size() > 0 )
342 QDomElement labelFontElem = labelFontList.at( 0 ).toElement();
343 mFont.fromString( labelFontElem.attribute(
"description" ) );
347 QDomNodeList fontColorList = itemElem.elementsByTagName(
"FontColor" );
348 if ( fontColorList.size() > 0 )
350 QDomElement fontColorElem = fontColorList.at( 0 ).toElement();
351 int red = fontColorElem.attribute(
"red",
"0" ).toInt();
352 int green = fontColorElem.attribute(
"green",
"0" ).toInt();
353 int blue = fontColorElem.attribute(
"blue",
"0" ).toInt();
362 QDomNodeList composerItemList = itemElem.elementsByTagName(
"ComposerItem" );
363 if ( composerItemList.size() > 0 )
365 QDomElement composerItemElem = composerItemList.at( 0 ).toElement();
368 if ( composerItemElem.attribute(
"rotation",
"0" ).toDouble() != 0 )
371 setItemRotation( composerItemElem.attribute(
"rotation",
"0" ).toDouble() );
383 double currentWidth = rect().width();
384 double currentHeight = rect().height();
392 xShift = - ( newWidth - currentWidth ) / 2.0;
396 xShift = - ( newWidth - currentWidth );
400 yShift = -( newHeight - currentHeight ) / 2.0;
404 yShift = - ( newHeight - currentHeight );
411 yShift = -( newHeight - currentHeight ) / 2.0;
415 yShift = -( newHeight - currentHeight );
419 xShift = -( newWidth - currentWidth );
423 xShift = -( newWidth - currentWidth / 2.0 );
430 xShift = -( newWidth - currentWidth ) / 2.0;
434 xShift = -( newWidth - currentWidth );
438 yShift = ( newHeight - currentHeight ) / 2.0;
442 yShift = ( newHeight - currentHeight );
449 yShift = -( newHeight - currentHeight ) / 2.0;
453 yShift = -( newHeight - currentHeight );
457 xShift = -( newWidth - currentWidth );
461 xShift = -( newWidth - currentWidth / 2.0 );
QgsComposition::AtlasMode atlasMode() const
Returns the current atlas mode of the composition.
QgsVectorLayer * mExpressionLayer
void replaceDateText(QString &text) const
Replaces replace '$CURRENT_DATE<(FORMAT)>' with the current date (e.g.
A item that forms part of a map composition.
bool enabled() const
Returns whether the atlas generation is enabled.
virtual void drawFrame(QPainter *p)
Draw black frame around item.
void setFont(const QFont &f)
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
void itemChanged()
Used e.g.
bool _readXML(const QDomElement &itemElem, const QDomDocument &doc)
Reads parameter that are not subclass specific in document.
QgsFeature * mExpressionFeature
int itemPageNumber(const QgsComposerItem *) const
Returns on which page number (0-based) is displayed an item.
virtual void drawSelectionBoxes(QPainter *p)
Draw selection boxes around item.
double textWidthMillimeters(const QFont &font, const QString &text) const
Returns the font width in millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE...
void itemShiftAdjustSize(double newWidth, double newHeight, double &xShift, double &yShift) const
Helper function to calculate x/y shift for adjustSizeToText() depending on rotation, current size and alignment.
int printResolution() const
void paint(QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget)
Reimplementation of QCanvasItem::paint.
QMap< QString, QVariant > mSubstitutions
QgsComposerLabel(QgsComposition *composition)
void drawText(QPainter *p, double x, double y, const QString &text, const QFont &font) const
Draws Text.
Qt::AlignmentFlag mHAlignment
QgsComposition * mComposition
Graphics scene for map printing.
QgsFeature * currentFeature()
Returns the current atlas feature.
virtual void setItemRotation(double r, bool adjustPosition=false)
Sets the item rotation.
bool _writeXML(QDomElement &itemElem, QDomDocument &doc) const
Writes parameter that are not subclass specific in document.
static QgsNetworkAccessManager * instance()
returns a pointer to the single instance
Qt::AlignmentFlag mVAlignment
virtual void drawBackground(QPainter *p)
Draw background.
bool hasFrame() const
Whether this item has a frame or not.
double fontHeightMillimeters(const QFont &font) const
Returns the font height in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCAL...
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...
void setText(const QString &text)
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.
bool readXML(const QDomElement &itemElem, const QDomDocument &doc)
sets state from Dom document
static QString replaceExpressionText(const QString &action, const QgsFeature *feat, QgsVectorLayer *layer, const QMap< QString, QVariant > *substitutionMap=0)
This function currently replaces each expression between [% and %] in the string with the result of i...
void loadingHtmlFinished(bool)
void adjustSizeToText()
resizes the widget such that the text fits to the item.
bool writeXML(QDomElement &elem, QDomDocument &doc) const
stores state in Dom element