|
37 | 37 | #include "qgsvectorlayer.h"
|
38 | 38 | #include "qgsmaplayerregistry.h"
|
39 | 39 | #include "qgsunittypes.h"
|
| 40 | +#include "qgstextlabelfeature.h" |
| 41 | + |
| 42 | +#include "pal/feature.h" |
| 43 | +#include "pal/pointset.h" |
| 44 | +#include "pal/labelposition.h" |
40 | 45 |
|
41 | 46 | #include <QIODevice>
|
42 | 47 |
|
@@ -942,12 +947,32 @@ void QgsDxfExport::writeEntities()
|
942 | 947 | attributes << layerAttr;
|
943 | 948 | }
|
944 | 949 |
|
945 |
| - QgsDxfLabelProvider* lp = new QgsDxfLabelProvider( vl, this ); |
946 |
| - engine.addProvider( lp ); |
947 |
| - if ( !lp->prepare( ctx, attributes ) ) |
| 950 | + const QgsAbstractVectorLayerLabeling *labeling = vl->labeling(); |
| 951 | + QgsDxfLabelProvider *lp = nullptr; |
| 952 | + QgsDxfRuleBasedLabelProvider *rblp = nullptr; |
| 953 | + if ( dynamic_cast<const QgsRuleBasedLabeling*>( labeling ) ) |
| 954 | + { |
| 955 | + const QgsRuleBasedLabeling *rbl = dynamic_cast<const QgsRuleBasedLabeling*>( labeling ); |
| 956 | + rblp = new QgsDxfRuleBasedLabelProvider( *rbl, vl, this ); |
| 957 | + rblp->reinit( vl ); |
| 958 | + engine.addProvider( rblp ); |
| 959 | + |
| 960 | + if ( !rblp->prepare( ctx, attributes ) ) |
| 961 | + { |
| 962 | + engine.removeProvider( rblp ); |
| 963 | + rblp = nullptr; |
| 964 | + } |
| 965 | + } |
| 966 | + else |
948 | 967 | {
|
949 |
| - engine.removeProvider( lp ); |
950 |
| - lp = nullptr; |
| 968 | + lp = new QgsDxfLabelProvider( vl, this, nullptr ); |
| 969 | + engine.addProvider( lp ); |
| 970 | + |
| 971 | + if ( !lp->prepare( ctx, attributes ) ) |
| 972 | + { |
| 973 | + engine.removeProvider( lp ); |
| 974 | + lp = nullptr; |
| 975 | + } |
951 | 976 | }
|
952 | 977 |
|
953 | 978 | if ( mSymbologyExport == QgsDxfExport::SymbolLayerSymbology &&
|
@@ -1012,6 +1037,10 @@ void QgsDxfExport::writeEntities()
|
1012 | 1037 | {
|
1013 | 1038 | lp->registerDxfFeature( fet, ctx, lName );
|
1014 | 1039 | }
|
| 1040 | + else if ( rblp ) |
| 1041 | + { |
| 1042 | + rblp->registerDxfFeature( fet, ctx, lName ); |
| 1043 | + } |
1015 | 1044 | }
|
1016 | 1045 | }
|
1017 | 1046 |
|
@@ -4169,3 +4198,112 @@ QString QgsDxfExport::layerName( QgsVectorLayer *vl ) const
|
4169 | 4198 | Q_ASSERT( vl );
|
4170 | 4199 | return mLayerTitleAsName && !vl->title().isEmpty() ? vl->title() : vl->name();
|
4171 | 4200 | }
|
| 4201 | + |
| 4202 | +void QgsDxfExport::drawLabel( QString layerId, QgsRenderContext& context, pal::LabelPosition* label, const QgsPalLayerSettings &settings ) |
| 4203 | +{ |
| 4204 | + Q_UNUSED( context ); |
| 4205 | + QgsTextLabelFeature* lf = dynamic_cast<QgsTextLabelFeature*>( label->getFeaturePart()->feature() ); |
| 4206 | + if ( !lf ) |
| 4207 | + return; |
| 4208 | + |
| 4209 | + //label text |
| 4210 | + QString txt = lf->text( label->getPartId() ); |
| 4211 | + |
| 4212 | + //angle |
| 4213 | + double angle = label->getAlpha() * 180 / M_PI; |
| 4214 | + |
| 4215 | + QgsFeatureId fid = label->getFeaturePart()->featureId(); |
| 4216 | + QString dxfLayer = mDxfLayerNames[layerId][fid]; |
| 4217 | + |
| 4218 | + //debug: show label rectangle |
| 4219 | +#if 0 |
| 4220 | + QgsPolyline line; |
| 4221 | + for ( int i = 0; i < 4; ++i ) |
| 4222 | + { |
| 4223 | + line.append( QgsPoint( label->getX( i ), label->getY( i ) ) ); |
| 4224 | + } |
| 4225 | + writePolyline( line, dxfLayer, "CONTINUOUS", 1, 0.01, true ); |
| 4226 | +#endif |
| 4227 | + |
| 4228 | + QString wrapchr = settings.wrapChar.isEmpty() ? "\n" : settings.wrapChar; |
| 4229 | + |
| 4230 | + //add the direction symbol if needed |
| 4231 | + if ( !txt.isEmpty() && settings.placement == QgsPalLayerSettings::Line && settings.addDirectionSymbol ) |
| 4232 | + { |
| 4233 | + bool prependSymb = false; |
| 4234 | + QString symb = settings.rightDirectionSymbol; |
| 4235 | + |
| 4236 | + if ( label->getReversed() ) |
| 4237 | + { |
| 4238 | + prependSymb = true; |
| 4239 | + symb = settings.leftDirectionSymbol; |
| 4240 | + } |
| 4241 | + |
| 4242 | + if ( settings.reverseDirectionSymbol ) |
| 4243 | + { |
| 4244 | + if ( symb == settings.rightDirectionSymbol ) |
| 4245 | + { |
| 4246 | + prependSymb = true; |
| 4247 | + symb = settings.leftDirectionSymbol; |
| 4248 | + } |
| 4249 | + else |
| 4250 | + { |
| 4251 | + prependSymb = false; |
| 4252 | + symb = settings.rightDirectionSymbol; |
| 4253 | + } |
| 4254 | + } |
| 4255 | + |
| 4256 | + if ( settings.placeDirectionSymbol == QgsPalLayerSettings::SymbolAbove ) |
| 4257 | + { |
| 4258 | + prependSymb = true; |
| 4259 | + symb = symb + wrapchr; |
| 4260 | + } |
| 4261 | + else if ( settings.placeDirectionSymbol == QgsPalLayerSettings::SymbolBelow ) |
| 4262 | + { |
| 4263 | + prependSymb = false; |
| 4264 | + symb = wrapchr + symb; |
| 4265 | + } |
| 4266 | + |
| 4267 | + if ( prependSymb ) |
| 4268 | + { |
| 4269 | + txt.prepend( symb ); |
| 4270 | + } |
| 4271 | + else |
| 4272 | + { |
| 4273 | + txt.append( symb ); |
| 4274 | + } |
| 4275 | + } |
| 4276 | + |
| 4277 | + txt = txt.replace( wrapchr, "\\P" ); |
| 4278 | + |
| 4279 | + if ( settings.textFont.underline() ) |
| 4280 | + { |
| 4281 | + txt.prepend( "\\L" ).append( "\\l" ); |
| 4282 | + } |
| 4283 | + |
| 4284 | + if ( settings.textFont.overline() ) |
| 4285 | + { |
| 4286 | + txt.prepend( "\\O" ).append( "\\o" ); |
| 4287 | + } |
| 4288 | + |
| 4289 | + if ( settings.textFont.strikeOut() ) |
| 4290 | + { |
| 4291 | + txt.prepend( "\\K" ).append( "\\k" ); |
| 4292 | + } |
| 4293 | + |
| 4294 | + txt.prepend( QString( "\\f%1|i%2|b%3;\\H%4;\\W0.75;" ) |
| 4295 | + .arg( settings.textFont.family() ) |
| 4296 | + .arg( settings.textFont.italic() ? 1 : 0 ) |
| 4297 | + .arg( settings.textFont.bold() ? 1 : 0 ) |
| 4298 | + .arg( label->getHeight() / ( 1 + txt.count( "\\P" ) ) * 0.75 ) ); |
| 4299 | + |
| 4300 | + writeMText( dxfLayer, txt, QgsPoint( label->getX(), label->getY() ), label->getWidth() * 1.1, angle, settings.textColor ); |
| 4301 | +} |
| 4302 | + |
| 4303 | +void QgsDxfExport::registerDxfLayer( QString layerId, QgsFeatureId fid, QString layerName ) |
| 4304 | +{ |
| 4305 | + if ( !mDxfLayerNames.contains( layerId ) ) |
| 4306 | + mDxfLayerNames[ layerId ] = QMap<QgsFeatureId, QString>(); |
| 4307 | + |
| 4308 | + mDxfLayerNames[layerId][fid] = layerName; |
| 4309 | +} |
0 commit comments