QGIS API Documentation  2.13.0-Master
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 "qgstextlabelfeature.h"
21 #include "qgspallabeling.h"
22 #include "qgsmapsettings.h"
23 
24 #include "pal/feature.h"
25 #include "pal/pointset.h"
26 #include "pal/labelposition.h"
27 
28 
30  : QgsVectorLayerLabelProvider( layer, false )
31  , mDxfExport( dxf )
32 {
33 }
34 
36 {
37  Q_UNUSED( context );
38 
39  //debug: print label infos
40  if ( mDxfExport )
41  {
42  QgsTextLabelFeature* lf = dynamic_cast<QgsTextLabelFeature*>( label->getFeaturePart()->feature() );
43  if ( !lf )
44  return;
45 
46  const QgsPalLayerSettings& tmpLyr = mSettings;
47 
48  //label text
49  QString txt = lf->text( label->getPartId() );
50 
51  //angle
52  double angle = label->getAlpha() * 180 / M_PI;
53 
54  QgsFeatureId fid = label->getFeaturePart()->featureId();
55  QString dxfLayer = mDxfLayerNames[fid];
56 
57  //debug: show label rectangle
58 #if 0
59  QgsPolyline line;
60  for ( int i = 0; i < 4; ++i )
61  {
62  line.append( QgsPoint( label->getX( i ), label->getY( i ) ) );
63  }
64  mDxfExport->writePolyline( line, dxfLayer, "CONTINUOUS", 1, 0.01, true );
65 #endif
66 
67  QString wrapchr = tmpLyr.wrapChar.isEmpty() ? "\n" : tmpLyr.wrapChar;
68 
69  //add the direction symbol if needed
70  if ( !txt.isEmpty() && tmpLyr.placement == QgsPalLayerSettings::Line && tmpLyr.addDirectionSymbol )
71  {
72  bool prependSymb = false;
73  QString symb = tmpLyr.rightDirectionSymbol;
74 
75  if ( label->getReversed() )
76  {
77  prependSymb = true;
78  symb = tmpLyr.leftDirectionSymbol;
79  }
80 
81  if ( tmpLyr.reverseDirectionSymbol )
82  {
83  if ( symb == tmpLyr.rightDirectionSymbol )
84  {
85  prependSymb = true;
86  symb = tmpLyr.leftDirectionSymbol;
87  }
88  else
89  {
90  prependSymb = false;
91  symb = tmpLyr.rightDirectionSymbol;
92  }
93  }
94 
96  {
97  prependSymb = true;
98  symb = symb + wrapchr;
99  }
101  {
102  prependSymb = false;
103  symb = wrapchr + symb;
104  }
105 
106  if ( prependSymb )
107  {
108  txt.prepend( symb );
109  }
110  else
111  {
112  txt.append( symb );
113  }
114  }
115 
116  txt = txt.replace( wrapchr, "\\P" );
117 
118  if ( tmpLyr.textFont.underline() )
119  {
120  txt.prepend( "\\L" ).append( "\\l" );
121  }
122 
123  if ( tmpLyr.textFont.overline() )
124  {
125  txt.prepend( "\\O" ).append( "\\o" );
126  }
127 
128  if ( tmpLyr.textFont.strikeOut() )
129  {
130  txt.prepend( "\\K" ).append( "\\k" );
131  }
132 
133  txt.prepend( QString( "\\f%1|i%2|b%3;\\H%4;\\W0.75;" )
134  .arg( tmpLyr.textFont.family() )
135  .arg( tmpLyr.textFont.italic() ? 1 : 0 )
136  .arg( tmpLyr.textFont.bold() ? 1 : 0 )
137  .arg( label->getHeight() / ( 1 + txt.count( "\\P" ) ) * 0.75 ) );
138 
139  mDxfExport->writeMText( dxfLayer, txt, QgsPoint( label->getX(), label->getY() ), label->getWidth() * 1.1, angle, tmpLyr.textColor );
140  }
141 }
142 
143 void QgsDxfLabelProvider::registerDxfFeature( QgsFeature& feature, QgsRenderContext& context, const QString& dxfLayerName )
144 {
145  registerFeature( feature, context );
146  mDxfLayerNames[feature.id()] = dxfLayerName;
147 }
QgsFeatureId id() const
Get the feature ID for this feature.
Definition: qgsfeature.cpp:65
QString & append(QChar ch)
QgsLabelFeature * feature()
Returns the parent feature.
Definition: feature.h:109
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.
FeaturePart * getFeaturePart()
return the feature corresponding to this labelposition
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:187
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
QgsPalLayerSettings mSettings
Layer&#39;s labeling configuration.
bool isEmpty() const
double getY(int i=0) const
get the down-left y coordinate
#define M_PI
bool underline() const
double getX(int i=0) const
get the down-left x coordinate
A class to represent a point.
Definition: qgspoint.h:65
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
virtual void registerFeature(QgsFeature &feature, QgsRenderContext &context, QgsGeometry *obstacleGeometry=nullptr)
Register a feature for labeling as one or more QgsLabelFeature objects stored into mLabels...
double getAlpha() const
get alpha
QString & replace(int position, int n, QChar after)
Arranges candidates over a point (or centroid of a polygon), or at a preset offset from the point...
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:50
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
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)
QgsFeatureId featureId() const
Returns the unique ID of the feature.
Definition: feature.cpp:154
DirectionSymbols placeDirectionSymbol