34 #include <QCoreApplication> 36 #include <QDomElement> 45 , mHtmlUnitsToMM( 1.0 )
46 , mHtmlLoaded( false )
49 , mFontColor(
QColor( 0, 0, 0 ) )
50 , mHAlignment(
Qt::AlignLeft )
51 , mVAlignment(
Qt::AlignTop )
52 , mExpressionLayer( nullptr )
53 , mDistanceArea( nullptr )
56 mHtmlUnitsToMM = htmlUnitsToMM();
61 if ( !defaultFontString.
isEmpty() )
84 mWebPage->setIdentifier(
tr(
"Composer label item" ) );
88 QPalette palette = mWebPage->palette();
89 palette.
setBrush( QPalette::Base, Qt::transparent );
90 mWebPage->setPalette( palette );
93 mWebPage->mainFrame()->setZoomFactor( 10.0 );
94 mWebPage->mainFrame()->setScrollBarPolicy( Qt::Horizontal, Qt::ScrollBarAlwaysOff );
95 mWebPage->mainFrame()->setScrollBarPolicy( Qt::Vertical, Qt::ScrollBarAlwaysOff );
97 connect( mWebPage, SIGNAL( loadFinished(
bool ) ), SLOT( loadingHtmlFinished(
bool ) ) );
102 delete mDistanceArea;
108 Q_UNUSED( itemStyle );
126 double xPenAdjust = mMarginX < 0 ? -penWidth : penWidth;
127 double yPenAdjust = mMarginY < 0 ? -penWidth : penWidth;
128 QRectF painterRect( xPenAdjust + mMarginX, yPenAdjust + mMarginY,
rect().width() - 2 * xPenAdjust - 2 * mMarginX,
rect().height() - 2 * yPenAdjust - 2 * mMarginY );
132 painter->
scale( 1.0 / mHtmlUnitsToMM / 10.0, 1.0 / mHtmlUnitsToMM / 10.0 );
144 QgsComposerUtils::drawText( painter, painterRect, textToDraw, mFont, mFontColor, mHAlignment, mVAlignment, Qt::TextWordWrap );
156 void QgsComposerLabel::contentChanged()
179 connect( mWebPage, SIGNAL( loadFinished(
bool ) ), &loop, SLOT( quit() ) );
184 connect( &timeoutTimer, SIGNAL( timeout() ), &loop, SLOT( quit() ) );
185 timeoutTimer.
start( 20000 );
194 void QgsComposerLabel::loadingHtmlFinished(
bool result )
200 double QgsComposerLabel::htmlUnitsToMM()
227 if ( state == mHtmlState )
244 mExpressionFeature.
reset( feature ?
new QgsFeature( *feature ) :
nullptr );
245 mExpressionLayer = layer;
246 mSubstitutions = substitutions;
271 mSubstitutions = substitutions;
276 mExpressionLayer =
nullptr;
277 mExpressionFeature.
reset();
308 replaceDateText( displayText );
314 if ( mExpressionFeature.
data() )
315 context->setFeature( *mExpressionFeature.
data() );
316 if ( mExpressionLayer )
317 context->setFields( mExpressionLayer->
fields() );
322 void QgsComposerLabel::replaceDateText(
QString&
text )
const 324 QString constant =
"$CURRENT_DATE";
325 int currentDatePos = text.
indexOf( constant );
326 if ( currentDatePos != -1 )
330 int openingBracketPos = text.
indexOf(
'(', currentDatePos );
331 int closingBracketPos = text.
indexOf(
')', openingBracketPos + 1 );
332 if ( openingBracketPos != -1 &&
333 closingBracketPos != -1 &&
334 ( closingBracketPos - openingBracketPos ) > 1 &&
335 openingBracketPos == currentDatePos + constant.
size() )
337 formatText = text.
mid( openingBracketPos + 1, closingBracketPos - openingBracketPos - 1 );
378 double width = textWidth + 2 * mMarginX + 2 * penWidth + 1;
379 double height = fontHeight + 2 * mMarginY + 2 * penWidth;
384 itemShiftAdjustSize( width, height, xShift, yShift );
405 composerLabelElem.
setAttribute(
"htmlState", mHtmlState );
410 composerLabelElem.
setAttribute(
"halign", mHAlignment );
411 composerLabelElem.
setAttribute(
"valign", mVAlignment );
425 return _writeXML( composerLabelElem, doc );
438 mText = itemElem.
attribute(
"labelText" );
444 bool marginXOk =
false;
445 bool marginYOk =
false;
448 if ( !marginXOk || !marginYOk )
457 mHAlignment =
static_cast< Qt::AlignmentFlag
>( itemElem.
attribute(
"halign" ).
toInt() );
460 mVAlignment =
static_cast< Qt::AlignmentFlag
>( itemElem.
attribute(
"valign" ).
toInt() );
467 if ( !fontColorList.
isEmpty() )
473 mFontColor =
QColor( red, green, blue );
477 mFontColor =
QColor( 0, 0, 0 );
482 if ( !composerItemList.
isEmpty() )
502 if ( !
id().isEmpty() )
509 return tr(
"<HTML label>" );
516 return tr(
"<label>" );
532 rectangle.
adjust( -penWidth, -penWidth, penWidth, penWidth );
536 rectangle.
adjust( mMarginX, 0, -mMarginX, 0 );
540 rectangle.
adjust( 0, mMarginY, 0, -mMarginY );
558 void QgsComposerLabel::itemShiftAdjustSize(
double newWidth,
double newHeight,
double& xShift,
double& yShift )
const 568 if ( mHAlignment == Qt::AlignHCenter )
570 xShift = - ( newWidth - currentWidth ) / 2.0;
572 else if ( mHAlignment == Qt::AlignRight )
574 xShift = - ( newWidth - currentWidth );
576 if ( mVAlignment == Qt::AlignVCenter )
578 yShift = -( newHeight - currentHeight ) / 2.0;
580 else if ( mVAlignment == Qt::AlignBottom )
582 yShift = - ( newHeight - currentHeight );
587 if ( mHAlignment == Qt::AlignHCenter )
589 yShift = -( newHeight - currentHeight ) / 2.0;
591 else if ( mHAlignment == Qt::AlignRight )
593 yShift = -( newHeight - currentHeight );
595 if ( mVAlignment == Qt::AlignTop )
597 xShift = -( newWidth - currentWidth );
599 else if ( mVAlignment == Qt::AlignVCenter )
601 xShift = -( newWidth - currentWidth / 2.0 );
606 if ( mHAlignment == Qt::AlignHCenter )
608 xShift = -( newWidth - currentWidth ) / 2.0;
610 else if ( mHAlignment == Qt::AlignLeft )
612 xShift = -( newWidth - currentWidth );
614 if ( mVAlignment == Qt::AlignVCenter )
616 yShift = ( newHeight - currentHeight ) / 2.0;
618 else if ( mVAlignment == Qt::AlignTop )
620 yShift = ( newHeight - currentHeight );
625 if ( mHAlignment == Qt::AlignHCenter )
627 yShift = -( newHeight - currentHeight ) / 2.0;
629 else if ( mHAlignment == Qt::AlignLeft )
631 yShift = -( newHeight - currentHeight );
633 if ( mVAlignment == Qt::AlignBottom )
635 xShift = -( newWidth - currentWidth );
637 else if ( mVAlignment == Qt::AlignVCenter )
639 xShift = -( newWidth - currentWidth / 2.0 );
644 QUrl QgsComposerLabel::createStylesheetUrl()
const 647 stylesheet +=
QString(
"body { margin: %1 %2;" ).
arg( qMax( mMarginY * mHtmlUnitsToMM, 0.0 ) ).
arg( qMax( mMarginX * mHtmlUnitsToMM, 0.0 ) );
650 stylesheet +=
QString(
"text-align: %1; }" ).
arg( mHAlignment == Qt::AlignLeft ?
"left" : mHAlignment == Qt::AlignRight ?
"right" :
"center" );
654 QUrl cssFileURL =
QUrl(
"data:text/css;charset=utf-8;base64," + ba.
toBase64() );
void setBrush(ColorRole role, const QBrush &brush)
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.
bool shouldDrawItem() const
Returns whether the item should be drawn in the current context.
void setRenderHint(RenderHint hint, bool on)
QDomNode appendChild(const QDomNode &newChild)
QString displayText() const
Returns the text as it appears on screen (with replaced data field)
QString attribute(const QString &name, const QString &defValue) const
void setSubstitutions(const QMap< QString, QVariant > &substitutions=(QMap< QString, QVariant >()))
Sets the list of local variable substitutions for evaluating expressions in label text...
QgsComposerModel * itemsModel()
Returns the items model attached to the composition.
void itemChanged()
Emitted when the item changes.
void setSourceCrs(long srsid)
sets source spatial reference system (by QGIS CRS)
int printResolution() const
void scale(qreal sx, qreal sy)
const QgsCoordinateReferenceSystem & crs() const
Returns layer's spatial reference system.
A item that forms part of a map composition.
QRectF evalItemRect(const QRectF &newRect, const bool resizeOnly=false, const QgsExpressionContext *context=nullptr)
Evaluates an item's bounding rect to consider data defined position and size of item and reference po...
QString simplified() const
bool hasCrsTransformEnabled() const
returns true if projections are enabled for this layer set
static void drawText(QPainter *painter, 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 ...
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...
bool _writeXML(QDomElement &itemElem, QDomDocument &doc) const
Writes parameter that are not subclass specific in document.
double toDouble(bool *ok) const
virtual QgsExpressionContext * createExpressionContext() const override
Creates an expression context relating to the item's current state.
QString tr(const char *sourceText, const char *disambiguation, int n)
void adjust(qreal dx1, qreal dy1, qreal dx2, qreal dy2)
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
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".
static QString asCSS(const QFont &font, double pointToPixelMultiplier=1.0)
Returns a CSS string representing the specified font as closely as possible.
QgsFields fields() const
Returns the list of fields of this layer.
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)
void setHtml(const QString &html, const QUrl &baseUrl)
void setUserStyleSheetUrl(const QUrl &location)
int exec(QFlags< QEventLoop::ProcessEventsFlag > flags)
virtual void drawSelectionBoxes(QPainter *p)
Draws additional graphics on selected items.
void setAttribute(const QString &name, const QString &value)
Q_DECL_DEPRECATED void setExpressionContext(QgsFeature *feature, QgsVectorLayer *layer, const QMap< QString, QVariant > &substitutions=(QMap< QString, QVariant >()))
Sets the current feature, the current layer and a list of local variable substitutions for evaluating...
int toInt(bool *ok, int base) const
QWebFrame * mainFrame() const
QgsComposerLabel(QgsComposition *composition)
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.
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.
const QgsMapSettings & mapSettings() const
Return setting of QGIS map canvas.
QByteArray & append(char ch)
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
void render(QPainter *painter)
QString mid(int position, int n) const
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.
static QgsProject * instance()
Returns the QgsProject singleton 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
static Q_DECL_DEPRECATED QString replaceExpressionText(const QString &action, const QgsFeature *feat, QgsVectorLayer *layer, const QMap< QString, QVariant > *substitutionMap=nullptr, const QgsDistanceArea *distanceArea=nullptr)
This function currently replaces each expression between [% and %] in the string with the result of i...
Q_DECL_DEPRECATED double margin()
Returns the margin between the edge of the frame and the label contents.
QgsVectorLayer * coverageLayer() const
Returns the coverage layer used for the atlas features.
void refreshExpressionContext()
QgsAtlasComposition & atlasComposition()
bool hasFrame() const
Whether this item has a frame or not.
QByteArray toBase64() const
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)
long srsid() const
Returns the SrsId, if available.
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 id() const
Get item's id (which is not necessarly unique)
QWebPage subclass which redirects JavaScript errors and console output to the QGIS message log...
const QgsCoordinateReferenceSystem & destinationCrs() const
returns CRS of destination coordinate reference system
double mItemRotation
Item rotation in degrees, clockwise.
bool enabled() const
Returns whether the atlas generation is enabled.
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.
QUrl fromLocalFile(const QString &localFile)
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.
QByteArray toUtf8() const
virtual void setFrameOutlineWidth(const double outlineWidth) override
Reimplemented to call prepareGeometryChange after changing outline width.