00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef QGSLABEL_H
00018 #define QGSLABEL_H
00019
00020 #include <vector>
00021
00022 #include <QColor>
00023 #include <QList>
00024 #include <QMap>
00025
00026 #include "qgspoint.h"
00027
00028 class QDomNode;
00029 class QDomDocument;
00030 class QDomElement;
00031 class QString;
00032 class QPainter;
00033 class QPaintDevice;
00034
00035 class QgsFeature;
00036 class QgsField;
00037 class QgsLabelAttributes;
00038
00039 #include "qgsfield.h"
00040 #include "qgsrectangle.h"
00041 #include "qgsrendercontext.h"
00042
00043 typedef QList<int> QgsAttributeList;
00044
00045 typedef QMap<int, QgsField> QgsFieldMap;
00046
00052 class CORE_EXPORT QgsLabel
00053 {
00054 public:
00055 QgsLabel( const QgsFieldMap & fields );
00056
00057 ~QgsLabel();
00058
00059
00060 enum LabelField
00061 {
00062 Text = 0,
00063 Family,
00064 Size,
00065 SizeType,
00066 Bold,
00067 Italic,
00068 Underline,
00069 Color,
00070 XCoordinate,
00071 YCoordinate,
00072 XOffset,
00073 YOffset,
00074 Angle,
00075 Alignment,
00076 BufferEnabled,
00077 BufferSize,
00078 BufferColor,
00079 BufferBrush,
00080 BorderWidth,
00081 BorderColor,
00082 BorderStyle,
00083 MultilineEnabled,
00084 StrikeOut,
00085 LabelFieldCount
00086 };
00087
00088 struct labelpoint
00089 {
00090 QgsPoint p;
00091 double angle;
00092 };
00093
00106 void renderLabel( QPainter* painter, const QgsRectangle& viewExtent,
00107 QgsCoordinateTransform* coordinateTransform,
00108 const QgsMapToPixel *transform,
00109 QgsFeature &feature, bool selected, QgsLabelAttributes *classAttributes = 0,
00110 double sizeScale = 1., double rasterScaleFactor = 1.0 )
00111 {
00112 QgsRenderContext r;
00113 r.setExtent( viewExtent );
00114 r.setCoordinateTransform( new QgsCoordinateTransform( coordinateTransform->sourceCrs(), coordinateTransform->destCRS() ) );
00115 r.setMapToPixel( *transform );
00116 r.setPainter( painter );
00117 r.setScaleFactor( sizeScale );
00118 r.setRasterScaleFactor( rasterScaleFactor );
00119 renderLabel( r, feature, selected, classAttributes );
00120 }
00121
00129 void renderLabel( QgsRenderContext &renderContext, QgsFeature &feature, bool selected, QgsLabelAttributes *classAttributes = 0 );
00130
00134 void readXML( const QDomNode& node );
00135
00137 void writeXML( QDomNode & label_node, QDomDocument & document ) const;
00138
00140 void addRequiredFields( QgsAttributeList& fields ) const;
00141
00143 void setFields( const QgsFieldMap & fields );
00144
00146 QgsFieldMap & fields( void );
00147
00152 QgsLabelAttributes *layerAttributes( void );
00153
00158 QgsLabelAttributes *labelAttributes( void );
00159
00161 void setLabelField( int attr, int fieldIndex );
00162
00164 bool setLabelFieldName( int attr, QString name );
00165
00167 QString labelField( int attr ) const;
00168
00174 QString fieldValue( int attr, QgsFeature& feature );
00175
00177 void setMinScale( float theMinScale );
00178 float minScale() const;
00179
00181 void setMaxScale( float theMaxScale );
00182 float maxScale() const;
00183
00185 void setScaleBasedVisibility( bool theVisibilityFlag );
00186 bool scaleBasedVisibility() const;
00187
00188 private:
00192 void renderLabel( QgsRenderContext &renderContext, QgsPoint point,
00193 QString text, QFont font, QPen pen,
00194 int dx, int dy,
00195 double xoffset, double yoffset,
00196 double ang,
00197 int width, int height, int alignment );
00198
00199 bool readLabelField( QDomElement &el, int attr, QString prefix );
00200
00202 void labelPoint( std::vector<labelpoint>&, QgsFeature &feature );
00203
00205 unsigned char* labelPoint( labelpoint& point, unsigned char* wkb, size_t wkblen );
00206
00208 QColor mSelectionColor;
00209
00211 QgsLabelAttributes *mLabelAttributes;
00212
00214 QgsFieldMap mField;
00215
00217 std::vector<QString> mLabelField;
00218
00220 std::vector<int> mLabelFieldIdx;
00221
00223 float mMinScale;
00225 float mMaxScale;
00227 bool mScaleBasedVisibility;
00228 };
00229
00230 #endif