QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsdxfpallabeling.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsdxfpallabeling.cpp
3  ---------------------
4  begin : January 2014
5  copyright : (C) 2014 by Marco Hugentobler
6  email : marco at sourcepole dot ch
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgsdxfpallabeling.h"
19 #include "qgsdxfexport.h"
20 #include "qgsmaplayerregistry.h"
21 #include "qgspalgeometry.h"
22 #include "qgsmapsettings.h"
23 
24 #include "pal/pointset.h"
25 #include "pal/labelposition.h"
26 
27 using namespace pal;
28 
30  : QgsPalLabeling()
31  , mDxfExport( dxf )
32  , mImage( 0 )
33  , mPainter( 0 )
34 {
36  mSettings->setMapUnits( mapUnits );
37  mSettings->setExtent( bbox );
38 
39  int dpi = 96;
40  double factor = 1000 * dpi / scale / 25.4 * QGis::fromUnitToUnitFactor( mapUnits, QGis::Meters );
41  mSettings->setOutputSize( QSize( bbox.width() * factor, bbox.height() * factor ) );
42  mSettings->setOutputDpi( dpi );
44  init( *mSettings );
45 
46  mImage = new QImage( 10, 10, QImage::Format_ARGB32_Premultiplied );
47  mImage->setDotsPerMeterX( 96 / 25.4 * 1000 );
48  mImage->setDotsPerMeterY( 96 / 25.4 * 1000 );
49  mPainter = new QPainter( mImage );
52  mRenderContext.setExtent( bbox );
53  mRenderContext.setScaleFactor( 96.0 / 25.4 );
54  mRenderContext.setMapToPixel( QgsMapToPixel( 1.0 / factor, bbox.xMinimum(), bbox.yMinimum(), bbox.height() * factor ) );
55 }
56 
58 {
59  delete mPainter;
60  delete mImage;
61  delete mSettings;
62 }
63 
64 void QgsDxfPalLabeling::drawLabel( pal::LabelPosition* label, QgsRenderContext& context, QgsPalLayerSettings& tmpLyr, DrawLabelType drawType, double dpiRatio )
65 {
66  Q_UNUSED( context );
67  Q_UNUSED( drawType );
68  Q_UNUSED( dpiRatio );
69 
70  if ( drawType == QgsPalLabeling::LabelBuffer )
71  {
72  return;
73  }
74 
75  //debug: print label infos
76  if ( mDxfExport )
77  {
78  //label text
79  QString text = (( QgsPalGeometry* )label->getFeaturePart()->getUserGeometry() )->text();
80 
81  //layer name
83  if ( !layer )
84  {
85  return;
86  }
87  QString layerName = mDxfExport->dxfLayerName( layer->name() );
88 
89  //angle
90  double angle = label->getAlpha() * 180 / M_PI;
91 
92  //debug: show label rectangle
93  /*QgsPolyline line;
94  for( int i = 0; i < 4; ++i )
95  {
96  line.append( QgsPoint( label->getX( i ), label->getY( i ) ) );
97  }
98  mDxfExport->writePolyline( line, layerName, "CONTINUOUS", 1, 0.01, true );*/
99 
100  QStringList textList;
101  if ( !tmpLyr.wrapChar.isEmpty() )
102  {
103  textList = text.split( tmpLyr.wrapChar );
104  }
105  else
106  {
107  textList = text.split( "\n" );
108  }
109  double textHeight = label->getHeight() / textList.size();
110  QFontMetricsF fm( tmpLyr.textFont );
111  double textAscent = textHeight * fm.ascent() / fm.height();
112 
113  for ( int i = 0; i < textList.size(); ++i )
114  {
115  mDxfExport->writeText( layerName, textList.at( i ), QgsPoint( label->getX(), label->getY() + ( textList.size() - 1 - i ) * textHeight ), textAscent, angle, mDxfExport->closestColorMatch( tmpLyr.textColor.rgb() ) );
116  }
117  }
118 }
QgsPalLayerSettings & layer(const QString &layerName)
returns PAL layer settings for a registered layer
FeaturePart * getFeaturePart()
return the feature corresponding to this labelposition
A rectangle specified with double values.
Definition: qgsrectangle.h:35
Base class for all map layer types.
Definition: qgsmaplayer.h:48
QgsDxfPalLabeling(QgsDxfExport *dxf, const QgsRectangle &bbox, double scale, QGis::UnitType mapUnits)
void writeText(const QString &layer, const QString &text, const QgsPoint &pt, double size, double angle, int color)
void setOutputDpi(int dpi)
Set DPI used for conversion between real world units (e.g. mm) and pixels.
virtual Q_DECL_DEPRECATED void init(QgsMapRenderer *mr)
called when we're going to start with rendering
void setRendererScale(double scale)
static int closestColorMatch(QRgb pixel)
void setExtent(const QgsRectangle &extent)
PalGeometry * getUserGeometry()
Definition: feature.h:298
QgsDxfExport * mDxfExport
The QgsMapSettings class contains configuration for rendering of the map.
const QString & name() const
Get the display name of the layer.
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:33
char * getLayerName() const
return pointer to layer's name.
void setScaleFactor(double factor)
double yMinimum() const
Get the y minimum value (bottom side of rectangle)
Definition: qgsrectangle.h:199
double getY(int i=0) const
get the down-left y coordinate
double getAlpha() const
get alpha
void setMapUnits(QGis::UnitType u)
Set units of map's geographical coordinates - used for scale calculation.
#define M_PI
void setPainter(QPainter *p)
A class to represent a point geometry.
Definition: qgspoint.h:63
double ANALYSIS_EXPORT angle(Point3D *p1, Point3D *p2, Point3D *p3, Point3D *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
static QString dxfLayerName(const QString &name)
Contains information about the context of a rendering operation.
static QgsMapLayerRegistry * instance()
Returns the instance pointer, creating the object on the first call.
double getHeight() const
void setOutputSize(const QSize &size)
Set the size of the resulting map image.
void setExtent(const QgsRectangle &rect)
Set coordinates of the rectangle which should be rendered.
void setMapToPixel(const QgsMapToPixel &mtp)
static double fromUnitToUnitFactor(QGis::UnitType fromUnit, QGis::UnitType toUnit)
Returns the conversion factor between the specified units.
Definition: qgis.cpp:123
LabelPositon is a candidate feature label position.
Definition: labelposition.h:53
UnitType
Map units that qgis supports.
Definition: qgis.h:229
QgsMapLayer * mapLayer(QString theLayerId)
Retrieve a pointer to a loaded layer by id.
double getX(int i=0) const
get the down-left x coordinate
void drawLabel(pal::LabelPosition *label, QgsRenderContext &context, QgsPalLayerSettings &tmpLyr, DrawLabelType drawType, double dpiRatio=1.0)
drawLabel
QgsMapSettings * mSettings
double width() const
Width of the rectangle.
Definition: qgsrectangle.h:204
double xMinimum() const
Get the x minimum value (left side of rectangle)
Definition: qgsrectangle.h:189
QgsRenderContext mRenderContext
double height() const
Height of the rectangle.
Definition: qgsrectangle.h:209
void setCrsTransformEnabled(bool enabled)
sets whether to use projections for this layer set