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 )
37 mFont.setPointSizeF( 10 );
46 Q_UNUSED( itemStyle );
56 double penWidth = pen().widthF();
58 painter->translate( rect().width() / 2.0, rect().height() / 2.0 );
66 QWebPage* webPage =
new QWebPage();
71 timeoutTimer.setSingleShot(
true );
74 QPalette palette = webPage->palette();
75 palette.setBrush( QPalette::Base, Qt::transparent );
76 webPage->setPalette( palette );
80 webPage->mainFrame()->setZoomFactor( 10.0 );
81 webPage->mainFrame()->setScrollBarPolicy( Qt::Horizontal, Qt::ScrollBarAlwaysOff );
82 webPage->mainFrame()->setScrollBarPolicy( Qt::Vertical, Qt::ScrollBarAlwaysOff );
90 webPage->settings()->setAttribute( QWebSettings::AutoLoadImages,
false );
94 connect( &timeoutTimer, SIGNAL( timeout() ), &loop, SLOT( quit() ) );
95 connect( webPage, SIGNAL( loadFinished(
bool ) ), &loop, SLOT( quit() ) );
111 timeoutTimer.start( 20000 );
116 webPage->mainFrame()->render( painter );
120 painter->setPen( QPen( QColor(
mFontColor ) ) );
121 painter->setFont(
mFont );
123 QFontMetricsF fontSize(
mFont );
184 QString constant =
"$CURRENT_DATE";
185 int currentDatePos = text.indexOf( constant );
186 if ( currentDatePos != -1 )
190 int openingBracketPos = text.indexOf(
"(", currentDatePos );
191 int closingBracketPos = text.indexOf(
")", openingBracketPos + 1 );
192 if ( openingBracketPos != -1 &&
193 closingBracketPos != -1 &&
194 ( closingBracketPos - openingBracketPos ) > 1 &&
195 openingBracketPos == currentDatePos + constant.size() )
197 formatText = text.mid( openingBracketPos + 1, closingBracketPos - openingBracketPos - 1 );
198 text.replace( currentDatePos, closingBracketPos - currentDatePos + 1, QDate::currentDate().toString( formatText ) );
202 text.replace(
"$CURRENT_DATE", QDate::currentDate().toString() );
245 double x = transform().dx() + rect().width() / 2.0 - width / 2.0;
246 double y = transform().dy() + rect().height() / 2.0 - height / 2.0;
252 if ( rectangle.width() != rect().width() || rectangle.height() != rect().height() )
254 double textBoxWidth = rectangle.width();
255 double textBoxHeight = rectangle.height();
272 QDomElement composerLabelElem = doc.createElement(
"ComposerLabel" );
274 composerLabelElem.setAttribute(
"htmlState",
mHtmlState );
276 composerLabelElem.setAttribute(
"labelText",
mText );
277 composerLabelElem.setAttribute(
"margin", QString::number(
mMargin ) );
279 composerLabelElem.setAttribute(
"halign",
mHAlignment );
280 composerLabelElem.setAttribute(
"valign",
mVAlignment );
283 QDomElement labelFontElem = doc.createElement(
"LabelFont" );
284 labelFontElem.setAttribute(
"description",
mFont.toString() );
285 composerLabelElem.appendChild( labelFontElem );
288 QDomElement fontColorElem = doc.createElement(
"FontColor" );
289 fontColorElem.setAttribute(
"red",
mFontColor.red() );
290 fontColorElem.setAttribute(
"green",
mFontColor.green() );
291 fontColorElem.setAttribute(
"blue",
mFontColor.blue() );
292 composerLabelElem.appendChild( fontColorElem );
294 elem.appendChild( composerLabelElem );
295 return _writeXML( composerLabelElem, doc );
302 if ( itemElem.isNull() )
310 mText = itemElem.attribute(
"labelText" );
313 mHtmlState = itemElem.attribute(
"htmlState" ).toInt();
316 mMargin = itemElem.attribute(
"margin" ).toDouble();
319 mHAlignment = ( Qt::AlignmentFlag )( itemElem.attribute(
"halign" ).toInt() );
322 mVAlignment = ( Qt::AlignmentFlag )( itemElem.attribute(
"valign" ).toInt() );
325 QDomNodeList labelFontList = itemElem.elementsByTagName(
"LabelFont" );
326 if ( labelFontList.size() > 0 )
328 QDomElement labelFontElem = labelFontList.at( 0 ).toElement();
329 mFont.fromString( labelFontElem.attribute(
"description" ) );
333 QDomNodeList fontColorList = itemElem.elementsByTagName(
"FontColor" );
334 if ( fontColorList.size() > 0 )
336 QDomElement fontColorElem = fontColorList.at( 0 ).toElement();
337 int red = fontColorElem.attribute(
"red",
"0" ).toInt();
338 int green = fontColorElem.attribute(
"green",
"0" ).toInt();
339 int blue = fontColorElem.attribute(
"blue",
"0" ).toInt();
348 QDomNodeList composerItemList = itemElem.elementsByTagName(
"ComposerItem" );
349 if ( composerItemList.size() > 0 )
351 QDomElement composerItemElem = composerItemList.at( 0 ).toElement();
361 double currentWidth = rect().width();
362 double currentHeight = rect().height();
370 xShift = - ( newWidth - currentWidth ) / 2.0;
374 xShift = - ( newWidth - currentWidth );
378 yShift = -( newHeight - currentHeight ) / 2.0;
382 yShift = - ( newHeight - currentHeight );
389 yShift = -( newHeight - currentHeight ) / 2.0;
393 yShift = -( newHeight - currentHeight );
397 xShift = -( newWidth - currentWidth );
401 xShift = -( newWidth - currentWidth / 2.0 );
408 xShift = -( newWidth - currentWidth ) / 2.0;
412 xShift = -( newWidth - currentWidth );
416 yShift = ( newHeight - currentHeight ) / 2.0;
420 yShift = ( newHeight - currentHeight );
427 yShift = -( newHeight - currentHeight ) / 2.0;
431 yShift = -( newHeight - currentHeight );
435 xShift = -( newWidth - currentWidth );
439 xShift = -( newWidth - currentWidth / 2.0 );
QgsVectorLayer * mExpressionLayer
virtual void setRotation(double r)
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...
static QString replaceExpressionText(QString action, 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...
double mRotation
Item rotation in degrees, clockwise.
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
virtual void setRotation(double r)
void paint(QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget)
Reimplementation of QCanvasItem::paint.
void setSceneRect(const QRectF &rectangle)
Sets this items bound in scene coordinates such that 1 item size units corresponds to 1 scene size un...
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
double mTextBoxHeight
Height of the text box.
Graphics scene for map printing.
double mTextBoxWidth
Width of the text box.
void sizeChangedByRotation(double &width, double &height)
Calculates width / height of the bounding box of a rotated rectangle (mRotation)
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...
bool imageSizeConsideringRotation(double &width, double &height) const
Calculates width and hight of the picture (in mm) such that it fits into the item frame with the give...
void setText(const QString &text)
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...
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)
bool readXML(const QDomElement &itemElem, const QDomDocument &doc)
sets state from Dom document
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