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