QGIS API Documentation  2.12.0-Lyon
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 "qgspallabeling.h"
22 #include "qgsmapsettings.h"
23 
24 #include "pal/pointset.h"
25 #include "pal/labelposition.h"
26 
27 
29  : QgsVectorLayerLabelProvider( layer, false )
30  , mDxfExport( dxf )
31 {
32 }
33 
35 {
36  Q_UNUSED( context );
37 
38  //debug: print label infos
39  if ( mDxfExport )
40  {
41  QgsTextLabelFeature* lf = dynamic_cast<QgsTextLabelFeature*>( label->getFeaturePart()->feature() );
42  if ( !lf )
43  return;
44 
45  const QgsPalLayerSettings& tmpLyr = mSettings;
46 
47  //label text
48  QString txt = lf->text( label->getPartId() );
49 
50  //angle
51  double angle = label->getAlpha() * 180 / M_PI;
52 
53  QgsFeatureId fid = label->getFeaturePart()->featureId();
54  QString dxfLayer = mDxfLayerNames[fid];
55 
56  //debug: show label rectangle
57 #if 0
58  QgsPolyline line;
59  for ( int i = 0; i < 4; ++i )
60  {
61  line.append( QgsPoint( label->getX( i ), label->getY( i ) ) );
62  }
63  mDxfExport->writePolyline( line, dxfLayer, "CONTINUOUS", 1, 0.01, true );
64 #endif
65 
66  QString wrapchr = tmpLyr.wrapChar.isEmpty() ? "\n" : tmpLyr.wrapChar;
67 
68  //add the direction symbol if needed
69  if ( !txt.isEmpty() && tmpLyr.placement == QgsPalLayerSettings::Line && tmpLyr.addDirectionSymbol )
70  {
71  bool prependSymb = false;
72  QString symb = tmpLyr.rightDirectionSymbol;
73 
74  if ( label->getReversed() )
75  {
76  prependSymb = true;
77  symb = tmpLyr.leftDirectionSymbol;
78  }
79 
80  if ( tmpLyr.reverseDirectionSymbol )
81  {
82  if ( symb == tmpLyr.rightDirectionSymbol )
83  {
84  prependSymb = true;
85  symb = tmpLyr.leftDirectionSymbol;
86  }
87  else
88  {
89  prependSymb = false;
90  symb = tmpLyr.rightDirectionSymbol;
91  }
92  }
93 
95  {
96  prependSymb = true;
97  symb = symb + wrapchr;
98  }
100  {
101  prependSymb = false;
102  symb = wrapchr + symb;
103  }
104 
105  if ( prependSymb )
106  {
107  txt.prepend( symb );
108  }
109  else
110  {
111  txt.append( symb );
112  }
113  }
114 
115  txt = txt.replace( wrapchr, "\\P" );
116 
117  if ( tmpLyr.textFont.underline() )
118  {
119  txt.prepend( "\\L" ).append( "\\l" );
120  }
121 
122  if ( tmpLyr.textFont.overline() )
123  {
124  txt.prepend( "\\O" ).append( "\\o" );
125  }
126 
127  if ( tmpLyr.textFont.strikeOut() )
128  {
129  txt.prepend( "\\K" ).append( "\\k" );
130  }
131 
132  txt.prepend( QString( "\\f%1|i%2|b%3;\\H%4;\\W0.75;" )
133  .arg( tmpLyr.textFont.family() )
134  .arg( tmpLyr.textFont.italic() ? 1 : 0 )
135  .arg( tmpLyr.textFont.bold() ? 1 : 0 )
136  .arg( label->getHeight() / ( 1 + txt.count( "\\P" ) ) * 0.75 ) );
137 
138  mDxfExport->writeMText( dxfLayer, txt, QgsPoint( label->getX(), label->getY() ), label->getWidth() * 1.1, angle, tmpLyr.textColor );
139  }
140 }
141 
142 void QgsDxfLabelProvider::registerDxfFeature( QgsFeature& feature, QgsRenderContext& context, const QString& dxfLayerName )
143 {
144  registerFeature( feature, context );
145  mDxfLayerNames[feature.id()] = dxfLayerName;
146 }
QgsFeatureId id() const
Get the feature ID for this feature.
Definition: qgsfeature.cpp:53
FeaturePart * getFeaturePart()
return the feature corresponding to this labelposition
QString & append(QChar ch)
virtual void registerFeature(QgsFeature &feature, QgsRenderContext &context)
Register a feature for labeling as one or more QgsLabelFeature objects stored into mLabels...
QgsLabelFeature * feature()
Returns the parent feature.
Definition: feature.h:140
void append(const T &value)
double getWidth() const
QgsDxfLabelProvider(QgsVectorLayer *layer, QgsDxfExport *dxf)
construct the provider
QString & prepend(QChar ch)
Class that adds extra information to QgsLabelFeature for text labels.
The QgsVectorLayerLabelProvider class implements a label provider for vector layers.
QgsFeatureId featureId() const
Returns the unique ID of the feature.
Definition: feature.cpp:152
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:176
virtual void drawLabel(QgsRenderContext &context, pal::LabelPosition *label) const override
re-implementation that writes to DXF file instead of drawing with QPainter
bool bold() const
bool italic() const
double getY(int i=0) const
get the down-left y coordinate
double getAlpha() const
get alpha
QgsPalLayerSettings mSettings
Layer's labeling configuration.
bool isEmpty() const
#define M_PI
bool underline() const
A class to represent a point.
Definition: qgspoint.h:63
void writePolyline(const QgsPolyline &line, const QString &layer, const QString &lineStyleName, const QColor &color, double width=-1)
Draw dxf primitives (LWPOLYLINE)
bool getReversed() const
QMap< QgsFeatureId, QString > mDxfLayerNames
DXF layer name for each label feature.
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
QString & replace(int position, int n, QChar after)
Contains information about the context of a rendering operation.
int getPartId() const
QString text(int partId) const
Returns the text component corresponding to a specified label part.
double getHeight() const
QgsDxfExport * mDxfExport
pointer to parent DXF export where this instance is used
int count() const
QString family() const
LabelPosition is a candidate feature label position.
Definition: labelposition.h:48
qint64 QgsFeatureId
Definition: qgsfeature.h:31
void registerDxfFeature(QgsFeature &feature, QgsRenderContext &context, const QString &dxfLayerName)
registration method that keeps track of DXF layer names of individual features
double getX(int i=0) const
get the down-left x coordinate
bool strikeOut() const
Represents a vector layer which manages a vector based data sets.
void writeMText(const QString &layer, const QString &text, const QgsPoint &pt, double width, double angle, const QColor &color)
Write mtext (MTEXT)
DirectionSymbols placeDirectionSymbol