QGIS API Documentation  2.9.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  if ( !g )
79  return;
80 
81  //label text
82  QString text = g->text();
83  QString txt = label->getPartId() == -1 ? text : QString( text[ label->getPartId()] );
84 
85  //angle
86  double angle = label->getAlpha() * 180 / M_PI;
87 
88  //debug: show label rectangle
89 #if 0
90  QgsPolyline line;
91  for ( int i = 0; i < 4; ++i )
92  {
93  line.append( QgsPoint( label->getX( i ), label->getY( i ) ) );
94  }
95  mDxfExport->writePolyline( line, g->dxfLayer(), "CONTINUOUS", 1, 0.01, true );
96 #endif
97 
98  QString wrapchr = tmpLyr.wrapChar.isEmpty() ? "\n" : tmpLyr.wrapChar;
99 
100  //add the direction symbol if needed
101  if ( !txt.isEmpty() && tmpLyr.placement == QgsPalLayerSettings::Line && tmpLyr.addDirectionSymbol )
102  {
103  bool prependSymb = false;
104  QString symb = tmpLyr.rightDirectionSymbol;
105 
106  if ( label->getReversed() )
107  {
108  prependSymb = true;
109  symb = tmpLyr.leftDirectionSymbol;
110  }
111 
112  if ( tmpLyr.reverseDirectionSymbol )
113  {
114  if ( symb == tmpLyr.rightDirectionSymbol )
115  {
116  prependSymb = true;
117  symb = tmpLyr.leftDirectionSymbol;
118  }
119  else
120  {
121  prependSymb = false;
122  symb = tmpLyr.rightDirectionSymbol;
123  }
124  }
125 
127  {
128  prependSymb = true;
129  symb = symb + wrapchr;
130  }
132  {
133  prependSymb = false;
134  symb = wrapchr + symb;
135  }
136 
137  if ( prependSymb )
138  {
139  txt.prepend( symb );
140  }
141  else
142  {
143  txt.append( symb );
144  }
145  }
146 
147  txt = txt.replace( wrapchr, "\\P" );
148 
149  if ( tmpLyr.textFont.underline() )
150  {
151  txt.prepend( "\\L" ).append( "\\l" );
152  }
153 
154  if ( tmpLyr.textFont.overline() )
155  {
156  txt.prepend( "\\O" ).append( "\\o" );
157  }
158 
159  if ( tmpLyr.textFont.strikeOut() )
160  {
161  txt.prepend( "\\K" ).append( "\\k" );
162  }
163 
164  txt.prepend( QString( "\\f%1|i%2|b%3;\\H%4;\\W0.75;" )
165  .arg( tmpLyr.textFont.family() )
166  .arg( tmpLyr.textFont.italic() ? 1 : 0 )
167  .arg( tmpLyr.textFont.bold() ? 1 : 0 )
168  .arg( label->getHeight() / ( 1 + txt.count( "\\P" ) ) * 0.75 ) );
169 
170  mDxfExport->writeMText( g->dxfLayer(), txt, QgsPoint( label->getX(), label->getY() ), label->getWidth() * 1.1, angle, tmpLyr.textColor );
171  }
172 }
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
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.
virtual Q_DECL_DEPRECATED void init(QgsMapRenderer *mr) override
called when we're going to start with rendering
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.
void drawLabel(pal::LabelPosition *label, QgsRenderContext &context, QgsPalLayerSettings &tmpLyr, DrawLabelType drawType, double dpiRatio=1.0) override
drawLabel
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)
draw dxf primitives
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:136
QString text()
LabelPosition is a candidate feature label position.
Definition: labelposition.h:53
double getX(int i=0) const
get the down-left x coordinate
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