-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqgsdxfexportdialog.cpp
1325 lines (1113 loc) · 46.6 KB
/
qgsdxfexportdialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
qgsdxfexportdialog.cpp
----------------------
begin : September 2013
copyright : (C) 2013 by Marco Hugentobler
email : marco at sourcepole dot ch
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsdxfexportdialog.h"
#include "qgsmaplayer.h"
#include "qgslayertree.h"
#include "qgslayertreegroup.h"
#include "qgsvectorlayer.h"
#include "qgsproject.h"
#include "qgshelp.h"
#include "qgis.h"
#include "qgsfieldcombobox.h"
#include "qgisapp.h"
#include "qgsmapthemecollection.h"
#include "qgsmapcanvas.h"
#include "qgssettings.h"
#include "qgsgui.h"
#include <QFileDialog>
#include <QPushButton>
#include <QMessageBox>
const int LAYER_COL = 0;
const int OUTPUT_LAYER_ATTRIBUTE_COL = 1;
const int ALLOW_DD_SYMBOL_BLOCKS_COL = 2;
const int MAXIMUM_DD_SYMBOL_BLOCKS_COL = 3;
FieldSelectorDelegate::FieldSelectorDelegate( QObject *parent )
: QItemDelegate( parent )
{
}
QWidget *FieldSelectorDelegate::createEditor( QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const
{
Q_UNUSED( option )
QgsVectorLayer *vl = indexToLayer( index.model(), index );
if ( !vl )
return nullptr;
if ( index.column() == LAYER_COL )
{
QgsFilterLineEdit *le = new QgsFilterLineEdit( parent, vl->name() );
return le;
}
else if ( index.column() == OUTPUT_LAYER_ATTRIBUTE_COL )
{
QgsFieldComboBox *w = new QgsFieldComboBox( parent );
w->setLayer( vl );
w->setAllowEmptyFieldName( true );
return w;
}
else if ( index.column() == ALLOW_DD_SYMBOL_BLOCKS_COL )
{
return nullptr;
}
else if ( index.column() == MAXIMUM_DD_SYMBOL_BLOCKS_COL )
{
QLineEdit *le = new QLineEdit( parent );
le->setValidator( new QIntValidator( le ) );
return le;
}
return nullptr;
}
void FieldSelectorDelegate::setEditorData( QWidget *editor, const QModelIndex &index ) const
{
QgsVectorLayer *vl = indexToLayer( index.model(), index );
if ( !vl )
return;
if ( index.column() == LAYER_COL )
{
QgsFilterLineEdit *le = qobject_cast< QgsFilterLineEdit * >( editor );
if ( le )
{
le->setText( index.data().toString() );
}
}
else if ( index.column() == OUTPUT_LAYER_ATTRIBUTE_COL )
{
QgsFieldComboBox *fcb = qobject_cast<QgsFieldComboBox *>( editor );
if ( !fcb )
return;
int idx = attributeIndex( index.model(), vl );
if ( vl->fields().exists( idx ) )
{
fcb->setField( vl->fields().at( idx ).name() );
}
}
else if ( index.column() == MAXIMUM_DD_SYMBOL_BLOCKS_COL )
{
QLineEdit *le = qobject_cast<QLineEdit *>( editor );
if ( le )
{
le->setText( index.data().toString() );
}
}
}
void FieldSelectorDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
{
QgsVectorLayer *vl = indexToLayer( index.model(), index );
if ( !vl )
return;
if ( index.column() == LAYER_COL )
{
QgsFilterLineEdit *le = qobject_cast<QgsFilterLineEdit *>( editor );
if ( le )
{
model->setData( index, le->text() );
}
}
else if ( index.column() == OUTPUT_LAYER_ATTRIBUTE_COL )
{
QgsFieldComboBox *fcb = qobject_cast<QgsFieldComboBox *>( editor );
if ( !fcb )
return;
model->setData( index, vl->fields().lookupField( fcb->currentField() ) );
}
else if ( index.column() == MAXIMUM_DD_SYMBOL_BLOCKS_COL )
{
QLineEdit *le = qobject_cast<QLineEdit *>( editor );
if ( le )
{
model->setData( index, le->text().toInt() );
}
}
}
QgsVectorLayer *FieldSelectorDelegate::indexToLayer( const QAbstractItemModel *model, const QModelIndex &index ) const
{
const QgsLayerTreeProxyModel *proxy = qobject_cast< const QgsLayerTreeProxyModel *>( model );
Q_ASSERT( proxy );
const QgsVectorLayerAndAttributeModel *m = qobject_cast< const QgsVectorLayerAndAttributeModel *>( proxy->sourceModel() );
Q_ASSERT( m );
return m->vectorLayer( proxy->mapToSource( index ) );
}
int FieldSelectorDelegate::attributeIndex( const QAbstractItemModel *model, const QgsVectorLayer *vl ) const
{
const QgsLayerTreeProxyModel *proxy = qobject_cast< const QgsLayerTreeProxyModel *>( model );
Q_ASSERT( proxy );
const QgsVectorLayerAndAttributeModel *m = qobject_cast< const QgsVectorLayerAndAttributeModel *>( proxy->sourceModel() );
Q_ASSERT( m );
return m->attributeIndex( vl );
}
QgsVectorLayerAndAttributeModel::QgsVectorLayerAndAttributeModel( QgsLayerTree *rootNode, QObject *parent )
: QgsLayerTreeModel( rootNode, parent )
{
//init mCreateDDBlockInfo, mDDBlocksMaxNumberOfClasses
QSet<QString> layerIds;
retrieveAllLayers( rootNode, layerIds );
for ( const auto &id : std::as_const( layerIds ) )
{
const QgsVectorLayer *vLayer = qobject_cast< const QgsVectorLayer *>( QgsProject::instance()->mapLayer( id ) );
if ( vLayer )
{
mCreateDDBlockInfo[vLayer] = DEFAULT_DXF_DATA_DEFINED_BLOCKS;
mDDBlocksMaxNumberOfClasses[vLayer] = -1;
}
}
}
int QgsVectorLayerAndAttributeModel::columnCount( const QModelIndex &parent ) const
{
Q_UNUSED( parent )
return 4;
}
Qt::ItemFlags QgsVectorLayerAndAttributeModel::flags( const QModelIndex &index ) const
{
QgsVectorLayer *vl = vectorLayer( index );
if ( index.column() == LAYER_COL )
{
return vl ? Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable : Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
}
else if ( index.column() == OUTPUT_LAYER_ATTRIBUTE_COL )
{
return vl ? Qt::ItemIsEnabled | Qt::ItemIsEditable : Qt::ItemIsEnabled;
}
else if ( index.column() == ALLOW_DD_SYMBOL_BLOCKS_COL )
{
return ( vl && vl->geometryType() == Qgis::GeometryType::Point ) ? Qt::ItemIsEnabled | Qt::ItemIsUserCheckable : Qt::ItemIsEnabled ;
}
else if ( index.column() == MAXIMUM_DD_SYMBOL_BLOCKS_COL )
{
return vl && vl->geometryType() == Qgis::GeometryType::Point ? Qt::ItemIsEnabled | Qt::ItemIsEditable : Qt::ItemIsEnabled;
}
return Qt::ItemIsEnabled;
}
QgsVectorLayer *QgsVectorLayerAndAttributeModel::vectorLayer( const QModelIndex &idx ) const
{
QgsLayerTreeNode *n = index2node( idx );
if ( !n || !QgsLayerTree::isLayer( n ) )
return nullptr;
return qobject_cast<QgsVectorLayer *>( QgsLayerTree::toLayer( n )->layer() );
}
int QgsVectorLayerAndAttributeModel::attributeIndex( const QgsVectorLayer *vl ) const
{
return mAttributeIdx.value( vl, -1 );
}
QVariant QgsVectorLayerAndAttributeModel::headerData( int section, Qt::Orientation orientation, int role ) const
{
if ( orientation == Qt::Horizontal )
{
if ( role == Qt::DisplayRole )
{
if ( section == LAYER_COL )
return tr( "Layer" );
else if ( section == OUTPUT_LAYER_ATTRIBUTE_COL )
return tr( "Output Layer Attribute" );
else if ( section == ALLOW_DD_SYMBOL_BLOCKS_COL )
{
return tr( "Allow data defined symbol blocks" );
}
else if ( section == MAXIMUM_DD_SYMBOL_BLOCKS_COL )
{
return tr( "Maximum number of symbol blocks" );
}
}
else if ( role == Qt::ToolTipRole )
{
if ( section == OUTPUT_LAYER_ATTRIBUTE_COL )
return tr( "Attribute containing the name of the destination layer in the DXF output." );
}
}
return QVariant();
}
QVariant QgsVectorLayerAndAttributeModel::data( const QModelIndex &idx, int role ) const
{
QgsVectorLayer *vl = vectorLayer( idx );
if ( idx.column() == LAYER_COL )
{
if ( role == Qt::CheckStateRole )
{
if ( !idx.isValid() )
return QVariant();
if ( mCheckedLeafs.contains( idx ) )
return Qt::Checked;
bool hasChecked = false, hasUnchecked = false;
int n;
for ( n = 0; !hasChecked || !hasUnchecked; n++ )
{
QVariant v = data( index( n, 0, idx ), role );
if ( !v.isValid() )
break;
switch ( v.toInt() )
{
case Qt::PartiallyChecked:
// parent of partially checked child shared state
return Qt::PartiallyChecked;
case Qt::Checked:
hasChecked = true;
break;
case Qt::Unchecked:
hasUnchecked = true;
break;
}
}
// unchecked leaf
if ( n == 0 )
return Qt::Unchecked;
// both
if ( hasChecked && hasUnchecked )
return Qt::PartiallyChecked;
if ( hasChecked )
return Qt::Checked;
Q_ASSERT( hasUnchecked );
return Qt::Unchecked;
}
else if ( role == Qt::DisplayRole && vl && mOverriddenName.contains( vl ) )
{
return mOverriddenName[ vl ];
}
else
{
return QgsLayerTreeModel::data( idx, role );
}
}
else if ( idx.column() == OUTPUT_LAYER_ATTRIBUTE_COL && vl )
{
int idx = mAttributeIdx.value( vl, -1 );
if ( role == Qt::EditRole )
return idx;
if ( role == Qt::DisplayRole )
{
if ( vl->fields().exists( idx ) )
return vl->fields().at( idx ).name();
else
return mOverriddenName.contains( vl ) ? mOverriddenName[ vl ] : vl->name();
}
if ( role == Qt::ToolTipRole )
{
return tr( "Attribute containing the name of the destination layer in the DXF output." );
}
}
else if ( idx.column() == ALLOW_DD_SYMBOL_BLOCKS_COL )
{
if ( !vl || vl->geometryType() != Qgis::GeometryType::Point )
{
return QVariant();
}
bool checked = mCreateDDBlockInfo.contains( vl ) ? mCreateDDBlockInfo[vl] : DEFAULT_DXF_DATA_DEFINED_BLOCKS;
if ( role == Qt::CheckStateRole )
{
return checked ? Qt::Checked : Qt::Unchecked;
}
else
{
return QgsLayerTreeModel::data( idx, role );
}
}
else if ( idx.column() == MAXIMUM_DD_SYMBOL_BLOCKS_COL )
{
if ( !vl || vl->geometryType() != Qgis::GeometryType::Point )
{
return QVariant();
}
if ( role == Qt::DisplayRole )
{
if ( !mDDBlocksMaxNumberOfClasses.contains( vl ) )
{
return QVariant( -1 );
}
else
{
return QVariant( mDDBlocksMaxNumberOfClasses[vl] );
}
}
}
return QVariant();
}
bool QgsVectorLayerAndAttributeModel::setData( const QModelIndex &index, const QVariant &value, int role )
{
QgsVectorLayer *vl = vectorLayer( index );
if ( index.column() == LAYER_COL )
{
if ( role == Qt::CheckStateRole )
{
int i = 0;
for ( i = 0; ; i++ )
{
QModelIndex child = QgsVectorLayerAndAttributeModel::index( i, 0, index );
if ( !child.isValid() )
break;
setData( child, value, role );
}
if ( i == 0 )
{
if ( value.toInt() == Qt::Checked )
mCheckedLeafs.insert( index );
else if ( value.toInt() == Qt::Unchecked )
mCheckedLeafs.remove( index );
else
Q_ASSERT( "expected checked or unchecked" );
emit dataChanged( QModelIndex(), index );
}
return true;
}
else if ( role == Qt::EditRole )
{
if ( !value.toString().trimmed().isEmpty() && value.toString() != vl->name() )
{
mOverriddenName[ vl ] = value.toString();
}
else
{
mOverriddenName.remove( vl );
}
return true;
}
}
else if ( index.column() == OUTPUT_LAYER_ATTRIBUTE_COL )
{
if ( role != Qt::EditRole )
return false;
if ( vl )
{
mAttributeIdx[ vl ] = value.toInt();
return true;
}
}
else if ( index.column() == ALLOW_DD_SYMBOL_BLOCKS_COL && role == Qt::CheckStateRole )
{
if ( vl )
{
mCreateDDBlockInfo[ vl ] = value.toBool();
return true;
}
}
else if ( index.column() == MAXIMUM_DD_SYMBOL_BLOCKS_COL && role == Qt::EditRole )
{
if ( vl )
{
mDDBlocksMaxNumberOfClasses[ vl ] = value.toInt();
return true;
}
}
return QgsLayerTreeModel::setData( index, value, role );
}
QList< QgsDxfExport::DxfLayer > QgsVectorLayerAndAttributeModel::layers() const
{
QList< QgsDxfExport::DxfLayer > layers;
QHash< QString, int > layerIdx;
for ( const QModelIndex &idx : std::as_const( mCheckedLeafs ) )
{
QgsLayerTreeNode *node = index2node( idx );
if ( QgsLayerTree::isGroup( node ) )
{
const auto childLayers = QgsLayerTree::toGroup( node )->findLayers();
for ( QgsLayerTreeLayer *treeLayer : childLayers )
{
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( treeLayer->layer() );
Q_ASSERT( vl );
if ( !layerIdx.contains( vl->id() ) )
{
layerIdx.insert( vl->id(), layers.size() );
layers << QgsDxfExport::DxfLayer( vl,
mAttributeIdx.value( vl, -1 ),
mCreateDDBlockInfo.value( vl, DEFAULT_DXF_DATA_DEFINED_BLOCKS ),
mDDBlocksMaxNumberOfClasses.value( vl, -1 ),
mOverriddenName.value( vl, QString() ) );
}
}
}
else if ( QgsLayerTree::isLayer( node ) )
{
QgsVectorLayer *vl = qobject_cast< QgsVectorLayer *>( QgsLayerTree::toLayer( node )->layer() );
Q_ASSERT( vl );
if ( !layerIdx.contains( vl->id() ) )
{
layerIdx.insert( vl->id(), layers.size() );
layers << QgsDxfExport::DxfLayer( vl,
mAttributeIdx.value( vl, -1 ),
mCreateDDBlockInfo.value( vl, DEFAULT_DXF_DATA_DEFINED_BLOCKS ),
mDDBlocksMaxNumberOfClasses.value( vl, -1 ),
mOverriddenName.value( vl, QString() ) );
}
}
}
QList< QgsDxfExport::DxfLayer > layersInROrder;
QList<QgsMapLayer *> layerOrder = mRootNode->layerOrder();
QList<QgsMapLayer *>::ConstIterator layerIterator = layerOrder.constEnd();
while ( layerIterator != layerOrder.constBegin() )
{
--layerIterator;
QgsMapLayer *l = *layerIterator;
int idx = layerIdx.value( l->id(), -1 );
if ( idx < 0 )
continue;
layersInROrder << layers[idx];
}
Q_ASSERT( layersInROrder.size() == layers.size() );
return layersInROrder;
}
void QgsVectorLayerAndAttributeModel::applyVisibilityPreset( const QString &name )
{
QSet<QString> visibleLayers;
if ( name.isEmpty() )
{
const auto constLayers = QgisApp::instance()->mapCanvas()->layers( true );
for ( const QgsMapLayer *ml : constLayers )
{
const QgsVectorLayer *vl = qobject_cast<const QgsVectorLayer *>( ml );
if ( !vl )
continue;
visibleLayers.insert( vl->id() );
}
}
else
{
visibleLayers = qgis::listToSet( QgsProject::instance()->mapThemeCollection()->mapThemeVisibleLayerIds( name ) );
}
if ( visibleLayers.isEmpty() )
return;
mCheckedLeafs.clear();
applyVisibility( visibleLayers, rootGroup() );
}
void QgsVectorLayerAndAttributeModel::applyVisibility( QSet<QString> &visibleLayers, QgsLayerTreeNode *node )
{
QgsLayerTreeGroup *group = QgsLayerTree::isGroup( node ) ? QgsLayerTree::toGroup( node ) : nullptr;
if ( !group )
return;
const auto constChildren = node->children();
for ( QgsLayerTreeNode *child : constChildren )
{
if ( QgsLayerTree::isLayer( child ) )
{
QgsVectorLayer *vl = qobject_cast< QgsVectorLayer * >( QgsLayerTree::toLayer( child )->layer() );
if ( vl )
{
QModelIndex idx = node2index( child );
if ( visibleLayers.contains( vl->id() ) )
{
visibleLayers.remove( vl->id() );
mCheckedLeafs.insert( idx );
}
emit dataChanged( idx, idx, QVector<int>() << Qt::CheckStateRole );
}
continue;
}
applyVisibility( visibleLayers, child );
}
}
void QgsVectorLayerAndAttributeModel::loadLayersOutputAttribute( QgsLayerTreeNode *node )
{
const auto constChildren = node->children();
for ( QgsLayerTreeNode *child : constChildren )
{
if ( QgsLayerTree::isLayer( child ) )
{
QgsVectorLayer *vl = qobject_cast< QgsVectorLayer * >( QgsLayerTree::toLayer( child )->layer() );
if ( vl )
{
QModelIndex idx = node2index( child );
const int attributeIndex = vl->fields().lookupField( vl->customProperty( QStringLiteral( "lastDxfOutputAttribute" ), -1 ).toString() );
if ( attributeIndex > -1 )
{
mAttributeIdx[vl] = attributeIndex;
idx = index( idx.row(), OUTPUT_LAYER_ATTRIBUTE_COL, idx.parent() );
emit dataChanged( idx, idx, QVector<int>() << Qt::EditRole );
}
if ( vl->geometryType() == Qgis::GeometryType::Point )
{
const bool allowDataDefinedBlocks = vl->customProperty( QStringLiteral( "lastAllowDataDefinedBlocks" ), DEFAULT_DXF_DATA_DEFINED_BLOCKS ).toBool();
if ( allowDataDefinedBlocks != DEFAULT_DXF_DATA_DEFINED_BLOCKS )
{
mCreateDDBlockInfo[vl] = allowDataDefinedBlocks;
idx = index( idx.row(), ALLOW_DD_SYMBOL_BLOCKS_COL, idx.parent() );
emit dataChanged( idx, idx, QVector<int>() << Qt::CheckStateRole );
}
const int maximumNumberOfBlocks = vl->customProperty( QStringLiteral( "lastMaximumNumberOfBlocks" ), -1 ).toInt();
if ( maximumNumberOfBlocks > -1 )
{
mDDBlocksMaxNumberOfClasses[vl] = maximumNumberOfBlocks;
idx = index( idx.row(), MAXIMUM_DD_SYMBOL_BLOCKS_COL, idx.parent() );
emit dataChanged( idx, idx, QVector<int>() << Qt::EditRole );
}
}
}
}
else if ( QgsLayerTree::isGroup( child ) )
{
loadLayersOutputAttribute( child );
}
}
}
void QgsVectorLayerAndAttributeModel::saveLayersOutputAttribute( QgsLayerTreeNode *node )
{
const auto constChildren = node->children();
for ( QgsLayerTreeNode *child : constChildren )
{
if ( QgsLayerTree::isLayer( child ) )
{
QgsVectorLayer *vl = qobject_cast< QgsVectorLayer * >( QgsLayerTree::toLayer( child )->layer() );
if ( vl )
{
QModelIndex idx = node2index( child );
const int attributeIndex = data( index( idx.row(), OUTPUT_LAYER_ATTRIBUTE_COL, idx.parent() ), Qt::EditRole ).toInt();
const QgsFields fields = vl->fields();
if ( attributeIndex > -1 && attributeIndex < fields.count() )
{
vl->setCustomProperty( QStringLiteral( "lastDxfOutputAttribute" ), fields.at( attributeIndex ).name() );
}
else
{
vl->removeCustomProperty( QStringLiteral( "lastDxfOutputAttribute" ) );
}
if ( vl->geometryType() == Qgis::GeometryType::Point )
{
const bool allowDataDefinedBlocks = data( index( idx.row(), ALLOW_DD_SYMBOL_BLOCKS_COL, idx.parent() ), Qt::CheckStateRole ).toBool();
vl->setCustomProperty( QStringLiteral( "lastAllowDataDefinedBlocks" ), allowDataDefinedBlocks );
const int maximumNumberOfBlocks = data( index( idx.row(), MAXIMUM_DD_SYMBOL_BLOCKS_COL, idx.parent() ), Qt::DisplayRole ).toInt();
if ( maximumNumberOfBlocks > -1 )
{
vl->setCustomProperty( QStringLiteral( "lastMaximumNumberOfBlocks" ), maximumNumberOfBlocks );
}
else
{
vl->removeCustomProperty( QStringLiteral( "lastMaximumNumberOfBlocks" ) );
}
}
}
}
else if ( QgsLayerTree::isGroup( child ) )
{
saveLayersOutputAttribute( child );
}
}
}
void QgsVectorLayerAndAttributeModel::retrieveAllLayers( QgsLayerTreeNode *node, QSet<QString> &set )
{
if ( QgsLayerTree::isLayer( node ) )
{
set << QgsLayerTree::toLayer( node )->layer()->id();
}
else if ( QgsLayerTree::isGroup( node ) )
{
const auto constChildren = QgsLayerTree::toGroup( node )->children();
for ( QgsLayerTreeNode *child : constChildren )
{
retrieveAllLayers( child, set );
}
}
}
void QgsVectorLayerAndAttributeModel::selectAll()
{
mCheckedLeafs.clear();
QSet<QString> allLayers;
retrieveAllLayers( rootGroup(), allLayers );
applyVisibility( allLayers, rootGroup() );
emit dataChanged( index( 0, 0 ), index( rowCount() - 1, 0 ) );
}
void QgsVectorLayerAndAttributeModel::deSelectAll()
{
mCheckedLeafs.clear();
QSet<QString> noLayers;
applyVisibility( noLayers, rootGroup() );
emit dataChanged( index( 0, 0 ), index( rowCount() - 1, 0 ) );
}
void QgsVectorLayerAndAttributeModel::selectDataDefinedBlocks()
{
enableDataDefinedBlocks( true );
}
void QgsVectorLayerAndAttributeModel::deselectDataDefinedBlocks()
{
enableDataDefinedBlocks( false );
}
void QgsVectorLayerAndAttributeModel::enableDataDefinedBlocks( bool enabled )
{
QHash<const QgsVectorLayer *, bool>::const_iterator it = mCreateDDBlockInfo.constBegin();
for ( ; it != mCreateDDBlockInfo.constEnd(); ++it )
{
mCreateDDBlockInfo[it.key()] = enabled;
}
emit dataChanged( index( 0, 0 ), index( rowCount() - 1, 0 ) );
}
QgsDxfExportLayerTreeView::QgsDxfExportLayerTreeView( QWidget *parent )
: QgsLayerTreeView( parent )
{
}
void QgsDxfExportLayerTreeView::resizeEvent( QResizeEvent *event )
{
header()->setMinimumSectionSize( viewport()->width() / 2 );
header()->setMaximumSectionSize( viewport()->width() / 2 );
QTreeView::resizeEvent( event ); // NOLINT(bugprone-parent-virtual-call) clazy:exclude=skipped-base-method
}
QgsDxfExportDialog::QgsDxfExportDialog( QWidget *parent, Qt::WindowFlags f )
: QDialog( parent, f )
{
setupUi( this );
mTreeView = new QgsDxfExportLayerTreeView( this );
mTreeViewContainer->layout()->addWidget( mTreeView );
QgsGui::enableAutoGeometryRestore( this );
connect( mVisibilityPresets, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsDxfExportDialog::mVisibilityPresets_currentIndexChanged );
connect( mCrsSelector, &QgsProjectionSelectionWidget::crsChanged, this, &QgsDxfExportDialog::mCrsSelector_crsChanged );
QgsSettings settings;
mLayerTreeGroup = QgsProject::instance()->layerTreeRoot()->clone();
cleanGroup( mLayerTreeGroup );
mFieldSelectorDelegate = new FieldSelectorDelegate( this );
mTreeView->setEditTriggers( QAbstractItemView::AllEditTriggers );
mTreeView->setItemDelegate( mFieldSelectorDelegate );
mModel = new QgsVectorLayerAndAttributeModel( mLayerTreeGroup, this );
mModel->setFlags( QgsLayerTreeModel::Flags() );
mTreeView->setModel( mModel );
mTreeView->header()->show();
mFileName->setStorageMode( QgsFileWidget::SaveFile );
mFileName->setFilter( tr( "DXF files" ) + " (*.dxf *.DXF)" );
mFileName->setDialogTitle( tr( "Export as DXF" ) );
mFileName->setDefaultRoot( settings.value( QStringLiteral( "qgis/lastDxfDir" ), QDir::homePath() ).toString() );
connect( this, &QDialog::accepted, this, &QgsDxfExportDialog::saveSettings );
connect( mSelectAllButton, &QAbstractButton::clicked, this, &QgsDxfExportDialog::selectAll );
connect( mDeselectAllButton, &QAbstractButton::clicked, this, &QgsDxfExportDialog::deSelectAll );
connect( mSelectDataDefinedBlocks, &QAbstractButton::clicked, this, &QgsDxfExportDialog::selectDataDefinedBlocks );
connect( mDeselectDataDefinedBlocks, &QAbstractButton::clicked, this, &QgsDxfExportDialog::deselectDataDefinedBlocks );
connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsDxfExportDialog::showHelp );
connect( mFileName, &QgsFileWidget::fileChanged, this, [ = ]( const QString & filePath )
{
QgsSettings settings;
QFileInfo tmplFileInfo( filePath );
settings.setValue( QStringLiteral( "qgis/lastDxfDir" ), tmplFileInfo.absolutePath() );
setOkEnabled();
} );
mSymbologyModeComboBox->addItem( tr( "No Symbology" ), QVariant::fromValue( Qgis::FeatureSymbologyExport::NoSymbology ) );
mSymbologyModeComboBox->addItem( tr( "Feature Symbology" ), QVariant::fromValue( Qgis::FeatureSymbologyExport::PerFeature ) );
mSymbologyModeComboBox->addItem( tr( "Symbol Layer Symbology" ), QVariant::fromValue( Qgis::FeatureSymbologyExport::PerSymbolLayer ) );
//last dxf symbology mode
mSymbologyModeComboBox->setCurrentIndex( QgsProject::instance()->readEntry( QStringLiteral( "dxf" ), QStringLiteral( "/lastDxfSymbologyMode" ), settings.value( QStringLiteral( "qgis/lastDxfSymbologyMode" ), "2" ).toString() ).toInt() );
//last symbol scale
mScaleWidget->setMapCanvas( QgisApp::instance()->mapCanvas() );
double oldScale = QgsProject::instance()->readEntry( QStringLiteral( "dxf" ), QStringLiteral( "/lastSymbologyExportScale" ), settings.value( QStringLiteral( "qgis/lastSymbologyExportScale" ), "1/50000" ).toString() ).toDouble();
if ( oldScale != 0.0 )
mScaleWidget->setScale( 1.0 / oldScale );
mLayerTitleAsName->setChecked( QgsProject::instance()->readEntry( QStringLiteral( "dxf" ), QStringLiteral( "/lastDxfLayerTitleAsName" ), settings.value( QStringLiteral( "qgis/lastDxfLayerTitleAsName" ), "false" ).toString() ) != QLatin1String( "false" ) );
mMapExtentCheckBox->setChecked( QgsProject::instance()->readEntry( QStringLiteral( "dxf" ), QStringLiteral( "/lastDxfMapRectangle" ), settings.value( QStringLiteral( "qgis/lastDxfMapRectangle" ), "false" ).toString() ) != QLatin1String( "false" ) );
mSelectedFeaturesOnly->setChecked( QgsProject::instance()->readEntry( QStringLiteral( "dxf" ), QStringLiteral( "/lastDxfSelectedFeaturesOnly" ), settings.value( QStringLiteral( "qgis/lastDxfSelectedFeaturesOnly" ), "false" ).toString() ) != QLatin1String( "false" ) );
mMTextCheckBox->setChecked( QgsProject::instance()->readEntry( QStringLiteral( "dxf" ), QStringLiteral( "/lastDxfUseMText" ), settings.value( QStringLiteral( "qgis/lastDxfUseMText" ), "true" ).toString() ) != QLatin1String( "false" ) );
mForce2d->setChecked( QgsProject::instance()->readEntry( QStringLiteral( "dxf" ), QStringLiteral( "/lastDxfForce2d" ), settings.value( QStringLiteral( "qgis/lastDxfForce2d" ), "false" ).toString() ) != QLatin1String( "false" ) );
mHairlineWidthExportCheckBox->setChecked( QgsProject::instance()->readEntry( QStringLiteral( "dxf" ), QStringLiteral( "/lastDxfHairlineWidthExport" ), settings.value( QStringLiteral( "qgis/lastDxfHairlineWidthExport" ), "false" ).toString() ) != QLatin1String( "false" ) );
QStringList ids = QgsProject::instance()->mapThemeCollection()->mapThemes();
ids.prepend( QString() );
mVisibilityPresets->addItems( ids );
mVisibilityPresets->setCurrentIndex( mVisibilityPresets->findText( QgsProject::instance()->readEntry( QStringLiteral( "dxf" ), QStringLiteral( "/lastVisibilityPreset" ), QString() ) ) );
buttonBox->button( QDialogButtonBox::Ok )->setEnabled( false );
long crsid = QgsProject::instance()->readEntry( QStringLiteral( "dxf" ), QStringLiteral( "/lastDxfCrs" ),
settings.value( QStringLiteral( "qgis/lastDxfCrs" ), QString::number( QgsProject::instance()->crs().srsid() ) ).toString()
).toLong();
mCRS = QgsCoordinateReferenceSystem::fromSrsId( crsid );
mCrsSelector->setCrs( mCRS );
mCrsSelector->setLayerCrs( mCRS );
mCrsSelector->setShowAccuracyWarnings( true );
mCrsSelector->setMessage( tr( "Select the coordinate reference system for the dxf file. "
"The data points will be transformed from the layer coordinate reference system." ) );
mEncoding->addItems( QgsDxfExport::encodings() );
mEncoding->setCurrentIndex( mEncoding->findText( QgsProject::instance()->readEntry( QStringLiteral( "dxf" ), QStringLiteral( "/lastDxfEncoding" ), settings.value( QStringLiteral( "qgis/lastDxfEncoding" ), "CP1252" ).toString() ) ) );
QPushButton *btnLoadSaveSettings = new QPushButton( tr( "Settings" ), this );
QMenu *menuSettings = new QMenu( this );
menuSettings->addAction( tr( "Load Settings from File…" ), this, &QgsDxfExportDialog::loadSettingsFromFile );
menuSettings->addAction( tr( "Save Settings to File…" ), this, &QgsDxfExportDialog::saveSettingsToFile );
btnLoadSaveSettings->setMenu( menuSettings );
buttonBox->addButton( btnLoadSaveSettings, QDialogButtonBox::ResetRole );
mMessageBar = new QgsMessageBar();
mMessageBar->setSizePolicy( QSizePolicy::Minimum, QSizePolicy::Fixed );
mainLayout->insertWidget( 0, mMessageBar );
mModel->loadLayersOutputAttribute( mModel->rootGroup() );
}
QgsDxfExportDialog::~QgsDxfExportDialog()
{
delete mLayerTreeGroup;
}
void QgsDxfExportDialog::mVisibilityPresets_currentIndexChanged( int index )
{
Q_UNUSED( index )
mModel->applyVisibilityPreset( mVisibilityPresets->currentText() );
}
void QgsDxfExportDialog::cleanGroup( QgsLayerTreeNode *node )
{
QgsLayerTreeGroup *group = QgsLayerTree::isGroup( node ) ? QgsLayerTree::toGroup( node ) : nullptr;
if ( !group )
return;
QList<QgsLayerTreeNode *> toRemove;
const auto constChildren = node->children();
for ( QgsLayerTreeNode *child : constChildren )
{
if ( QgsLayerTree::isLayer( child ) &&
( QgsLayerTree::toLayer( child )->layer()->type() != Qgis::LayerType::Vector ||
! QgsLayerTree::toLayer( child )->layer()->isSpatial() ||
! QgsLayerTree::toLayer( child )->layer()->isValid() ) )
{
toRemove << child;
continue;
}
cleanGroup( child );
if ( QgsLayerTree::isGroup( child ) && child->children().isEmpty() )
toRemove << child;
}
const auto constToRemove = toRemove;
for ( QgsLayerTreeNode *child : constToRemove )
group->removeChildNode( child );
}
void QgsDxfExportDialog::selectAll()
{
mModel->selectAll();
}
void QgsDxfExportDialog::deSelectAll()
{
mModel->deSelectAll();
}
void QgsDxfExportDialog::selectDataDefinedBlocks()
{
mModel->selectDataDefinedBlocks();
}
void QgsDxfExportDialog::deselectDataDefinedBlocks()
{
mModel->deselectDataDefinedBlocks();
}
void QgsDxfExportDialog::loadSettingsFromFile()
{
const QString fileName = QFileDialog::getOpenFileName( this, tr( "Load DXF Export Settings" ),
QgsDxfExportDialog::settingsDxfLastSettingsDir->value(),
tr( "XML file" ) + " (*.xml)" );
if ( fileName.isNull() )
{
return;
}
bool resultFlag = false;
QDomDocument domDocument( QStringLiteral( "qgis" ) );
// location of problem associated with errorMsg
int line, column;
QString errorMessage;
QFile file( fileName );
if ( file.open( QFile::ReadOnly ) )
{
QgsDebugMsgLevel( QStringLiteral( "file found %1" ).arg( fileName ), 2 );
// read file
resultFlag = domDocument.setContent( &file, &errorMessage, &line, &column );
if ( !resultFlag )
errorMessage = tr( "%1 at line %2 column %3" ).arg( errorMessage ).arg( line ).arg( column );
file.close();
}
if ( QMessageBox::question( this,
tr( "DXF Export - Load from XML File" ),
tr( "Are you sure you want to load settings from XML? This will change some values in the DXF Export dialog." ) ) == QMessageBox::Yes )
{
resultFlag = loadSettingsFromXML( domDocument, errorMessage );
if ( !resultFlag )
{
mMessageBar->pushWarning( tr( "Load DXF Export Settings" ), tr( "Failed to load DXF Export settings file as %1. Details: %2" ).arg( fileName, errorMessage ) );
}
else
{
QgsDxfExportDialog::settingsDxfLastSettingsDir->setValue( QFileInfo( fileName ).path() );
mMessageBar->pushMessage( QString(), tr( "DXF Export settings loaded!" ), Qgis::MessageLevel::Success, 0 );
}
}
}
bool QgsDxfExportDialog::loadSettingsFromXML( QDomDocument &doc, QString &errorMessage ) const
{
const QDomElement rootElement = doc.firstChildElement( QStringLiteral( "qgis" ) );
if ( rootElement.isNull() )
{
errorMessage = tr( "Root <qgis> element could not be found." );
return false;
}
const QDomElement dxfElement = rootElement.firstChildElement( QStringLiteral( "dxf_settings" ) );
if ( dxfElement.isNull() )
{
errorMessage = tr( "The XML file does not correspond to DXF Export settings. It must have a <dxf-settings> element." );
return false;
}
QDomElement element;
QVariant value;
element = dxfElement.namedItem( QStringLiteral( "symbology_mode" ) ).toElement();
value = QgsXmlUtils::readVariant( element.firstChildElement() );
if ( !value.isNull() )
mSymbologyModeComboBox->setCurrentIndex( value.toInt() );
element = dxfElement.namedItem( QStringLiteral( "symbology_scale" ) ).toElement();
value = QgsXmlUtils::readVariant( element.firstChildElement() );
if ( !value.isNull() )
mScaleWidget->setScale( value.toDouble() );
element = dxfElement.namedItem( QStringLiteral( "encoding" ) ).toElement();
value = QgsXmlUtils::readVariant( element.firstChildElement() );
if ( !value.isNull() )
mEncoding->setCurrentText( value.toString() );
element = dxfElement.namedItem( QStringLiteral( "crs" ) ).toElement();
value = QgsXmlUtils::readVariant( element.firstChildElement() );
if ( !value.isNull() )
mCrsSelector->setCrs( value.value< QgsCoordinateReferenceSystem >() );
element = dxfElement.namedItem( QStringLiteral( "map_theme" ) ).toElement();
value = QgsXmlUtils::readVariant( element.firstChildElement() );
if ( !value.isNull() )
mVisibilityPresets->setCurrentText( value.toString() );
// layer settings
element = dxfElement.namedItem( QStringLiteral( "layers" ) ).toElement();
QDomNodeList layerNodeList = element.elementsByTagName( QStringLiteral( "layer" ) );