33 #include <QCoreApplication>
35 #include <QDomElement>
44 , mHtmlUnitsToMM( 1.0 )
45 , mHtmlLoaded( false )
48 , mFontColor(
QColor( 0, 0, 0 ) )
49 , mHAlignment( Qt::AlignLeft )
50 , mVAlignment( Qt::AlignTop )
51 , mExpressionFeature( 0 )
52 , mExpressionLayer( 0 )
56 mHtmlUnitsToMM = htmlUnitsToMM();
61 if ( !defaultFontString.
isEmpty() )
94 Q_UNUSED( itemStyle );
112 double xPenAdjust = mMarginX < 0 ? -penWidth : penWidth;
113 double yPenAdjust = mMarginY < 0 ? -penWidth : penWidth;
114 QRectF painterRect( xPenAdjust + mMarginX, yPenAdjust + mMarginY,
rect().width() - 2 * xPenAdjust - 2 * mMarginX,
rect().height() - 2 * yPenAdjust - 2 * mMarginY );
120 painter->
scale( 1.0 / mHtmlUnitsToMM / 10.0, 1.0 / mHtmlUnitsToMM / 10.0 );
131 palette.
setBrush( QPalette::Base, Qt::transparent );
150 connect( &timeoutTimer, SIGNAL( timeout() ), &loop, SLOT( quit() ) );
151 connect( webPage, SIGNAL( loadFinished(
bool ) ), &loop, SLOT( quit() ) );
157 connect( webPage, SIGNAL( loadFinished(
bool ) ), SLOT( loadingHtmlFinished(
bool ) ) );
167 timeoutTimer.
start( 20000 );
179 QgsComposerUtils::drawText( painter, painterRect, textToDraw, mFont, mFontColor, mHAlignment, mVAlignment, Qt::TextWordWrap );
192 void QgsComposerLabel::loadingHtmlFinished(
bool result )
198 double QgsComposerLabel::htmlUnitsToMM()
223 if ( state == mHtmlState )
239 mExpressionFeature = feature;
240 mExpressionLayer = layer;
241 mSubstitutions = substitutions;
283 replaceDateText( displayText );
289 void QgsComposerLabel::replaceDateText(
QString& text )
const
291 QString constant =
"$CURRENT_DATE";
292 int currentDatePos = text.
indexOf( constant );
293 if ( currentDatePos != -1 )
297 int openingBracketPos = text.
indexOf(
"(", currentDatePos );
298 int closingBracketPos = text.
indexOf(
")", openingBracketPos + 1 );
299 if ( openingBracketPos != -1 &&
300 closingBracketPos != -1 &&
301 ( closingBracketPos - openingBracketPos ) > 1 &&
302 openingBracketPos == currentDatePos + constant.
size() )
304 formatText = text.
mid( openingBracketPos + 1, closingBracketPos - openingBracketPos - 1 );
345 double width = textWidth + 2 * mMarginX + 2 * penWidth + 1;
346 double height = fontHeight + 2 * mMarginY + 2 * penWidth;
351 itemShiftAdjustSize( width, height, xShift, yShift );
374 composerLabelElem.
setAttribute(
"htmlState", mHtmlState );
379 composerLabelElem.
setAttribute(
"halign", mHAlignment );
380 composerLabelElem.
setAttribute(
"valign", mVAlignment );
394 return _writeXML( composerLabelElem, doc );
409 mText = itemElem.
attribute(
"labelText" );
415 bool marginXOk =
false;
416 bool marginYOk =
false;
419 if ( !marginXOk || !marginYOk )
428 mHAlignment = ( Qt::AlignmentFlag )( itemElem.
attribute(
"halign" ).
toInt() );
431 mVAlignment = ( Qt::AlignmentFlag )( itemElem.
attribute(
"valign" ).
toInt() );
438 if ( fontColorList.
size() > 0 )
444 mFontColor =
QColor( red, green, blue );
448 mFontColor =
QColor( 0, 0, 0 );
453 if ( composerItemList.
size() > 0 )
472 if ( !
id().isEmpty() )
479 return tr(
"<HTML label>" );
486 return tr(
"<label>" );
502 rectangle.
adjust( -penWidth, -penWidth, penWidth, penWidth );
506 rectangle.
adjust( mMarginX, 0, -mMarginX, 0 );
510 rectangle.
adjust( 0, mMarginY, 0, -mMarginY );
528 void QgsComposerLabel::itemShiftAdjustSize(
double newWidth,
double newHeight,
double& xShift,
double& yShift )
const
538 if ( mHAlignment == Qt::AlignHCenter )
540 xShift = - ( newWidth - currentWidth ) / 2.0;
542 else if ( mHAlignment == Qt::AlignRight )
544 xShift = - ( newWidth - currentWidth );
546 if ( mVAlignment == Qt::AlignVCenter )
548 yShift = -( newHeight - currentHeight ) / 2.0;
550 else if ( mVAlignment == Qt::AlignBottom )
552 yShift = - ( newHeight - currentHeight );
557 if ( mHAlignment == Qt::AlignHCenter )
559 yShift = -( newHeight - currentHeight ) / 2.0;
561 else if ( mHAlignment == Qt::AlignRight )
563 yShift = -( newHeight - currentHeight );
565 if ( mVAlignment == Qt::AlignTop )
567 xShift = -( newWidth - currentWidth );
569 else if ( mVAlignment == Qt::AlignVCenter )
571 xShift = -( newWidth - currentWidth / 2.0 );
576 if ( mHAlignment == Qt::AlignHCenter )
578 xShift = -( newWidth - currentWidth ) / 2.0;
580 else if ( mHAlignment == Qt::AlignLeft )
582 xShift = -( newWidth - currentWidth );
584 if ( mVAlignment == Qt::AlignVCenter )
586 yShift = ( newHeight - currentHeight ) / 2.0;
588 else if ( mVAlignment == Qt::AlignTop )
590 yShift = ( newHeight - currentHeight );
595 if ( mHAlignment == Qt::AlignHCenter )
597 yShift = -( newHeight - currentHeight ) / 2.0;
599 else if ( mHAlignment == Qt::AlignLeft )
601 yShift = -( newHeight - currentHeight );
603 if ( mVAlignment == Qt::AlignBottom )
605 xShift = -( newWidth - currentWidth );
607 else if ( mVAlignment == Qt::AlignVCenter )
609 xShift = -( newWidth - currentWidth / 2.0 );
void setBrush(ColorRole role, const QBrush &brush)
QgsComposition::AtlasMode atlasMode() const
Returns the current atlas mode of the composition.
QDomNodeList elementsByTagName(const QString &tagname) const
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
void setHtmlState(int state)
bool readXML(const QDomElement &itemElem, const QDomDocument &doc) override
sets state from Dom document
void setRenderHint(RenderHint hint, bool on)
QDomNode appendChild(const QDomNode &newChild)
QString attribute(const QString &name, const QString &defValue) const
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)
void scale(qreal sx, qreal sy)
A item that forms part of a map composition.
QString simplified() const
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...
double toDouble(bool *ok) const
QString tr(const char *sourceText, const char *disambiguation, int n)
void adjust(qreal dx1, qreal dy1, qreal dx2, qreal dy2)
void update(const QRectF &rect)
void updateItemDisplayName(QgsComposerItem *item)
Must be called when an item's display name is modified.
const QString GEO_NONE
Constant that holds the string representation for "No ellips/No CRS".
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.
QDomElement toElement() const
void setFont(const QFont &font)
QString number(int n, int base)
int itemPageNumber(const QgsComposerItem *) const
Returns on which page number (0-based) is displayed an item.
void setHtml(const QString &html, const QUrl &baseUrl)
int exec(QFlags< QEventLoop::ProcessEventsFlag > flags)
virtual void drawSelectionBoxes(QPainter *p)
Draws additional graphics on selected items.
int printResolution() const
void setAttribute(const QString &name, const QString &value)
const QgsCoordinateReferenceSystem & destinationCrs() const
returns CRS of destination coordinate reference system
int toInt(bool *ok, int base) const
QWebFrame * mainFrame() const
The QWebPage class is a collection of stubs to mimic the API of a QWebPage on systems where QtWebkit ...
QgsComposerLabel(QgsComposition *composition)
void setZoomFactor(qreal factor)
static bool setFromXmlChildNode(QFont &font, const QDomElement &element, const QString &childNode)
Sets the properties of a font to match the properties stored in an XML child node.
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.
void prepareGeometryChange()
Graphics scene for map printing.
QgsFeature * currentFeature()
Returns the current atlas feature.
void setAttribute(WebAttribute attribute, bool on)
void setMarginY(const double margin)
Sets the vertical margin between the edge of the frame and the label contents.
QWebSettings * settings() const
void setPointSizeF(qreal pointSize)
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
QString & replace(int position, int n, QChar after)
static double fontHeightMM(const QFont &font)
Calculate font height in millimeters, including workarounds for QT font rendering issues The font hei...
QVariant value(const QString &key, const QVariant &defaultValue) const
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...
void render(QPainter *painter)
QString mid(int position, int n) const
bool _writeXML(QDomElement &itemElem, QDomDocument &doc) const
Writes parameter that are not subclass specific in document.
char * toString(const T &value)
static QgsNetworkAccessManager * instance()
returns a pointer to the single instance
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 setFamily(const QString &family)
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...
void setText(const QString &text)
QString left(int n) const
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.
static QDomElement toXmlElement(const QFont &font, QDomDocument &document, const QString &elementName)
Returns a DOM element containing the properties of the font.
QDomElement createElement(const QString &tagName)
QgsComposition::PlotStyle plotStyle() const
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Represents a vector layer which manages a vector based data sets.
void setViewportSize(const QSize &size) const
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QString displayText() const
Returns the text as it appears on screen (with replaced data field)
void setNetworkAccessManager(QNetworkAccessManager *manager)
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
QDomNode at(int index) const
virtual void setFrameEnabled(const bool drawFrame) override
Reimplemented to call prepareGeometryChange after toggling frame.
void setSingleShot(bool singleShot)
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)