From 816367eb06820f0a9db138d2323a05d77a446808 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Wed, 7 Jan 2015 11:24:57 -0800 Subject: CSV import UI: First step towards recognizing column headers This doesn't do a good job of matching the titles, but it works for some of them. Signed-off-by: Dirk Hohndel --- qt-ui/divelogimportdialog.cpp | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/qt-ui/divelogimportdialog.cpp b/qt-ui/divelogimportdialog.cpp index 3c5d595e8..0eebb1a1e 100644 --- a/qt-ui/divelogimportdialog.cpp +++ b/qt-ui/divelogimportdialog.cpp @@ -257,9 +257,9 @@ void ColumnNameResult::setColumnValues(QList columns) QStringList first = columns.first(); beginInsertColumns(QModelIndex(), 0, first.count()-1); - for(int i = 0; i < first.count(); i++){ + for(int i = 0; i < first.count(); i++) columnNames.append(QString()); - } + endInsertColumns(); beginInsertRows(QModelIndex(), 0, columns.count()-1); @@ -339,6 +339,7 @@ void DiveLogImportDialog::loadFileContents() { QFile f(fileNames.first()); QList fileColumns; QStringList currColumns; + QStringList headers; f.open(QFile::ReadOnly); // guess the separator @@ -357,6 +358,23 @@ void DiveLogImportDialog::loadFileContents() { separator = ui->CSVSeparator->currentText() == tr("Tab") ? "\t" : ui->CSVSeparator->currentText(); if (ui->CSVSeparator->currentText() != separator) ui->CSVSeparator->setCurrentText(separator); + + // now try and guess the columns + currColumns = firstLine.split(separator); + Q_FOREACH (QString columnText, currColumns) { + ColumnNameProvider *model = (ColumnNameProvider *)ui->avaliableColumns->model(); + columnText.replace("\"", ""); + columnText.replace("number", "#", Qt::CaseInsensitive); + QModelIndexList matches = model->match(model->index(0, 0), Qt::DisplayRole, columnText, Qt::MatchFixedString); + if (!matches.isEmpty()) { + QString foundHeading = model->data(matches.first(), Qt::DisplayRole).toString(); + model->removeRow(matches.first().row()); + headers.append(foundHeading); + } else { + headers.append(""); + } + + } f.reset(); int rows = 0; while (rows < 10 || !f.atEnd()) { @@ -366,6 +384,9 @@ void DiveLogImportDialog::loadFileContents() { rows += 1; } resultModel->setColumnValues(fileColumns); + for (int i = 0; i < headers.count(); i++) + if (!headers.at(i).isEmpty()) + resultModel->setData(resultModel->index(0, i),headers.at(i),Qt::EditRole); } void DiveLogImportDialog::on_buttonBox_accepted() -- cgit v1.2.3-70-g09d2