00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef QGSPALLABELING_H
00022 #define QGSPALLABELING_H
00023
00024 class QFontMetricsF;
00025 class QPainter;
00026 class QgsMapRenderer;
00027 class QgsRectangle;
00028 class QgsCoordinateTransform;
00029
00030 #include <QString>
00031 #include <QFont>
00032 #include <QColor>
00033 #include <QList>
00034 #include <QRectF>
00035
00036 namespace pal
00037 {
00038 class Pal;
00039 class Layer;
00040 class LabelPosition;
00041 }
00042
00043 class QgsMapToPixel;
00044 class QgsFeature;
00045 #include "qgspoint.h"
00046
00047 #include "qgsvectorlayer.h"
00048
00049 class QgsPalGeometry;
00050
00051 class CORE_EXPORT QgsPalLayerSettings
00052 {
00053 public:
00054 QgsPalLayerSettings();
00055 QgsPalLayerSettings( const QgsPalLayerSettings& s );
00056 ~QgsPalLayerSettings();
00057
00058 enum Placement
00059 {
00060 AroundPoint,
00061 OverPoint,
00062 Line,
00063 Curved,
00064 Horizontal,
00065 Free
00066 };
00067
00068 enum LinePlacementFlags
00069 {
00070 OnLine = 1,
00071 AboveLine = 2,
00072 BelowLine = 4,
00073 MapOrientation = 8
00074 };
00075
00076 enum DataDefinedProperties
00077 {
00078 Size = 0,
00079 Bold,
00080 Italic,
00081 Underline,
00082 Color,
00083 Strikeout,
00084 Family,
00085 BufferSize,
00086 BufferColor,
00087 PositionX,
00088 PositionY,
00089 Hali,
00090 Vali,
00091 LabelDistance,
00092 Rotation
00093 };
00094
00095 QString fieldName;
00096 Placement placement;
00097 unsigned int placementFlags;
00098 QFont textFont;
00099 QColor textColor;
00100 bool enabled;
00101 int priority;
00102 bool obstacle;
00103 double dist;
00104 double vectorScaleFactor;
00105 double rasterCompressFactor;
00106 int scaleMin, scaleMax;
00107 double bufferSize;
00108 QColor bufferColor;
00109 bool labelPerPart;
00110 bool mergeLines;
00111 bool multiLineLabels;
00112 double minFeatureSize;
00113
00114
00115 bool addDirectionSymbol;
00116 bool fontSizeInMapUnits;
00117
00118
00119 void calculateLabelSize( const QFontMetricsF* fm, QString text, double& labelX, double& labelY );
00120
00121
00122 void registerFeature( QgsFeature& f, const QgsRenderContext& context );
00123
00124 void readFromLayer( QgsVectorLayer* layer );
00125 void writeToLayer( QgsVectorLayer* layer );
00126
00128 void setDataDefinedProperty( DataDefinedProperties p, int attributeIndex );
00130 void removeDataDefinedProperty( DataDefinedProperties p );
00131
00132
00133 pal::Layer* palLayer;
00134 int fieldIndex;
00135 QFontMetricsF* fontMetrics;
00136 const QgsMapToPixel* xform;
00137 const QgsCoordinateTransform* ct;
00138 QgsPoint ptZero, ptOne;
00139 QList<QgsPalGeometry*> geometries;
00140
00142 QMap< DataDefinedProperties, int > dataDefinedProperties;
00143
00148 int sizeToPixel( double size, const QgsRenderContext& c ) const;
00149
00150 private:
00153 bool checkMinimumSizeMM( const QgsRenderContext& ct, QgsGeometry* geom, double minSize ) const;
00154 };
00155
00156 class CORE_EXPORT QgsLabelCandidate
00157 {
00158 public:
00159 QgsLabelCandidate( QRectF r, double c ): rect( r ), cost( c ) {}
00160
00161 QRectF rect;
00162 double cost;
00163 };
00164
00165 class CORE_EXPORT QgsPalLabeling : public QgsLabelingEngineInterface
00166 {
00167 public:
00168 QgsPalLabeling();
00169 ~QgsPalLabeling();
00170
00171 QgsPalLayerSettings& layer( const char* layerName );
00172
00173 void numCandidatePositions( int& candPoint, int& candLine, int& candPolygon );
00174 void setNumCandidatePositions( int candPoint, int candLine, int candPolygon );
00175
00176 enum Search { Chain, Popmusic_Tabu, Popmusic_Chain, Popmusic_Tabu_Chain, Falp };
00177
00178 void setSearchMethod( Search s );
00179 Search searchMethod() const;
00180
00181 bool isShowingCandidates() const { return mShowingCandidates; }
00182 void setShowingCandidates( bool showing ) { mShowingCandidates = showing; }
00183 const QList<QgsLabelCandidate>& candidates() { return mCandidates; }
00184
00185 bool isShowingAllLabels() const { return mShowingAllLabels; }
00186 void setShowingAllLabels( bool showing ) { mShowingAllLabels = showing; }
00187
00188
00189
00191 virtual void init( QgsMapRenderer* mr );
00193 virtual bool willUseLayer( QgsVectorLayer* layer );
00195 virtual int prepareLayer( QgsVectorLayer* layer, QSet<int>& attrIndices, QgsRenderContext& ctx );
00197 virtual void registerFeature( QgsVectorLayer* layer, QgsFeature& feat, const QgsRenderContext& context = QgsRenderContext() );
00199 virtual void drawLabeling( QgsRenderContext& context );
00201 virtual void exit();
00202
00204 virtual QgsLabelingEngineInterface* clone();
00205
00206 void drawLabelCandidateRect( pal::LabelPosition* lp, QPainter* painter, const QgsMapToPixel* xform );
00208 void drawLabel( pal::LabelPosition* label, QPainter* painter, const QFont& f, const QColor& c, const QgsMapToPixel* xform, double bufferSize = -1, \
00209 const QColor& bufferColor = QColor( 255, 255, 255 ), bool drawBuffer = false );
00210 static void drawLabelBuffer( QPainter* p, QString text, const QFont& font, double size, QColor color );
00211
00212 protected:
00213
00214 void initPal();
00215
00216 protected:
00217
00218 QHash<QgsVectorLayer*, QgsPalLayerSettings> mActiveLayers;
00219 QgsPalLayerSettings mInvalidLayerSettings;
00220
00221 QgsMapRenderer* mMapRenderer;
00222 int mCandPoint, mCandLine, mCandPolygon;
00223 Search mSearch;
00224
00225 pal::Pal* mPal;
00226
00227
00228 QList<QgsLabelCandidate> mCandidates;
00229 bool mShowingCandidates;
00230
00231 bool mShowingAllLabels;
00232 };
00233
00234 #endif // QGSPALLABELING_H