QGIS API Documentation  2.11.0-Master
qgspalgeometry.h
Go to the documentation of this file.
1 #ifndef QGSPALGEOMETRY_H
2 #define QGSPALGEOMETRY_H
3 
4 #include "qgsgeometry.h"
5 #include "qgspallabeling.h"
6 #include <pal/feature.h>
7 #include <pal/palgeometry.h>
8 
9 using namespace pal;
10 
12 {
13  public:
14  QgsPalGeometry( QgsFeatureId id, QString text, GEOSGeometry* g,
15  qreal ltrSpacing = 0.0, qreal wordSpacing = 0.0, bool curvedLabeling = false )
16  : mG( g )
17  , mText( text )
18  , mId( id )
19  , mInfo( NULL )
20  , mIsDiagram( false )
21  , mIsPinned( false )
22  , mFontMetrics( NULL )
23  , mLetterSpacing( ltrSpacing )
24  , mWordSpacing( wordSpacing )
25  , mCurvedLabeling( curvedLabeling )
26  {
27  mStrId = FID_TO_STRING( mId ).toAscii();
28  mDefinedFont = QFont();
29  }
30 
32  {
33  if ( mG )
34  GEOSGeom_destroy_r( QgsGeometry::getGEOSHandler(), mG );
35  delete mInfo;
36  delete mFontMetrics;
37  }
38 
39  // getGeosGeometry + releaseGeosGeometry is called twice: once when adding, second time when labeling
40 
41  const GEOSGeometry* getGeosGeometry() override
42  {
43  return mG;
44  }
45  void releaseGeosGeometry( const GEOSGeometry* /*geom*/ ) override
46  {
47  // nothing here - we'll delete the geometry in destructor
48  }
49 
50  const char* strId() { return mStrId.data(); }
51  QString text() { return mText; }
52 
58  QString text( int partId ) const
59  {
60  if ( partId == -1 )
61  return mText;
62  else
63  return mClusters.at( partId );
64  }
65 
66  pal::LabelInfo* info( QFontMetricsF* fm, const QgsMapToPixel* xform, double fontScale, double maxinangle, double maxoutangle )
67  {
68  if ( mInfo )
69  return mInfo;
70 
71  mFontMetrics = new QFontMetricsF( *fm ); // duplicate metrics for when drawing label
72 
73  // max angle between curved label characters (20.0/-20.0 was default in QGIS <= 1.8)
74  if ( maxinangle < 20.0 )
75  maxinangle = 20.0;
76  if ( 60.0 < maxinangle )
77  maxinangle = 60.0;
78  if ( maxoutangle > -20.0 )
79  maxoutangle = -20.0;
80  if ( -95.0 > maxoutangle )
81  maxoutangle = -95.0;
82 
83  // create label info!
84  double mapScale = xform->mapUnitsPerPixel();
85  double labelHeight = mapScale * fm->height() / fontScale;
86 
87  // mLetterSpacing/mWordSpacing = 0.0 is default for non-curved labels
88  // (non-curved spacings handled by Qt in QgsPalLayerSettings/QgsPalLabeling)
89  qreal charWidth;
90  qreal wordSpaceFix;
91 
92  //split string by valid grapheme boundaries - required for certain scripts (see #6883)
93  mClusters = QgsPalLabeling::splitToGraphemes( mText );
94 
95  mInfo = new pal::LabelInfo( mClusters.count(), labelHeight, maxinangle, maxoutangle );
96  for ( int i = 0; i < mClusters.count(); i++ )
97  {
98  //doesn't appear to be used anywhere:
99  //mInfo->char_info[i].chr = textClusters[i].unicode();
100 
101  // reconstruct how Qt creates word spacing, then adjust per individual stored character
102  // this will allow PAL to create each candidate width = character width + correct spacing
103  charWidth = fm->width( mClusters[i] );
104  if ( mCurvedLabeling )
105  {
106  wordSpaceFix = qreal( 0.0 );
107  if ( mClusters[i] == QString( " " ) )
108  {
109  // word spacing only gets added once at end of consecutive run of spaces, see QTextEngine::shapeText()
110  int nxt = i + 1;
111  wordSpaceFix = ( nxt < mClusters.count() && mClusters[nxt] != QString( " " ) ) ? mWordSpacing : qreal( 0.0 );
112  }
113  if ( fm->width( QString( mClusters[i] ) ) - fm->width( mClusters[i] ) - mLetterSpacing != qreal( 0.0 ) )
114  {
115  // word spacing applied when it shouldn't be
116  wordSpaceFix -= mWordSpacing;
117  }
118  charWidth = fm->width( QString( mClusters[i] ) ) + wordSpaceFix;
119  }
120 
121  double labelWidth = mapScale * charWidth / fontScale;
122  mInfo->char_info[i].width = labelWidth;
123  }
124  return mInfo;
125  }
126 
128  void addDataDefinedValue( QgsPalLayerSettings::DataDefinedProperties p, QVariant v ) { mDataDefinedValues.insert( p, v ); }
129 
130  void setIsDiagram( bool d ) { mIsDiagram = d; }
131  bool isDiagram() const { return mIsDiagram; }
132 
133  void setIsPinned( bool f ) { mIsPinned = f; }
134  bool isPinned() const { return mIsPinned; }
135 
136  void setDefinedFont( QFont f ) { mDefinedFont = QFont( f ); }
137  QFont definedFont() { return mDefinedFont; }
138 
139  QFontMetricsF* getLabelFontMetrics() { return mFontMetrics; }
140 
141  void setDiagramAttributes( const QgsAttributes& attrs ) { mDiagramAttributes = attrs; }
142  const QgsAttributes& diagramAttributes() { return mDiagramAttributes; }
143 
144  void feature( QgsFeature& feature )
145  {
146  feature.setFeatureId( mId );
147  feature.setAttributes( mDiagramAttributes );
148  feature.setValid( true );
149  }
150 
151  void setDxfLayer( QString dxfLayer ) { mDxfLayer = dxfLayer; }
152  QString dxfLayer() const { return mDxfLayer; }
153 
154  protected:
155  GEOSGeometry* mG;
162  bool mIsPinned;
165  qreal mLetterSpacing; // for use with curved labels
166  qreal mWordSpacing; // for use with curved labels
167  bool mCurvedLabeling; // whether the geometry is to be used for curved labeling placement
170 
173 
175 };
176 
177 #endif //QGSPALGEOMETRY_H
const GEOSGeometry * getGeosGeometry() override
get the GEOSGeometry of the feature This method is called by Pal each time it needs a geom's coordina...
void addDataDefinedValue(QgsPalLayerSettings::DataDefinedProperties p, QVariant v)
void setDxfLayer(QString dxfLayer)
const QgsAttributes & diagramAttributes()
#define FID_TO_STRING(fid)
Definition: qgsfeature.h:89
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
Definition: qgsfeature.cpp:95
QString text(int partId) const
Returns the text component corresponding to a specified label part.
void setIsDiagram(bool d)
QFontMetricsF * getLabelFontMetrics()
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:119
qreal width(const QString &text) const
QMap< QgsPalLayerSettings::DataDefinedProperties, QVariant > mDataDefinedValues
Stores attribute values for data defined properties.
QByteArray mStrId
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:34
const QMap< QgsPalLayerSettings::DataDefinedProperties, QVariant > & dataDefinedValues() const
void setFeatureId(QgsFeatureId id)
Sets the feature ID for this feature.
Definition: qgsfeature.cpp:81
QString dxfLayer() const
Optional additional info about label (for curved labels)
Definition: feature.h:47
GEOSGeometry * mG
double mapUnitsPerPixel() const
Return current map units per pixel.
QgsPalGeometry(QgsFeatureId id, QString text, GEOSGeometry *g, qreal ltrSpacing=0.0, qreal wordSpacing=0.0, bool curvedLabeling=false)
void setDiagramAttributes(const QgsAttributes &attrs)
const char * strId()
void releaseGeosGeometry(const GEOSGeometry *) override
Called by Pal when it doesn't need the coordinates anymore.
void setValid(bool validity)
Sets the validity of the feature.
Definition: qgsfeature.cpp:173
void setIsPinned(bool f)
bool isPinned() const
void setDefinedFont(QFont f)
static GEOSContextHandle_t getGEOSHandler()
return GEOS context handle
const QChar at(int position) const
static QStringList splitToGraphemes(const QString &text)
Splits a text string to a list of graphemes, which are the smallest allowable character divisions in ...
QgsAttributes mDiagramAttributes
Stores attribute values for diagram rendering.
QString text()
qint64 QgsFeatureId
Definition: qgsfeature.h:31
bool isDiagram() const
Interface that allows Pal to access user's geometries.
Definition: palgeometry.h:42
LabelInfo * mInfo
QgsFeatureId mId
void feature(QgsFeature &feature)
QFontMetricsF * mFontMetrics
QStringList mClusters
pal::LabelInfo * info(QFontMetricsF *fm, const QgsMapToPixel *xform, double fontScale, double maxinangle, double maxoutangle)
qreal height() const