summaryrefslogtreecommitdiffstats
path: root/qt-ui/divelogimportdialog.cpp
diff options
context:
space:
mode:
authorGravatar Dirk Hohndel <dirk@hohndel.org>2015-01-07 08:56:01 -0800
committerGravatar Dirk Hohndel <dirk@hohndel.org>2015-01-07 08:57:08 -0800
commit481de7da74e095cb4843c7a993b21f39cbe12dc3 (patch)
treeacf621d69c8e2966db83d8c297eb3b29c4e5e8e9 /qt-ui/divelogimportdialog.cpp
parent925bb019c716ce5244765e54f1de584ece340b23 (diff)
downloadsubsurface-481de7da74e095cb4843c7a993b21f39cbe12dc3.tar.gz
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 <dirk@hohndel.org>
Diffstat (limited to 'qt-ui/divelogimportdialog.cpp')
-rw-r--r--qt-ui/divelogimportdialog.cpp19
1 files changed, 17 insertions, 2 deletions
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;