QGIS API Documentation  2.10.1-Pisa
 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 "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 );
54  mRenderContext.setMapToPixel( QgsMapToPixel( 1.0 / factor, bbox.xMinimum(), bbox.yMinimum(), bbox.height() * factor ) );
56 }
57 
59 {
60  delete mPainter;
61  delete mImage;
62  delete mSettings;
63 }
64 
65 void QgsDxfPalLabeling::drawLabel( pal::LabelPosition* label, QgsRenderContext& context, QgsPalLayerSettings& tmpLyr, DrawLabelType drawType, double dpiRatio )
66 {
67  Q_UNUSED( context );
68  Q_UNUSED( drawType );
69  Q_UNUSED( dpiRatio );
70 
71  if ( drawType == QgsPalLabeling::LabelBuffer )
72  {
73  return;
74  }
75 
76  //debug: print label infos
77  if ( mDxfExport )
78  {
79  QgsPalGeometry *g = dynamic_cast< QgsPalGeometry* >( label->getFeaturePart()->getUserGeometry() );
80  if ( !g )
81  return;
82 
83  //label text
84  QString txt = g->text( label->getPartId() );
85 
86  //angle
87  double angle = label->getAlpha() * 180 / M_PI;
88 
89  //debug: show label rectangle
90 #if 0
91  QgsPolyline line;
92  for ( int i = 0; i < 4; ++i )
93  {
94  line.append( QgsPoint( label->getX( i ), label->getY( i ) ) );
95  }
96  mDxfExport->writePolyline( line, g->dxfLayer(), "CONTINUOUS", 1, 0.01, true );
97 #endif
98 
99  QString wrapchr = tmpLyr.wrapChar.isEmpty() ? "\n" : tmpLyr.wrapChar;
100 
101  //add the direction symbol if needed
102  if ( !txt.isEmpty() && tmpLyr.placement == QgsPalLayerSettings::Line && tmpLyr.addDirectionSymbol )
103  {
104  bool prependSymb = false;
105  QString symb = tmpLyr.rightDirectionSymbol;
106 
107  if ( label->getReversed() )
108  {
109  prependSymb = true;
110  symb = tmpLyr.leftDirectionSymbol;
111  }
112 
113  if ( tmpLyr.reverseDirectionSymbol )
114  {
115  if ( symb == tmpLyr.rightDirectionSymbol )
116  {
117  prependSymb = true;
118  symb = tmpLyr.leftDirectionSymbol;
119  }
120  else
121  {
122  prependSymb = false;
123  symb = tmpLyr.rightDirectionSymbol;
124  }
125  }
126 
128  {
129  prependSymb = true;
130  symb = symb + wrapchr;
131  }
133  {
134  prependSymb = false;
135  symb = wrapchr + symb;
136  }
137 
138  if ( prependSymb )
139  {
140  txt.prepend( symb );
141  }
142  else
143  {
144  txt.append( symb );
145  }
146  }
147 
148  txt = txt.replace( wrapchr, "\\P" );
149 
150  if ( tmpLyr.textFont.underline() )
151  {
152  txt.prepend( "\\L" ).append( "\\l" );
153  }
154 
155  if ( tmpLyr.textFont.overline() )
156  {
157  txt.prepend( "\\O" ).append( "\\o" );
158  }
159 
160  if ( tmpLyr.textFont.strikeOut() )
161  {
162  txt.prepend( "\\K" ).append( "\\k" );
163  }
164 
165  txt.prepend( QString( "\\f%1|i%2|b%3;\\H%4;\\W0.75;" )
166  .arg( tmpLyr.textFont.family() )
167  .arg( tmpLyr.textFont.italic() ? 1 : 0 )
168  .arg( tmpLyr.textFont.bold() ? 1 : 0 )
169  .arg( label->getHeight() / ( 1 + txt.count( "\\P" ) ) * 0.75 ) );
170 
171  mDxfExport->writeMText( g->dxfLayer(), txt, QgsPoint( label->getX(), label->getY() ), label->getWidth() * 1.1, angle, tmpLyr.textColor );
172  }
173 }
FeaturePart * getFeaturePart()
return the feature corresponding to this labelposition
A rectangle specified with double values.
Definition: qgsrectangle.h:35
void setDotsPerMeterX(int x)
void setDotsPerMeterY(int y)
QString & append(QChar ch)
QgsDxfPalLabeling(QgsDxfExport *dxf, const QgsRectangle &bbox, double scale, QGis::UnitType mapUnits)
void append(const T &value)
double getWidth() const
void setOutputDpi(int dpi)
Set DPI used for conversion between real world units (e.g. mm) and pixels.
QString & prepend(QChar ch)
void setRendererScale(double scale)
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:464
void setExtent(const QgsRectangle &extent)
PalGeometry * getUserGeometry()
Definition: feature.h:298
bool bold() const
bool italic() const
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:197
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
bool isEmpty() const
QString dxfLayer() const
#define M_PI
void setPainter(QPainter *p)
bool underline() const
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)
bool overline() const
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:465
QString & replace(int position, int n, QChar after)
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.
int count() const
QString family() const
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:54
UnitType
Map units that qgis supports.
Definition: qgis.h:229
double getX(int i=0) const
get the down-left x coordinate
bool strikeOut() const
double width() const
Width of the rectangle.
Definition: qgsrectangle.h:202
void writePolyline(const QgsPolyline &line, const QString &layer, const QString &lineStyleName, QColor color, double width=-1)
Draw dxf primitives (LWPOLYLINE)
double xMinimum() const
Get the x minimum value (left side of rectangle)
Definition: qgsrectangle.h:187
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:207
void setCrsTransformEnabled(bool enabled)
sets whether to use projections for this layer set
DirectionSymbols placeDirectionSymbol