mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
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>
This commit is contained in:
parent
481de7da74
commit
816367eb06
1 changed files with 23 additions and 2 deletions
|
@ -257,9 +257,9 @@ void ColumnNameResult::setColumnValues(QList<QStringList> columns)
|
||||||
|
|
||||||
QStringList first = columns.first();
|
QStringList first = columns.first();
|
||||||
beginInsertColumns(QModelIndex(), 0, first.count()-1);
|
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());
|
columnNames.append(QString());
|
||||||
}
|
|
||||||
endInsertColumns();
|
endInsertColumns();
|
||||||
|
|
||||||
beginInsertRows(QModelIndex(), 0, columns.count()-1);
|
beginInsertRows(QModelIndex(), 0, columns.count()-1);
|
||||||
|
@ -339,6 +339,7 @@ void DiveLogImportDialog::loadFileContents() {
|
||||||
QFile f(fileNames.first());
|
QFile f(fileNames.first());
|
||||||
QList<QStringList> fileColumns;
|
QList<QStringList> fileColumns;
|
||||||
QStringList currColumns;
|
QStringList currColumns;
|
||||||
|
QStringList headers;
|
||||||
|
|
||||||
f.open(QFile::ReadOnly);
|
f.open(QFile::ReadOnly);
|
||||||
// guess the separator
|
// guess the separator
|
||||||
|
@ -357,6 +358,23 @@ void DiveLogImportDialog::loadFileContents() {
|
||||||
separator = ui->CSVSeparator->currentText() == tr("Tab") ? "\t" : ui->CSVSeparator->currentText();
|
separator = ui->CSVSeparator->currentText() == tr("Tab") ? "\t" : ui->CSVSeparator->currentText();
|
||||||
if (ui->CSVSeparator->currentText() != separator)
|
if (ui->CSVSeparator->currentText() != separator)
|
||||||
ui->CSVSeparator->setCurrentText(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();
|
f.reset();
|
||||||
int rows = 0;
|
int rows = 0;
|
||||||
while (rows < 10 || !f.atEnd()) {
|
while (rows < 10 || !f.atEnd()) {
|
||||||
|
@ -366,6 +384,9 @@ void DiveLogImportDialog::loadFileContents() {
|
||||||
rows += 1;
|
rows += 1;
|
||||||
}
|
}
|
||||||
resultModel->setColumnValues(fileColumns);
|
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()
|
void DiveLogImportDialog::on_buttonBox_accepted()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue