mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
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>
This commit is contained in:
parent
925bb019c7
commit
481de7da74
1 changed files with 17 additions and 2 deletions
|
@ -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;
|
||||
|
|
Loading…
Add table
Reference in a new issue