Skip to content

Commit 1535ff8

Browse files
committedNov 23, 2017
[dxf][feature] Add option to export labels as text elements, instead of mtext
Sponsored by SMEC
1 parent d236942 commit 1535ff8

File tree

7 files changed

+199
-92
lines changed

7 files changed

+199
-92
lines changed
 

‎python/core/dxf/qgsdxfexport.sip

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ class QgsDxfExport
2727
SymbolLayerSymbology
2828
};
2929

30+
enum Flag
31+
{
32+
FlagNoMText,
33+
};
34+
typedef QFlags<QgsDxfExport::Flag> Flags;
35+
36+
3037
QgsDxfExport();
3138
%Docstring
3239
Constructor for QgsDxfExport.
@@ -38,6 +45,21 @@ class QgsDxfExport
3845
\param settings map settings to apply
3946
%End
4047

48+
void setFlags( QgsDxfExport::Flags flags );
49+
%Docstring
50+
Sets the export flags.
51+
.. versionadded:: 3.0
52+
.. seealso:: flags()
53+
%End
54+
55+
QgsDxfExport::Flags flags() const;
56+
%Docstring
57+
Returns the export flags.
58+
.. versionadded:: 3.0
59+
.. seealso:: setFlags()
60+
:rtype: QgsDxfExport.Flags
61+
%End
62+
4163
void addLayers( const QList< QPair<QgsVectorLayer *, int > > &layers );
4264
%Docstring
4365
Add layers to export
@@ -342,6 +364,9 @@ return list of available DXF encodings
342364

343365
};
344366

