2013-10-16 19:05:19 +00:00
|
|
|
#include <QtDebug>
|
|
|
|
#include <QFileDialog>
|
2014-04-25 17:44:23 +00:00
|
|
|
#include <QShortcut>
|
2013-12-29 16:11:20 +00:00
|
|
|
#include "divelogimportdialog.h"
|
2013-10-16 19:05:19 +00:00
|
|
|
#include "mainwindow.h"
|
2013-12-29 16:11:20 +00:00
|
|
|
#include "ui_divelogimportdialog.h"
|
2015-01-06 16:32:03 +00:00
|
|
|
#include <QAbstractListModel>
|
|
|
|
#include <QAbstractTableModel>
|
2015-01-06 17:03:58 +00:00
|
|
|
#include <QMouseEvent>
|
|
|
|
#include <QDrag>
|
|
|
|
#include <QMimeData>
|
2013-12-29 16:11:20 +00:00
|
|
|
const DiveLogImportDialog::CSVAppConfig DiveLogImportDialog::CSVApps[CSVAPPS] = {
|
2014-07-10 18:54:18 +00:00
|
|
|
// time, depth, temperature, po2, cns, ndl, tts, stopdepth, pressure
|
2014-02-28 04:09:57 +00:00
|
|
|
{ "", },
|
2014-07-10 18:54:18 +00:00
|
|
|
{ "APD Log Viewer", 1, 2, 16, 7, 18, -1, -1, 19, -1, "Tab" },
|
|
|
|
{ "XP5", 1, 2, 10, -1, -1, -1, -1, -1, -1, "Tab" },
|
|
|
|
{ "SensusCSV", 10, 11, -1, -1, -1, -1, -1, -1, -1, "," },
|
|
|
|
{ "Seabear CSV", 1, 2, 6, -1, -1, 3, 4, 5, 7, ";" },
|
2014-12-26 14:11:38 +00:00
|
|
|
{ "SubsurfaceCSV", -1, -1, -1, -1, -1, -1, -1, -1, -1, "," },
|
2014-02-28 04:09:57 +00:00
|
|
|
{ NULL, }
|
2013-10-16 19:05:19 +00:00
|
|
|
};
|
|
|
|
|
2015-01-06 16:45:49 +00:00
|
|
|
ColumnNameProvider::ColumnNameProvider(QObject *parent) : QAbstractListModel(parent)
|
2015-01-06 16:32:03 +00:00
|
|
|
{
|
2015-01-06 16:39:56 +00:00
|
|
|
columnNames << tr("Dive #") << tr("Date") << tr("Time") << tr("Duration") << tr("Location") << tr("GPS") << tr("Weight") << tr("Cyl size") << tr("Start Pressure")
|
|
|
|
<< tr("End Press") << tr("Max depth") << tr("Mean depth") << tr("Buddy") << tr("Notes") << tr("Tags") << tr("Air temp") << tr("Water temp")
|
|
|
|
<< tr("O₂") << tr("He");
|
2015-01-06 16:32:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ColumnNameProvider::insertRows(int row, int count, const QModelIndex &parent)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ColumnNameProvider::removeRows(int row, int count, const QModelIndex &parent)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ColumnNameProvider::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant ColumnNameProvider::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
2015-01-06 16:39:56 +00:00
|
|
|
if (!index.isValid())
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
if (role != Qt::DisplayRole)
|
|
|
|
return QVariant();
|
2015-01-06 16:32:03 +00:00
|
|
|
|
2015-01-06 16:39:56 +00:00
|
|
|
return QVariant(columnNames[index.row()]);
|
2015-01-06 16:32:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ColumnNameProvider::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
2015-01-06 16:39:56 +00:00
|
|
|
Q_UNUSED(parent)
|
|
|
|
return columnNames.count();
|
2015-01-06 16:32:03 +00:00
|
|
|
}
|
|
|
|
|
2015-01-06 16:54:58 +00:00
|
|
|
ColumnNameView::ColumnNameView(QWidget *parent)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void ColumnNameView::mousePressEvent(QMouseEvent *press)
|
|
|
|
{
|
2015-01-06 17:03:58 +00:00
|
|
|
QModelIndex atClick = indexAt(press->pos());
|
|
|
|
if (!atClick.isValid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
qDebug() << atClick.data();
|
|
|
|
QDrag *drag = new QDrag(this);
|
|
|
|
QMimeData *mimeData = new QMimeData;
|
|
|
|
mimeData->setText(atClick.data().toString());
|
|
|
|
drag->setMimeData(mimeData);
|
|
|
|
drag->exec();
|
2015-01-06 16:54:58 +00:00
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
DiveLogImportDialog::DiveLogImportDialog(QStringList *fn, QWidget *parent) : QDialog(parent),
|
2013-10-16 19:05:19 +00:00
|
|
|
selector(true),
|
2013-12-29 16:11:20 +00:00
|
|
|
ui(new Ui::DiveLogImportDialog)
|
2013-10-16 19:05:19 +00:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
2014-01-07 20:01:28 +00:00
|
|
|
fileNames = *fn;
|
2014-12-25 13:25:29 +00:00
|
|
|
column = 0;
|
2013-10-16 19:05:19 +00:00
|
|
|
|
2014-01-16 20:50:16 +00:00
|
|
|
/* Add indexes of XSLTs requiring special handling to the list */
|
|
|
|
specialCSV << 3;
|
2014-12-26 14:11:38 +00:00
|
|
|
specialCSV << 5;
|
2014-01-16 20:50:16 +00:00
|
|
|
|
2013-10-16 19:05:19 +00:00
|
|
|
for (int i = 0; !CSVApps[i].name.isNull(); ++i)
|
|
|
|
ui->knownImports->addItem(CSVApps[i].name);
|
|
|
|
|
2015-01-06 16:12:29 +00:00
|
|
|
ui->CSVSeparator->addItems( QStringList() << tr("Separator") << tr("Tab") << ";" << ",");
|
2013-10-16 19:05:19 +00:00
|
|
|
ui->knownImports->setCurrentIndex(1);
|
2014-12-25 13:25:29 +00:00
|
|
|
|
2015-01-06 16:45:49 +00:00
|
|
|
ColumnNameProvider *provider = new ColumnNameProvider(this);
|
|
|
|
ui->avaliableColumns->setModel(provider);
|
|
|
|
|
2014-12-25 13:25:29 +00:00
|
|
|
/* manually import CSV file */
|
2014-04-25 17:44:23 +00:00
|
|
|
QShortcut *close = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), this);
|
|
|
|
connect(close, SIGNAL(activated()), this, SLOT(close()));
|
|
|
|
QShortcut *quit = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q), this);
|
|
|
|
connect(quit, SIGNAL(activated()), parent, SLOT(close()));
|
2013-10-16 19:05:19 +00:00
|
|
|
}
|
|
|
|
|
2013-12-29 16:11:20 +00:00
|
|
|
DiveLogImportDialog::~DiveLogImportDialog()
|
2013-10-16 19:05:19 +00:00
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
#define VALUE_IF_CHECKED(x) (ui->x->isEnabled() ? ui->x->value() - 1 : -1)
|
2013-12-29 16:11:20 +00:00
|
|
|
void DiveLogImportDialog::on_buttonBox_accepted()
|
2013-10-16 19:05:19 +00:00
|
|
|
{
|
2015-01-06 16:12:29 +00:00
|
|
|
/*
|
|
|
|
for (int i = 0; i < fileNames.size(); ++i) {
|
|
|
|
if (ui->knownImports->currentText() == QString("Seabear CSV")) {
|
|
|
|
parse_seabear_csv_file(fileNames[i].toUtf8().data(), ui->CSVTime->value() - 1,
|
|
|
|
ui->CSVDepth->value() - 1, VALUE_IF_CHECKED(CSVTemperature),
|
2014-02-28 04:09:57 +00:00
|
|
|
VALUE_IF_CHECKED(CSVpo2),
|
|
|
|
VALUE_IF_CHECKED(CSVcns),
|
2014-07-09 20:13:37 +00:00
|
|
|
VALUE_IF_CHECKED(CSVndl),
|
2014-07-09 20:13:38 +00:00
|
|
|
VALUE_IF_CHECKED(CSVtts),
|
2014-02-28 04:09:57 +00:00
|
|
|
VALUE_IF_CHECKED(CSVstopdepth),
|
2014-07-10 18:54:18 +00:00
|
|
|
VALUE_IF_CHECKED(CSVpressure),
|
2014-02-28 04:09:57 +00:00
|
|
|
ui->CSVSeparator->currentIndex(),
|
|
|
|
specialCSV.contains(ui->knownImports->currentIndex()) ? CSVApps[ui->knownImports->currentIndex()].name.toUtf8().data() : "csv",
|
2014-03-14 18:26:07 +00:00
|
|
|
ui->CSVUnits->currentIndex());
|
2014-10-28 09:14:00 +00:00
|
|
|
|
2015-01-06 16:12:29 +00:00
|
|
|
// Seabear CSV stores NDL and TTS in Minutes, not seconds
|
2014-10-28 09:14:00 +00:00
|
|
|
struct dive *dive = dive_table.dives[dive_table.nr - 1];
|
|
|
|
for(int s_nr = 0 ; s_nr <= dive->dc.samples ; s_nr++) {
|
|
|
|
struct sample *sample = dive->dc.sample + s_nr;
|
|
|
|
sample->ndl.seconds *= 60;
|
|
|
|
sample->tts.seconds *= 60;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
parse_csv_file(fileNames[i].toUtf8().data(), ui->CSVTime->value() - 1,
|
|
|
|
ui->CSVDepth->value() - 1, VALUE_IF_CHECKED(CSVTemperature),
|
|
|
|
VALUE_IF_CHECKED(CSVpo2),
|
|
|
|
VALUE_IF_CHECKED(CSVcns),
|
|
|
|
VALUE_IF_CHECKED(CSVndl),
|
|
|
|
VALUE_IF_CHECKED(CSVtts),
|
|
|
|
VALUE_IF_CHECKED(CSVstopdepth),
|
|
|
|
VALUE_IF_CHECKED(CSVpressure),
|
|
|
|
ui->CSVSeparator->currentIndex(),
|
|
|
|
specialCSV.contains(ui->knownImports->currentIndex()) ? CSVApps[ui->knownImports->currentIndex()].name.toUtf8().data() : "csv",
|
|
|
|
ui->CSVUnits->currentIndex());
|
2014-01-25 07:49:22 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int i = 0; i < fileNames.size(); ++i) {
|
|
|
|
parse_manual_file(fileNames[i].toUtf8().data(),
|
2014-02-28 04:09:57 +00:00
|
|
|
ui->ManualSeparator->currentIndex(),
|
|
|
|
ui->Units->currentIndex(),
|
2014-12-25 13:25:29 +00:00
|
|
|
ui->DateFormat->currentIndex(),
|
2014-12-31 16:27:31 +00:00
|
|
|
ui->DurationFormat->currentIndex(),
|
2014-02-28 04:09:57 +00:00
|
|
|
VALUE_IF_CHECKED(DiveNumber),
|
|
|
|
VALUE_IF_CHECKED(Date), VALUE_IF_CHECKED(Time),
|
|
|
|
VALUE_IF_CHECKED(Duration), VALUE_IF_CHECKED(Location),
|
|
|
|
VALUE_IF_CHECKED(Gps), VALUE_IF_CHECKED(MaxDepth),
|
|
|
|
VALUE_IF_CHECKED(MeanDepth), VALUE_IF_CHECKED(Buddy),
|
|
|
|
VALUE_IF_CHECKED(Notes), VALUE_IF_CHECKED(Weight),
|
2014-12-20 19:23:34 +00:00
|
|
|
VALUE_IF_CHECKED(Tags),
|
|
|
|
VALUE_IF_CHECKED(CylinderSize), VALUE_IF_CHECKED(StartPressure),
|
|
|
|
VALUE_IF_CHECKED(EndPressure), VALUE_IF_CHECKED(O2),
|
|
|
|
VALUE_IF_CHECKED(He), VALUE_IF_CHECKED(AirTemp),
|
|
|
|
VALUE_IF_CHECKED(WaterTemp));
|
2013-12-29 16:11:19 +00:00
|
|
|
}
|
2013-10-16 19:05:19 +00:00
|
|
|
}
|
2015-01-06 16:12:29 +00:00
|
|
|
*/
|
2014-01-15 08:30:42 +00:00
|
|
|
process_dives(true, false);
|
2013-10-16 19:05:19 +00:00
|
|
|
|
2014-02-12 14:22:54 +00:00
|
|
|
MainWindow::instance()->refreshDisplay();
|
2013-10-16 19:05:19 +00:00
|
|
|
}
|
|
|
|
|
2013-11-21 22:48:37 +00:00
|
|
|
#define SET_VALUE_AND_CHECKBOX(CSV, BOX, VAL) ({\
|
|
|
|
ui->CSV->blockSignals(true);\
|
|
|
|
ui->CSV->setValue(VAL);\
|
|
|
|
ui->CSV->setEnabled(VAL >= 0);\
|
|
|
|
ui->BOX->setChecked(VAL >= 0);\
|
2014-03-03 21:25:55 +00:00
|
|
|
ui->CSV->blockSignals(false); })
|
2015-01-06 16:12:29 +00:00
|
|
|
|
2013-12-29 16:11:20 +00:00
|
|
|
void DiveLogImportDialog::on_knownImports_currentIndexChanged(int index)
|
2013-10-16 19:05:19 +00:00
|
|
|
{
|
|
|
|
if (index == 0)
|
|
|
|
return;
|
|
|
|
|
2013-11-21 22:48:37 +00:00
|
|
|
}
|
|
|
|
|
2013-12-29 16:11:20 +00:00
|
|
|
void DiveLogImportDialog::unknownImports()
|
2013-10-16 19:05:19 +00:00
|
|
|
{
|
2014-01-16 20:50:16 +00:00
|
|
|
if (!specialCSV.contains(ui->knownImports->currentIndex()))
|
|
|
|
ui->knownImports->setCurrentIndex(0);
|
2013-10-16 19:05:19 +00:00
|
|
|
}
|