/
qgsdelimitedtextsourceselect.cpp
722 lines (631 loc) · 24.2 KB
/
qgsdelimitedtextsourceselect.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
/***************************************************************************
* Copyright (C) 2004 by Gary Sherman *
* sherman at mrcc.com *
* *
* GUI for loading a delimited text file as a layer in QGIS *
* This plugin works in conjuction with the delimited text data *
* provider plugin *
* *
* 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 "qgsdelimitedtextsourceselect.h"
#include "qgisinterface.h"
#include "qgscontexthelp.h"
#include "qgslogger.h"
#include "qgsvectordataprovider.h"
#include "qgsdelimitedtextprovider.h"
#include "qgsdelimitedtextfile.h"
#include <QFile>
#include <QFileDialog>
#include <QFileInfo>
#include <QMessageBox>
#include <QRegExp>
#include <QSettings>
#include <QTextStream>
#include <QTextCodec>
#include <QUrl>
const int MAX_SAMPLE_LENGTH=200;
QgsDelimitedTextSourceSelect::QgsDelimitedTextSourceSelect( QWidget * parent, Qt::WFlags fl, bool embedded ):
QDialog( parent, fl ),
mFile( new QgsDelimitedTextFile() ),
mExampleRowCount( 20 ),
mBadRowCount( 0 ),
mPluginKey( "/Plugin-DelimitedText" ),
mLastFileType( "" )
{
setupUi( this );
QSettings settings;
restoreGeometry( settings.value( mPluginKey + "/geometry" ).toByteArray() );
if ( embedded )
{
buttonBox->button( QDialogButtonBox::Cancel )->hide();
buttonBox->button( QDialogButtonBox::Ok )->hide();
}
cmbEncoding->clear();
cmbEncoding->addItems( QgsVectorDataProvider::availableEncodings());
cmbEncoding->setCurrentIndex( cmbEncoding->findText( "UTF-8" ) );
loadSettings();
updateFieldsAndEnable();
connect( txtFilePath, SIGNAL( textChanged( QString ) ), this, SLOT( updateFileName() ) );
connect( txtLayerName, SIGNAL( textChanged( QString ) ), this, SLOT( enableAccept() ) );
connect( cmbEncoding, SIGNAL( currentIndexChanged( int ) ), this, SLOT( updateFieldsAndEnable() ) );
connect( delimiterCSV, SIGNAL( toggled( bool ) ), this, SLOT( updateFieldsAndEnable() ) );
connect( delimiterChars, SIGNAL( toggled( bool ) ), this, SLOT( updateFieldsAndEnable() ) );
connect( delimiterRegexp, SIGNAL( toggled( bool ) ), this, SLOT( updateFieldsAndEnable() ) );
connect( cbxDelimComma, SIGNAL( stateChanged( int ) ), this, SLOT( updateFieldsAndEnable() ) );
connect( cbxDelimSpace, SIGNAL( stateChanged( int ) ), this, SLOT( updateFieldsAndEnable() ) );
connect( cbxDelimTab, SIGNAL( stateChanged( int ) ), this, SLOT( updateFieldsAndEnable() ) );
connect( cbxDelimSemicolon, SIGNAL( stateChanged( int ) ), this, SLOT( updateFieldsAndEnable() ) );
connect( cbxDelimColon, SIGNAL( stateChanged( int ) ), this, SLOT( updateFieldsAndEnable() ) );
connect( txtDelimiterOther, SIGNAL( textChanged( QString ) ), this, SLOT( updateFieldsAndEnable() ) );
connect( txtQuoteChars, SIGNAL( textChanged( QString ) ), this, SLOT( updateFieldsAndEnable() ) );
connect( txtEscapeChars, SIGNAL( textChanged( QString ) ), this, SLOT( updateFieldsAndEnable() ) );
connect( txtDelimiterRegexp, SIGNAL( textChanged( QString ) ), this, SLOT( updateFieldsAndEnable() ) );
connect( rowCounter, SIGNAL( valueChanged( int ) ), this, SLOT( updateFieldsAndEnable() ) );
connect( cbxUseHeader, SIGNAL( stateChanged( int ) ), this, SLOT( updateFieldsAndEnable() ) );
connect( cbxSkipEmptyFields, SIGNAL( stateChanged( int ) ), this, SLOT( updateFieldsAndEnable() ) );
connect( cbxTrimFields, SIGNAL( stateChanged( int ) ), this, SLOT( updateFieldsAndEnable() ) );
connect( cbxPointIsComma, SIGNAL( toggled( bool ) ), this, SLOT( updateFieldsAndEnable() ) );
connect( cbxXyDms, SIGNAL( toggled( bool ) ), this, SLOT( updateFieldsAndEnable() ) );
}
QgsDelimitedTextSourceSelect::~QgsDelimitedTextSourceSelect()
{
QSettings settings;
settings.setValue( mPluginKey + "/geometry", saveGeometry() );
delete mFile;
}
void QgsDelimitedTextSourceSelect::on_btnBrowseForFile_clicked()
{
getOpenFileName();
}
void QgsDelimitedTextSourceSelect::on_buttonBox_accepted()
{
// The following conditions should not be hit! OK will not be enabled...
if ( txtLayerName->text().isEmpty() )
{
QMessageBox::warning( this, tr( "No layer name" ), tr( "Please enter a layer name before adding the layer to the map" ) );
txtLayerName->setFocus();
return;
}
if ( delimiterChars->isChecked() )
{
if ( selectedChars().size() == 0 )
{
QMessageBox::warning( this, tr( "No delimiters set" ), tr( "Please one or more characters to use as the delimiter, or choose a different delimiter type" ) );
txtDelimiterOther->setFocus();
return;
}
}
if ( delimiterRegexp->isChecked() )
{
QRegExp re( txtDelimiterRegexp->text() );
if ( ! re.isValid() )
{
QMessageBox::warning( this, tr( "Invalid regular expression" ), tr( "Please enter a valid regular expression as the delimiter, or choose a different delimiter type" ) );
txtDelimiterRegexp->setFocus();
return;
}
}
if ( ! mFile->isValid() )
{
QMessageBox::warning( this, tr( "Invalid delimited text file" ), tr( "Please enter a valid file and delimiter" ) );
return;
}
//Build the delimited text URI from the user provided information
QUrl url = mFile->url();
if ( cbxPointIsComma->isChecked() )
{
url.addQueryItem( "decimalPoint", "," );
}
if ( cbxXyDms->isChecked() )
{
url.addQueryItem( "xyDms", "yes" );
}
if ( geomTypeXY->isChecked() )
{
if ( !cmbXField->currentText().isEmpty() && !cmbYField->currentText().isEmpty() )
{
QString field = cmbXField->currentText();
url.addQueryItem( "xField", field );
field = cmbYField->currentText();
url.addQueryItem( "yField", field );
}
}
else if ( geomTypeWKT->isChecked() )
{
if ( ! cmbWktField->currentText().isEmpty() )
{
QString field = cmbWktField->currentText();
url.addQueryItem( "wktField", field );
}
if ( cmbGeometryType->currentIndex() > 0 )
{
url.addQueryItem( "geomType", cmbGeometryType->currentText() );
}
}
else
{
url.addQueryItem( "geomType", "none" );
}
// store the settings
saveSettings();
saveSettingsForFile( txtFilePath->text() );
// add the layer to the map
emit addVectorLayer( QString::fromAscii( url.toEncoded() ), txtLayerName->text(), "delimitedtext" );
accept();
}
void QgsDelimitedTextSourceSelect::on_buttonBox_rejected()
{
reject();
}
QString QgsDelimitedTextSourceSelect::selectedChars()
{
QString chars = "";
if ( cbxDelimComma->isChecked() )
chars.append( "," );
if ( cbxDelimSpace->isChecked() )
chars.append( " " );
if ( cbxDelimTab->isChecked() )
chars.append( "\t" );
if ( cbxDelimSemicolon->isChecked() )
chars.append( ";" );
if ( cbxDelimColon->isChecked() )
chars.append( ":" );
chars = QgsDelimitedTextFile::encodeChars( chars );
chars.append( txtDelimiterOther->text() );
return chars;
}
void QgsDelimitedTextSourceSelect::setSelectedChars( QString delimiters )
{
QString chars = QgsDelimitedTextFile::decodeChars( delimiters );
cbxDelimComma->setChecked( chars.contains( "," ) );
cbxDelimSpace->setChecked( chars.contains( " " ) );
cbxDelimTab->setChecked( chars.contains( "\t" ) );
cbxDelimColon->setChecked( chars.contains( ":" ) );
cbxDelimSemicolon->setChecked( chars.contains( ";" ) );
chars = chars.remove( QRegExp( "[ ,:;\t]" ) );
chars = QgsDelimitedTextFile::encodeChars( chars );
txtDelimiterOther->setText( chars );
}
void QgsDelimitedTextSourceSelect::loadSettings( QString subkey, bool loadGeomSettings )
{
QSettings settings;
// at startup, fetch the last used delimiter and directory from
// settings
QString key = mPluginKey;
if ( ! subkey.isEmpty() ) key.append( "/" ).append( subkey );
// and how to use the delimiter
QString delimiterType = settings.value( key + "/delimiterType", "" ).toString();
if ( delimiterType == "chars" )
{
delimiterChars->setChecked( true );
}
else if ( delimiterType == "regexp" )
{
delimiterRegexp->setChecked( true );
}
else if ( delimiterType == "csv" )
{
delimiterCSV->setChecked( true );
}
QString encoding = settings.value( key + "/encoding", "" ).toString();
if ( ! encoding.isEmpty() ) cmbEncoding->setCurrentIndex( cmbEncoding->findText( encoding ) );
QString delimiters = settings.value( key + "/delimiters", "" ).toString();
if ( ! delimiters.isEmpty() ) setSelectedChars( delimiters );
txtQuoteChars->setText( settings.value( key + "/quoteChars", "\"" ).toString() );
txtEscapeChars->setText( settings.value( key + "/escapeChars", "\"" ).toString() );
QString regexp = settings.value( key + "/delimiterRegexp", "" ).toString();
if ( ! regexp.isEmpty() ) txtDelimiterRegexp->setText( regexp );
rowCounter->setValue( settings.value( key + "/startFrom", 0 ).toInt() );
cbxUseHeader->setChecked( settings.value( key + "/useHeader", "true" ) != "false" );
cbxTrimFields->setChecked( settings.value( key + "/trimFields", "false" ) == "true" );
cbxSkipEmptyFields->setChecked( settings.value( key + "/skipEmptyFields", "false" ) == "true" );
cbxPointIsComma->setChecked( settings.value( key + "/decimalPoint", "." ).toString().contains( "," ) );
if ( loadGeomSettings )
{
QString geomColumnType = settings.value( key + "/geomColumnType", "xy" ).toString();
if ( geomColumnType == "xy" ) geomTypeXY->setChecked( true );
else if ( geomColumnType == "wkt" ) geomTypeWKT->setChecked( true );
else geomTypeNone->setChecked( true );
cbxXyDms->setChecked( settings.value( key + "/xyDms", "false" ) == "true" );
}
}
void QgsDelimitedTextSourceSelect::saveSettings( QString subkey, bool saveGeomSettings )
{
QSettings settings;
QString key = mPluginKey;
if ( ! subkey.isEmpty() ) key.append( "/" ).append( subkey );
settings.setValue( key + "/encoding", cmbEncoding->currentText() );
settings.setValue( key + "/geometry", saveGeometry() );
if ( delimiterCSV->isChecked() )
settings.setValue( key + "/delimiterType", "csv" );
else if ( delimiterChars->isChecked() )
settings.setValue( key + "/delimiterType", "chars" );
else
settings.setValue( key + "/delimiterType", "regexp" );
settings.setValue( key + "/delimiters", selectedChars() );
settings.setValue( key + "/quoteChars", txtQuoteChars->text() );
settings.setValue( key + "/escapeChars", txtEscapeChars->text() );
settings.setValue( key + "/delimiterRegexp", txtDelimiterRegexp->text() );
settings.setValue( key + "/startFrom", rowCounter->value() );
settings.setValue( key + "/useHeader", cbxUseHeader->isChecked() ? "true" : "false" );
settings.setValue( key + "/trimFields", cbxTrimFields->isChecked() ? "true" : "false" );
settings.setValue( key + "/skipEmptyFields", cbxSkipEmptyFields->isChecked() ? "true" : "false" );
settings.setValue( key + "/decimalPoint", cbxPointIsComma->isChecked() ? "," : "." );
if ( saveGeomSettings )
{
QString geomColumnType = "none";
if ( geomTypeXY->isChecked() ) geomColumnType = "xy";
if ( geomTypeWKT->isChecked() ) geomColumnType = "wkt";
settings.setValue( key + "/geomColumnType", geomColumnType );
settings.setValue( key + "/xyDms", cbxXyDms->isChecked() ? "true" : "false" );
}
}
void QgsDelimitedTextSourceSelect::loadSettingsForFile( QString filename )
{
if ( filename.isEmpty() ) return;
QFileInfo fi( filename );
QString filetype = fi.suffix();
// Don't expect to change settings if not changing file type
if ( filetype != mLastFileType ) loadSettings( fi.suffix(), true );
mLastFileType = filetype;
}
void QgsDelimitedTextSourceSelect::saveSettingsForFile( QString filename )
{
if ( filename.isEmpty() ) return;
QFileInfo fi( filename );
saveSettings( fi.suffix(), true );
}
bool QgsDelimitedTextSourceSelect::loadDelimitedFileDefinition()
{
mFile->setFileName( txtFilePath->text() );
mFile->setEncoding( cmbEncoding->currentText() );
if ( delimiterChars->isChecked() )
{
mFile->setTypeCSV( selectedChars(), txtQuoteChars->text(), txtEscapeChars->text() );
}
else if ( delimiterRegexp->isChecked() )
{
mFile->setTypeRegexp( txtDelimiterRegexp->text() );
}
else
{
mFile->setTypeCSV();
}
mFile->setSkipLines( rowCounter->value() );
mFile->setUseHeader( cbxUseHeader->isChecked() );
mFile->setDiscardEmptyFields( cbxSkipEmptyFields->isChecked() );
mFile->setTrimFields( cbxTrimFields->isChecked() );
return mFile->isValid();
}
void QgsDelimitedTextSourceSelect::updateFieldLists()
{
// Update the x and y field dropdown boxes
QgsDebugMsg( "Updating field lists" );
disconnect( cmbXField, SIGNAL( currentIndexChanged( int ) ), this, SLOT( enableAccept() ) );
disconnect( cmbYField, SIGNAL( currentIndexChanged( int ) ), this, SLOT( enableAccept() ) );
disconnect( cmbWktField, SIGNAL( currentIndexChanged( int ) ), this, SLOT( enableAccept() ) );
disconnect( geomTypeXY, SIGNAL( toggled( bool ) ), this, SLOT( enableAccept() ) );
disconnect( geomTypeWKT, SIGNAL( toggled( bool ) ), this, SLOT( enableAccept() ) );
disconnect( geomTypeNone, SIGNAL( toggled( bool ) ), this, SLOT( enableAccept() ) );
QString columnX = cmbXField->currentText();
QString columnY = cmbYField->currentText();
QString columnWkt = cmbWktField->currentText();
// clear the field lists
cmbXField->clear();
cmbYField->clear();
cmbWktField->clear();
frmGeometry->setEnabled( false );
// clear the sample text box
tblSample->clear();
tblSample->setColumnCount( 0 );
tblSample->setRowCount( 0 );
if ( ! loadDelimitedFileDefinition() )
return;
// Put a sample set of records into the sample box. Also while scanning assess suitability of
// fields for use as coordinate and WKT fields
QList<bool> isValidCoordinate;
QList<bool> isValidWkt;
QList<bool> isEmpty;
int counter = 0;
mBadRowCount = 0;
QStringList values;
QRegExp wktre( "^\\s*(?:MULTI)?(?:POINT|LINESTRING|POLYGON)\\s*Z?\\s*M?\\(", Qt::CaseInsensitive );
while ( counter < mExampleRowCount )
{
QgsDelimitedTextFile::Status status = mFile->nextRecord( values );
if ( status == QgsDelimitedTextFile::RecordEOF ) break;
if ( status != QgsDelimitedTextFile::RecordOk ) { mBadRowCount++; continue; }
counter++;
// Look at count of non-blank fields
int nv = values.size();
while ( nv > 0 && values[nv-1].isEmpty() ) nv--;
if ( isEmpty.size() < nv )
{
while ( isEmpty.size() < nv )
{
isEmpty.append( true );
isValidCoordinate.append( false );
isValidWkt.append( false );
}
tblSample->setColumnCount( nv );
}
tblSample->setRowCount( counter );
bool xyDms = cbxXyDms->isChecked();
for ( int i = 0; i < tblSample->columnCount(); i++ )
{
QString value = i < nv ? values[i] : "";
if( value.length() > MAX_SAMPLE_LENGTH ) value = value.mid(0,MAX_SAMPLE_LENGTH)+"...";
QTableWidgetItem *item = new QTableWidgetItem( value );
tblSample->setItem( counter - 1, i, item );
if ( ! value.isEmpty() )
{
if ( isEmpty[i] )
{
isEmpty[i] = false;
isValidCoordinate[i] = true;
isValidWkt[i] = true;
}
if ( isValidCoordinate[i] )
{
bool ok = true;
if ( cbxPointIsComma->isChecked() )
{
value.replace( ",", "." );
}
if ( xyDms )
{
ok = QgsDelimitedTextProvider::CrdDmsRegexp.indexIn( value ) == 0;
}
else
{
value.toDouble( &ok );
}
isValidCoordinate[i] = ok;
}
if ( isValidWkt[i] )
{
value.remove( QgsDelimitedTextProvider::WktPrefixRegexp );
isValidWkt[i] = value.contains( wktre );
}
}
}
}
QStringList fieldList = mFile->fieldNames();
if ( isEmpty.size() < fieldList.size() )
{
while ( isEmpty.size() < fieldList.size() )
{
isEmpty.append( true );
isValidCoordinate.append( false );
isValidWkt.append( false );
}
tblSample->setColumnCount( fieldList.size() );
}
tblSample->setHorizontalHeaderLabels( fieldList );
tblSample->resizeColumnsToContents();
tblSample->resizeRowsToContents();
// We don't know anything about a text based field other
// than its name. All fields are assumed to be text
// As we ignore blank fields we need to map original index
// of selected fields to index in combo box.
int fieldNo = 0;
for ( int i = 0; i < fieldList.size(); i++ )
{
QString field = fieldList[i];
// skip empty field names
if ( field.isEmpty() ) continue;
cmbXField->addItem( field );
cmbYField->addItem( field );
cmbWktField->addItem( field );
fieldNo++;
}
// Try resetting current values for column names
cmbWktField->setCurrentIndex( cmbWktField->findText( columnWkt ) );
cmbXField->setCurrentIndex( cmbXField->findText( columnX ) );
cmbYField->setCurrentIndex( cmbYField->findText( columnY ) );
// Now try setting optional X,Y fields - will only reset the fields if
// not already set.
trySetXYField( fieldList, isValidCoordinate, "longitude", "latitude" );
trySetXYField( fieldList, isValidCoordinate, "lon", "lat" );
trySetXYField( fieldList, isValidCoordinate, "east", "north" );
trySetXYField( fieldList, isValidCoordinate, "x", "y" );
trySetXYField( fieldList, isValidCoordinate, "e", "n" );
// And also a WKT field if there is one
if ( cmbWktField->currentIndex() < 0 )
{
for ( int i = 0; i < fieldList.size(); i++ )
{
if ( ! isValidWkt[i] ) continue;
int index = cmbWktField->findText( fieldList[i] );
if ( index >= 0 )
{
cmbWktField->setCurrentIndex( index );
break;
}
}
}
bool haveFields = fieldNo > 0;
bool isXY = cmbWktField->currentIndex() < 0 ||
( geomTypeXY->isChecked() &&
( cmbXField->currentIndex() >= 0 && cmbYField->currentIndex() >= 0 ) );
geomTypeXY->setChecked( isXY );
geomTypeWKT->setChecked( ! isXY );
if ( haveFields )
{
frmGeometry->setEnabled( true );
connect( cmbXField, SIGNAL( currentIndexChanged( int ) ), this, SLOT( enableAccept() ) );
connect( cmbYField, SIGNAL( currentIndexChanged( int ) ), this, SLOT( enableAccept() ) );
connect( cmbWktField, SIGNAL( currentIndexChanged( int ) ), this, SLOT( enableAccept() ) );
connect( geomTypeXY, SIGNAL( toggled( bool ) ), this, SLOT( enableAccept() ) );
connect( geomTypeWKT, SIGNAL( toggled( bool ) ), this, SLOT( enableAccept() ) );
connect( geomTypeNone, SIGNAL( toggled( bool ) ), this, SLOT( enableAccept() ) );
}
}
bool QgsDelimitedTextSourceSelect::trySetXYField( QStringList &fields, QList<bool> &isValidNumber, QString xname, QString yname )
{
// If fields already set, then nothing to do
if ( cmbXField->currentIndex() >= 0 && cmbYField->currentIndex() >= 0 ) return true;
// Try and find a valid field name matching the x field
int indexX = -1;
int indexY = -1;
for ( int i = 0; i < fields.size(); i++ )
{
// Only interested in number fields containing the xname string
// that are in the X combo box
if ( ! isValidNumber[i] ) continue;
if ( ! fields[i].contains( xname, Qt::CaseInsensitive ) ) continue;
indexX = cmbXField->findText( fields[i] );
if ( indexX < 0 ) continue;
// Now look for potential y fields, like xname with x replaced with y
QString xfield( fields[i] );
int from = 0;
while ( true )
{
int pos = xfield.indexOf( xname, from, Qt::CaseInsensitive );
if ( pos < 0 ) break;
from = pos + 1;
QString yfield = xfield.mid( 0, pos ) + yname + xfield.mid( pos + xname.size() );
if ( ! fields.contains( yfield, Qt::CaseInsensitive ) ) continue;
for ( int iy = 0; iy < fields.size(); iy++ )
{
if ( ! isValidNumber[iy] ) continue;
if ( iy == i ) continue;
if ( fields[iy].compare( yfield, Qt::CaseInsensitive ) == 0 )
{
indexY = cmbYField->findText( fields[iy] );
break;
}
}
if ( indexY >= 0 ) break;
}
if ( indexY >= 0 ) break;
}
if ( indexY >= 0 )
{
cmbXField->setCurrentIndex( indexX );
cmbYField->setCurrentIndex( indexY );
}
return indexY >= 0;
}
void QgsDelimitedTextSourceSelect::getOpenFileName()
{
// Get a file to process, starting at the current directory
// Set inital dir to last used
QSettings settings;
QString selectedFilter = settings.value( mPluginKey + "/file_filter", "" ).toString();
QString s = QFileDialog::getOpenFileName(
this,
tr( "Choose a delimited text file to open" ),
settings.value( mPluginKey + "/text_path", "./" ).toString(),
tr( "Text files" ) + " (*.txt *.csv *.dat *.wkt);;"
+ tr( "All files" ) + " (* *.*)",
&selectedFilter
);
// set path
if ( s.isNull() ) return;
settings.setValue( mPluginKey + "/file_filter", selectedFilter );
txtFilePath->setText( s );
}
void QgsDelimitedTextSourceSelect::updateFileName()
{
// put a default layer name in the text entry
QString filename = txtFilePath->text();
QFileInfo finfo( filename );
if ( finfo.exists() )
{
QSettings settings;
settings.setValue( mPluginKey + "/text_path", finfo.path() );
}
txtLayerName->setText( finfo.completeBaseName() );
loadSettingsForFile( filename );
updateFieldsAndEnable();
}
void QgsDelimitedTextSourceSelect::updateFieldsAndEnable()
{
updateFieldLists();
enableAccept();
}
bool QgsDelimitedTextSourceSelect::validate()
{
// Check that input data is valid - provide a status message if not..
QString message( "" );
bool enabled = false;
if ( txtFilePath->text().trimmed().isEmpty() )
{
message = tr( "Please select an input file" );
}
else if ( ! QFileInfo( txtFilePath->text() ).exists() )
{
message = tr( "File %1 does not exist" ).arg( txtFilePath->text() );
}
else if ( txtLayerName->text().isEmpty() )
{
message = tr( "Please enter a layer name" );
}
else if ( delimiterChars->isChecked() && selectedChars().size() == 0 )
{
message = tr( "At least one delimiter character must be specified" );
}
if ( message.isEmpty() && delimiterRegexp->isChecked() )
{
QRegExp re( txtDelimiterRegexp->text() );
if ( ! re.isValid() )
{
message = tr( "Regular expression is not valid" );
}
else if ( re.pattern().startsWith( "^" ) && re.captureCount() == 0 )
{
message = tr( "^.. expression needs capture groups" );
}
lblRegexpError->setText( message );
}
if ( ! message.isEmpty() )
{
// continue...
}
// Hopefully won't hit this none-specific message, but just in case ...
else if ( ! mFile->isValid() )
{
message = tr( "Definition of filename and delimiters is not valid" );
}
// Assume that the sample table will have been populated if data was found
else if ( tblSample->rowCount() == 0 )
{
message = tr( "No data found in file" );
if( mBadRowCount > 0 )
{
message = message + " (" + tr("%1 badly formatted records discarded").arg(mBadRowCount)+")";
}
}
else if ( geomTypeXY->isChecked() && ( cmbXField->currentText().isEmpty() || cmbYField->currentText().isEmpty() ) )
{
message = tr( "X and Y field names must be selected" );
}
else if ( geomTypeXY->isChecked() && ( cmbXField->currentText() == cmbYField->currentText() ) )
{
message = tr( "X and Y field names cannot be the same" );
}
else if ( geomTypeWKT->isChecked() && cmbWktField->currentText().isEmpty() )
{
message = tr( "The WKT field name must be selected" );
}
else
{
enabled = true;
if( mBadRowCount > 0 )
{
message = tr("%1 badly formatted records discarded from sample data").arg(mBadRowCount);
}
}
lblStatus->setText( message );
return enabled;
}
void QgsDelimitedTextSourceSelect::enableAccept()
{
bool enabled = validate();
buttonBox->button( QDialogButtonBox::Ok )->setEnabled( enabled );
}