QGIS API Documentation  2.11.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 );
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  QString strId() { return mStrId; }
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  // this workaround only works for clusters with a single character. Not sure how it should be handled
114  // with multi-character clusters.
115  if ( mClusters[i].length() == 1 &&
116  !qgsDoubleNear( fm->width( QString( mClusters[i].at( 0 ) ) ), fm->width( mClusters[i].at( 0 ) ) + mLetterSpacing ) )
117  {
118  // word spacing applied when it shouldn't be
119  wordSpaceFix -= mWordSpacing;
120  }
121 
122  charWidth = fm->width( QString( mClusters[i] ) ) + wordSpaceFix;
123  }
124 
125  double labelWidth = mapScale * charWidth / fontScale;
126  mInfo->char_info[i].width = labelWidth;
127  }
128  return mInfo;
129  }
130 
132  void addDataDefinedValue( QgsPalLayerSettings::DataDefinedProperties p, QVariant v ) { mDataDefinedValues.insert( p, v ); }
133 
134  void setIsDiagram( bool d ) { mIsDiagram = d; }
135  bool isDiagram() const { return mIsDiagram; }
136 
137  void setIsPinned( bool f ) { mIsPinned = f; }
138  bool isPinned() const { return mIsPinned; }
139 
140  void setDefinedFont( QFont f ) { mDefinedFont = QFont( f ); }
141  QFont definedFont() { return mDefinedFont; }
142 
143  QFontMetricsF* getLabelFontMetrics() { return mFontMetrics; }
144 
145  void setDiagramAttributes( const QgsAttributes& attrs ) { mDiagramAttributes = attrs; }
146  const QgsAttributes& diagramAttributes() { return mDiagramAttributes; }
147 
148  void feature( QgsFeature& feature )
149  {
150  feature.setFeatureId( mId );
151  feature.setAttributes( mDiagramAttributes );
152  feature.setValid( true );
153  }
154 
155  void setDxfLayer( QString dxfLayer ) { mDxfLayer = dxfLayer; }
156  QString dxfLayer() const { return mDxfLayer; }
157 
158  protected:
159  GEOSGeometry* mG;
166  bool mIsPinned;
169  qreal mLetterSpacing; // for use with curved labels
170  qreal mWordSpacing; // for use with curved labels
171  bool mCurvedLabeling; // whether the geometry is to be used for curved labeling placement
174 
177 
179 };
180 
181 #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:162
qreal width(const QString &text) const
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Definition: qgis.h:350
QMap< QgsPalLayerSettings::DataDefinedProperties, QVariant > mDataDefinedValues
Stores attribute values for data defined properties.
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:46
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)
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.
QString strId()
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:41
LabelInfo * mInfo
QgsFeatureId mId
A vector of attributes.
Definition: qgsfeature.h:109
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