Skip to content

Commit 9bef8a8

Browse files
committed
importcsv: Save last used settings and add a trim fields option
1 parent 04e08cb commit 9bef8a8

3 files changed

Lines changed: 95 additions & 9 deletions

File tree

src/ImportCsvDialog.cpp

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
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

1719
ImportCsvDialog::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

92101
void 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+
282315
char 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+
294343
char 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+
303367
QString 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

src/ImportCsvDialog.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,13 @@ private slots:
2929
DBBrowserDB* pdb;
3030
QCompleter* encodingCompleter;
3131

32+
void setQuoteChar(const QChar& c);
3233
char currentQuoteChar() const;
34+
35+
void setSeparatorChar(const QChar& c);
3336
char currentSeparatorChar() const;
37+
38+
void setEncoding(const QString& sEnc);
3439
QString currentEncoding() const;
3540
};
3641

src/ImportCsvDialog.ui

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
</property>
5050
</widget>
5151
</item>
52-
<item row="2" column="0">
52+
<item row="3" column="0">
5353
<widget class="QLabel" name="labelSeparator">
5454
<property name="text">
5555
<string>Field &amp;separator</string>
@@ -59,7 +59,7 @@
5959
</property>
6060
</widget>
6161
</item>
62-
<item row="2" column="1">
62+
<item row="3" column="1">
6363
<layout class="QHBoxLayout" name="horizontalLayout">
6464
<item>
6565
<widget class="QComboBox" name="comboSeparator">
@@ -112,7 +112,7 @@
112112
</item>
113113
</layout>
114114
</item>
115-
<item row="3" column="0">
115+
<item row="4" column="0">
116116
<widget class="QLabel" name="labelQuote">
117117
<property name="text">
118118
<string>&amp;Quote character</string>
@@ -122,7 +122,7 @@
122122
</property>
123123
</widget>
124124
</item>
125-
<item row="3" column="1">
125+
<item row="4" column="1">
126126
<layout class="QHBoxLayout" name="horizontalLayout_2">
127127
<item>
128128
<widget class="QComboBox" name="comboQuote">
@@ -170,7 +170,7 @@
170170
</item>
171171
</layout>
172172
</item>
173-
<item row="4" column="0">
173+
<item row="5" column="0">
174174
<widget class="QLabel" name="labelEncoding">
175175
<property name="text">
176176
<string>&amp;Encoding</string>
@@ -180,7 +180,7 @@
180180
</property>
181181
</widget>
182182
</item>
183-
<item row="4" column="1">
183+
<item row="5" column="1">
184184
<layout class="QHBoxLayout" name="horizontalLayout_3">
185185
<item>
186186
<widget class="QComboBox" name="comboEncoding">
@@ -224,6 +224,23 @@
224224
</item>
225225
</layout>
226226
</item>
227+
<item row="6" column="0">
228+
<widget class="QLabel" name="labelTrim">
229+
<property name="text">
230+
<string>Trim fields?</string>
231+
</property>
232+
</widget>
233+
</item>
234+
<item row="6" column="1">
235+
<widget class="QCheckBox" name="checkBoxTrimFields">
236+
<property name="text">
237+
<string/>
238+
</property>
239+
<property name="checked">
240+
<bool>true</bool>
241+
</property>
242+
</widget>
243+
</item>
227244
</layout>
228245
</item>
229246
<item>

0 commit comments

Comments
 (0)