367+
QFlags<QgsDxfExport::Flag> operator|(QgsDxfExport::Flag f1, QFlags<QgsDxfExport::Flag> f2);
368+
369+
345370
/************************************************************************
346371
* This file has been generated automatically from *
347372
* *

‎src/app/qgisapp.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5700,6 +5700,12 @@ void QgisApp::dxfExport()
57005700
dxfExport.setLayerTitleAsName( d.layerTitleAsName() );
57015701
dxfExport.setDestinationCrs( d.crs() );
57025702
dxfExport.setForce2d( d.force2d() );
5703+
5704+
QgsDxfExport::Flags flags = 0;
5705+
if ( !d.useMText() )
5706+
flags = flags | QgsDxfExport::FlagNoMText;
5707+
dxfExport.setFlags( flags );
5708+
57035709
if ( mapCanvas() )
57045710
{
57055711
//extent

‎src/app/qgsdxfexportdialog.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,7 @@ QgsDxfExportDialog::QgsDxfExportDialog( QWidget *parent, Qt::WindowFlags f )
458458
mScaleWidget->setScale( 1.0 / oldScale );
459459
mLayerTitleAsName->setChecked( QgsProject::instance()->readEntry( QStringLiteral( "dxf" ), QStringLiteral( "/lastDxfLayerTitleAsName" ), s.value( QStringLiteral( "qgis/lastDxfLayerTitleAsName" ), "false" ).toString() ) != QLatin1String( "false" ) );
460460
mMapExtentCheckBox->setChecked( QgsProject::instance()->readEntry( QStringLiteral( "dxf" ), QStringLiteral( "/lastDxfMapRectangle" ), s.value( QStringLiteral( "qgis/lastDxfMapRectangle" ), "false" ).toString() ) != QLatin1String( "false" ) );
461+
mMTextCheckBox->setChecked( QgsProject::instance()->readEntry( QStringLiteral( "dxf" ), QStringLiteral( "/lastDxfUseMText" ), s.value( QStringLiteral( "qgis/lastDxfUseMText" ), "true" ).toString() ) != QLatin1String( "false" ) );
461462

462463
QStringList ids = QgsProject::instance()->mapThemeCollection()->mapThemes();
463464
ids.prepend( QLatin1String( "" ) );
@@ -608,7 +609,12 @@ bool QgsDxfExportDialog::layerTitleAsName() const
608609

609610
bool QgsDxfExportDialog::force2d() const
610611
{
611-
return mForce2d->isChecked();
612+
return mForce2d->isChecked();
613+
}
614+
615+
bool QgsDxfExportDialog::useMText() const
616+
{
617+
return mMTextCheckBox->isChecked();
612618
}
613619

614620
void QgsDxfExportDialog::saveSettings()
@@ -622,6 +628,7 @@ void QgsDxfExportDialog::saveSettings()
622628
s.setValue( QStringLiteral( "qgis/lastDxfLayerTitleAsName" ), mLayerTitleAsName->isChecked() );
623629
s.setValue( QStringLiteral( "qgis/lastDxfEncoding" ), mEncoding->currentText() );
624630
s.setValue( QStringLiteral( "qgis/lastDxfCrs" ), QString::number( mCRS.srsid() ) );
631+
s.setValue( QStringLiteral( "qgis/lastDxfUseMText" ), mMTextCheckBox->isChecked() );
625632

626633
QgsProject::instance()->writeEntry( QStringLiteral( "dxf" ), QStringLiteral( "/lastDxfSymbologyMode" ), mSymbologyModeComboBox->currentIndex() );
627634
QgsProject::instance()->writeEntry( QStringLiteral( "dxf" ), QStringLiteral( "/lastSymbologyExportScale" ), mScaleWidget->scale() != 0 ? 1.0 / mScaleWidget->scale() : 0 );
@@ -630,6 +637,7 @@ void QgsDxfExportDialog::saveSettings()
630637
QgsProject::instance()->writeEntry( QStringLiteral( "dxf" ), QStringLiteral( "/lastDxfEncoding" ), mEncoding->currentText() );
631638
QgsProject::instance()->writeEntry( QStringLiteral( "dxf" ), QStringLiteral( "/lastVisibilityPreset" ), mVisibilityPresets->currentText() );
632639
QgsProject::instance()->writeEntry( QStringLiteral( "dxf" ), QStringLiteral( "/lastDxfCrs" ), QString::number( mCRS.srsid() ) );
640+
QgsProject::instance()->writeEntry( QStringLiteral( "dxf" ), QStringLiteral( "/lastDxfUseMText" ), mMTextCheckBox->isChecked() );
633641
}
634642

635643

‎src/app/qgsdxfexportdialog.h

100644100755
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class QgsDxfExportDialog : public QDialog, private Ui::QgsDxfExportDialogBase
8888
bool exportMapExtent() const;
8989
bool layerTitleAsName() const;
9090
bool force2d() const;
91+
bool useMText() const;
9192
QString mapTheme() const;
9293
QString encoding() const;
9394
QgsCoordinateReferenceSystem crs() const;

‎src/core/dxf/qgsdxfexport.cpp

100644100755
Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,16 @@ void QgsDxfExport::setMapSettings( const QgsMapSettings &settings )
389389
mMapSettings = settings;
390390
}
391391

392+
void QgsDxfExport::setFlags( QgsDxfExport::Flags flags )
393+
{
394+
mFlags = flags;
395+
}
396+
397+
QgsDxfExport::Flags QgsDxfExport::flags() const
398+
{
399+
return mFlags;
400+
}
401+
392402
void QgsDxfExport::addLayers( const QList< QPair< QgsVectorLayer *, int > > &layers )
393403
{
394404
QList<QgsMapLayer *> layerList;
@@ -3633,6 +3643,7 @@ void QgsDxfExport::writeText( const QString &layer, const QString &text, const Q
36333643
writeGroup( 1, text );
36343644
writeGroup( 50, angle );
36353645
writeGroup( 7, QStringLiteral( "STANDARD" ) ); // so far only support for standard font
3646+
writeGroup( 100, QStringLiteral( "AcDbText" ) );
36363647
}
36373648

36383649
void QgsDxfExport::writeMText( const QString &layer, const QString &text, const QgsPoint &pt, double width, double angle, const QColor &color )
@@ -4400,30 +4411,36 @@ void QgsDxfExport::drawLabel( const QString &layerId, QgsRenderContext &context,
44004411
}
44014412
}
44024413

4403-
txt = txt.replace( wrapchr, QLatin1String( "\\P" ) );
4404-
4405-
if ( tmpLyr.format().font().underline() )
4414+
if ( mFlags & FlagNoMText )
44064415
{
4407-
txt.prepend( "\\L" ).append( "\\l" );
4416+
writeText( dxfLayer, txt, QgsPoint( label->getX(), label->getY() ), label->getHeight(), label->getAlpha() * 180.0 / M_PI, tmpLyr.format().color() );
44084417
}
4409-
4410-
if ( tmpLyr.format().font().overline() )
4418+
else
44114419
{
4412-
txt.prepend( "\\O" ).append( "\\o" );
4413-
}
4420+
txt = txt.replace( wrapchr, QLatin1String( "\\P" ) );
44144421

4415-
if ( tmpLyr.format().font().strikeOut() )
4416-
{
4417-
txt.prepend( "\\K" ).append( "\\k" );
4418-
}
4422+
if ( tmpLyr.format().font().underline() )
4423+
{
4424+
txt.prepend( "\\L" ).append( "\\l" );
4425+
}
4426+
4427+
if ( tmpLyr.format().font().overline() )
4428+
{
4429+
txt.prepend( "\\O" ).append( "\\o" );
4430+
}
44194431

4420-
txt.prepend( QStringLiteral( "\\f%1|i%2|b%3;\\H%4;" )
4421-
.arg( tmpLyr.format().font().family() )
4422-
.arg( tmpLyr.format().font().italic() ? 1 : 0 )
4423-
.arg( tmpLyr.format().font().bold() ? 1 : 0 )
4424-
.arg( label->getHeight() / ( 1 + txt.count( QStringLiteral( "\\P" ) ) ) * 0.75 ) );
4432+
if ( tmpLyr.format().font().strikeOut() )
4433+
{
4434+
txt.prepend( "\\K" ).append( "\\k" );
4435+
}
44254436

4426-
writeMText( dxfLayer, txt, QgsPoint( label->getX(), label->getY() ), label->getWidth(), label->getAlpha() * 180.0 / M_PI, tmpLyr.format().color() );
4437+
txt.prepend( QStringLiteral( "\\f%1|i%2|b%3;\\H%4;" )
4438+
.arg( tmpLyr.format().font().family() )
4439+
.arg( tmpLyr.format().font().italic() ? 1 : 0 )
4440+
.arg( tmpLyr.format().font().bold() ? 1 : 0 )
4441+
.arg( label->getHeight() / ( 1 + txt.count( QStringLiteral( "\\P" ) ) ) * 0.75 ) );
4442+
writeMText( dxfLayer, txt, QgsPoint( label->getX(), label->getY() ), label->getWidth(), label->getAlpha() * 180.0 / M_PI, tmpLyr.format().color() );
4443+
}
44274444
}
44284445

44294446

‎src/core/dxf/qgsdxfexport.h

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,18 @@ class CORE_EXPORT QgsDxfExport
5353
public:
5454
enum SymbologyExport
5555
{
56-
NoSymbology = 0, //export only data
57-
FeatureSymbology, //Keeps the number of features and export symbology per feature (using the first symbol level)
58-
SymbolLayerSymbology //Exports one feature per symbol layer (considering symbol levels)
56+
NoSymbology = 0, //!< Export only data
57+
FeatureSymbology, //!< Keeps the number of features and export symbology per feature (using the first symbol level)
58+
SymbolLayerSymbology //!< Exports one feature per symbol layer (considering symbol levels)
5959
};
6060

61+
//! Export flags
62+
enum Flag
63+
{
64+
FlagNoMText = 1 << 1, //!< Export text as TEXT elements. If not set, text will be exported as MTEXT elements.
65+
};
66+
Q_DECLARE_FLAGS( Flags, Flag )
67+
6168
/**
6269
* Constructor for QgsDxfExport.
6370
*/
@@ -71,6 +78,20 @@ class CORE_EXPORT QgsDxfExport
7178
*/
7279
void setMapSettings( const QgsMapSettings &settings );
7380

