/
qgsspatialquerydialog.cpp
1084 lines (990 loc) · 33.9 KB
/
qgsspatialquerydialog.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
/***************************************************************************
qgsspatialquerydialog.cpp
-------------------
begin : Dec 29, 2009
copyright : (C) 2009 by Diego Moreira And Luiz Motta
email : moreira.geo at gmail.com And motta.luiz at gmail.com
***************************************************************************/
/***************************************************************************
* *
* 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 <QMessageBox>
#include <QDateTime>
#include <QPushButton>
#include "qgis.h"
#include "qgsapplication.h"
#include "qgsmaplayer.h"
#include "qgsmaplayerregistry.h"
#include "qgsproject.h"
#include "qgsvectordataprovider.h"
#include "qgsspatialquerydialog.h"
#include "qgsspatialquery.h"
#include "qgsrubberselectid.h"
#include "qgsmngprogressbar.h"
QgsSpatialQueryDialog::QgsSpatialQueryDialog( QWidget* parent, QgisInterface* iface ): QDialog( parent )
{
setupUi( this );
QSettings settings;
restoreGeometry( settings.value( "SpatialQuery/geometry" ).toByteArray() );
mLayerReference = mLayerTarget = NULL;
mIface = iface;
mRubberSelectId = new QgsRubberSelectId( iface->mapCanvas() );
initGui();
connectAll();
} // QgsSpatialQueryDialog::QgsSpatialQueryDialog( QWidget* parent, QgisInterface* iface )
QgsSpatialQueryDialog::~QgsSpatialQueryDialog()
{
QSettings settings;
settings.setValue( "SpatialQuery/geometry", saveGeometry() );
disconnectAll();
delete mRubberSelectId;
mMapIdVectorLayers.clear();
mFeatureResult.clear();
mFeatureInvalidTarget.clear();
mFeatureInvalidReference.clear();
} // QgsSpatialQueryDialog::~QgsSpatialQueryDialog()
bool QgsSpatialQueryDialog::hasPossibleQuery( QString &msg )
{
// Count the number of vector layer
QMap <QString, QgsMapLayer*> layers = QgsMapLayerRegistry::instance()->mapLayers();
QMapIterator <QString, QgsMapLayer*> item( layers );
QgsMapLayer * mapLayer = NULL;
QgsVectorLayer * lyr = NULL;
unsigned int totalVector = 0;
while ( item.hasNext() )
{
item.next();
mapLayer = item.value();
if ( mapLayer->type() != QgsMapLayer::VectorLayer )
{
continue;
}
lyr = qobject_cast<QgsVectorLayer *>( mapLayer );
if ( !lyr )
{
continue;
}
totalVector++;
}
// check is possible query
if ( totalVector < 2 )
{
msg = tr( "The spatial query requires at least two vector layers" );
return false;
}
else
{
return true;
}
} // bool QgsSpatialQueryDialog::hasPossibleQuery( QString &msg )
void QgsSpatialQueryDialog::initGui()
{
mRubberSelectId->setStyle( 250, 0, 0, 2 ); // Same identify
visibleResult( false );
populateTypeItems();
populateCbTargetLayer();
if ( cbTargetLayer->count() > 1 )
{
setLayer( true, 0 );
setSelectedGui();
evaluateCheckBoxLayer( true );
populateCbReferenceLayer();
setLayer( false, 0 );
evaluateCheckBoxLayer( false );
populateCbOperation();
}
else
{
bbMain->button( QDialogButtonBox::Apply )->hide();
}
populateCbResulFor(); // Depend if Target is selected
adjustSize();
} // QgsSpatialQueryDialog::initGui()
void QgsSpatialQueryDialog::setLayer( bool isTarget, int index )
{
if ( isTarget )
{
if ( mLayerTarget )
{
disconnect( mLayerTarget, SIGNAL( selectionChanged() ),
this, SLOT( signal_layerTarget_selectionFeaturesChanged() ) );
}
mLayerTarget = getLayerFromCombobox( isTarget, index );
connect( mLayerTarget, SIGNAL( selectionChanged() ),
this, SLOT( signal_layerTarget_selectionFeaturesChanged() ) );
}
else
{
if ( mLayerReference )
{
disconnect( mLayerReference, SIGNAL( selectionChanged() ),
this, SLOT( signal_layerReference_selectionFeaturesChanged() ) );
}
mLayerReference = getLayerFromCombobox( isTarget, index );
connect( mLayerReference, SIGNAL( selectionChanged() ),
this, SLOT( signal_layerReference_selectionFeaturesChanged() ) );
}
} // void QgsSpatialQueryDialog::setLayer(bool isTarget, int index)
void QgsSpatialQueryDialog::evaluateCheckBoxLayer( bool isTarget )
{
QgsVectorLayer* lyr = NULL;
QCheckBox* checkbox = NULL;
if ( isTarget )
{
lyr = mLayerTarget;
checkbox = ckbUsingSelectedTarget;
}
else
{
lyr = mLayerReference;
checkbox = ckbUsingSelectedReference;
}
int selectedCount = lyr->selectedFeatureCount();
bool isCheckBoxValid = ( lyr != NULL && selectedCount > 0 );
checkbox->setChecked( isCheckBoxValid );
checkbox->setEnabled( isCheckBoxValid );
QString textCheckBox = isCheckBoxValid
? tr( "%n selected geometries", "selected geometries", selectedCount )
: tr( "Selected geometries" );
checkbox->setText( textCheckBox );
} // void QgsSpatialQueryDialog::evaluateCheckBoxLayer(bool isTarget)
void QgsSpatialQueryDialog::runQuery()
{
bbMain->setEnabled( false );
MngProgressBar* pb = new MngProgressBar( pgbStatus );
QgsSpatialQuery* spatialQuery = new QgsSpatialQuery( pb );
if ( ckbUsingSelectedTarget->isChecked() )
{
spatialQuery->setSelectedFeaturesTarget( true );
}
if ( ckbUsingSelectedReference->isChecked() )
{
spatialQuery->setSelectedFeaturesReference( true );
}
pgbStatus->setTextVisible( true );
mFeatureResult.clear();
mFeatureInvalidTarget.clear();
mFeatureInvalidReference.clear();
int currentItem = cbOperation->currentIndex();
int operation = cbOperation->itemData( currentItem ).toInt();
spatialQuery->runQuery( mFeatureResult, mFeatureInvalidTarget, mFeatureInvalidReference, operation, mLayerTarget, mLayerReference );
delete spatialQuery;
delete pb;
bbMain->setEnabled( true );
} // void QgsSpatialQueryDialog::runQuery()
void QgsSpatialQueryDialog::showResultQuery( QDateTime *datetimeStart, QDateTime *datetimeEnd )
{
static int countQuery = 0;
// Report processing
countQuery++;
QString msg = tr( "%1)Query" ).arg( countQuery );
teStatus->append( msg );
msg = tr( "Begin at %L1" ).arg( datetimeStart->toString() );
teStatus->append( msg );
teStatus->append( "" );
msg = QString( "%1" ).arg( getDescriptionLayerShow( true ) );
teStatus->append( msg );
msg = tr( "< %1 >" ).arg( cbOperation->currentText() );
teStatus->append( msg );
msg = QString( "%1" ).arg( getDescriptionLayerShow( false ) );
teStatus->append( msg );
msg = tr( "Total of features = %1" ).arg( mFeatureResult.size() );
teStatus->append( msg );
teStatus->append( "" );
teStatus->append( tr( "Total of invalid features:" ) );
teStatus->append( getDescriptionInvalidFeaturesShow( true ) );
teStatus->append( getDescriptionInvalidFeaturesShow( false ) );
teStatus->append( "" );
double timeProcess = ( double )datetimeStart->secsTo( *datetimeEnd ) / 60.0;
msg = tr( "Finish at %L1 (processing time %L2 minutes)" ).arg( datetimeEnd->toString() ).arg( timeProcess, 0, 'f', 2 );
teStatus->append( msg );
teStatus->append( "" );
ckbLogProcessing->setChecked( false );
QVariant item = QVariant::fromValue(( int )itemsResult );
int index = cbTypeItems->findData( item );
cbTypeItems->setCurrentIndex( index );
on_cbTypeItems_currentIndexChanged( index );
// Result target
if ( mFeatureResult.size() > 0 )
{
// Select features
TypeResultFor typeResultFor = ( TypeResultFor ) cbResultFor->itemData( cbResultFor->currentIndex() ).toInt();
switch ( typeResultFor )
{
case selectedNew:
mLayerTarget->setSelectedFeatures( mFeatureResult );
break;
case selectedAdd:
mLayerTarget->setSelectedFeatures( mLayerTarget->selectedFeaturesIds() + mFeatureResult );
break;
case selectedRemove:
mLayerTarget->setSelectedFeatures( mLayerTarget->selectedFeaturesIds() - mFeatureResult );
break;
default:
return;
}
}
} // void QgsSpatialQueryDialog::showResultQuery(QDateTime *datetimeStart, QDateTime *datetimeEnd)
QString QgsSpatialQueryDialog::getSubsetFIDs( const QgsFeatureIds *fids, QString fieldFID )
{
if ( fids->size() == 0 )
{
return QString( "" );
}
QSetIterator <QgsFeatureId> item( *fids );
QStringList lstFID;
while ( item.hasNext() )
{
lstFID.append( FID_TO_STRING( item.next() ) );
}
QString qFormat( "%1 in (%2)" );
QString qReturn = qFormat.arg( fieldFID ).arg( lstFID.join( "," ) );
lstFID.clear();
return qReturn;
} // QString QgsSpatialQueryDialog::getSubsetFIDs( const QSet< int > *fids, QString fieldFID )
QgsSpatialQueryDialog::TypeVerifyCreateSubset QgsSpatialQueryDialog::verifyCreateSubset( QString &msg, QString &fieldFID )
{
QString providerType = mLayerTarget->providerType().toUpper();
// OGR
if ( providerType == "OGR" )
{
fieldFID = QString( "FID" );
return verifyOk;
}
// Database Postgis and Spatialite
if ( providerType == "POSTGRES" || providerType == "SPATIALITE" )
{
fieldFID = mLayerTarget->dataProvider()->fields().at( 0 ).name();
msg = tr( "Using the field \"%1\" for subset" ).arg( fieldFID );
return verifyTry;
}
msg = tr( "Sorry! Only this providers are enable: OGR, POSTGRES and SPATIALITE." );
return verifyImpossible;
} // TypeVerifyCreateSubset QgsSpatialQueryDialog::verifyCreateSubset(QString &msg, QString &fieldFID)
bool QgsSpatialQueryDialog::addLayerSubset( QString name, QString subset )
{
QgsVectorLayer *addLyr = new QgsVectorLayer( mLayerTarget->source(), name, mLayerTarget->providerType() );
if ( ! addLyr->setSubsetString( subset ) )
{
delete addLyr;
return false;
}
QgsMapLayerRegistry::instance()->addMapLayers(
QList<QgsMapLayer *>() << addLyr );
return true;
} // bool QgsSpatialQueryDialog::addLayerSubset( QString name, QString subset )
QString QgsSpatialQueryDialog::getDescriptionLayerShow( bool isTarget )
{
QgsVectorLayer* lyr = NULL;
QCheckBox * checkBox = NULL;
if ( isTarget )
{
lyr = mLayerTarget;
checkBox = ckbUsingSelectedTarget;
}
else
{
lyr = mLayerReference;
checkBox = ckbUsingSelectedReference;
}
QString sDescFeatures = checkBox->isChecked()
? tr( "%1 of %2" ).arg( lyr->selectedFeatureCount() ).arg( lyr->featureCount() )
: tr( "all = %1" ).arg( lyr->featureCount() );
return QString( "%1 (%2)" ).arg( lyr->name() ).arg( sDescFeatures );
} // QString QgsSpatialQueryDialog::getDescriptionLayerShow(bool isTarget)
QString QgsSpatialQueryDialog::getDescriptionInvalidFeaturesShow( bool isTarget )
{
QgsVectorLayer* lyr = NULL;
QCheckBox* checkBox = NULL;
int totalInvalid = 0;
if ( isTarget )
{
lyr = mLayerTarget;
checkBox = ckbUsingSelectedTarget;
totalInvalid = mFeatureInvalidTarget.size();
}
else
{
lyr = mLayerReference;
checkBox = ckbUsingSelectedReference;
totalInvalid = mFeatureInvalidReference.size();
}
QString sDescFeatures = checkBox->isChecked()
? tr( "%1 of %2(selected features)" ).arg( totalInvalid ).arg( lyr->selectedFeatureCount() )
: tr( "%1 of %2" ).arg( totalInvalid ).arg( lyr->featureCount() );
return QString( "%1: %2" ).arg( lyr->name() ).arg( sDescFeatures );
} // QString QgsSpatialQueryDialog::getDescriptionInvalidFeatures(bool isTarget)
void QgsSpatialQueryDialog::connectAll()
{
connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWasAdded( QgsMapLayer* ) ),
this, SLOT( signal_qgis_layerWasAdded( QgsMapLayer* ) ) ) ;
connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWillBeRemoved( QString ) ),
this, SLOT( signal_qgis_layerWillBeRemoved( QString ) ) );
connect( ckbLogProcessing, SIGNAL( clicked( bool ) ),
this, SLOT( on_ckbLogProcessing_clicked( bool ) ) );
} // QgsSpatialQueryDialog::connectAll()
void QgsSpatialQueryDialog::disconnectAll()
{
disconnect( QgsMapLayerRegistry::instance(), SIGNAL( layerWasAdded( QgsMapLayer* ) ),
this, SLOT( signal_qgis_layerWasAdded( QgsMapLayer* ) ) ) ;
disconnect( QgsMapLayerRegistry::instance(), SIGNAL( layerWillBeRemoved( QString ) ),
this, SLOT( signal_qgis_layerWillBeRemoved( QString ) ) );
if ( mLayerTarget )
{
disconnect( mLayerTarget, SIGNAL( selectionChanged() ),
this, SLOT( signal_layerTarget_selectionFeaturesChanged() ) );
}
if ( mLayerReference )
{
disconnect( mLayerReference, SIGNAL( selectionChanged() ),
this, SLOT( signal_layerReference_selectionFeaturesChanged() ) );
}
} // QgsSpatialQueryDialog::disconnectAll()
void QgsSpatialQueryDialog::reject()
{
disconnectAll();
mRubberSelectId->reset();
mLayerTarget = mLayerReference = NULL;
mFeatureResult.clear();
mFeatureInvalidTarget.clear();
mFeatureInvalidReference.clear();
mMapIdVectorLayers.clear();
QDialog::reject();
} // QgsSpatialQueryDialog::reject()
QgsVectorLayer * QgsSpatialQueryDialog::getLayerFromCombobox( bool isTarget, int index )
{
QVariant data = isTarget
? cbTargetLayer->itemData( index )
: cbReferenceLayer->itemData( index );
QgsVectorLayer* lyr = static_cast<QgsVectorLayer*>( data.value<void *>() );
return lyr;
} // QgsVectorLayer * QgsSpatialQueryDialog::getLayerFromCombobox(bool isTarget, int index)
QIcon QgsSpatialQueryDialog::getIconTypeGeometry( QGis::GeometryType geomType )
{
QString theName;
if ( geomType == QGis::Point )
{
theName = "/mIconPointLayer.svg";
}
else if ( geomType == QGis::Line )
{
theName = "/mIconLineLayer.svg";
}
else // Polygon
{
theName = "/mIconPolygonLayer.svg";
}
// Copy from qgisapp.cpp
QString myPreferredPath = QgsApplication::activeThemePath() + QDir::separator() + theName;
QString myDefaultPath = QgsApplication::defaultThemePath() + QDir::separator() + theName;
if ( QFile::exists( myPreferredPath ) )
{
return QIcon( myPreferredPath );
}
else if ( QFile::exists( myDefaultPath ) )
{
//could still return an empty icon if it
//doesnt exist in the default theme either!
return QIcon( myDefaultPath );
}
else
{
return QIcon();
}
} // QIcon QgsSpatialQueryDialog::getIconTypeGeometry(int typeGeometry)
void QgsSpatialQueryDialog::addCbLayer( bool isTarget, QgsVectorLayer* lyr )
{
QVariant item = QVariant::fromValue(( void * )lyr );
QComboBox * cmb = isTarget ? cbTargetLayer : cbReferenceLayer;
int idNew = cmb->count();
QIcon icon = getIconTypeGeometry( lyr->geometryType() );
cmb->addItem( icon, lyr->name(), item );
cmb->setItemData( idNew, QVariant( lyr->source() ), Qt::ToolTipRole );
} // void QgsSpatialQueryDialog::removeLayerCombobox(bool isTarget, QgsVectorLayer* lyr)
int QgsSpatialQueryDialog::getCbIndexLayer( bool isTarget, QgsVectorLayer* lyr )
{
QVariant item = QVariant::fromValue(( void * )lyr );
QComboBox * cmb = isTarget ? cbTargetLayer : cbReferenceLayer;
return cmb->findData( item );
}
void QgsSpatialQueryDialog::removeLayer( bool isTarget, QgsVectorLayer* lyr )
{
QComboBox * cmb = isTarget ? cbTargetLayer : cbReferenceLayer;
cmb->blockSignals( true );
// Remove Combobox
int index = getCbIndexLayer( isTarget, lyr );
if ( index > -1 )
{
cmb->removeItem( index );
}
else
{
return;
}
// Set Layers (Target or Reference)
QgsVectorLayer* lyrThis = mLayerTarget;
if ( !isTarget )
{
lyrThis = mLayerReference;
}
if ( lyr == lyrThis )
{
lyrThis = NULL;
if ( cmb->count() > 0 )
{
cmb->setCurrentIndex( 0 );
setLayer( isTarget, 0 );
evaluateCheckBoxLayer( isTarget );
if ( isTarget )
{
if ( gbResultQuery->isVisible() )
{
visibleResult( false );
}
}
}
}
cmb->blockSignals( false );
} // void QgsSpatialQueryDialog::removeLayer(bool isTarget, QgsVectorLayer* lyr)
void QgsSpatialQueryDialog::populateCbResulFor()
{
cbResultFor->blockSignals( true );
cbResultFor->clear();
QVariant item;
item = QVariant::fromValue(( int )selectedNew );
cbResultFor->addItem( tr( "Create new selection" ), item );
if ( mLayerTarget->selectedFeatureCount() == 0 )
{
return;
}
if ( ! ckbUsingSelectedTarget->isChecked() )
{
item = QVariant::fromValue(( int )selectedAdd );
cbResultFor->addItem( tr( "Add to current selection" ), item );
}
item = QVariant::fromValue(( int )selectedRemove );
cbResultFor->addItem( tr( "Remove from current selection" ), item );
cbResultFor->blockSignals( false );
} // void QgsSpatialQueryDialog::populateCbResulFor()
void QgsSpatialQueryDialog::populateTypeItems()
{
QVariant item;
cbTypeItems->blockSignals( true );
item = QVariant::fromValue(( int )itemsResult );
cbTypeItems->addItem( tr( "Result query" ), item );
item = QVariant::fromValue(( int )itemsInvalidTarget );
cbTypeItems->addItem( tr( "Invalid source" ), item );
item = QVariant::fromValue(( int )itemsInvalidReference );
cbTypeItems->addItem( tr( "Invalid reference" ), item );
cbTypeItems->blockSignals( false );
}
void QgsSpatialQueryDialog::populateCbTargetLayer()
{
cbTargetLayer->blockSignals( true );
QMap <QString, QgsMapLayer*> layers = QgsMapLayerRegistry::instance()->mapLayers();
QMapIterator <QString, QgsMapLayer*> item( layers );
QgsMapLayer * mapLayer = NULL;
QgsVectorLayer * lyr = NULL;
QString layerId;
while ( item.hasNext() )
{
item.next();
mapLayer = item.value();
if ( mapLayer->type() != QgsMapLayer::VectorLayer )
{
continue;
}
lyr = qobject_cast<QgsVectorLayer *>( mapLayer );
if ( !lyr )
{
continue;
}
addCbLayer( true, lyr );
mMapIdVectorLayers.insert( lyr->id(), lyr );
}
cbTargetLayer->setCurrentIndex( 0 );
cbTargetLayer->blockSignals( false );
} // void QgsSpatialQueryDialog::populateCbTargetLayer()
void QgsSpatialQueryDialog::populateCbReferenceLayer()
{
cbReferenceLayer->blockSignals( true );
cbReferenceLayer->clear();
// Populate new values and Set current item keeping the previous value
QString itemText;
QVariant itemData;
QIcon itemIcon;
QgsVectorLayer * lyr = NULL;
int idNew = 0;
for ( int id = 0; id < cbTargetLayer->count(); id++ )
{
itemText = cbTargetLayer->itemText( id );
itemData = cbTargetLayer->itemData( id );
itemIcon = cbTargetLayer->itemIcon( id );
lyr = static_cast<QgsVectorLayer *>( itemData.value<void *>() );
if ( lyr == mLayerTarget )
{
continue;
}
cbReferenceLayer->addItem( itemIcon, itemText, itemData );
cbReferenceLayer->setItemData( idNew, QVariant( lyr->source() ), Qt::ToolTipRole );
idNew++;
}
int idCurrent = getCbIndexLayer( false, mLayerReference );
if ( idCurrent == -1 )
{
idCurrent = 0;
}
cbReferenceLayer->setCurrentIndex( idCurrent );
cbReferenceLayer->blockSignals( false );
} // QgsSpatialQueryDialog::populateCbReferenceLayer()
void QgsSpatialQueryDialog::populateCbOperation()
{
QVariant currentValueItem;
bool isStartEmpty = false;
if ( cbOperation->count() == 0 )
{
isStartEmpty = true;
}
else
{
currentValueItem = cbOperation->itemData( cbOperation->currentIndex() );
}
// Populate new values
QMap<QString, int> * map = QgsSpatialQuery::getTypesOperations( mLayerTarget, mLayerReference );
QMapIterator <QString, int> item( *map );
cbOperation->blockSignals( true );
cbOperation->clear();
while ( item.hasNext() )
{
item.next();
cbOperation->addItem( item.key(), QVariant( item.value() ) );
}
delete map;
// Set current item keeping the previous value
int idCurrent = 0;
if ( !isStartEmpty )
{
idCurrent = cbOperation->findData( currentValueItem );
if ( idCurrent == -1 )
{
idCurrent = 0;
}
}
cbOperation->setCurrentIndex( idCurrent );
cbOperation->blockSignals( false );
} // QgsSpatialQueryDialog::populatecbOperation()
void QgsSpatialQueryDialog::setSelectedGui()
{
int selectedFeat = mLayerTarget->selectedFeatureCount();
int totalFeat = mLayerTarget->featureCount();
QString formatLabel( tr( "%1 of %2 selected by \"%3\"" ) );
if ( ! mIsSelectedOperator )
{
mSourceSelected = tr( "user" );
}
lbStatusSelected->setText( formatLabel.arg( selectedFeat ).arg( totalFeat ).arg( mSourceSelected ) );
mIsSelectedOperator = false;
pbCreateLayerSelected->setEnabled( selectedFeat > 0 );
} // void QgsSpatialQueryDialog::setSelectedGui()
void QgsSpatialQueryDialog::changeLwFeature( QgsVectorLayer* lyr, QgsFeatureId fid )
{
lwFeatures->setEnabled( false ); // The showRubberFeature can be slow
showRubberFeature( lyr, fid );
// Zoom
if ( ckbZoomItem->isChecked() )
{
zoomFeature( lyr, fid );
}
lwFeatures->setEnabled( true );
lwFeatures->setFocus();
} // void QgsSpatialQueryDialog::changeLwFeature( QListWidget *listWidget, QgsVectorLayer* lyr, int fid )
void QgsSpatialQueryDialog::zoomFeature( QgsVectorLayer* lyr, QgsFeatureId fid )
{
static QgsVectorLayer* lyrCheck = NULL;
static bool hasMsg = false;
if ( ! lyrCheck || lyrCheck != lyr )
{
lyrCheck = lyr;
hasMsg = true;
}
else
{
hasMsg = false;
}
QgsFeature feat;
if ( !lyr->getFeatures( QgsFeatureRequest().setFilterFid( fid ).setSubsetOfAttributes( QgsAttributeList() ) ).nextFeature( feat ) )
{
return;
}
if ( !feat.geometry() )
{
return;
}
// Set system reference
QgsCoordinateReferenceSystem srsSource = lyr->dataProvider()->crs();
QgsCoordinateReferenceSystem srcMapcanvas = mIface->mapCanvas()->mapRenderer()->destinationCrs();
if ( ! srsSource.isValid() )
{
if ( hasMsg )
{
QString crsMapcanvas = srcMapcanvas.authid();
bool isFly = mIface->mapCanvas()->mapRenderer()->hasCrsTransformEnabled();
QString msgFly = tr( "Map \"%1\" \"on the fly\" transformation." ).arg( isFly ? tr( "enable" ) : tr( "disable" ) );
QString msg = tr( "Coordinate reference system(CRS) of\n\"%1\" is invalid(see CRS of provider)." ).arg( lyr->name() );
msg.append( tr( "\n\nCRS of map is %1.\n%2." ).arg( crsMapcanvas ).arg( msgFly ) );
msg.append( "\n\nUsing CRS of map for all features!" );
QMessageBox::warning( this, tr( "Zoom to feature" ), msg, QMessageBox::Ok );
}
mIface->mapCanvas()->setExtent( feat.geometry()->boundingBox() );
}
else if ( srsSource == srcMapcanvas )
{
mIface->mapCanvas()->setExtent( feat.geometry()->boundingBox() );
}
else
{
QgsCoordinateTransform * coordTransform = new QgsCoordinateTransform( srsSource, srcMapcanvas );
QgsRectangle rectExtent = coordTransform->transform( feat.geometry()->boundingBox() );
delete coordTransform;
mIface->mapCanvas()->setExtent( rectExtent );
}
mIface->mapCanvas()->refresh();
} // void QgsSpatialQueryDialog::zoomFeatureTarget(QgsVectorLayer* lyr, int fid)
void QgsSpatialQueryDialog::showRubberFeature( QgsVectorLayer* lyr, QgsFeatureId id )
{
mRubberSelectId->reset();
Qt::CursorShape shapeCurrent = cursor().shape();
QCursor c;
c.setShape( Qt::WaitCursor );
setCursor( c );
mRubberSelectId->addFeature( lyr, id );
mRubberSelectId->show();
c.setShape( shapeCurrent );
setCursor( c );
} // void QgsSpatialQueryDialog::showRubberFeature( QgsVectorLayer* lyr, int id )
void QgsSpatialQueryDialog::apply()
{
if ( ! mLayerReference )
{
QMessageBox::warning( 0, tr( "Missing reference layer" ), tr( "Select reference layer!" ), QMessageBox::Ok );
return;
}
if ( ! mLayerTarget )
{
QMessageBox::warning( 0, tr( "Missing target layer" ), tr( "Select target layer!" ), QMessageBox::Ok );
return;
}
pgbStatus->setVisible( true );
QDateTime datetimeStart = QDateTime::currentDateTime();
mSourceSelected = cbResultFor->currentText();
mIsSelectedOperator = true;
runQuery();
QDateTime datetimeEnd = QDateTime::currentDateTime();
if ( mFeatureResult.count() == 0 )
{
mIsSelectedOperator = false;
}
showResultQuery( &datetimeStart, &datetimeEnd );
visibleResult( true );
} // void QgsSpatialQueryDialog::apply()
void QgsSpatialQueryDialog::visibleResult( bool show )
{
blockSignals( true );
if ( show == false )
{
mRubberSelectId->reset();
}
leSpace->setVisible( show );
pgbStatus->setVisible( show );
gbResultQuery->setVisible( show );
gbSelected->setVisible( show );
ckbLogProcessing->setVisible( show );
teStatus->setVisible( false );
pgbStatus->setVisible( !show );
blockSignals( false );
adjustSize();
} // void QgsSpatialQueryDialog::visibleResult( bool show )
//! Slots for signs of Dialog
void QgsSpatialQueryDialog::on_bbMain_clicked( QAbstractButton * button )
{
switch ( bbMain->buttonRole( button ) )
{
case QDialogButtonBox::ApplyRole:
apply();
break;
case QDialogButtonBox::DestructiveRole:
case QDialogButtonBox::RejectRole:
reject();
break;
default:
return;
}
} // QgsSpatialQueryDialog::on_bbMain_accepted()
void QgsSpatialQueryDialog::on_pbCreateLayerItems_clicked()
{
TypeItems typeItem = ( TypeItems ) cbTypeItems->itemData( cbTypeItems->currentIndex() ).toInt();
QgsFeatureIds *fids = 0;
switch ( typeItem )
{
case itemsResult:
fids = &mFeatureResult;
break;
case itemsInvalidTarget:
fids = &mFeatureInvalidTarget;
break;
case itemsInvalidReference:
fids = &mFeatureInvalidReference;
break;
default:
return;
}
QString title = tr( "Create new layer from items" );
QString msg;
QString fieldFID;
TypeVerifyCreateSubset verify = verifyCreateSubset( msg, fieldFID );
if ( verify == verifyImpossible )
{
QMessageBox::critical( this, title, msg, QMessageBox::Ok );
return;
}
if ( verify == verifyTry )
{
QMessageBox::warning( this, title, msg, QMessageBox::Ok );
}
QString subset = getSubsetFIDs( fids, fieldFID );
QString name = QString( "%1 < %2 > %3" ).arg( mLayerTarget->name() ).arg( cbOperation->currentText() ).arg( mLayerReference->name() );
if ( ! addLayerSubset( name, subset ) )
{
msg = tr( "The query from \"%1\" using \"%2\" in field not possible." ).arg( mLayerTarget->name() ).arg( fieldFID );
QMessageBox::critical( this, title, msg, QMessageBox::Ok );
}
} // void QgsSpatialQueryDialog::on_pbCreateLayerItems_clicked()
void QgsSpatialQueryDialog::on_pbCreateLayerSelected_clicked()
{
const QgsFeatureIds *fids = & ( mLayerTarget->selectedFeaturesIds() );
QString title = tr( "Create new layer from selected" );
QString msg;
QString fieldFID;
TypeVerifyCreateSubset verify = verifyCreateSubset( msg, fieldFID );
if ( verify == verifyImpossible )
{
QMessageBox::critical( this, title, msg, QMessageBox::Ok );
return;
}
if ( verify == verifyTry )
{
QMessageBox::warning( this, title, msg, QMessageBox::Ok );
}
QString subset = getSubsetFIDs( fids, fieldFID );
QString name = QString( "%1 selected" ).arg( mLayerTarget->name() );
if ( ! addLayerSubset( name, subset ) )
{
msg = tr( "The query from \"%1\" using \"%2\" in field not possible." ).arg( mLayerTarget->name() ).arg( fieldFID );
QMessageBox::critical( this, title, msg, QMessageBox::Ok );
}
} // void QgsSpatialQueryDialog::on_pbCreateLayerSelected_clicked()
void QgsSpatialQueryDialog::on_cbTargetLayer_currentIndexChanged( int index )
{
// Add old target layer in reference combobox
addCbLayer( false, mLayerTarget );
// Set target layer
setLayer( true, index );
evaluateCheckBoxLayer( true );
setSelectedGui();
// Remove new target layer in reference combobox
removeLayer( false, mLayerTarget );
populateCbOperation();
if ( gbResultQuery->isVisible() )
{
visibleResult( false );
}
} // QgsSpatialQueryDialog::on_cbTargetLayer_currentIndexChanged(int index)
void QgsSpatialQueryDialog::on_cbReferenceLayer_currentIndexChanged( int index )
{
setLayer( false, index );
evaluateCheckBoxLayer( false );
populateCbOperation();
if ( gbResultQuery->isVisible() )
{
visibleResult( false );
}
} // QgsSpatialQueryDialog::on_cbReferenceLayer_currentIndexChanged(int index);
void QgsSpatialQueryDialog::on_cbTypeItems_currentIndexChanged( int index )
{
// Get Value type Item
QVariant qtypItem = cbTypeItems->itemData( index );
TypeItems typeItem = ( TypeItems ) qtypItem.toInt();
QgsFeatureIds *setItems = 0;
int totalFeat = mLayerTarget->featureCount();
switch ( typeItem )
{
case itemsResult:
setItems = &mFeatureResult;
break;
case itemsInvalidTarget:
setItems = &mFeatureInvalidTarget;
break;
case itemsInvalidReference:
setItems = &mFeatureInvalidReference;
totalFeat = mLayerReference->featureCount();
break;
default:
return;
}
lwFeatures->blockSignals( true );
lwFeatures->clear();
int totalItens = setItems->size();
if ( totalItens > 0 )
{
// Populate lwFeatures
QSetIterator <QgsFeatureId> item( *setItems );
QListWidgetItem *lwItem = NULL;
while ( item.hasNext() )
{
lwItem = new QListWidgetItem( lwFeatures );
QVariant fid = QVariant( item.next() );
lwItem->setData( Qt::UserRole, fid ); // Data
lwItem->setData( Qt::DisplayRole, fid ); // Label
lwFeatures->addItem( lwItem );
}
lwFeatures->sortItems();
lwFeatures->blockSignals( false );
lwFeatures->setCurrentRow( 0 ); // Has signal/slot for change current item in ListWidget
}
else
{
mRubberSelectId->reset();
lwFeatures->blockSignals( false );
}
// Set lbStatusItems and pbCreateLayer
QString formatLabel( tr( "%1 of %2 identified" ) );
lbStatusItems->setText( formatLabel.arg( totalItens ).arg( totalFeat ) );
pbCreateLayerItems->setEnabled( totalItens > 0 );
ckbZoomItem->setEnabled( totalItens > 0 );
}
void QgsSpatialQueryDialog::on_cbResultFor_currentIndexChanged()
{
if ( gbResultQuery->isVisible() )
{
visibleResult( false );
}
} // void QgsSpatialQueryDialog::on_cbResultFor_currentIndexChanged()
void QgsSpatialQueryDialog::on_cbOperation_currentIndexChanged()
{
if ( gbResultQuery->isVisible() )
{
visibleResult( false );
}
} // void QgsSpatialQueryDialog::on_cbOperation_currentIndexChanged()
void QgsSpatialQueryDialog::on_lwFeatures_currentItemChanged( QListWidgetItem * item )
{
TypeItems typeItem = ( TypeItems )( cbTypeItems->itemData( cbTypeItems->currentIndex() ).toInt() );
QgsVectorLayer *lyr = typeItem == itemsInvalidReference ? mLayerReference : mLayerTarget;
changeLwFeature( lyr, STRING_TO_FID( item->data( Qt::UserRole ).toString() ) );
} // void QgsSpatialQueryDialog::on_lwFeatures_currentItemChanged( QListWidgetItem * item )
void QgsSpatialQueryDialog::on_ckbUsingSelectedTarget_toggled()
{
populateCbResulFor();
} // void QgsSpatialQueryDialog::on_ckbUsingSelectedTarget_clicked( bool checked )
void QgsSpatialQueryDialog::on_ckbLogProcessing_clicked( bool checked )
{
teStatus->setVisible( checked );
adjustSize();
} // void QgsSpatialQueryDialog::on_ckbLogProcessing_clicked(bool checked)
void QgsSpatialQueryDialog::on_ckbZoomItem_clicked( bool checked )
{
if ( checked )
{
if ( lwFeatures->count() > 0 )
{
QgsFeatureId fid = STRING_TO_FID( lwFeatures->currentItem()->data( Qt::UserRole ).toString() );
TypeItems typeItem = ( TypeItems )( cbTypeItems->itemData( cbTypeItems->currentIndex() ).toInt() );
QgsVectorLayer *lyr = typeItem == itemsInvalidReference ? mLayerReference : mLayerTarget;
zoomFeature( lyr, fid );
}
}
} // QgsSpatialQueryDialog::on_ckbZoomItem_clicked( bool checked )
//! Slots for signs of QGIS