QGIS API Documentation  2.8.6-Wien
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 txt = g->text( label->getPartId() );
83 
84  //angle
85  double angle = label->getAlpha() * 180 / M_PI;
86 
87  //debug: show label rectangle
88 #if 0
89  QgsPolyline line;
90  for ( int i = 0; i < 4; ++i )
91  {
92  line.append( QgsPoint( label->getX( i ), label->getY( i ) ) );
93  }
94  mDxfExport->writePolyline( line, g->dxfLayer(), "CONTINUOUS", 1, 0.01, true );
95 #endif
96 
97  QString wrapchr = tmpLyr.wrapChar.isEmpty() ? "\n" : tmpLyr.wrapChar;
98 
99  //add the direction symbol if needed
100  if ( !txt.isEmpty() && tmpLyr.placement == QgsPalLayerSettings::Line && tmpLyr.addDirectionSymbol )
101  {
102  bool prependSymb = false;
103  QString symb = tmpLyr.rightDirectionSymbol;
104 
105  if ( label->getReversed() )
106  {
107  prependSymb = true;
108  symb = tmpLyr.leftDirectionSymbol;
109  }
110 
111  if ( tmpLyr.reverseDirectionSymbol )
112  {
113  if ( symb == tmpLyr.rightDirectionSymbol )
114  {
115  prependSymb = true;
116  symb = tmpLyr.leftDirectionSymbol;
117  }
118  else
119  {
120  prependSymb = false;
121  symb = tmpLyr.rightDirectionSymbol;
122  }
123  }
124 
126  {
127  prependSymb = true;
128  symb = symb + wrapchr;
129  }
131  {
132  prependSymb = false;
133  symb = wrapchr + symb;
134  }
135 
136  if ( prependSymb )
137  {
138  txt.prepend( symb );
139  }
140  else
141  {
142  txt.append( symb );
143  }
144  }
145 
146  txt = txt.replace( wrapchr, "\\P" );
147 
148  if ( tmpLyr.textFont.underline() )
149  {
150  txt.prepend( "\\L" ).append( "\\l" );
151  }
152 
153  if ( tmpLyr.textFont.overline() )
154  {
155  txt.prepend( "\\O" ).append( "\\o" );
156  }
157 
158  if ( tmpLyr.textFont.strikeOut() )
159  {
160  txt.prepend( "\\K" ).append( "\\k" );
161  }
162 
163  txt.prepend( QString( "\\f%1|i%2|b%3;\\H%4;\\W0.75;" )
164  .arg( tmpLyr.textFont.family() )
165  .arg( tmpLyr.textFont.italic() ? 1 : 0 )
166  .arg( tmpLyr.textFont.bold() ? 1 : 0 )
167  .arg( label->getHeight() / ( 1 + txt.count( "\\P" ) ) * 0.75 ) );
168 
169  mDxfExport->writeMText( g->dxfLayer(), txt, QgsPoint( label->getX(), label->getY() ), label->getWidth() * 1.1, angle, tmpLyr.textColor );
170  }
171 }
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.
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&#39;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&#39;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)
void writePolyline(const QgsPolyline &line, const QString &layer, const QString &lineStyleName, QColor color, double width=-1, bool unusedPolygonFlag=false)
draw dxf primitives
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)
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:135
QString text()
LabelPositon is a candidate feature label position.
Definition: labelposition.h:53
UnitType
Map units that qgis supports.
Definition: qgis.h:229
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)
Write mtext (MTEXT)
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