21 #include <QCoreApplication>
23 #include <QDomElement>
31 mHtmlLoaded( false ), mMargin( 1.0 ), mFontColor( QColor( 0, 0, 0 ) ),
32 mHAlignment( Qt::AlignLeft ), mVAlignment( Qt::AlignTop ),
33 mExpressionFeature( 0 ), mExpressionLayer( 0 )
39 QString defaultFontString = settings.value(
"/Composer/defaultFont" ).toString();
40 if ( !defaultFontString.isEmpty() )
42 mFont.setFamily( defaultFontString );
46 mFont.setPointSizeF( 10 );
62 Q_UNUSED( itemStyle );
72 double penWidth = pen().widthF();
73 QRectF painterRect( penWidth +
mMargin, penWidth +
mMargin, rect().width() - 2 * penWidth - 2 *
mMargin, rect().height() - 2 * penWidth - 2 *
mMargin );
81 QWebPage* webPage =
new QWebPage();
86 timeoutTimer.setSingleShot(
true );
89 QPalette palette = webPage->palette();
90 palette.setBrush( QPalette::Base, Qt::transparent );
91 webPage->setPalette( palette );
95 webPage->mainFrame()->setZoomFactor( 10.0 );
96 webPage->mainFrame()->setScrollBarPolicy( Qt::Horizontal, Qt::ScrollBarAlwaysOff );
97 webPage->mainFrame()->setScrollBarPolicy( Qt::Vertical, Qt::ScrollBarAlwaysOff );
105 webPage->settings()->setAttribute( QWebSettings::AutoLoadImages,
false );
109 connect( &timeoutTimer, SIGNAL( timeout() ), &loop, SLOT( quit() ) );
110 connect( webPage, SIGNAL( loadFinished(
bool ) ), &loop, SLOT( quit() ) );
118 webPage->mainFrame()->setHtml( textToDraw );
126 timeoutTimer.start( 20000 );
131 webPage->mainFrame()->render( painter );
135 painter->setPen( QPen( QColor(
mFontColor ) ) );
136 painter->setFont(
mFont );
138 QFontMetricsF fontSize(
mFont );
199 QString constant =
"$CURRENT_DATE";
200 int currentDatePos = text.indexOf( constant );
201 if ( currentDatePos != -1 )
205 int openingBracketPos = text.indexOf(
"(", currentDatePos );
206 int closingBracketPos = text.indexOf(
")", openingBracketPos + 1 );
207 if ( openingBracketPos != -1 &&
208 closingBracketPos != -1 &&
209 ( closingBracketPos - openingBracketPos ) > 1 &&
210 openingBracketPos == currentDatePos + constant.size() )
212 formatText = text.mid( openingBracketPos + 1, closingBracketPos - openingBracketPos - 1 );
213 text.replace( currentDatePos, closingBracketPos - currentDatePos + 1, QDate::currentDate().toString( formatText ) );
217 text.replace(
"$CURRENT_DATE", QDate::currentDate().toString() );
232 double width = textWidth + 2 *
mMargin + 2 * pen().widthF() + 1;
233 double height = fontAscent + 2 *
mMargin + 2 * pen().widthF() + 1;
240 setSceneRect( QRectF( pos().x() + xShift, pos().y() + yShift, width, height ) );
257 QDomElement composerLabelElem = doc.createElement(
"ComposerLabel" );
259 composerLabelElem.setAttribute(
"htmlState",
mHtmlState );
261 composerLabelElem.setAttribute(
"labelText",
mText );
262 composerLabelElem.setAttribute(
"margin", QString::number(
mMargin ) );
264 composerLabelElem.setAttribute(
"halign",
mHAlignment );
265 composerLabelElem.setAttribute(
"valign",
mVAlignment );
268 QDomElement labelFontElem = doc.createElement(
"LabelFont" );
269 labelFontElem.setAttribute(
"description",
mFont.toString() );
270 composerLabelElem.appendChild( labelFontElem );
273 QDomElement fontColorElem = doc.createElement(
"FontColor" );
274 fontColorElem.setAttribute(
"red",
mFontColor.red() );
275 fontColorElem.setAttribute(
"green",
mFontColor.green() );
276 fontColorElem.setAttribute(
"blue",
mFontColor.blue() );
277 composerLabelElem.appendChild( fontColorElem );
279 elem.appendChild( composerLabelElem );
280 return _writeXML( composerLabelElem, doc );
287 if ( itemElem.isNull() )
295 mText = itemElem.attribute(
"labelText" );
298 mHtmlState = itemElem.attribute(
"htmlState" ).toInt();
301 mMargin = itemElem.attribute(
"margin" ).toDouble();
304 mHAlignment = ( Qt::AlignmentFlag )( itemElem.attribute(
"halign" ).toInt() );
307 mVAlignment = ( Qt::AlignmentFlag )( itemElem.attribute(
"valign" ).toInt() );
310 QDomNodeList labelFontList = itemElem.elementsByTagName(
"LabelFont" );
311 if ( labelFontList.size() > 0 )
313 QDomElement labelFontElem = labelFontList.at( 0 ).toElement();
314 mFont.fromString( labelFontElem.attribute(
"description" ) );
318 QDomNodeList fontColorList = itemElem.elementsByTagName(
"FontColor" );
319 if ( fontColorList.size() > 0 )
321 QDomElement fontColorElem = fontColorList.at( 0 ).toElement();
322 int red = fontColorElem.attribute(
"red",
"0" ).toInt();
323 int green = fontColorElem.attribute(
"green",
"0" ).toInt();
324 int blue = fontColorElem.attribute(
"blue",
"0" ).toInt();
333 QDomNodeList composerItemList = itemElem.elementsByTagName(
"ComposerItem" );
334 if ( composerItemList.size() > 0 )
336 QDomElement composerItemElem = composerItemList.at( 0 ).toElement();
339 if ( composerItemElem.attribute(
"rotation",
"0" ).toDouble() != 0 )
342 setItemRotation( composerItemElem.attribute(
"rotation",
"0" ).toDouble() );
354 double currentWidth = rect().width();
355 double currentHeight = rect().height();
363 xShift = - ( newWidth - currentWidth ) / 2.0;
367 xShift = - ( newWidth - currentWidth );
371 yShift = -( newHeight - currentHeight ) / 2.0;
375 yShift = - ( newHeight - currentHeight );
382 yShift = -( newHeight - currentHeight ) / 2.0;
386 yShift = -( newHeight - currentHeight );
390 xShift = -( newWidth - currentWidth );
394 xShift = -( newWidth - currentWidth / 2.0 );
401 xShift = -( newWidth - currentWidth ) / 2.0;
405 xShift = -( newWidth - currentWidth );
409 yShift = ( newHeight - currentHeight ) / 2.0;
413 yShift = ( newHeight - currentHeight );
420 yShift = -( newHeight - currentHeight ) / 2.0;
424 yShift = -( newHeight - currentHeight );
428 xShift = -( newWidth - currentWidth );
432 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.
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.
Qt::AlignmentFlag mVAlignment
virtual void drawBackground(QPainter *p)
Draw background.
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)
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
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)
double fontAscentMillimeters(const QFont &font) const
Returns the font ascent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCAL...
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