99#include < QDateTime>
1010#include < QTextCodec>
1111#include < QCompleter>
12+ #include < QComboBox>
1213#include < sqlite3.h>
1314#include < QFile>
1415#include < QTextStream>
16+ #include < QSettings>
1517#include < memory>
1618
1719ImportCsvDialog::ImportCsvDialog (const QString& filename, DBBrowserDB* db, QWidget* parent)
@@ -29,6 +31,13 @@ ImportCsvDialog::ImportCsvDialog(const QString& filename, DBBrowserDB* db, QWidg
2931 encodingCompleter->setCaseSensitivity (Qt::CaseInsensitive);
3032 ui->editCustomEncoding ->setCompleter (encodingCompleter);
3133
34+ QSettings settings (QApplication::organizationName (), QApplication::organizationName ());
35+ ui->checkboxHeader ->setChecked (settings.value (" importcsv/firstrowheader" , false ).toBool ());
36+ ui->checkBoxTrimFields ->setChecked (settings.value (" importcsv/trimfields" , true ).toBool ());
37+ setSeparatorChar (QChar (settings.value (" importcsv/separator" , 44 ).toInt ()));
38+ setQuoteChar (QChar (settings.value (" importcsv/quotecharacter" , 34 ).toInt ()));
39+ setEncoding (settings.value (" importcsv/encoding" , " UTF-8" ).toString ());
40+
3241 checkInput ();
3342 updatePreview ();
3443}
@@ -91,13 +100,21 @@ class CSVImportProgress : public CSVProgress
91100
92101void ImportCsvDialog::accept ()
93102{
94- QString sql;
103+ // save settings
104+ QSettings settings (QApplication::organizationName (), QApplication::organizationName ());
105+ settings.beginGroup (" importcsv" );
106+ settings.setValue (" firstrowheader" , ui->checkboxHeader ->isChecked ());
107+ settings.setValue (" separator" , currentSeparatorChar ());
108+ settings.setValue (" quotecharacter" , currentQuoteChar ());
109+ settings.setValue (" trimfields" , ui->checkBoxTrimFields ->isChecked ());
110+ settings.setValue (" encoding" , currentEncoding ());
111+ settings.endGroup ();
95112
96113 // Parse all csv data
97114 QFile file (csvFilename);
98115 file.open (QIODevice::ReadOnly | QIODevice::Text);
99116
100- CSVParser csv (true , currentSeparatorChar (), currentQuoteChar ());
117+ CSVParser csv (ui-> checkBoxTrimFields -> isChecked () , currentSeparatorChar (), currentQuoteChar ());
101118 csv.setCSVProgress (new CSVImportProgress (file.size ()));
102119
103120 QTextStream tstream (&file);
@@ -184,6 +201,7 @@ void ImportCsvDialog::accept()
184201 it != csv.csv ().end ();
185202 ++it)
186203 {
204+ QString sql;
187205 sql = QString (" INSERT INTO `%1` VALUES(" ).arg (ui->editName ->text ());
188206
189207 for (QStringList::const_iterator jt = it->begin (); jt != it->end (); ++jt)
@@ -223,7 +241,7 @@ void ImportCsvDialog::updatePreview()
223241 QFile file (csvFilename);
224242 file.open (QIODevice::ReadOnly | QIODevice::Text);
225243
226- CSVParser csv (true , currentSeparatorChar (), currentQuoteChar ());
244+ CSVParser csv (ui-> checkBoxTrimFields -> isChecked () , currentSeparatorChar (), currentQuoteChar ());
227245
228246 QTextStream tstream (&file);
229247 tstream.setCodec (currentEncoding ().toUtf8 ());
@@ -279,6 +297,21 @@ void ImportCsvDialog::checkInput()
279297 ui->buttonBox ->button (QDialogButtonBox::Ok)->setEnabled (valid);
280298}
281299
300+ void ImportCsvDialog::setQuoteChar (const QChar& c)
301+ {
302+ QComboBox* combo = ui->comboQuote ;
303+ int index = combo->findText (c);
304+ if (index == -1 )
305+ {
306+ combo->setCurrentIndex (combo->count ());
307+ ui->editCustomQuote ->setText (c);
308+ }
309+ else
310+ {
311+ combo->setCurrentIndex (index);
312+ }
313+ }
314+
282315char ImportCsvDialog::currentQuoteChar () const
283316{
284317 // The last item in the combobox is the 'Other' item; if it is selected return the text of the line edit field instead
@@ -291,6 +324,22 @@ char ImportCsvDialog::currentQuoteChar() const
291324 return 0 ;
292325}
293326
327+ void ImportCsvDialog::setSeparatorChar (const QChar& c)
328+ {
329+ QComboBox* combo = ui->comboSeparator ;
330+ QString sText = c == ' \t ' ? QString (" Tab" ) : QString (c);
331+ int index = combo->findText (sText );
332+ if (index == -1 )
333+ {
334+ combo->setCurrentIndex (combo->count ());
335+ ui->editCustomSeparator ->setText (c);
336+ }
337+ else
338+ {
339+ combo->setCurrentIndex (index);
340+ }
341+ }
342+
294343char ImportCsvDialog::currentSeparatorChar () const
295344{
296345 // The last item in the combobox is the 'Other' item; if it is selected return the text of the line edit field instead
@@ -300,6 +349,21 @@ char ImportCsvDialog::currentSeparatorChar() const
300349 return ui->comboSeparator ->currentText () == tr (" Tab" ) ? ' \t ' : ui->comboSeparator ->currentText ().at (0 ).toLatin1 ();
301350}
302351
352+ void ImportCsvDialog::setEncoding (const QString& sEnc )
353+ {
354+ QComboBox* combo = ui->comboEncoding ;
355+ int index = combo->findText (sEnc );
356+ if (index == -1 )
357+ {
358+ combo->setCurrentIndex (combo->count ());
359+ ui->editCustomEncoding ->setText (sEnc );
360+ }
361+ else
362+ {
363+ combo->setCurrentIndex (index);
364+ }
365+ }
366+
303367QString ImportCsvDialog::currentEncoding () const
304368{
305369 // The last item in the combobox is the 'Other' item; if it is selected return the text of the line edit field instead
0 commit comments