From 481de7da74e095cb4843c7a993b21f39cbe12dc3 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Wed, 7 Jan 2015 08:56:01 -0800 Subject: CSV import dialog: try to guess the separator based on the file This is taking a very simplistic approach. It picks the predominant potential separator. If there is no clear winner, it uses the UI default and makes the user pick (and either way, this can always be overwritten by the user). Signed-off-by: Dirk Hohndel --- qt-ui/divelogimportdialog.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'qt-ui') diff --git a/qt-ui/divelogimportdialog.cpp b/qt-ui/divelogimportdialog.cpp index ebd46f2d3..3c5d595e8 100644 --- a/qt-ui/divelogimportdialog.cpp +++ b/qt-ui/divelogimportdialog.cpp @@ -341,11 +341,26 @@ void DiveLogImportDialog::loadFileContents() { QStringList currColumns; f.open(QFile::ReadOnly); + // guess the separator + QString firstLine = f.readLine(); + QString separator; + int tabs = firstLine.count('\t'); + int commas = firstLine.count(','); + int semis = firstLine.count(';'); + if (tabs > commas && tabs > semis) + separator = "\t"; + else if (commas > tabs && commas > semis) + separator = ","; + else if (semis > tabs && semis > commas) + separator = ";"; + else + separator = ui->CSVSeparator->currentText() == tr("Tab") ? "\t" : ui->CSVSeparator->currentText(); + if (ui->CSVSeparator->currentText() != separator) + ui->CSVSeparator->setCurrentText(separator); + f.reset(); int rows = 0; while (rows < 10 || !f.atEnd()) { QString currLine = f.readLine(); - QString separator = ui->CSVSeparator->currentText() == tr("Tab") ? "\t" - : ui->CSVSeparator->currentText(); currColumns = currLine.split(separator); fileColumns.append(currColumns); rows += 1; -- cgit v1.2.3-70-g09d2