/
ImportCsvDialog.cpp
688 lines (592 loc) · 26 KB
/
ImportCsvDialog.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
#include "ImportCsvDialog.h"
#include "ui_ImportCsvDialog.h"
#include "sqlitedb.h"
#include "csvparser.h"
#include "sqlite.h"
#include <QMessageBox>
#include <QProgressDialog>
#include <QPushButton>
#include <QDateTime>
#include <QTextCodec>
#include <QCompleter>
#include <QComboBox>
#include <QFile>
#include <QTextStream>
#include <QSettings>
#include <QFileInfo>
#include <memory>
// Enable this line to show basic performance stats after each imported CSV file. Please keep in mind that while these
// numbers might help to estimate the performance of the algorithm, this is not a proper benchmark.
//#define CSV_BENCHMARK
#ifdef CSV_BENCHMARK
#include <QElapsedTimer>
#endif
ImportCsvDialog::ImportCsvDialog(const QStringList &filenames, DBBrowserDB* db, QWidget* parent)
: QDialog(parent),
ui(new Ui::ImportCsvDialog),
csvFilenames(filenames),
pdb(db)
{
ui->setupUi(this);
// Get the actual file name out of the provided path and use it as the default table name for import
// For importing several files at once, the fields have to be the same so we can safely use the first
QFileInfo file(filenames.first());
ui->editName->setText(file.baseName());
// Create a list of all available encodings and create an auto completion list from them
QStringList encodingList;
foreach(QString enc, QTextCodec::availableCodecs())
encodingList.push_back(enc);
encodingCompleter = new QCompleter(encodingList, this);
encodingCompleter->setCaseSensitivity(Qt::CaseInsensitive);
ui->editCustomEncoding->setCompleter(encodingCompleter);
// Load last used settings and apply them
ui->checkboxHeader->blockSignals(true);
ui->checkBoxTrimFields->blockSignals(true);
ui->checkBoxSeparateTables->blockSignals(true);
ui->comboSeparator->blockSignals(true);
ui->comboQuote->blockSignals(true);
ui->comboEncoding->blockSignals(true);
QSettings settings(QApplication::organizationName(), QApplication::organizationName());
ui->checkboxHeader->setChecked(settings.value("importcsv/firstrowheader", false).toBool());
ui->checkBoxTrimFields->setChecked(settings.value("importcsv/trimfields", true).toBool());
ui->checkBoxSeparateTables->setChecked(settings.value("importcsv/separatetables", false).toBool());
setSeparatorChar(QChar(settings.value("importcsv/separator", ',').toInt()));
setQuoteChar(QChar(settings.value("importcsv/quotecharacter", '"').toInt()));
setEncoding(settings.value("importcsv/encoding", "UTF-8").toString());
ui->checkboxHeader->blockSignals(false);
ui->checkBoxTrimFields->blockSignals(false);
ui->checkBoxSeparateTables->blockSignals(false);
ui->comboSeparator->blockSignals(false);
ui->comboQuote->blockSignals(false);
ui->comboEncoding->blockSignals(false);
// Prepare and show interface depending on how many files are selected
if (csvFilenames.length() > 1)
{
ui->separateTables->setVisible(true);
ui->checkBoxSeparateTables->setVisible(true);
ui->filePickerBlock->setVisible(true);
selectFiles();
}
else if (csvFilenames.length() == 1)
{
ui->separateTables->setVisible(false);
ui->checkBoxSeparateTables->setVisible(false);
ui->filePickerBlock->setVisible(false);
}
selectedFile = csvFilenames.first();
updatePreview();
checkInput();
}
ImportCsvDialog::~ImportCsvDialog()
{
delete ui;
}
namespace {
void rollback(
ImportCsvDialog* dialog,
DBBrowserDB* pdb,
const QString& savepointName,
size_t nRecord,
const QString& message)
{
QApplication::restoreOverrideCursor(); // restore original cursor
if(!message.isEmpty())
{
QString sCSVInfo = QObject::tr("Error importing data");
if(nRecord)
sCSVInfo += QObject::tr(" from record number %1").arg(nRecord);
QString error = sCSVInfo + QObject::tr(".\n%1").arg(message);
QMessageBox::warning(dialog, QApplication::applicationName(), error);
}
pdb->revertToSavepoint(savepointName);
}
}
class CSVImportProgress : public CSVProgress
{
public:
explicit CSVImportProgress(qint64 filesize)
{
m_pProgressDlg = new QProgressDialog(
QObject::tr("Importing CSV file..."),
QObject::tr("Cancel"),
0,
filesize);
m_pProgressDlg->setWindowModality(Qt::ApplicationModal);
}
~CSVImportProgress()
{
delete m_pProgressDlg;
}
void start()
{
m_pProgressDlg->show();
}
bool update(unsigned long long pos)
{
m_pProgressDlg->setValue(pos);
qApp->processEvents();
return !m_pProgressDlg->wasCanceled();
}
void end()
{
m_pProgressDlg->hide();
}
private:
QProgressDialog* m_pProgressDlg;
};
void ImportCsvDialog::accept()
{
// save settings
QSettings settings(QApplication::organizationName(), QApplication::organizationName());
settings.beginGroup("importcsv");
settings.setValue("firstrowheader", ui->checkboxHeader->isChecked());
settings.setValue("separator", currentSeparatorChar());
settings.setValue("quotecharacter", currentQuoteChar());
settings.setValue("trimfields", ui->checkBoxTrimFields->isChecked());
settings.setValue("separatetables", ui->checkBoxSeparateTables->isChecked());
settings.setValue("encoding", currentEncoding());
settings.endGroup();
// Get all the selected files and start the import
if (ui->filePickerBlock->isVisible())
{
bool filesLeft = false;
// Loop through all the rows in the file picker list
for (int i = 0; i < ui->filePicker->count(); i++) {
auto item = ui->filePicker->item(i);
int row = ui->filePicker->row(item);
// Check for files that aren't hidden (=imported) yet but that are checked and thus marked for import
if (item->checkState() == Qt::Checked && !ui->filePicker->isRowHidden(row)) {
importCsv(item->data(Qt::DisplayRole).toString(), item->data(Qt::UserRole).toString());
// Hide each row after it's done
ui->filePicker->setRowHidden(row, true);
} else if(!ui->filePicker->isRowHidden(row)) {
// Check for files that aren't hidden yet but that aren't checked either. These are files that are still left
// to be imported
filesLeft = true;
}
}
// Don't close the window if there are still files left to be imported
if(filesLeft)
{
QApplication::restoreOverrideCursor(); // restore original cursor
return;
}
} else {
importCsv(csvFilenames.first());
}
QMessageBox::information(this, QApplication::applicationName(), tr("Import completed"));
QApplication::restoreOverrideCursor(); // restore original cursor
QDialog::accept();
}
void ImportCsvDialog::updatePreview()
{
// Show/hide custom quote/separator input fields
ui->editCustomQuote->setVisible(ui->comboQuote->currentIndex() == ui->comboQuote->count()-1);
ui->editCustomSeparator->setVisible(ui->comboSeparator->currentIndex() == ui->comboSeparator->count()-1);
ui->editCustomEncoding->setVisible(ui->comboEncoding->currentIndex() == ui->comboEncoding->count()-1);
// Reset preview widget
ui->tablePreview->clear();
ui->tablePreview->setColumnCount(0);
ui->tablePreview->setRowCount(0);
// Analyse CSV file
sqlb::FieldVector fieldList = generateFieldList(selectedFile);
// Reset preview widget
ui->tablePreview->clear();
ui->tablePreview->setColumnCount(fieldList.size());
// Exit if there are no lines to preview at all
if(fieldList.size() == 0)
return;
// Set horizontal header data
QStringList horizontalHeader;
foreach(const sqlb::FieldPtr& field, fieldList)
horizontalHeader.push_back(field->name());
ui->tablePreview->setHorizontalHeaderLabels(horizontalHeader);
// Parse file
parseCSV(selectedFile, [this](size_t rowNum, const CSVRow& data) -> bool {
// Skip first row if it is to be used as header
if(rowNum == 0 && ui->checkboxHeader->isChecked())
return true;
// Decrease the row number by one if the header checkbox is checked to take into account that the first row was used for the table header labels
// and therefore all data rows move one row up.
if(ui->checkboxHeader->isChecked())
rowNum--;
// Fill data section
ui->tablePreview->setRowCount(ui->tablePreview->rowCount() + 1);
for(size_t i=0;i<data.num_fields;i++)
{
// Generate vertical header items
if(i == 0)
ui->tablePreview->setVerticalHeaderItem(rowNum, new QTableWidgetItem(QString::number(rowNum + 1)));
// Add table item
ui->tablePreview->setItem(
rowNum,
i,
new QTableWidgetItem(QString::fromUtf8(data.fields[i].data, data.fields[i].data_length)));
}
return true;
}, 20);
}
void ImportCsvDialog::checkInput()
{
bool allowImporting = false;
if (ui->filePickerBlock->isVisible()) {
bool checkedItem = false;
for (int i = 0; i < ui->filePicker->count(); i++) {
if (ui->filePicker->item(i)->checkState() == Qt::Checked) checkedItem = true;
}
allowImporting = !ui->editName->text().isEmpty() && checkedItem;
} else {
allowImporting = !ui->editName->text().isEmpty();
}
if (ui->filePicker->currentItem()) {
ui->filePicker->currentItem()->setData(Qt::UserRole, ui->editName->text());
}
ui->matchSimilar->setEnabled(ui->filePicker->currentItem() != nullptr);
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(allowImporting);
}
void ImportCsvDialog::selectFiles()
{
for (auto fileName : csvFilenames) {
auto fInfo = QFileInfo(fileName);
auto item = new QListWidgetItem();
item->setText(fileName);
item->setData(Qt::UserRole, fInfo.baseName());
item->setCheckState(Qt::Checked);
ui->filePicker->addItem(item);
}
connect(ui->filePicker, SIGNAL(itemClicked(QListWidgetItem*)), SLOT(updateSelectedFilePreview(QListWidgetItem*)));
}
void ImportCsvDialog::updateSelectedFilePreview(QListWidgetItem* item)
{
selectedFile = item->data(Qt::DisplayRole).toString();
QFileInfo fileInfo(selectedFile);
if (ui->checkBoxSeparateTables->isChecked()) {
ui->editName->setText(item->data(Qt::UserRole).toString());
} else {
ui->editName->setText(fileInfo.baseName());
}
updatePreview();
checkInput();
}
void ImportCsvDialog::updateSelection(bool selected)
{
for (int i = 0; i < ui->filePicker->count(); i++)
ui->filePicker->item(i)->setCheckState(selected ? Qt::Checked : Qt::Unchecked);
ui->toggleSelected->setText(selected ? tr("Deselect All") : tr("Select All"));
checkInput();
}
void ImportCsvDialog::matchSimilar()
{
auto item = ui->filePicker->currentItem();
auto selectedHeader = generateFieldList(item->data(Qt::DisplayRole).toString());
for (int i = 0; i < ui->filePicker->count(); i++)
{
auto item = ui->filePicker->item(i);
auto header = generateFieldList(item->data(Qt::DisplayRole).toString());
bool matchingHeader = false;
if (selectedHeader.count() == header.count())
{
matchingHeader = std::equal(selectedHeader.begin(), selectedHeader.end(), header.begin(),
[](const sqlb::FieldPtr& item1, const sqlb::FieldPtr& item2) -> bool {
return (item1->name() == item2->name());
});
if (matchingHeader) {
item->setCheckState(Qt::Checked);
item->setBackgroundColor(Qt::green);
}
}
else
{
item->setCheckState(Qt::Unchecked);
item->setBackground(Qt::white);
}
}
checkInput();
}
CSVParser::ParserResult ImportCsvDialog::parseCSV(const QString &fileName, std::function<bool(size_t, CSVRow)> rowFunction, size_t count)
{
// Parse all csv data
QFile file(fileName);
file.open(QIODevice::ReadOnly);
CSVParser csv(ui->checkBoxTrimFields->isChecked(), currentSeparatorChar(), currentQuoteChar());
// Only show progress dialog if we parse all rows. The assumption here is that if a row count limit has been set, it won't be a very high one.
if(count == 0)
csv.setCSVProgress(new CSVImportProgress(file.size()));
QTextStream tstream(&file);
tstream.setCodec(currentEncoding().toUtf8());
return csv.parse(rowFunction, tstream, count);
}
sqlb::FieldVector ImportCsvDialog::generateFieldList(const QString& filename)
{
sqlb::FieldVector fieldList; // List of fields in the file
// Parse the first couple of records of the CSV file and only analyse them
parseCSV(filename, [this, &fieldList](size_t rowNum, const CSVRow& data) -> bool {
// Has this row more columns than the previous one? Then add more fields to the field list as necessary.
for(size_t i=fieldList.size();i<data.num_fields;i++)
{
QString fieldname;
// If the user wants to use the first row as table header and if this is the first row, extract a field name
if(rowNum == 0 && ui->checkboxHeader->isChecked())
{
// Take field name from CSV and remove invalid characters
fieldname = QString::fromUtf8(data.fields[i].data, data.fields[i].data_length);
fieldname.replace("`", "");
fieldname.replace(" ", "");
fieldname.replace('"', "");
fieldname.replace("'","");
fieldname.replace(",","");
fieldname.replace(";","");
}
// If we don't have a field name by now, generate one
if(fieldname.isEmpty())
fieldname = QString("field%1").arg(i+1);
// Add field to the column list. For now we set the data type to nothing but this might be overwritten later in the automatic
// type detection code.
fieldList.push_back(sqlb::FieldPtr(new sqlb::Field(fieldname, "")));
}
// Try to find out a data type for each column
if(!(rowNum == 0 && ui->checkboxHeader->isChecked()))
{
for(size_t i=0;i<data.num_fields;i++)
{
// If the data type has been set to TEXT, there's no going back because it means we had at least one row with text-only
// content and that means we don't want to set the data type to any number type.
QString old_type = fieldList.at(i)->type();
if(old_type != "TEXT")
{
QString content = QString::fromUtf8(data.fields[i].data, data.fields[i].data_length);
// Check if the content can be converted to an integer or to float
bool convert_to_int, convert_to_float;
content.toInt(&convert_to_int);
content.toFloat(&convert_to_float);
// Set new data type. If we don't find any better data type, we fall back to the TEXT data type
QString new_type = "TEXT";
if(old_type == "INTEGER" && !convert_to_int && convert_to_float) // So far it's integer, but now it's only convertible to float
new_type = "REAL";
else if(old_type == "" && convert_to_int) // No type yet, but this bit is convertible to integer
new_type = "INTEGER";
else if(old_type == "" && convert_to_float) // No type yet and only convertible to float (less 'difficult' than integer)
new_type = "REAL";
else if(old_type == "REAL" && convert_to_float) // It was float so far and still is
new_type = "INTEGER";
else if(old_type == "INTEGER" && convert_to_int) // It was integer so far and still is
new_type = "INTEGER";
fieldList.at(i)->setType(new_type);
}
}
}
// All good
return true;
}, 20);
return fieldList;
}
void ImportCsvDialog::importCsv(const QString& fileName, const QString &name)
{
#ifdef CSV_BENCHMARK
// If benchmark mode is enabled start measuring the performance now
qint64 timesRowFunction = 0;
QElapsedTimer timer;
timer.start();
#endif
QString tableName;
if (csvFilenames.size() > 1 && ui->checkBoxSeparateTables->isChecked()) {
if (name.isEmpty()) {
QFileInfo fileInfo(fileName);
tableName = fileInfo.baseName();
} else {
tableName = name;
}
} else {
tableName = ui->editName->text();
}
// Analyse CSV file
sqlb::FieldVector fieldList = generateFieldList(fileName);
if(fieldList.size() == 0)
return;
// Are we importing into an existing table?
bool importToExistingTable = false;
const sqlb::ObjectPtr obj = pdb->getObjectByName(sqlb::ObjectIdentifier("main", tableName));
if(obj && obj->type() == sqlb::Object::Types::Table)
{
if(obj.dynamicCast<sqlb::Table>()->fields().size() != fieldList.size())
{
QMessageBox::warning(this, QApplication::applicationName(),
tr("There is already a table of that name and an import into an existing table is only possible if the number of columns match."));
return;
} else {
// If we are importing multiple files, we can skip the warning and perform the inserts
// To omit further warnings there is a filter button to select all conforming sql
if (!ui->checkBoxSeparateTables->isChecked())
importToExistingTable = true;
if(QMessageBox::question(this, QApplication::applicationName(), tr("There is already a table of that name. Do you want to import the data into it?"), QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes)
importToExistingTable = true;
else
return;
}
}
// Create a savepoint, so we can rollback in case of any errors during importing
// db needs to be saved or an error will occur
QString restorepointName = pdb->generateSavepointName("csvimport");
if(!pdb->setSavepoint(restorepointName))
return rollback(this, pdb, restorepointName, 0, tr("Creating restore point failed: %1").arg(pdb->lastError()));
// Create table
QVector<QByteArray> nullValues;
if(!importToExistingTable)
{
if(!pdb->createTable(sqlb::ObjectIdentifier("main", tableName), fieldList))
return rollback(this, pdb, restorepointName, 0, tr("Creating the table failed: %1").arg(pdb->lastError()));
} else {
// Importing into an existing table. So find out something about it's structure.
// Prepare the values for each table column that are to be inserted if the field in the CSV file is empty. Depending on the data type
// and the constraints of a field, we need to handle this case differently.
sqlb::TablePtr tbl = pdb->getObjectByName(sqlb::ObjectIdentifier("main", tableName)).dynamicCast<sqlb::Table>();
if(tbl)
{
foreach(const sqlb::FieldPtr& f, tbl->fields())
{
if(f->isInteger() && f->notnull()) // If this is an integer column but NULL isn't allowed, insert 0
nullValues << "0";
else if(f->isInteger() && !f->notnull()) // If this is an integer column and NULL is allowed, insert NULL
nullValues << QByteArray();
else // Otherwise (i.e. if this isn't an integer column), insert an empty string
nullValues << "";
}
}
}
// Prepare the INSERT statement. The prepared statement can then be reused for each row to insert
QString sQuery = QString("INSERT INTO %1 VALUES(").arg(sqlb::escapeIdentifier(tableName));
for(int i=1;i<=fieldList.size();i++)
sQuery.append(QString("?%1,").arg(i));
sQuery.chop(1); // Remove last comma
sQuery.append(")");
sqlite3_stmt* stmt;
sqlite3_prepare_v2(pdb->_db, sQuery.toUtf8(), sQuery.toUtf8().length(), &stmt, nullptr);
// Parse entire file
size_t lastRowNum = 0;
CSVParser::ParserResult result = parseCSV(fileName, [&](size_t rowNum, const CSVRow& data) -> bool {
// Process the parser results row by row
#ifdef CSV_BENCHMARK
qint64 timeAtStartOfRowFunction = timer.elapsed();
#endif
// Save row num for later use. This is used in the case of an error to tell the user in which row the error ocurred
lastRowNum = rowNum;
// If this is the first row and we want to use the first row as table header, skip it now because this is the data import, not the header parsing
if(rowNum == 0 && ui->checkboxHeader->isChecked())
return true;
// Bind all values
for(size_t i=0;i<data.num_fields;i++)
{
// Empty values need special treatment, but only when importing into an existing table where we could find out something about
// its table definition
if(importToExistingTable && data.fields[i].data_length == 0 && static_cast<size_t>(nullValues.size()) > i)
{
// This is an empty value. We'll need to look up how to handle it depending on the field to be inserted into.
const QByteArray& val = nullValues.at(i);
if(!val.isNull()) // No need to bind NULL values here as that is the default bound value in SQLite
sqlite3_bind_text(stmt, i+1, val, val.size(), SQLITE_STATIC);
} else {
// This is a non-empty value. Just add it to the statement
sqlite3_bind_text(stmt, i+1, data.fields[i].data, data.fields[i].data_length, SQLITE_STATIC);
}
}
// Insert row
if(sqlite3_step(stmt) != SQLITE_DONE)
return false;
// Reset statement for next use. Also reset all bindings to NULL. This is important, so we don't need to bind missing columns or empty values in NULL
// columns manually.
sqlite3_reset(stmt);
sqlite3_clear_bindings(stmt);
#ifdef CSV_BENCHMARK
timesRowFunction += timer.elapsed() - timeAtStartOfRowFunction;
#endif
return true;
});
// Success?
if(result != CSVParser::ParserResult::ParserResultSuccess)
{
// Some error occurred or the user cancelled the action
// Rollback the entire import. If the action was cancelled, don't show an error message. If it errored, show an error message.
sqlite3_finalize(stmt);
if(result == CSVParser::ParserResult::ParserResultCancelled)
return rollback(this, pdb, restorepointName, 0, QString());
else
return rollback(this, pdb, restorepointName, lastRowNum, tr("Inserting row failed: %1").arg(pdb->lastError()));
}
// Clean up prepared statement
sqlite3_finalize(stmt);
#ifdef CSV_BENCHMARK
QMessageBox::information(this, qApp->applicationName(),
tr("Importing the file '%1' took %2ms. Of this %3ms were spent in the row function.")
.arg(fileName)
.arg(timer.elapsed())
.arg(timesRowFunction));
#endif
}
void ImportCsvDialog::setQuoteChar(const QChar& c)
{
QComboBox* combo = ui->comboQuote;
int index = combo->findText(c);
if(index == -1)
{
combo->setCurrentIndex(combo->count());
ui->editCustomQuote->setText(c);
}
else
{
combo->setCurrentIndex(index);
}
}
char ImportCsvDialog::currentQuoteChar() const
{
// The last item in the combobox is the 'Other' item; if it is selected return the text of the line edit field instead
if(ui->comboQuote->currentIndex() == ui->comboQuote->count()-1)
return ui->editCustomQuote->text().length() ? ui->editCustomQuote->text().at(0).toLatin1() : 0;
if(ui->comboQuote->currentText().length())
return ui->comboQuote->currentText().at(0).toLatin1();
else
return 0;
}
void ImportCsvDialog::setSeparatorChar(const QChar& c)
{
QComboBox* combo = ui->comboSeparator;
QString sText = c == '\t' ? QString("Tab") : QString(c);
int index = combo->findText(sText);
if(index == -1)
{
combo->setCurrentIndex(combo->count());
ui->editCustomSeparator->setText(c);
}
else
{
combo->setCurrentIndex(index);
}
}
char ImportCsvDialog::currentSeparatorChar() const
{
// The last item in the combobox is the 'Other' item; if it is selected return the text of the line edit field instead
if(ui->comboSeparator->currentIndex() == ui->comboSeparator->count()-1)
return ui->editCustomSeparator->text().length() ? ui->editCustomSeparator->text().at(0).toLatin1() : 0;
return ui->comboSeparator->currentText() == tr("Tab") ? '\t' : ui->comboSeparator->currentText().at(0).toLatin1();
}
void ImportCsvDialog::setEncoding(const QString& sEnc)
{
QComboBox* combo = ui->comboEncoding;
int index = combo->findText(sEnc);
if(index == -1)
{
combo->setCurrentIndex(combo->count());
ui->editCustomEncoding->setText(sEnc);
}
else
{
combo->setCurrentIndex(index);
}
}
QString ImportCsvDialog::currentEncoding() const
{
// The last item in the combobox is the 'Other' item; if it is selected return the text of the line edit field instead
if(ui->comboEncoding->currentIndex() == ui->comboEncoding->count()-1)
return ui->editCustomEncoding->text().length() ? ui->editCustomEncoding->text() : "UTF-8";
else
return ui->comboEncoding->currentText();
}