diff options
author | Dirk Hohndel <dirk@hohndel.org> | 2015-01-07 11:24:57 -0800 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2015-01-07 11:24:57 -0800 |
commit | 816367eb06820f0a9db138d2323a05d77a446808 (patch) | |
tree | 620da7999890bd92b79336609aca6555eeb267cd | |
parent | 481de7da74e095cb4843c7a993b21f39cbe12dc3 (diff) | |
download | subsurface-816367eb06820f0a9db138d2323a05d77a446808.tar.gz |
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 <dirk@hohndel.org>
-rw-r--r-- | qt-ui/divelogimportdialog.cpp | 25 |
1 files 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<QStringList> 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<QStringList> 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() |