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() )
    91   Q_UNUSED( itemStyle );
   109   double xPenAdjust = mMarginX < 0 ? -penWidth : penWidth;
   110   double yPenAdjust = mMarginY < 0 ? -penWidth : penWidth;
   111   QRectF painterRect( xPenAdjust + mMarginX, yPenAdjust + mMarginY, 
rect().width() - 2 * xPenAdjust - 2 * mMarginX, 
rect().height() - 2 * yPenAdjust - 2 * mMarginY );
   117     painter->
scale( 1.0 / mHtmlUnitsToMM / 10.0, 1.0 / mHtmlUnitsToMM / 10.0 );
   128     palette.
setBrush( QPalette::Base, Qt::transparent );
   147     connect( &timeoutTimer, SIGNAL( timeout() ), &loop, SLOT( quit() ) );
   148     connect( webPage, SIGNAL( loadFinished( 
bool ) ), &loop, SLOT( quit() ) );
   154     connect( webPage, SIGNAL( loadFinished( 
bool ) ), SLOT( loadingHtmlFinished( 
bool ) ) );
   164       timeoutTimer.
start( 20000 );
   176     QgsComposerUtils::drawText( painter, painterRect, textToDraw, mFont, mFontColor, mHAlignment, mVAlignment, Qt::TextWordWrap );
   189 void QgsComposerLabel::loadingHtmlFinished( 
bool result )
   195 double QgsComposerLabel::htmlUnitsToMM()
   220   if ( state == mHtmlState )
   236   mExpressionFeature.
reset( feature ? 
new QgsFeature( *feature ) : 
nullptr );
   237   mExpressionLayer = layer;
   238   mSubstitutions = substitutions;
   262   mSubstitutions = substitutions;
   267   mExpressionLayer = 
nullptr;
   268   mExpressionFeature.
reset();
   298   replaceDateText( displayText );
   304   if ( mExpressionFeature.
data() )
   305     context->setFeature( *mExpressionFeature.
data() );
   306   if ( mExpressionLayer )
   307     context->setFields( mExpressionLayer->
fields() );
   312 void QgsComposerLabel::replaceDateText( 
QString& 
text )
 const   314   QString constant = 
"$CURRENT_DATE";
   315   int currentDatePos = text.
indexOf( constant );
   316   if ( currentDatePos != -1 )
   320     int openingBracketPos = text.
indexOf( 
'(', currentDatePos );
   321     int closingBracketPos = text.
indexOf( 
')', openingBracketPos + 1 );
   322     if ( openingBracketPos != -1 &&
   323          closingBracketPos != -1 &&
   324          ( closingBracketPos - openingBracketPos ) > 1 &&
   325          openingBracketPos == currentDatePos + constant.
size() )
   327       formatText = text.
mid( openingBracketPos + 1, closingBracketPos - openingBracketPos - 1 );
   368   double width = textWidth + 2 * mMarginX + 2 * penWidth + 1;
   369   double height = fontHeight + 2 * mMarginY + 2 * penWidth;
   374   itemShiftAdjustSize( width, height, xShift, yShift );
   397   composerLabelElem.
setAttribute( 
"htmlState", mHtmlState );
   402   composerLabelElem.
setAttribute( 
"halign", mHAlignment );
   403   composerLabelElem.
setAttribute( 
"valign", mVAlignment );
   417   return _writeXML( composerLabelElem, doc );
   432   mText = itemElem.
attribute( 
"labelText" );
   438   bool marginXOk = 
false;
   439   bool marginYOk = 
false;
   442   if ( !marginXOk || !marginYOk )
   451   mHAlignment = 
static_cast< Qt::AlignmentFlag 
>( itemElem.
attribute( 
"halign" ).
toInt() );
   454   mVAlignment = 
static_cast< Qt::AlignmentFlag 
>( itemElem.
attribute( 
"valign" ).
toInt() );
   461   if ( !fontColorList.
isEmpty() )
   467     mFontColor = 
QColor( red, green, blue );
   471     mFontColor = 
QColor( 0, 0, 0 );
   476   if ( !composerItemList.
isEmpty() )
   495   if ( !
id().isEmpty() )
   502     return tr( 
"<HTML label>" );
   509     return tr( 
"<label>" );
   525   rectangle.
adjust( -penWidth, -penWidth, penWidth, penWidth );
   529     rectangle.
adjust( mMarginX, 0, -mMarginX, 0 );
   533     rectangle.
adjust( 0, mMarginY, 0, -mMarginY );
   551 void QgsComposerLabel::itemShiftAdjustSize( 
double newWidth, 
double newHeight, 
double& xShift, 
double& yShift )
 const   561     if ( mHAlignment == Qt::AlignHCenter )
   563       xShift = - ( newWidth - currentWidth ) / 2.0;
   565     else if ( mHAlignment == Qt::AlignRight )
   567       xShift = - ( newWidth - currentWidth );
   569     if ( mVAlignment == Qt::AlignVCenter )
   571       yShift = -( newHeight - currentHeight ) / 2.0;
   573     else if ( mVAlignment == Qt::AlignBottom )
   575       yShift = - ( newHeight - currentHeight );
   580     if ( mHAlignment == Qt::AlignHCenter )
   582       yShift = -( newHeight  - currentHeight ) / 2.0;
   584     else if ( mHAlignment == Qt::AlignRight )
   586       yShift = -( newHeight  - currentHeight );
   588     if ( mVAlignment == Qt::AlignTop )
   590       xShift = -( newWidth - currentWidth );
   592     else if ( mVAlignment == Qt::AlignVCenter )
   594       xShift = -( newWidth - currentWidth / 2.0 );
   599     if ( mHAlignment == Qt::AlignHCenter )
   601       xShift = -( newWidth - currentWidth ) / 2.0;
   603     else if ( mHAlignment == Qt::AlignLeft )
   605       xShift = -( newWidth - currentWidth );
   607     if ( mVAlignment == Qt::AlignVCenter )
   609       yShift = ( newHeight - currentHeight ) / 2.0;
   611     else if ( mVAlignment == Qt::AlignTop )
   613       yShift = ( newHeight - currentHeight );
   618     if ( mHAlignment == Qt::AlignHCenter )
   620       yShift = -( newHeight  - currentHeight ) / 2.0;
   622     else if ( mHAlignment == Qt::AlignLeft )
   624       yShift = -( newHeight  - currentHeight );
   626     if ( mVAlignment == Qt::AlignBottom )
   628       xShift = -( newWidth - currentWidth );
   630     else if ( mVAlignment == Qt::AlignVCenter )
   632       xShift = -( newWidth - currentWidth / 2.0 );
 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. 
 
void setRenderHint(RenderHint hint, bool on)
 
QDomNode appendChild(const QDomNode &newChild)
 
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. 
 
QgsFields fields() const 
Returns the list of fields of this layer. 
 
long srsid() const 
Returns the SrsId, if available. 
 
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. 
 
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
 
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 ...
 
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
 
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)
 
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)
 
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)
 
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...
 
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. 
 
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
 
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. 
 
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
 
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...
 
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()
 
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. 
 
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)