81+
/**
82+
* Sets the export flags.
83+
* \since QGIS 3.0
84+
* \see flags()
85+
*/
86+
void setFlags( QgsDxfExport::Flags flags );
87+
88+
/**
89+
* Returns the export flags.
90+
* \since QGIS 3.0
91+
* \see setFlags()
92+
*/
93+
QgsDxfExport::Flags flags() const;
94+
7495
/**
7596
* Add layers to export
7697
* \param layers list of layers and corresponding attribute indexes that determine the layer name (-1 for original layer name or title)
@@ -440,6 +461,11 @@ class CORE_EXPORT QgsDxfExport
440461
QHash<QString, int> mLayerNameAttribute;
441462
double mFactor = 1.0;
442463
bool mForce2d = false;
464+
465+
QgsDxfExport::Flags mFlags = 0;
466+
443467
};
444468

469+
Q_DECLARE_OPERATORS_FOR_FLAGS( QgsDxfExport::Flags )
470+
445471
#endif // QGSDXFEXPORT_H

‎src/ui/qgsdxfexportdialogbase.ui

100644100755
Lines changed: 94 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,24 @@
1414
<string>DXF Export</string>
1515
</property>
1616
<layout class="QGridLayout" name="gridLayout_2">
17-
<item row="2" column="0">
18-
<widget class="QLabel" name="mSymbologyScaleLabel">
17+
<item row="1" column="0">
18+
<widget class="QLabel" name="mSymbologyModeLabel">
1919
<property name="text">
20-
<string>Symbology scale</string>
20+
<string>Symbology mode</string>
2121
</property>
2222
</widget>
2323
</item>
24-
<item row="0" column="2">
25-
<widget class="QToolButton" name="mFileSelectionButton">
24+
<item row="0" column="0">
25+
<widget class="QLabel" name="mSaveAsLabel">
2626
<property name="text">
27-
<string></string>
27+
<string>Save as</string>
2828
</property>
2929
</widget>
3030
</item>
31-
<item row="0" column="0">
32-
<widget class="QLabel" name="mSaveAsLabel">
31+
<item row="0" column="2">
32+
<widget class="QToolButton" name="mFileSelectionButton">
3333
<property name="text">
34-
<string>Save as</string>
34+
<string></string>
3535
</property>
3636
</widget>
3737
</item>
@@ -54,13 +54,16 @@
5454
</item>
5555
</widget>
5656
</item>
57-
<item row="1" column="0">
58-
<widget class="QLabel" name="mSymbologyModeLabel">
57+
<item row="2" column="0">
58+
<widget class="QLabel" name="mSymbologyScaleLabel">
5959
<property name="text">
60-
<string>Symbology mode</string>
60+
<string>Symbology scale</string>
6161
</property>
6262
</widget>
6363
</item>
64+
<item row="3" column="1" colspan="2">
65+
<widget class="QComboBox" name="mEncoding"/>
66+
</item>
6467
<item row="7" column="0" colspan="3">
6568
<widget class="QgsLayerTreeView" name="mTreeView">
6669
<property name="selectionMode">
@@ -81,8 +84,35 @@
8184
</property>
8285
</widget>
8386
</item>
84-
<item row="3" column="1" colspan="2">
85-
<widget class="QComboBox" name="mEncoding"/>
87+
<item row="5" column="1" colspan="2">
88+
<widget class="QgsProjectionSelectionWidget" name="mCrsSelector" native="true">
89+
<property name="focusPolicy">
90+
<enum>Qt::StrongFocus</enum>
91+
</property>
92+
</widget>
93+
</item>
94+
<item row="4" column="1" colspan="2">
95+
<widget class="QComboBox" name="mVisibilityPresets"/>
96+
</item>
97+
<item row="15" column="0" colspan="3">
98+
<widget class="QDialogButtonBox" name="buttonBox">
99+
<property name="orientation">
100+
<enum>Qt::Horizontal</enum>
101+
</property>
102+
<property name="standardButtons">
103+
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Help|QDialogButtonBox::Ok</set>
104+
</property>
105+
</widget>
106+
</item>
107+
<item row="0" column="1">
108+
<widget class="QLineEdit" name="mFileLineEdit"/>
109+
</item>
110+
<item row="5" column="0">
111+
<widget class="QLabel" name="label_3">
112+
<property name="text">
113+
<string>CRS</string>
114+
</property>
115+
</widget>
86116
</item>
87117
<item row="9" column="0" colspan="3">
88118
<layout class="QGridLayout" name="gridLayout_3">
@@ -115,70 +145,67 @@
115145
</item>
116146
</layout>
117147
</item>
118-
<item row="0" column="1">
119-
<widget class="QLineEdit" name="mFileLineEdit"/>
120-
</item>
121-
<item row="11" column="0" colspan="3">
122-
<widget class="QCheckBox" name="mMapExtentCheckBox">
123-
<property name="text">
124-
<string>Export features intersecting the current map extent</string>
125-
</property>
126-
</widget>
127-
</item>
128-
<item row="13" column="0" colspan="3">
129-
<widget class="QDialogButtonBox" name="buttonBox">
130-
<property name="orientation">
131-
<enum>Qt::Horizontal</enum>
132-
</property>
133-
<property name="standardButtons">
134-
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Help|QDialogButtonBox::Ok</set>
135-
</property>
136-
</widget>
137-
</item>
138-
<item row="4" column="1" colspan="2">
139-
<widget class="QComboBox" name="mVisibilityPresets"/>
140-
</item>
141-
<item row="4" column="0">
142-
<widget class="QLabel" name="mSymbologyScaleLabel_2">
143-
<property name="text">
144-
<string>Map themes</string>
145-
</property>
146-
</widget>
147-
</item>
148148
<item row="3" column="0">
149149
<widget class="QLabel" name="label">
150150
<property name="text">
151151
<string>Encoding</string>
152152
</property>
153153
</widget>
154154
</item>
155-
<item row="10" column="0" colspan="3">
156-
<widget class="QCheckBox" name="mLayerTitleAsName">
157-
<property name="text">
158-
<string>Use layer title as name if set</string>
159-
</property>
160-
</widget>
161-
</item>
162-
<item row="5" column="0">
163-
<widget class="QLabel" name="label_3">
155+
<item row="4" column="0">
156+
<widget class="QLabel" name="mSymbologyScaleLabel_2">
164157
<property name="text">
165-
<string>CRS</string>
166-
</property>
167-
</widget>
168-
</item>
169-
<item row="5" column="1" colspan="2">
170-
<widget class="QgsProjectionSelectionWidget" name="mCrsSelector" native="true">
171-
<property name="focusPolicy">
172-
<enum>Qt::StrongFocus</enum>
158+
<string>Map themes</string>
173159
</property>
174160
</widget>
175161
</item>
176-
<item row="12" column="0" colspan="3">
177-
<widget class="QCheckBox" name="mForce2d">
178-
<property name="text">
179-
<string>Force 2d output (eg. to support polyline width)</string>
180-
</property>
181-
</widget>
162+
<item row="11" column="0" colspan="3">
163+
<layout class="QGridLayout" name="gridLayout">
164+
<item row="1" column="0">
165+
<widget class="QCheckBox" name="mMapExtentCheckBox">
166+
<property name="text">
167+
<string>Export features intersecting the current map extent</string>
168+
</property>
169+
</widget>
170+
</item>
171+
<item row="0" column="0">
172+
<widget class="QCheckBox" name="mLayerTitleAsName">
173+
<property name="text">
174+
<string>Use layer title as name if set</string>
175+
</property>
176+
</widget>
177+
</item>
178+
<item row="2" column="0">
179+
<widget class="QCheckBox" name="mForce2d">
180+
<property name="text">
181+
<string>Force 2d output (eg. to support polyline width)</string>
182+
</property>
183+
</widget>
184+
</item>
185+
<item row="0" column="1">
186+
<widget class="QCheckBox" name="mMTextCheckBox">
187+
<property name="text">
188+
<string>Export labels as MTEXT elements</string>
189+
</property>
190+
<property name="checked">
191+
<bool>true</bool>
192+
</property>
193+
</widget>
194+
</item>
195+
<item row="0" column="2">
196+
<spacer name="horizontalSpacer_2">
197+
<property name="orientation">
198+
<enum>Qt::Horizontal</enum>
199+
</property>
200+
<property name="sizeHint" stdset="0">
201+
<size>
202+
<width>40</width>
203+
<height>20</height>
204+
</size>
205+
</property>
206+
</spacer>
207+
</item>
208+
</layout>
182209
</item>
183210
</layout>
184211
</widget>
@@ -211,9 +238,6 @@
211238
<tabstop>mTreeView</tabstop>
212239
<tabstop>mSelectAllButton</tabstop>
213240
<tabstop>mDeselectAllButton</tabstop>
214-
<tabstop>mLayerTitleAsName</tabstop>
215-
<tabstop>mMapExtentCheckBox</tabstop>
216-
<tabstop>mForce2d</tabstop>
217241
<tabstop>buttonBox</tabstop>
218242
</tabstops>
219243
<resources/>

0 commit comments

Comments
 (0)
Please sign in to comment.