QGIS API Documentation  2.3.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgspalgeometry.h
Go to the documentation of this file.
1 #ifndef QGSPALGEOMETRY_H
2 #define QGSPALGEOMETRY_H
3 
4 #include <pal/feature.h>
5 #include <pal/palgeometry.h>
6 
7 using namespace pal;
8 
9 class QgsPalGeometry : public PalGeometry
10 {
11  public:
12  QgsPalGeometry( QgsFeatureId id, QString text, GEOSGeometry* g,
13  qreal ltrSpacing = 0.0, qreal wordSpacing = 0.0, bool curvedLabeling = false )
14  : mG( g )
15  , mText( text )
16  , mId( id )
17  , mInfo( NULL )
18  , mIsDiagram( false )
19  , mIsPinned( false )
20  , mFontMetrics( NULL )
21  , mLetterSpacing( ltrSpacing )
22  , mWordSpacing( wordSpacing )
23  , mCurvedLabeling( curvedLabeling )
24  {
25  mStrId = FID_TO_STRING( mId ).toAscii();
26  mDefinedFont = QFont();
27  }
28 
30  {
31  if ( mG )
32  GEOSGeom_destroy( mG );
33  delete mInfo;
34  delete mFontMetrics;
35  }
36 
37  // getGeosGeometry + releaseGeosGeometry is called twice: once when adding, second time when labeling
38 
39  const GEOSGeometry* getGeosGeometry()
40  {
41  return mG;
42  }
43  void releaseGeosGeometry( const GEOSGeometry* /*geom*/ )
44  {
45  // nothing here - we'll delete the geometry in destructor
46  }
47 
48  const char* strId() { return mStrId.data(); }
49  QString text() { return mText; }
50 
51  pal::LabelInfo* info( QFontMetricsF* fm, const QgsMapToPixel* xform, double fontScale, double maxinangle, double maxoutangle )
52  {
53  if ( mInfo )
54  return mInfo;
55 
56  mFontMetrics = new QFontMetricsF( *fm ); // duplicate metrics for when drawing label
57 
58  // max angle between curved label characters (20.0/-20.0 was default in QGIS <= 1.8)
59  if ( maxinangle < 20.0 )
60  maxinangle = 20.0;
61  if ( 60.0 < maxinangle )
62  maxinangle = 60.0;
63  if ( maxoutangle > -20.0 )
64  maxoutangle = -20.0;
65  if ( -95.0 > maxoutangle )
66  maxoutangle = -95.0;
67 
68  // create label info!
69  QgsPoint ptZero = xform->toMapCoordinates( 0, 0 );
70  QgsPoint ptSize = xform->toMapCoordinatesF( 0.0, -fm->height() / fontScale );
71 
72  // mLetterSpacing/mWordSpacing = 0.0 is default for non-curved labels
73  // (non-curved spacings handled by Qt in QgsPalLayerSettings/QgsPalLabeling)
74  qreal charWidth;
75  qreal wordSpaceFix;
76  mInfo = new pal::LabelInfo( mText.count(), ptSize.y() - ptZero.y(), maxinangle, maxoutangle );
77  for ( int i = 0; i < mText.count(); i++ )
78  {
79  mInfo->char_info[i].chr = mText[i].unicode();
80 
81  // reconstruct how Qt creates word spacing, then adjust per individual stored character
82  // this will allow PAL to create each candidate width = character width + correct spacing
83  charWidth = fm->width( mText[i] );
84  if ( mCurvedLabeling )
85  {
86  wordSpaceFix = qreal( 0.0 );
87  if ( mText[i] == QString( " " )[0] )
88  {
89  // word spacing only gets added once at end of consecutive run of spaces, see QTextEngine::shapeText()
90  int nxt = i + 1;
91  wordSpaceFix = ( nxt < mText.count() && mText[nxt] != QString( " " )[0] ) ? mWordSpacing : qreal( 0.0 );
92  }
93  if ( fm->width( QString( mText[i] ) ) - fm->width( mText[i] ) - mLetterSpacing != qreal( 0.0 ) )
94  {
95  // word spacing applied when it shouldn't be
96  wordSpaceFix -= mWordSpacing;
97  }
98  charWidth = fm->width( QString( mText[i] ) ) + wordSpaceFix;
99  }
100 
101  ptSize = xform->toMapCoordinatesF((( double ) charWidth ) / fontScale , 0.0 );
102  mInfo->char_info[i].width = ptSize.x() - ptZero.x();
103  }
104  return mInfo;
105  }
106 
107  const QMap< QgsPalLayerSettings::DataDefinedProperties, QVariant >& dataDefinedValues() const { return mDataDefinedValues; }
108  void addDataDefinedValue( QgsPalLayerSettings::DataDefinedProperties p, QVariant v ) { mDataDefinedValues.insert( p, v ); }
109 
110  void setIsDiagram( bool d ) { mIsDiagram = d; }
111  bool isDiagram() const { return mIsDiagram; }
112 
113  void setIsPinned( bool f ) { mIsPinned = f; }
114  bool isPinned() const { return mIsPinned; }
115 
116  void setDefinedFont( QFont f ) { mDefinedFont = QFont( f ); }
117  QFont definedFont() { return mDefinedFont; }
118 
119  QFontMetricsF* getLabelFontMetrics() { return mFontMetrics; }
120 
121  void setDiagramAttributes( const QgsAttributes& attrs ) { mDiagramAttributes = attrs; }
122  const QgsAttributes& diagramAttributes() { return mDiagramAttributes; }
123 
124  void feature( QgsFeature& feature )
125  {
126  feature.setFeatureId( mId );
127  feature.setAttributes( mDiagramAttributes );
128  feature.setValid( true );
129  }
130 
131  protected:
132  GEOSGeometry* mG;
133  QString mText;
134  QByteArray mStrId;
136  LabelInfo* mInfo;
138  bool mIsPinned;
140  QFontMetricsF* mFontMetrics;
141  qreal mLetterSpacing; // for use with curved labels
142  qreal mWordSpacing; // for use with curved labels
143  bool mCurvedLabeling; // whether the geometry is to be used for curved labeling placement
145  QMap< QgsPalLayerSettings::DataDefinedProperties, QVariant > mDataDefinedValues;
146 
149 };
150 
151 #endif //QGSPALGEOMETRY_H
void addDataDefinedValue(QgsPalLayerSettings::DataDefinedProperties p, QVariant v)
const QgsAttributes & diagramAttributes()
#define FID_TO_STRING(fid)
Definition: qgsfeature.h:83
void setAttributes(const QgsAttributes &attrs)
Definition: qgsfeature.h:145
void releaseGeosGeometry(const GEOSGeometry *)
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:114
double x() const
Definition: qgspoint.h:110
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:33
const QMap< QgsPalLayerSettings::DataDefinedProperties, QVariant > & dataDefinedValues() const
void setFeatureId(QgsFeatureId id)
Set the feature id for this feature.
Definition: qgsfeature.cpp:128
GEOSGeometry * mG
A class to represent a point geometry.
Definition: qgspoint.h:63
QgsPalGeometry(QgsFeatureId id, QString text, GEOSGeometry *g, qreal ltrSpacing=0.0, qreal wordSpacing=0.0, bool curvedLabeling=false)
void setDiagramAttributes(const QgsAttributes &attrs)
const GEOSGeometry * getGeosGeometry()
const char * strId()
QgsPoint toMapCoordinatesF(double x, double y) const
QgsPoint toMapCoordinates(int x, int y) const
void setValid(bool validity)
Set the validity of the feature.
Definition: qgsfeature.cpp:176
void setIsPinned(bool f)
bool isPinned() const
void setDefinedFont(QFont f)
QVector< QVariant > QgsAttributes
Definition: qgsfeature.h:100
QgsAttributes mDiagramAttributes
Stores attribute values for diagram rendering.
QString text()
qint64 QgsFeatureId
Definition: qgsfeature.h:30
double y() const
Definition: qgspoint.h:118
bool isDiagram() const
LabelInfo * mInfo
QgsFeatureId mId
void feature(QgsFeature &feature)
QFontMetricsF * mFontMetrics
pal::LabelInfo * info(QFontMetricsF *fm, const QgsMapToPixel *xform, double fontScale, double maxinangle, double maxoutangle)