QGIS API Documentation  2.2.0-Valmiera
 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, const QgsFields* fields ) { mDiagramAttributes = attrs; mDiagramFields = fields; }
122  const QgsAttributes& diagramAttributes() { return mDiagramAttributes; }
123 
124  void feature( QgsFeature& feature )
125  {
126  feature.setFeatureId( mId );
127  feature.setFields( mDiagramFields, false );
128  feature.setAttributes( mDiagramAttributes );
129  feature.setValid( true );
130  }
131 
132  protected:
133  GEOSGeometry* mG;
134  QString mText;
135  QByteArray mStrId;
137  LabelInfo* mInfo;
139  bool mIsPinned;
141  QFontMetricsF* mFontMetrics;
142  qreal mLetterSpacing; // for use with curved labels
143  qreal mWordSpacing; // for use with curved labels
144  bool mCurvedLabeling; // whether the geometry is to be used for curved labeling placement
146  QMap< QgsPalLayerSettings::DataDefinedProperties, QVariant > mDataDefinedValues;
147 
151 };
152 
153 #endif //QGSPALGEOMETRY_H