21
21
#include " qgssettings.h"
22
22
#include " qgsproviderregistry.h"
23
23
#include " qgsgui.h"
24
+ #include " qgsapplication.h"
24
25
25
26
#include < QButtonGroup>
26
27
#include < QFile>
@@ -138,86 +139,15 @@ void QgsDelimitedTextSourceSelect::addButtonClicked()
138
139
}
139
140
140
141
// Build the delimited text URI from the user provided information
142
+ const QString datasourceUrl { url ( )};
141
143
142
- QUrl url = mFile ->url ();
143
- QUrlQuery query ( url );
144
-
145
- query.addQueryItem ( QStringLiteral ( " detectTypes" ), cbxDetectTypes->isChecked () ? QStringLiteral ( " yes" ) : QStringLiteral ( " no" ) );
146
-
147
- if ( cbxPointIsComma->isChecked () )
148
- {
149
- query.addQueryItem ( QStringLiteral ( " decimalPoint" ), QStringLiteral ( " ," ) );
150
- }
151
- if ( cbxXyDms->isChecked () )
152
- {
153
- query.addQueryItem ( QStringLiteral ( " xyDms" ), QStringLiteral ( " yes" ) );
154
- }
155
-
156
- bool haveGeom = true ;
157
- if ( geomTypeXY->isChecked () )
158
- {
159
- QString field;
160
- if ( !cmbXField->currentText ().isEmpty () && !cmbYField->currentText ().isEmpty () )
161
- {
162
- field = cmbXField->currentText ();
163
- query.addQueryItem ( QStringLiteral ( " xField" ), field );
164
- field = cmbYField->currentText ();
165
- query.addQueryItem ( QStringLiteral ( " yField" ), field );
166
- }
167
- if ( !cmbZField->currentText ().isEmpty () )
168
- {
169
- field = cmbZField->currentText ();
170
- query.addQueryItem ( QStringLiteral ( " zField" ), field );
171
- }
172
- if ( !cmbMField->currentText ().isEmpty () )
173
- {
174
- field = cmbMField->currentText ();
175
- query.addQueryItem ( QStringLiteral ( " mField" ), field );
176
- }
177
- }
178
- else if ( geomTypeWKT->isChecked () )
179
- {
180
- if ( ! cmbWktField->currentText ().isEmpty () )
181
- {
182
- const QString field = cmbWktField->currentText ();
183
- query.addQueryItem ( QStringLiteral ( " wktField" ), field );
184
- }
185
- if ( cmbGeometryType->currentIndex () > 0 )
186
- {
187
- query.addQueryItem ( QStringLiteral ( " geomType" ), cmbGeometryType->currentText () );
188
- }
189
- }
190
- else
191
- {
192
- haveGeom = false ;
193
- query.addQueryItem ( QStringLiteral ( " geomType" ), QStringLiteral ( " none" ) );
194
- }
195
- if ( haveGeom )
196
- {
197
- const QgsCoordinateReferenceSystem crs = crsGeometry->crs ();
198
- if ( crs.isValid () )
199
- {
200
- query.addQueryItem ( QStringLiteral ( " crs" ), crs.authid () );
201
- }
202
-
203
- }
204
-
205
- if ( ! geomTypeNone->isChecked () )
206
- {
207
- query.addQueryItem ( QStringLiteral ( " spatialIndex" ), cbxSpatialIndex->isChecked () ? QStringLiteral ( " yes" ) : QStringLiteral ( " no" ) );
208
- }
209
-
210
- query.addQueryItem ( QStringLiteral ( " subsetIndex" ), cbxSubsetIndex->isChecked () ? QStringLiteral ( " yes" ) : QStringLiteral ( " no" ) );
211
- query.addQueryItem ( QStringLiteral ( " watchFile" ), cbxWatchFile->isChecked () ? QStringLiteral ( " yes" ) : QStringLiteral ( " no" ) );
212
-
213
- url.setQuery ( query );
214
144
// store the settings
215
145
saveSettings ();
216
146
saveSettingsForFile ( mFileWidget ->filePath () );
217
147
218
148
219
149
// add the layer to the map
220
- emit addVectorLayer ( QString::fromLatin1 ( url. toEncoded () ) , txtLayerName->text () );
150
+ emit addVectorLayer ( datasourceUrl , txtLayerName->text () );
221
151
222
152
// clear the file and layer name show something has happened, ready for another file
223
153
@@ -462,6 +392,7 @@ void QgsDelimitedTextSourceSelect::updateFieldLists()
462
392
if ( status != QgsDelimitedTextFile::RecordOk ) { mBadRowCount ++; continue ; }
463
393
counter++;
464
394
395
+
465
396
// Look at count of non-blank fields
466
397
467
398
int nv = values.size ();
@@ -524,6 +455,7 @@ void QgsDelimitedTextSourceSelect::updateFieldLists()
524
455
}
525
456
}
526
457
458
+
527
459
QStringList fieldList = mFile ->fieldNames ();
528
460
529
461
if ( isEmpty.size () < fieldList.size () )
@@ -537,6 +469,46 @@ void QgsDelimitedTextSourceSelect::updateFieldLists()
537
469
tblSample->setColumnCount ( fieldList.size () );
538
470
}
539
471
472
+ tblSample->insertRow ( 0 );
473
+ QStringList verticalHeaderLabels;
474
+ verticalHeaderLabels.push_back ( QString ( ) );
475
+
476
+ for ( int i = 1 ; i <= tblSample->rowCount (); i++ )
477
+ {
478
+ verticalHeaderLabels.push_back ( QString::number ( i ) );
479
+ }
480
+
481
+ tblSample->setVerticalHeaderLabels ( verticalHeaderLabels );
482
+
483
+ // This may be slow on huge files, maybe we need a separate thread
484
+ mFields = QgsDelimitedTextProvider (
485
+ url (),
486
+ QgsDataProvider::ProviderOptions (),
487
+ QgsDataProvider::ReadFlag::SkipFeatureCount | QgsDataProvider::ReadFlag::SkipGetExtent )
488
+ .fields ();
489
+
490
+ for ( int i = 0 ; i < tblSample->columnCount (); i++ )
491
+ {
492
+ QComboBox *typeCombo = new QComboBox ( tblSample );
493
+ typeCombo->addItem ( QgsApplication::getThemeIcon ( QStringLiteral ( " /mIconFieldText.svg" ) ), tr ( " Text" ), " string" );
494
+ typeCombo->addItem ( QgsApplication::getThemeIcon ( QStringLiteral ( " /mIconFieldInteger.svg" ) ), tr ( " Whole Number" ), " integer" );
495
+ typeCombo->addItem ( QgsApplication::getThemeIcon ( QStringLiteral ( " /mIconFieldFloat.svg" ) ), tr ( " Decimal Number" ), " double" );
496
+ typeCombo->addItem ( QgsApplication::getThemeIcon ( QStringLiteral ( " /mIconFieldBool.svg" ) ), tr ( " Boolean" ), " bool" );
497
+ typeCombo->addItem ( QgsApplication::getThemeIcon ( QStringLiteral ( " /mIconFieldDate.svg" ) ), tr ( " Date" ), " date" );
498
+ typeCombo->addItem ( QgsApplication::getThemeIcon ( QStringLiteral ( " /mIconFieldTime.svg" ) ), tr ( " Time" ), " time" );
499
+ typeCombo->addItem ( QgsApplication::getThemeIcon ( QStringLiteral ( " /mIconFieldDateTime.svg" ) ), tr ( " Date and Time" ), " datetime" );
500
+ if ( mFields .lookupField ( fieldList[ i ] ) >= 0 )
501
+ {
502
+ const QString typeName { mFields .field ( fieldList[ i ] ).typeName () };
503
+ const int idx {typeCombo->findData ( typeName )};
504
+ if ( idx >= 0 )
505
+ {
506
+ typeCombo->setCurrentIndex ( idx );
507
+ }
508
+ }
509
+ tblSample->setCellWidget ( 0 , i, typeCombo );
510
+ }
511
+
540
512
tblSample->setHorizontalHeaderLabels ( fieldList );
541
513
tblSample->resizeColumnsToContents ();
542
514
tblSample->resizeRowsToContents ();
@@ -792,3 +764,99 @@ void QgsDelimitedTextSourceSelect::showCrsWidget()
792
764
crsGeometry->setVisible ( !geomTypeNone->isChecked () );
793
765
textLabelCrs->setVisible ( !geomTypeNone->isChecked () );
794
766
}
767
+
768
+ QString QgsDelimitedTextSourceSelect::url ()
769
+ {
770
+ if ( ! validate () )
771
+ {
772
+ return QString ();
773
+ }
774
+ QUrl url = mFile ->url ();
775
+ QUrlQuery query ( url );
776
+
777
+ query.addQueryItem ( QStringLiteral ( " detectTypes" ), cbxDetectTypes->isChecked () ? QStringLiteral ( " yes" ) : QStringLiteral ( " no" ) );
778
+
779
+ if ( cbxPointIsComma->isChecked () )
780
+ {
781
+ query.addQueryItem ( QStringLiteral ( " decimalPoint" ), QStringLiteral ( " ," ) );
782
+ }
783
+ if ( cbxXyDms->isChecked () )
784
+ {
785
+ query.addQueryItem ( QStringLiteral ( " xyDms" ), QStringLiteral ( " yes" ) );
786
+ }
787
+
788
+ bool haveGeom = true ;
789
+ if ( geomTypeXY->isChecked () )
790
+ {
791
+ QString field;
792
+ if ( !cmbXField->currentText ().isEmpty () && !cmbYField->currentText ().isEmpty () )
793
+ {
794
+ field = cmbXField->currentText ();
795
+ query.addQueryItem ( QStringLiteral ( " xField" ), field );
796
+ field = cmbYField->currentText ();
797
+ query.addQueryItem ( QStringLiteral ( " yField" ), field );
798
+ }
799
+ if ( !cmbZField->currentText ().isEmpty () )
800
+ {
801
+ field = cmbZField->currentText ();
802
+ query.addQueryItem ( QStringLiteral ( " zField" ), field );
803
+ }
804
+ if ( !cmbMField->currentText ().isEmpty () )
805
+ {
806
+ field = cmbMField->currentText ();
807
+ query.addQueryItem ( QStringLiteral ( " mField" ), field );
808
+ }
809
+ }
810
+ else if ( geomTypeWKT->isChecked () )
811
+ {
812
+ if ( ! cmbWktField->currentText ().isEmpty () )
813
+ {
814
+ const QString field = cmbWktField->currentText ();
815
+ query.addQueryItem ( QStringLiteral ( " wktField" ), field );
816
+ }
817
+ if ( cmbGeometryType->currentIndex () > 0 )
818
+ {
819
+ query.addQueryItem ( QStringLiteral ( " geomType" ), cmbGeometryType->currentText () );
820
+ }
821
+ }
822
+ else
823
+ {
824
+ haveGeom = false ;
825
+ query.addQueryItem ( QStringLiteral ( " geomType" ), QStringLiteral ( " none" ) );
826
+ }
827
+ if ( haveGeom )
828
+ {
829
+ const QgsCoordinateReferenceSystem crs = crsGeometry->crs ();
830
+ if ( crs.isValid () )
831
+ {
832
+ query.addQueryItem ( QStringLiteral ( " crs" ), crs.authid () );
833
+ }
834
+
835
+ }
836
+
837
+ if ( ! geomTypeNone->isChecked () )
838
+ {
839
+ query.addQueryItem ( QStringLiteral ( " spatialIndex" ), cbxSpatialIndex->isChecked () ? QStringLiteral ( " yes" ) : QStringLiteral ( " no" ) );
840
+ }
841
+
842
+ query.addQueryItem ( QStringLiteral ( " subsetIndex" ), cbxSubsetIndex->isChecked () ? QStringLiteral ( " yes" ) : QStringLiteral ( " no" ) );
843
+ query.addQueryItem ( QStringLiteral ( " watchFile" ), cbxWatchFile->isChecked () ? QStringLiteral ( " yes" ) : QStringLiteral ( " no" ) );
844
+
845
+ // Set field types if overridden
846
+ for ( int column = 0 ; column < tblSample->columnCount (); column++ )
847
+ {
848
+ const QString fieldName { tblSample->horizontalHeaderItem ( column )->text () };
849
+ const int fieldIdx { mFields .lookupField ( fieldName ) };
850
+ if ( fieldIdx >= 0 )
851
+ {
852
+ QComboBox *typeCombo { qobject_cast<QComboBox *>( tblSample->cellWidget ( 0 , column ) ) };
853
+ if ( typeCombo && typeCombo->currentData ().toString () != mFields .field ( fieldName ).typeName () )
854
+ {
855
+ query.addQueryItem ( QStringLiteral ( " field" ), QString ( fieldName ).replace ( ' :' , QStringLiteral ( " \\ :" ) ) + ' :' + mFields .field ( fieldName ).typeName () );
856
+ }
857
+ }
858
+ }
859
+
860
+ url.setQuery ( query );
861
+ return QString::fromLatin1 ( url.toEncoded () );
862
+ }
0 commit comments