mirror of
https://github.com/subsurface/subsurface.git
synced 2025-01-20 06:45:27 +00:00
CSV import: make predefined imports work again
We need to reparse the file when the known type changes and want to make sure that we only try to guess the separator and the columns if the user hasn't told us otherwise. For the predefined imports this then looks up the correct columns and places the correct headings there - and then allows the user to modify them if needed. This has been lightly tested, there may be dragons. Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
parent
db988aa62b
commit
5f4770a5bd
2 changed files with 103 additions and 44 deletions
|
@ -330,14 +330,8 @@ DiveLogImportDialog::DiveLogImportDialog(QStringList fn, QWidget *parent) : QDia
|
||||||
ui->knownImports->addItem(CSVApps[i].name);
|
ui->knownImports->addItem(CSVApps[i].name);
|
||||||
|
|
||||||
ui->CSVSeparator->addItems( QStringList() << tr("Tab") << ";" << ",");
|
ui->CSVSeparator->addItems( QStringList() << tr("Tab") << ";" << ",");
|
||||||
ui->knownImports->setCurrentIndex(1);
|
|
||||||
|
|
||||||
ColumnNameProvider *provider = new ColumnNameProvider(this);
|
loadFileContents(-1, INITIAL);
|
||||||
ui->avaliableColumns->setModel(provider);
|
|
||||||
ui->avaliableColumns->setItemDelegate(new TagDragDelegate(ui->avaliableColumns));
|
|
||||||
resultModel = new ColumnNameResult(this);
|
|
||||||
ui->tableView->setModel(resultModel);
|
|
||||||
loadFileContents();
|
|
||||||
|
|
||||||
/* manually import CSV file */
|
/* manually import CSV file */
|
||||||
QShortcut *close = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), this);
|
QShortcut *close = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), this);
|
||||||
|
@ -345,7 +339,8 @@ DiveLogImportDialog::DiveLogImportDialog(QStringList fn, QWidget *parent) : QDia
|
||||||
QShortcut *quit = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), this);
|
QShortcut *quit = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), this);
|
||||||
connect(quit, SIGNAL(activated()), parent, SLOT(close()));
|
connect(quit, SIGNAL(activated()), parent, SLOT(close()));
|
||||||
|
|
||||||
connect(ui->CSVSeparator, SIGNAL(currentIndexChanged(int)), this, SLOT(loadFileContents()));
|
connect(ui->CSVSeparator, SIGNAL(currentIndexChanged(int)), this, SLOT(loadFileContentsSeperatorSelected(int)));
|
||||||
|
connect(ui->knownImports, SIGNAL(currentIndexChanged(int)), this, SLOT(loadFileContentsKnownTypesSelected(int)));
|
||||||
}
|
}
|
||||||
|
|
||||||
DiveLogImportDialog::~DiveLogImportDialog()
|
DiveLogImportDialog::~DiveLogImportDialog()
|
||||||
|
@ -353,51 +348,112 @@ DiveLogImportDialog::~DiveLogImportDialog()
|
||||||
delete ui;
|
delete ui;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DiveLogImportDialog::loadFileContents() {
|
void DiveLogImportDialog::loadFileContentsSeperatorSelected(int value)
|
||||||
|
{
|
||||||
|
loadFileContents(value, SEPARATOR);
|
||||||
|
}
|
||||||
|
|
||||||
|
void DiveLogImportDialog::loadFileContentsKnownTypesSelected(int value)
|
||||||
|
{
|
||||||
|
loadFileContents(value, KNOWNTYPES);
|
||||||
|
}
|
||||||
|
|
||||||
|
void DiveLogImportDialog::loadFileContents(int value, whatChanged triggeredBy)
|
||||||
|
{
|
||||||
QFile f(fileNames.first());
|
QFile f(fileNames.first());
|
||||||
QList<QStringList> fileColumns;
|
QList<QStringList> fileColumns;
|
||||||
QStringList currColumns;
|
QStringList currColumns;
|
||||||
QStringList headers;
|
QStringList headers;
|
||||||
bool matchedSome = false;
|
bool matchedSome = false;
|
||||||
|
|
||||||
f.open(QFile::ReadOnly);
|
// reset everything
|
||||||
// guess the separator
|
ColumnNameProvider *provider = new ColumnNameProvider(this);
|
||||||
QString firstLine = f.readLine();
|
ui->avaliableColumns->setModel(provider);
|
||||||
QString separator;
|
ui->avaliableColumns->setItemDelegate(new TagDragDelegate(ui->avaliableColumns));
|
||||||
int tabs = firstLine.count('\t');
|
resultModel = new ColumnNameResult(this);
|
||||||
int commas = firstLine.count(',');
|
ui->tableView->setModel(resultModel);
|
||||||
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);
|
|
||||||
|
|
||||||
// now try and guess the columns
|
f.open(QFile::ReadOnly);
|
||||||
|
QString firstLine = f.readLine();
|
||||||
|
QString separator = ui->CSVSeparator->currentText() == tr("Tab") ? "\t" : ui->CSVSeparator->currentText();
|
||||||
currColumns = firstLine.split(separator);
|
currColumns = firstLine.split(separator);
|
||||||
Q_FOREACH (QString columnText, currColumns) {
|
if (triggeredBy == INITIAL) {
|
||||||
ColumnNameProvider *model = (ColumnNameProvider *)ui->avaliableColumns->model();
|
// guess the separator
|
||||||
columnText.replace("\"", "");
|
int tabs = firstLine.count('\t');
|
||||||
columnText.replace("number", "#", Qt::CaseInsensitive);
|
int commas = firstLine.count(',');
|
||||||
int idx = model->mymatch(columnText);
|
int semis = firstLine.count(';');
|
||||||
if (idx >= 0) {
|
if (tabs > commas && tabs > semis)
|
||||||
QString foundHeading = model->data(model->index(idx, 0), Qt::DisplayRole).toString();
|
separator = "\t";
|
||||||
model->removeRow(idx);
|
else if (commas > tabs && commas > semis)
|
||||||
headers.append(foundHeading);
|
separator = ",";
|
||||||
matchedSome = true;
|
else if (semis > tabs && semis > commas)
|
||||||
} else {
|
separator = ";";
|
||||||
headers.append("");
|
if (ui->CSVSeparator->currentText() != separator) {
|
||||||
|
blockSignals(true);
|
||||||
|
ui->CSVSeparator->setCurrentText(separator);
|
||||||
|
blockSignals(false);
|
||||||
|
currColumns = firstLine.split(separator);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (matchedSome) {
|
if (triggeredBy == INITIAL || (triggeredBy == KNOWNTYPES && value == 0) || triggeredBy == SEPARATOR) {
|
||||||
ui->dragInstructions->setText(tr("Some column headers were pre-populated; please drag and drop the headers so they match the column they are in."));
|
// now try and guess the columns
|
||||||
ui->knownImports->setCurrentIndex(0); // <- that's "Manual import"
|
Q_FOREACH (QString columnText, currColumns) {
|
||||||
|
ColumnNameProvider *model = (ColumnNameProvider *)ui->avaliableColumns->model();
|
||||||
|
columnText.replace("\"", "");
|
||||||
|
columnText.replace("number", "#", Qt::CaseInsensitive);
|
||||||
|
int idx = model->mymatch(columnText);
|
||||||
|
if (idx >= 0) {
|
||||||
|
QString foundHeading = model->data(model->index(idx, 0), Qt::DisplayRole).toString();
|
||||||
|
model->removeRow(idx);
|
||||||
|
headers.append(foundHeading);
|
||||||
|
matchedSome = true;
|
||||||
|
} else {
|
||||||
|
headers.append("");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (matchedSome) {
|
||||||
|
ui->dragInstructions->setText(tr("Some column headers were pre-populated; please drag and drop the headers so they match the column they are in."));
|
||||||
|
if (triggeredBy != KNOWNTYPES) {
|
||||||
|
blockSignals(true);
|
||||||
|
ui->knownImports->setCurrentIndex(0); // <- that's "Manual import"
|
||||||
|
blockSignals(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
if (triggeredBy == KNOWNTYPES && value != 0) {
|
||||||
|
// an actual known type
|
||||||
|
separator = CSVApps[value].separator;
|
||||||
|
if (ui->CSVSeparator->currentText() != separator) {
|
||||||
|
blockSignals(true);
|
||||||
|
ui->CSVSeparator->setCurrentText(separator);
|
||||||
|
blockSignals(false);
|
||||||
|
if (separator == "Tab")
|
||||||
|
separator = "\t";
|
||||||
|
currColumns = firstLine.split(separator);
|
||||||
|
}
|
||||||
|
// now set up time, depth, temperature, po2, cns, ndl, tts, stopdepth, pressure
|
||||||
|
for (int i = 0; i < currColumns.count(); i++)
|
||||||
|
headers.append("");
|
||||||
|
if (CSVApps[value].time != -1)
|
||||||
|
headers.replace(CSVApps[value].time, tr("Sample time"));
|
||||||
|
if (CSVApps[value].depth != -1)
|
||||||
|
headers.replace(CSVApps[value].depth, tr("Sample depth"));
|
||||||
|
if (CSVApps[value].temperature != -1)
|
||||||
|
headers.replace(CSVApps[value].temperature, tr("Sample temperature"));
|
||||||
|
if (CSVApps[value].po2 != -1)
|
||||||
|
headers.replace(CSVApps[value].po2, tr("Sample po2"));
|
||||||
|
if (CSVApps[value].cns != -1)
|
||||||
|
headers.replace(CSVApps[value].cns, tr("Sample cns"));
|
||||||
|
if (CSVApps[value].ndl != -1)
|
||||||
|
headers.replace(CSVApps[value].ndl, tr("Sample ndl"));
|
||||||
|
if (CSVApps[value].tts != -1)
|
||||||
|
headers.replace(CSVApps[value].tts, tr("Sample tts"));
|
||||||
|
if (CSVApps[value].stopdepth != -1)
|
||||||
|
headers.replace(CSVApps[value].stopdepth, tr("Sample stopdepth"));
|
||||||
|
if (CSVApps[value].pressure != -1)
|
||||||
|
headers.replace(CSVApps[value].pressure, tr("Samples pressure"));
|
||||||
|
}
|
||||||
|
|
||||||
f.reset();
|
f.reset();
|
||||||
int rows = 0;
|
int rows = 0;
|
||||||
while (rows < 10 || !f.atEnd()) {
|
while (rows < 10 || !f.atEnd()) {
|
||||||
|
|
|
@ -80,11 +80,14 @@ class DiveLogImportDialog : public QDialog {
|
||||||
public:
|
public:
|
||||||
explicit DiveLogImportDialog(QStringList fn, QWidget *parent = 0);
|
explicit DiveLogImportDialog(QStringList fn, QWidget *parent = 0);
|
||||||
~DiveLogImportDialog();
|
~DiveLogImportDialog();
|
||||||
|
enum whatChanged { INITIAL, SEPARATOR, KNOWNTYPES };
|
||||||
private
|
private
|
||||||
slots:
|
slots:
|
||||||
void on_buttonBox_accepted();
|
void on_buttonBox_accepted();
|
||||||
void loadFileContents();
|
void loadFileContentsSeperatorSelected(int value);
|
||||||
|
void loadFileContentsKnownTypesSelected(int value);
|
||||||
|
void loadFileContents(int value, enum whatChanged triggeredBy);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool selector;
|
bool selector;
|
||||||
QStringList fileNames;
|
QStringList fileNames;
|
||||||
|
|
Loading…
Add table
Reference in a new issue