22 #include <QFontMetrics>
26 #include <QDomElement>
44 #define M_PI 4*atan(1.0)
49 , mMaxScale( 100000000 )
50 , mScaleBasedVisibility( false )
56 mLabelFieldIdx[i] = -1;
63 delete mLabelAttributes;
68 int idx = mLabelFieldIdx[attr];
76 Q_UNUSED( classAttributes );
89 double x1 = point.
x();
91 double x2 = point.
x();
92 double scale = ( x2 - x1 ) * 0.001;
98 text = mLabelAttributes->
text();
120 size = mLabelAttributes->
size();
129 sizeType = mLabelAttributes->
sizeType();
133 if ( value.
compare(
"mapunits" ) == 0 )
144 double sizeMM = size * 0.3527;
152 if ((
int )size <= 0 )
210 bool useOverridePoint =
false;
215 useOverridePoint =
true;
221 useOverridePoint =
true;
234 for (
int i = 0; i < texts.
size(); i++ )
236 int w = fm.
width( texts[i] );
245 width = fm.
width( text );
255 alignment = mLabelAttributes->
alignment();
264 alignment |= Qt::AlignLeft;
265 else if ( value.
contains(
"right" ) )
266 alignment |= Qt::AlignRight;
268 alignment |= Qt::AlignHCenter;
271 alignment |= Qt::AlignBottom;
273 alignment |= Qt::AlignTop;
275 alignment |= Qt::AlignVCenter;
278 if ( alignment & Qt::AlignLeft )
282 else if ( alignment & Qt::AlignHCenter )
286 else if ( alignment & Qt::AlignRight )
291 if ( alignment & Qt::AlignBottom )
295 else if ( alignment & Qt::AlignVCenter )
299 else if ( alignment & Qt::AlignTop )
305 double xoffset, yoffset;
309 xoffset = mLabelAttributes->
xOffset();
318 yoffset = mLabelAttributes->
yOffset();
333 xoffset = xoffset * 0.3527 * renderContext.
scaleFactor();
334 yoffset = yoffset * 0.3527 * renderContext.
scaleFactor();
342 ang = mLabelAttributes->
angle();
353 if ( useOverridePoint )
355 renderLabel( renderContext, overridePoint, text, font, pen, dx, dy,
356 xoffset, yoffset, ang, width, height, alignment );
360 std::vector<labelpoint> points;
361 labelPoint( points, feature );
362 for ( uint i = 0; i < points.size(); ++i )
364 renderLabel( renderContext, points[i].p, text, font, pen, dx, dy,
365 xoffset, yoffset, mLabelAttributes->
angleIsAuto() ? points[i].angle : ang, width, height, alignment );
374 double xoffset,
double yoffset,
376 int width,
int height,
int alignment )
390 QgsDebugMsg(
"Caught transform error. Skipping rendering this label" );
397 double x = point.
x();
398 double y = point.
y();
400 double rad = ang *
M_PI / 180;
402 x = x + xoffset * cos( rad ) - yoffset * sin( rad );
403 y = y - xoffset * sin( rad ) - yoffset * cos( rad );
427 painter->
setPen( bufferPen );
429 double bufferStepSize;
439 for (
double i = dx - myBufferSize; i <= dx + myBufferSize; i += bufferStepSize )
441 for (
double j = dy - myBufferSize; j <= dy + myBufferSize; j += bufferStepSize )
444 painter->
drawText(
QRectF( i, j - height, width, height ), alignment, text );
453 painter->
drawText( dx, dy - height, width, height, alignment, text );
463 if ( mLabelFieldIdx[i] == -1 )
468 if ( *it == mLabelFieldIdx[i] )
476 fields.
append( mLabelFieldIdx[i] );
496 mLabelFieldIdx[attr] = fieldIndex;
504 int fieldIndex = mLabelFieldIdx[attr];
505 if ( fieldIndex < 0 || fieldIndex >= mFields.
count() )
507 return mFields[fieldIndex].name();
512 return mLabelAttributes;
515 void QgsLabel::labelPoint( std::vector<labelpoint>& points,
QgsFeature & feature )
518 const unsigned char *geom = geometry->
asWkb();
519 size_t geomlen = geometry->
wkbSize();
532 labelPoint( point, geom, geomlen );
533 points.push_back( point );
545 Q_ASSERT( 1 +
sizeof( wkbType ) +
sizeof(
int ) <= geomlen );
546 geom += 1 +
sizeof( wkbType );
547 int nFeatures = *(
unsigned int * )geom;
548 geom +=
sizeof( int );
550 const unsigned char *feature = geom;
551 for (
int i = 0; i < nFeatures && feature; ++i )
553 feature = labelPoint( point, feature, geom + geomlen - feature );
554 points.push_back( point );
563 const unsigned char* QgsLabel::labelPoint( labelpoint& point,
const unsigned char *geom,
size_t geomlen )
566 Q_ASSERT(
sizeof(
int ) == 4 );
568 Q_ASSERT(
sizeof(
double ) == 8 );
578 const unsigned char *geomend = geom + geomlen;
582 Q_ASSERT( geom + 1 +
sizeof( wkbType ) <= geomend );
585 memcpy( &wkbType, geom,
sizeof( wkbType ) );
586 geom +=
sizeof( wkbType );
595 Q_ASSERT( geom + 2*
sizeof(
double ) <= geomend );
596 double *pts = (
double * )geom;
597 point.p.set( pts[0], pts[1] );
599 geom += 2 *
sizeof( double );
608 Q_ASSERT( geom +
sizeof(
int ) <= geomend );
609 int nPoints = *(
unsigned int * )geom;
610 geom +=
sizeof( int );
612 Q_ASSERT( geom + nPoints*
sizeof(
double )*dims <= geomend );
615 double *pts = (
double * )geom;
617 for (
int i = 1; i < nPoints; i++ )
619 double dx = pts[dims*i] - pts[dims*( i-1 )];
620 double dy = pts[dims*i+1] - pts[dims*( i-1 )+1];
621 tl += sqrt( dx * dx + dy * dy );
627 for (
int i = 1; i < nPoints; i++ )
629 double dx = pts[dims*i] - pts[dims*( i-1 )];
630 double dy = pts[dims*i+1] - pts[dims*( i-1 )+1];
631 double dl = sqrt( dx * dx + dy * dy );
635 double k = ( tl - l ) / dl;
637 point.p.set( pts[dims*( i-1 )] + k * dx,
638 pts[dims*( i-1 )+1] + k * dy );
639 point.angle = atan2( dy, dx ) * 180.0 * M_1_PI;
646 geom += nPoints *
sizeof( double ) * dims;
655 Q_ASSERT( geom +
sizeof(
int ) <= geomend );
656 int nRings = *(
unsigned int * )geom;
657 geom +=
sizeof( int );
659 for (
int i = 0; i < nRings; ++i )
661 Q_ASSERT( geom +
sizeof(
int ) <= geomend );
662 int nPoints = *(
unsigned int * )geom;
663 geom +=
sizeof( int );
665 Q_ASSERT( geom + nPoints*
sizeof(
double )*dims <= geomend );
669 double sx = 0.0, sy = 0.0;
670 double *pts = (
double* ) geom;
671 for (
int j = 0; j < nPoints - 1; j++ )
676 point.p.set( sx / ( nPoints - 1 ),
677 sy / ( nPoints - 1 ) );
681 geom += nPoints *
sizeof( double ) * dims;
698 QString name = prefix +
"name";
705 for ( ; idx < mFields.
count(); ++idx )
707 if ( mFields[idx].name() == name )
713 if ( idx != mFields.
count() )
715 mLabelFieldIdx[attr] = idx;
722 mLabelFieldIdx[attr] = value.
isEmpty() ? -1 : value.
toInt();
726 mLabelFieldIdx[attr] = -1;
738 int red, green, blue;
744 if ( scratchNode.isNull() )
746 QgsDebugMsg(
"couldn't find QgsLabel ``label'' attribute" );
752 readLabelField( el,
Text );
756 scratchNode = node.
namedItem(
"family" );
758 if ( scratchNode.isNull() )
760 QgsDebugMsg(
"couldn't find QgsLabel ``family'' attribute" );
766 readLabelField( el,
Family );
772 if ( scratchNode.isNull() )
774 QgsDebugMsg(
"couldn't find QgsLabel ``size'' attribute" );
786 readLabelField( el,
SizeType,
"unitfield" );
788 readLabelField( el,
Size );
794 if ( scratchNode.isNull() )
796 QgsDebugMsg(
"couldn't find QgsLabel ``bold'' attribute" );
802 readLabelField( el,
Bold );
806 scratchNode = node.
namedItem(
"italic" );
808 if ( scratchNode.isNull() )
810 QgsDebugMsg(
"couldn't find QgsLabel ``italic'' attribute" );
816 readLabelField( el,
Italic );
820 scratchNode = node.
namedItem(
"underline" );
822 if ( scratchNode.isNull() )
824 QgsDebugMsg(
"couldn't find QgsLabel ``underline'' attribute" );
834 scratchNode = node.
namedItem(
"strikeout" );
836 if ( scratchNode.isNull() )
838 QgsDebugMsg(
"couldn't find QgsLabel ``strikeout'' attribute" );
850 if ( scratchNode.isNull() )
852 QgsDebugMsg(
"couldn't find QgsLabel ``color'' attribute" );
864 readLabelField( el,
Color );
870 if ( scratchNode.isNull() )
872 QgsDebugMsg(
"couldn't find QgsLabel ``x'' attribute" );
883 if ( scratchNode.isNull() )
885 QgsDebugMsg(
"couldn't find QgsLabel ``y'' attribute" );
895 scratchNode = node.
namedItem(
"offset" );
897 if ( scratchNode.isNull() )
899 QgsDebugMsg(
"couldn't find QgsLabel ``offset'' attribute" );
903 double xoffset, yoffset;
911 mLabelAttributes->
setOffset( xoffset, yoffset, type );
912 readLabelField( el,
XOffset,
"xfield" );
913 readLabelField( el,
YOffset,
"yfield" );
919 if ( scratchNode.isNull() )
921 QgsDebugMsg(
"couldn't find QgsLabel ``angle'' attribute" );
927 readLabelField( el,
Angle );
932 scratchNode = node.
namedItem(
"alignment" );
934 if ( scratchNode.isNull() )
936 QgsDebugMsg(
"couldn't find QgsLabel ``alignment'' attribute" );
947 scratchNode = node.
namedItem(
"buffercolor" );
949 if ( scratchNode.isNull() )
951 QgsDebugMsg(
"couldn't find QgsLabel ``buffercolor'' attribute" );
965 scratchNode = node.
namedItem(
"buffersize" );
967 if ( scratchNode.isNull() )
969 QgsDebugMsg(
"couldn't find QgsLabel ``bffersize'' attribute" );
980 scratchNode = node.
namedItem(
"bufferenabled" );
982 if ( scratchNode.isNull() )
984 QgsDebugMsg(
"couldn't find QgsLabel ``bufferenabled'' attribute" );
994 scratchNode = node.
namedItem(
"multilineenabled" );
996 if ( scratchNode.isNull() )
998 QgsDebugMsg(
"couldn't find QgsLabel ``multilineenabled'' attribute" );
1008 scratchNode = node.
namedItem(
"selectedonly" );
1010 if ( scratchNode.isNull() )
1012 QgsDebugMsg(
"couldn't find QgsLabel ``selectedonly'' attribute" );
1031 if ( mLabelAttributes->
textIsSet() && mLabelFieldIdx[
Text] != -1 )
1045 if ( mLabelFieldIdx[
Family] != -1 )
1068 if ( mLabelFieldIdx[
Size] != -1 )
1070 if ( mLabelFieldIdx[
SizeType] != -1 )
1099 if ( mLabelFieldIdx[
Bold] != -1 )
1120 if ( mLabelFieldIdx[
Italic] != -1 )
1185 if ( mLabelFieldIdx[
Color] != -1 )
1244 if ( mLabelFieldIdx[
Angle] != -1 )
1376 mScaleBasedVisibility = theVisibilityFlag;
1381 return mScaleBasedVisibility;
1386 mMinScale = theMinScale;
1396 mMaxScale = theMaxScale;
void setBufferColor(const QColor &color)
double bufferSize() const
void setMultilineEnabled(bool useMultiline)
void setBold(bool enable)
void setLabelField(int attr, int fieldIndex)
Set label field.
void setSize(double size, int type)
void setUnderline(bool enable)
void setSelectedOnly(bool selectedonly)
static int unitsCode(const QString &name)
QDomNode appendChild(const QDomNode &newChild)
void setFamily(const QString &family)
bool alignmentIsSet() const
size_t wkbSize() const
Returns the size of the WKB in asWkb().
QString attribute(const QString &name, const QString &defValue) const
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
static QString alignmentName(int alignment)
void scale(qreal sx, qreal sy)
QgsPoint transform(const QgsPoint &p) const
Transform the point from map (world) coordinates to device coordinates.
bool selectedOnly() const
void renderLabel(QgsRenderContext &renderContext, QgsFeature &feature, bool selected, QgsLabelAttributes *classAttributes=0)
render label
void setUnderline(bool enable)
Container of fields for a vector layer.
A geometry is the spatial representation of a feature.
bool bufferEnabled() const
WkbType
Used for symbology operations.
void setFields(const QgsFields &fields)
Set available fields.
double scaleFactor() const
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
int bufferSizeType() const
const QgsCoordinateTransform * coordinateTransform() const
double toDouble(bool *ok) const
QgsLabelAttributes * labelAttributes()
Pointer to default attributes.
QDomElement toElement() const
void setAutoAngle(bool state)
void setBold(bool enable)
static QString unitsName(int units)
void setPixelSize(int pixelSize)
QColor bufferColor() const
void setFont(const QFont &font)
QString number(int n, int base)
void append(const T &value)
static int alignmentCode(const QString &name)
bool hasAttribute(const QString &name) const
void setPen(const QColor &color)
void setAttribute(const QString &name, const QString &value)
const QColor & color() const
void setBufferSize(double size, int type)
int toInt(bool *ok, int base) const
void setScaleBasedVisibility(bool theVisibilityFlag)
Accessor and mutator for the scale based visilibility flag.
bool scaleBasedVisibility() const
void drawText(const QPointF &position, const QString &text)
double rasterScaleFactor() const
int count() const
Return number of items.
QGis::WkbType wkbType() const
Returns type of the geometry as a WKB type (point / linestring / polygon etc.)
QString labelField(int attr) const
label field
QgsFields & fields()
Available vector fields.
void setColor(const QColor &color)
void setText(const QString &text)
void readXML(const QDomNode &node)
Reads the renderer configuration from an XML file.
bool multilineEnabled() const
void setAngle(double angle)
A class to represent a point.
void writeXML(QDomNode &label_node, QDomDocument &document) const
Writes the contents of the renderer to a configuration file.
QString fieldValue(int attr, QgsFeature &feature)
Get field value if : 1) field name is not empty 2) field exists 3) value is defined otherwise returns...
bool bufferSizeIsSet() const
double ANALYSIS_EXPORT angle(Point3D *p1, Point3D *p2, Point3D *p3, Point3D *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
QDomNode namedItem(const QString &name) const
bool contains(QChar ch, Qt::CaseSensitivity cs) const
int width(const QString &text, int len) const
const QString family() const
void setItalic(bool enable)
void setStrikeOut(bool enable)
void addRequiredFields(QgsAttributeList &fields) const
add vector of required fields to existing list of fields
Contains information about the context of a rendering operation.
void setMaxScale(float theMaxScale)
Accessor and mutator for the maximum scale member.
void setAlignment(int alignment)
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
void setOffset(double x, double y, int type)
QgsLabel(const QgsFields &fields)
bool strikeOutIsSet() const
void setFamily(const QString &family)
const QgsGeometry * constGeometry() const
Gets a const pointer to the geometry object associated with this feature.
void setStrikeOut(bool enable)
void translate(const QPointF &offset)
const QgsMapToPixel & mapToPixel() const
bool underlineIsSet() const
void setMinScale(float theMinScale)
Accessor and mutator for the minimum scale member.
Custom exception class for Coordinate Reference System related exceptions.
QDomElement createElement(const QString &tagName)
A class to store attributes needed for label rendering.
void setColor(const QColor &color)
int compare(const QString &other) const
void setBufferEnabled(bool useBufferFlag)
const unsigned char * asWkb() const
Returns the buffer containing this geometry in WKB format.
bool bufferColorIsSet() const
void setItalic(bool enable)
const QString text() const