/
qgsdelimitedtextplugingui.cpp
482 lines (398 loc) · 14.1 KB
/
qgsdelimitedtextplugingui.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
/***************************************************************************
* 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. *
***************************************************************************/
/* $Id$ */
#include "qgsdelimitedtextplugingui.h"
#include "qgscontexthelp.h"
#include "qgisinterface.h"
#include <QFileDialog>
#include <QFile>
#include <QComboBox>
#include <QSettings>
#include <QFileInfo>
#include <QRegExp>
#include <QMessageBox>
#include <QTextStream>
#include <QUrl>
#include "qgslogger.h"
QgsDelimitedTextPluginGui::QgsDelimitedTextPluginGui( QgisInterface * _qI, QWidget * parent, Qt::WFlags fl )
: QDialog( parent, fl ), qI( _qI )
{
setupUi( this );
pbnOK = buttonBox->button( QDialogButtonBox::Ok );
updateFieldsAndEnable();
// at startup, fetch the last used delimiter and directory from
// settings
QSettings settings;
QString key = "/Plugin-DelimitedText";
txtDelimiter->setText( settings.value( key + "/delimiter" ).toString() );
// and how to use the delimiter
QString delimiterType = settings.value( key + "/delimiterType", "plain" ).toString();
if ( delimiterType == "selection" )
{
delimiterSelection->setChecked( true );
}
else if ( delimiterType == "plain" )
{
delimiterPlain->setChecked( true );
}
else
{
delimiterRegexp->setChecked( true );
}
QString delimiterChars = settings.value( key + "/delimiterChars", " " ).toString();
cbxDelimSpace->setChecked( delimiterChars.contains( " " ) );
cbxDelimTab->setChecked( delimiterChars.contains( "\\t" ) );
cbxDelimColon->setChecked( delimiterChars.contains( ":" ) );
cbxDelimSemicolon->setChecked( delimiterChars.contains( ":" ) );
cbxDelimComma->setChecked( delimiterChars.contains( "," ) );
cmbXField->setDisabled( true );
cmbYField->setDisabled( true );
cmbWktField->setDisabled( true );
connect( txtFilePath, SIGNAL( textChanged( QString ) ), this, SLOT( updateFieldsAndEnable() ) );
connect( delimiterSelection, SIGNAL( toggled( bool ) ), this, SLOT( updateFieldsAndEnable() ) );
connect( delimiterPlain, SIGNAL( toggled( bool ) ), this, SLOT( updateFieldsAndEnable() ) );
connect( delimiterRegexp, SIGNAL( toggled( bool ) ), 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( cbxDelimComma, SIGNAL( stateChanged( int ) ), this, SLOT( updateFieldsAndEnable() ) );
connect( cbxDelimColon, SIGNAL( stateChanged( int ) ), this, SLOT( updateFieldsAndEnable() ) );
connect( txtDelimiter, SIGNAL( editingFinished() ), this, SLOT( updateFieldsAndEnable() ) );
connect( rowCounter, SIGNAL( valueChanged( int ) ), this, SLOT( updateFieldsAndEnable() ) );
}
QgsDelimitedTextPluginGui::~QgsDelimitedTextPluginGui()
{
}
/** Autoconnected slots **/
void QgsDelimitedTextPluginGui::on_btnBrowseForFile_clicked()
{
getOpenFileName();
}
void QgsDelimitedTextPluginGui::on_buttonBox_accepted()
{
if ( !txtLayerName->text().isEmpty() )
{
//Build the delimited text URI from the user provided information
QString delimiterType;
if ( delimiterSelection->isChecked() )
delimiterType = "regexp";
else if ( delimiterPlain->isChecked() )
delimiterType = "plain";
else if ( delimiterRegexp->isChecked() )
delimiterType = "regexp";
QUrl url = QUrl::fromLocalFile( txtFilePath->text() );
url.addQueryItem( "delimiter", txtDelimiter->text() );
url.addQueryItem( "delimiterType", delimiterType );
if ( geomTypeXY->isChecked() )
{
if ( !cmbXField->currentText().isEmpty() && !cmbYField->currentText().isEmpty() )
{
url.addQueryItem( "xField", cmbXField->currentText() );
url.addQueryItem( "yField", cmbYField->currentText() );
}
}
else
{
if ( ! cmbWktField->currentText().isEmpty() )
{
url.addQueryItem( "wktField", cmbWktField->currentText() );
}
}
int skipLines = rowCounter->value();
if ( skipLines > 0 )
url.addQueryItem( "skipLines", QString( "%1" ).arg( skipLines ) );
// add the layer to the map
QString uri( url.toEncoded() );
emit drawVectorLayer( uri, txtLayerName->text(), "delimitedtext" );
// store the settings
QSettings settings;
QString key = "/Plugin-DelimitedText";
settings.setValue( key + "/delimiter", txtDelimiter->text() );
QFileInfo fi( txtFilePath->text() );
settings.setValue( key + "/text_path", fi.path() );
if ( delimiterSelection->isChecked() )
settings.setValue( key + "/delimiterType", "selection" );
else if ( delimiterPlain->isChecked() )
settings.setValue( key + "/delimiterType", "plain" );
else
settings.setValue( key + "/delimiterType", "regexp" );
settings.setValue( key + "/delimiterChars", selectedChars() );
accept();
}
else
{
QMessageBox::warning( this, tr( "No layer name" ), tr( "Please enter a layer name before adding the layer to the map" ) );
}
}
void QgsDelimitedTextPluginGui::on_buttonBox_rejected()
{
reject();
}
QString QgsDelimitedTextPluginGui::selectedChars()
{
QString chars = "";
if ( cbxDelimSpace->isChecked() ) chars += " ";
if ( cbxDelimTab->isChecked() ) chars += "\\t";
if ( cbxDelimSemicolon->isChecked() ) chars += ";";
if ( cbxDelimComma->isChecked() ) chars += ",";
if ( cbxDelimColon->isChecked() ) chars += ":";
return chars;
}
QStringList QgsDelimitedTextPluginGui::splitLine( QString line )
{
QStringList fieldList;
QString delimiter = txtDelimiter->text();
if ( delimiterPlain->isChecked() )
{
// convert \t to tabulator
delimiter = txtDelimiter->text();
delimiter.replace( "\\t", "\t" );
fieldList = line.split( delimiter );
}
else if ( delimiterSelection->isChecked() )
{
delimiter = "[";
delimiter += selectedChars();
delimiter += "]";
txtDelimiter->setText( delimiter );
fieldList = line.split( QRegExp( delimiter ) );
}
else
{
QRegExp del( delimiter );
fieldList = line.split( QRegExp( delimiter ) );
}
return fieldList;
}
bool QgsDelimitedTextPluginGui::haveValidFileAndDelimiters()
{
bool valid = true;
// If there is no valid file or field delimiters than cannot determine fields
if ( txtFilePath->text().isEmpty() || !QFile( txtFilePath->text() ).exists() )
{
valid = false;
}
else if ( delimiterSelection->isChecked() )
{
valid =
cbxDelimSpace->isChecked() ||
cbxDelimTab->isChecked() ||
cbxDelimSemicolon->isChecked() ||
cbxDelimComma->isChecked() ||
cbxDelimColon->isChecked();
}
else
{
valid = !txtDelimiter->text().isEmpty();
}
return valid;
}
void QgsDelimitedTextPluginGui::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 ) ), cmbXField, SLOT( setEnabled( bool ) ) );
disconnect( geomTypeXY, SIGNAL( toggled( bool ) ), cmbYField, SLOT( setEnabled( bool ) ) );
disconnect( geomTypeXY, SIGNAL( toggled( bool ) ), cmbWktField, SLOT( setDisabled( bool ) ) );
QString columnX = cmbXField->currentText();
QString columnY = cmbYField->currentText();
QString columnWkt = cmbWktField->currentText();
// clear the field lists
cmbXField->clear();
cmbYField->clear();
cmbWktField->clear();
geomTypeXY->setEnabled( false );
geomTypeWKT->setEnabled( false );
cmbXField->setEnabled( false );
cmbYField->setEnabled( false );
cmbWktField->setEnabled( false );
if ( ! haveValidFileAndDelimiters() ) return;
QFile file( txtFilePath->text() );
if ( !file.open( QIODevice::ReadOnly ) )
return;
int skipLines = rowCounter->value();
QTextStream stream( &file );
QString line;
do
{
line = readLine( stream ); // line of text excluding '\n'
}
while ( !line.isEmpty() && skipLines-- > 0 );
QgsDebugMsg( QString( "Attempting to split the input line: %1 using delimiter %2" ).arg( line ).arg( txtDelimiter->text() ) );
QString delimiter = txtDelimiter->text();
QStringList fieldList = splitLine( line );
QgsDebugMsg( QString( "Split line into %1 parts" ).arg( fieldList.size() ) );
//
// We don't know anything about a text based field other
// than its name. All fields are assumed to be text
bool haveFields = false;
foreach( QString field, fieldList )
{
if (( field.left( 1 ) == "'" || field.left( 1 ) == "\"" ) &&
field.left( 1 ) == field.right( 1 ) )
// eat quotes
field = field.mid( 1, field.length() - 2 );
if ( field.length() == 0 )
// skip empty field names
continue;
cmbXField->addItem( field );
cmbYField->addItem( field );
cmbWktField->addItem( field );
haveFields = true;
}
int indexWkt = -1;
if ( ! columnWkt.isEmpty() )
{
indexWkt = cmbWktField->findText( columnWkt );
}
if ( indexWkt < 0 )
{
indexWkt = cmbWktField->findText( "wkt", Qt::MatchContains );
}
if ( indexWkt < 0 )
{
indexWkt = cmbWktField->findText( "geometry", Qt::MatchContains );
}
if ( indexWkt < 0 )
{
indexWkt = cmbWktField->findText( "shape", Qt::MatchContains );
}
cmbWktField->setCurrentIndex( indexWkt );
int indexX = -1;
if ( !columnX.isEmpty() )
{
indexX = cmbXField->findText( columnX );
}
if ( indexX < 0 )
{
indexX = cmbXField->findText( "lon", Qt::MatchContains );
}
if ( indexX < 0 )
{
indexX = cmbXField->findText( "x", Qt::MatchContains );
}
cmbXField->setCurrentIndex( indexX );
int indexY = -1;
if ( !columnY.isEmpty() )
{
indexY = cmbYField->findText( columnY );
}
if ( indexY < 0 )
{
indexY = cmbYField->findText( "lat", Qt::MatchContains );
}
if ( indexY < 0 )
{
indexY = cmbYField->findText( "y", Qt::MatchContains );
}
cmbYField->setCurrentIndex( indexY );
bool isXY = ( geomTypeXY->isChecked() && indexX >= 0 && indexY >= 0 ) || indexWkt < 0;
geomTypeXY->setChecked( isXY );
geomTypeWKT->setChecked( ! isXY );
if ( haveFields )
{
geomTypeXY->setEnabled( true );
geomTypeWKT->setEnabled( true );
cmbXField->setEnabled( isXY );
cmbYField->setEnabled( isXY );
cmbWktField->setEnabled( ! isXY );
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 ) ), cmbXField, SLOT( setEnabled( bool ) ) );
connect( geomTypeXY, SIGNAL( toggled( bool ) ), cmbYField, SLOT( setEnabled( bool ) ) );
connect( geomTypeXY, SIGNAL( toggled( bool ) ), cmbWktField, SLOT( setDisabled( bool ) ) );
}
// clear the sample text box
tblSample->clear();
tblSample->setColumnCount( fieldList.size() );
tblSample->setHorizontalHeaderLabels( fieldList );
// put a few more lines into the sample box
int counter = 0;
line = readLine( stream );
while ( !line.isEmpty() && counter < 20 )
{
QStringList values = splitLine( line );
tblSample->setRowCount( counter + 1 );
for ( int i = 0; i < tblSample->columnCount(); i++ )
{
tblSample->setItem( counter, i, new QTableWidgetItem( i < values.size() ? values[i] : "" ) );
}
counter++;
line = readLine( stream );
}
// close the file
file.close();
// put a default layer name in the text entry
QFileInfo finfo( txtFilePath->text() );
txtLayerName->setText( finfo.completeBaseName() );
}
void QgsDelimitedTextPluginGui::getOpenFileName()
{
// Get a file to process, starting at the current directory
// Set inital dir to last used
QSettings settings;
QString s = QFileDialog::getOpenFileName(
this,
tr( "Choose a delimited text file to open" ),
settings.value( "/Plugin-DelimitedText/text_path", "./" ).toString(),
"Text files (*.txt *.csv);; Well Known Text files (*.wkt);; All files (* *.*)" );
// set path
txtFilePath->setText( s );
}
void QgsDelimitedTextPluginGui::updateFieldsAndEnable()
{
updateFieldLists();
enableAccept();
}
void QgsDelimitedTextPluginGui::enableAccept()
{
// If the geometry type field is enabled then there must be
// a valid file, and it must be
bool enabled = haveValidFileAndDelimiters();
if ( enabled )
{
if ( geomTypeXY->isChecked() )
{
enabled = !( cmbXField->currentText().isEmpty() || cmbYField->currentText().isEmpty() || cmbXField->currentText() == cmbYField->currentText() );
}
else
{
enabled = !cmbWktField->currentText().isEmpty();
}
}
pbnOK->setEnabled( enabled );
}
QString QgsDelimitedTextPluginGui::readLine( QTextStream &stream )
{
QString buffer;
while ( !stream.atEnd() )
{
QChar c = stream.read( 1 ).at( 0 );
if ( c == '\r' || c == '\n' )
{
if ( buffer.isEmpty() )
{
// skip leading CR / LF
continue;
}
break;
}
buffer.append( c );
}
return buffer;
}