QGIS API Documentation  2.7.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 "qgspalgeometry.h"
21 #include "qgsmapsettings.h"
22 
23 #include "pal/pointset.h"
24 #include "pal/labelposition.h"
25 
26 using namespace pal;
27 
29  : QgsPalLabeling()
30  , mDxfExport( dxf )
31  , mImage( 0 )
32  , mPainter( 0 )
33 {
34  mSettings = new QgsMapSettings;
35  mSettings->setMapUnits( mapUnits );
36  mSettings->setExtent( bbox );
37 
38  int dpi = 96;
39  double factor = 1000 * dpi / scale / 25.4 * QGis::fromUnitToUnitFactor( mapUnits, QGis::Meters );
40  mSettings->setOutputSize( QSize( bbox.width() * factor, bbox.height() * factor ) );
41  mSettings->setOutputDpi( dpi );
42  mSettings->setCrsTransformEnabled( false );
43  init( *mSettings );
44 
45  mImage = new QImage( 10, 10, QImage::Format_ARGB32_Premultiplied );
46  mImage->setDotsPerMeterX( 96 / 25.4 * 1000 );
47  mImage->setDotsPerMeterY( 96 / 25.4 * 1000 );
48  mPainter = new QPainter( mImage );
49  mRenderContext.setPainter( mPainter );
50  mRenderContext.setRendererScale( scale );
51  mRenderContext.setExtent( bbox );
52  mRenderContext.setScaleFactor( 96.0 / 25.4 );
53  mRenderContext.setMapToPixel( QgsMapToPixel( 1.0 / factor, bbox.xMinimum(), bbox.yMinimum(), bbox.height() * factor ) );
54 }
55 
57 {
58  delete mPainter;
59  delete mImage;
60  delete mSettings;
61 }
62 
63 void QgsDxfPalLabeling::drawLabel( pal::LabelPosition* label, QgsRenderContext& context, QgsPalLayerSettings& tmpLyr, DrawLabelType drawType, double dpiRatio )
64 {
65  Q_UNUSED( context );
66  Q_UNUSED( drawType );
67  Q_UNUSED( dpiRatio );
68 
69  if ( drawType == QgsPalLabeling::LabelBuffer )
70  {
71  return;
72  }
73 
74  //debug: print label infos
75  if ( mDxfExport )
76  {
77  QgsPalGeometry *g = dynamic_cast< QgsPalGeometry* >( label->getFeaturePart()->getUserGeometry() );
78 
79  //label text
80  QString text = g->text();
81  QString txt = label->getPartId() == -1 ? text : QString( text[ label->getPartId()] );
82 
83  //angle
84  double angle = label->getAlpha() * 180 / M_PI;
85 
86  //debug: show label rectangle
87 #if 0
88  QgsPolyline line;
89  for ( int i = 0; i < 4; ++i )
90  {
91  line.append( QgsPoint( label->getX( i ), label->getY( i ) ) );
92  }
93  mDxfExport->writePolyline( line, g->dxfLayer(), "CONTINUOUS", 1, 0.01, true );
94 #endif
95 
96  QString wrapchr = tmpLyr.wrapChar.isEmpty() ? "\n" : tmpLyr.wrapChar;
97 
98  //add the direction symbol if needed
99  if ( !txt.isEmpty() && tmpLyr.placement == QgsPalLayerSettings::Line && tmpLyr.addDirectionSymbol )
100  {
101  bool prependSymb = false;
102  QString symb = tmpLyr.rightDirectionSymbol;
103 
104  if ( label->getReversed() )
105  {
106  prependSymb = true;
107  symb = tmpLyr.leftDirectionSymbol;
108  }
109 
110  if ( tmpLyr.reverseDirectionSymbol )
111  {
112  if ( symb == tmpLyr.rightDirectionSymbol )
113  {
114  prependSymb = true;
115  symb = tmpLyr.leftDirectionSymbol;
116  }
117  else
118  {
119  prependSymb = false;
120  symb = tmpLyr.rightDirectionSymbol;
121  }
122  }
123 
125  {
126  prependSymb = true;
127  symb = symb + wrapchr;
128  }
130  {
131  prependSymb = false;
132  symb = wrapchr + symb;
133  }
134 
135  if ( prependSymb )
136  {
137  txt.prepend( symb );
138  }
139  else
140  {
141  txt.append( symb );
142  }
143  }
144 
145  txt = txt.replace( wrapchr, "\\P" );
146 
147  if ( tmpLyr.textFont.underline() )
148  {
149  txt.prepend( "\\L" ).append( "\\l" );
150  }
151 
152  if ( tmpLyr.textFont.overline() )
153  {
154  txt.prepend( "\\O" ).append( "\\o" );
155  }
156 
157  if ( tmpLyr.textFont.strikeOut() )
158  {
159  txt.prepend( "\\K" ).append( "\\k" );
160  }
161 
162  txt.prepend( QString( "\\f%1|i%2|b%3;\\H%4;\\W0.75;" )
163  .arg( tmpLyr.textFont.family() )
164  .arg( tmpLyr.textFont.italic() ? 1 : 0 )
165  .arg( tmpLyr.textFont.bold() ? 1 : 0 )
166  .arg( label->getHeight() / ( 1 + txt.count( "\\P" ) ) * 0.75 ) );
167 
168  mDxfExport->writeMText( g->dxfLayer(), txt, QgsPoint( label->getX(), label->getY() ), label->getWidth() * 1.1, angle, tmpLyr.textColor );
169  }
170 }
FeaturePart * getFeaturePart()
return the feature corresponding to this labelposition
A rectangle specified with double values.
Definition: qgsrectangle.h:35
QgsDxfPalLabeling(QgsDxfExport *dxf, const QgsRectangle &bbox, double scale, QGis::UnitType mapUnits)
double getWidth() const
void setOutputDpi(int dpi)
Set DPI used for conversion between real world units (e.g. mm) and pixels.
UnitType
Map units that qgis supports.
Definition: qgis.h:229
virtual Q_DECL_DEPRECATED void init(QgsMapRenderer *mr)
called when we're going to start with rendering
QVector< QgsPoint > QgsPolyline
polyline is represented as a vector of points
Definition: qgsgeometry.h:33
void setRendererScale(double scale)
void setExtent(const QgsRectangle &extent)
PalGeometry * getUserGeometry()
Definition: feature.h:298
The QgsMapSettings class contains configuration for rendering of the map.
Perform transforms between map coordinates and device coordinates.
Definition: qgsmaptopixel.h:34
void setScaleFactor(double factor)
double yMinimum() const
Get the y minimum value (bottom side of rectangle)
Definition: qgsrectangle.h:193
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.
QString dxfLayer() const
#define M_PI
void setPainter(QPainter *p)
A class to represent a point.
Definition: qgspoint.h:63
bool getReversed() const
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)
void writePolyline(const QgsPolyline &line, const QString &layer, const QString &lineStyleName, QColor color, double width=-1, bool polygon=false)
Contains information about the context of a rendering operation.
int getPartId() const
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
QString text()
LabelPositon is a candidate feature label position.
Definition: labelposition.h:53
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
double width() const
Width of the rectangle.
Definition: qgsrectangle.h:198
double xMinimum() const
Get the x minimum value (left side of rectangle)
Definition: qgsrectangle.h:183
void writeMText(const QString &layer, const QString &text, const QgsPoint &pt, double width, double angle, QColor color)
double height() const
Height of the rectangle.
Definition: qgsrectangle.h:203
void setCrsTransformEnabled(bool enabled)
sets whether to use projections for this layer set
DirectionSymbols placeDirectionSymbol