QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 <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
QString mText
Definition: qgspalgeometry.h:137
bool mCurvedLabeling
Definition: qgspalgeometry.h:147
void addDataDefinedValue(QgsPalLayerSettings::DataDefinedProperties p, QVariant v)
Definition: qgspalgeometry.h:109
void setDxfLayer(QString dxfLayer)
Definition: qgspalgeometry.h:132
const QgsAttributes & diagramAttributes()
Definition: qgspalgeometry.h:123
#define FID_TO_STRING(fid)
Definition: qgsfeature.h:83
void setAttributes(const QgsAttributes &attrs)
Definition: qgsfeature.h:144
void releaseGeosGeometry(const GEOSGeometry *)
Called by Pal when it doesn't need the coordinates anymore.
Definition: qgspalgeometry.h:44
void setIsDiagram(bool d)
Definition: qgspalgeometry.h:111
QFontMetricsF * getLabelFontMetrics()
Definition: qgspalgeometry.h:120
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:113
QString mDxfLayer
Definition: qgspalgeometry.h:154
double x() const
Definition: qgspoint.h:110
DataDefinedProperties
Definition: qgspallabeling.h:167
CharacterInfo * char_info
Definition: feature.h:76
QMap< QgsPalLayerSettings::DataDefinedProperties, QVariant > mDataDefinedValues
Stores attribute values for data defined properties.
Definition: qgspalgeometry.h:149
QByteArray mStrId
Definition: qgspalgeometry.h:138
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:33
qreal mWordSpacing
Definition: qgspalgeometry.h:146
bool mIsDiagram
Definition: qgspalgeometry.h:141
const QMap< QgsPalLayerSettings::DataDefinedProperties, QVariant > & dataDefinedValues() const
Definition: qgspalgeometry.h:108
void setFeatureId(QgsFeatureId id)
Set the feature id for this feature.
Definition: qgsfeature.cpp:128
QFont mDefinedFont
Definition: qgspalgeometry.h:143
QString dxfLayer() const
Definition: qgspalgeometry.h:133
optional additional info about label (for curved labels)
Definition: feature.h:52
GEOSGeometry * mG
Definition: qgspalgeometry.h:136
QFont definedFont()
Definition: qgspalgeometry.h:118
Definition: qgspalgeometry.h:10
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)
Definition: qgspalgeometry.h:13
void setDiagramAttributes(const QgsAttributes &attrs)
Definition: qgspalgeometry.h:122
bool mIsPinned
Definition: qgspalgeometry.h:142
const GEOSGeometry * getGeosGeometry()
get the GEOSGeometry of the feature This method is called by Pal each time it needs a geom's coordina...
Definition: qgspalgeometry.h:40
const char * strId()
Definition: qgspalgeometry.h:49
Definition: qgsdiagramrendererv2.h:37
QgsPoint toMapCoordinatesF(double x, double y) const
Definition: qgsmaptopixel.cpp:58
QgsPoint toMapCoordinates(int x, int y) const
Definition: qgsmaptopixel.cpp:53
void setValid(bool validity)
Set the validity of the feature.
Definition: qgsfeature.cpp:176
void setIsPinned(bool f)
Definition: qgspalgeometry.h:114
bool isPinned() const
Definition: qgspalgeometry.h:115
void setDefinedFont(QFont f)
Definition: qgspalgeometry.h:117
unsigned short chr
Definition: feature.h:57
static GEOSContextHandle_t getGEOSHandler()
return GEOS context handle
Definition: qgsgeometry.cpp:138
QVector< QVariant > QgsAttributes
Definition: qgsfeature.h:100
~QgsPalGeometry()
Definition: qgspalgeometry.h:30
QgsAttributes mDiagramAttributes
Stores attribute values for diagram rendering.
Definition: qgspalgeometry.h:152
QString text()
Definition: qgspalgeometry.h:50
qint64 QgsFeatureId
Definition: qgsfeature.h:30
double y() const
Definition: qgspoint.h:118
bool isDiagram() const
Definition: qgspalgeometry.h:112
Interface that allows Pal to access user's geometries.
Definition: palgeometry.h:42
LabelInfo * mInfo
Definition: qgspalgeometry.h:140
QgsFeatureId mId
Definition: qgspalgeometry.h:139
qreal mLetterSpacing
Definition: qgspalgeometry.h:145
void feature(QgsFeature &feature)
Definition: qgspalgeometry.h:125
QFontMetricsF * mFontMetrics
Definition: qgspalgeometry.h:144
pal::LabelInfo * info(QFontMetricsF *fm, const QgsMapToPixel *xform, double fontScale, double maxinangle, double maxoutangle)
Definition: qgspalgeometry.h:52