QGIS API Documentation  2.8.2-Wien
 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 );
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 }