| 
									
										
										
										
											2017-04-27 20:13:48 +02:00
										 |  |  | // SPDX-License-Identifier: GPL-2.0
 | 
					
						
							| 
									
										
										
										
											2015-11-08 18:50:05 +01:00
										 |  |  | #include "smrtk2ssrfc_window.h"
 | 
					
						
							|  |  |  | #include "ui_smrtk2ssrfc_window.h"
 | 
					
						
							| 
									
										
										
										
											2016-06-15 20:59:34 +02:00
										 |  |  | #include "qt-models/filtermodels.h"
 | 
					
						
							|  |  |  | #include "core/dive.h"
 | 
					
						
							|  |  |  | #include "core/divelist.h"
 | 
					
						
							| 
									
										
										
										
											2019-08-05 19:41:15 +02:00
										 |  |  | #include "core/errorhelper.h"
 | 
					
						
							| 
									
										
										
										
											2018-08-18 20:07:35 +02:00
										 |  |  | #include "core/settings/qPrefDisplay.h"
 | 
					
						
							| 
									
										
										
										
											2015-11-08 18:50:05 +01:00
										 |  |  | #include <QFileDialog>
 | 
					
						
							|  |  |  | #include <QFileInfo>
 | 
					
						
							|  |  |  | #include <QDebug>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | QStringList inputFiles; | 
					
						
							|  |  |  | QString outputFile; | 
					
						
							| 
									
										
										
										
											2018-02-11 22:41:06 +01:00
										 |  |  | QString error_buf; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | extern "C" void getErrorFromC(char *buf) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	QString error(buf); | 
					
						
							|  |  |  | 	free(buf); | 
					
						
							|  |  |  | 	error_buf = error; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-11-08 18:50:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | Smrtk2ssrfcWindow::Smrtk2ssrfcWindow(QWidget *parent) : | 
					
						
							|  |  |  | 	QMainWindow(parent), | 
					
						
							|  |  |  | 	ui(new Ui::Smrtk2ssrfcWindow) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	ui->setupUi(this); | 
					
						
							|  |  |  | 	ui->plainTextEdit->setDisabled(true); | 
					
						
							|  |  |  | 	ui->progressBar->setDisabled(true); | 
					
						
							|  |  |  | 	ui->statusBar->adjustSize(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Smrtk2ssrfcWindow::~Smrtk2ssrfcWindow() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	delete ui; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-09 16:24:39 +02:00
										 |  |  | static QString lastUsedDir() | 
					
						
							| 
									
										
										
										
											2015-11-08 18:50:05 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	QString lastDir = QDir::homePath(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-18 20:07:35 +02:00
										 |  |  | 	if (QDir(qPrefDisplay::lastDir()).exists()) | 
					
						
							|  |  |  | 		lastDir = qPrefDisplay::lastDir(); | 
					
						
							| 
									
										
										
										
											2015-11-08 18:50:05 +01:00
										 |  |  | 	return lastDir; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Smrtk2ssrfcWindow::updateLastUsedDir(const QString &dir) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-08-18 20:07:35 +02:00
										 |  |  | 	qPrefDisplay::set_lastDir(dir); | 
					
						
							| 
									
										
										
										
											2015-11-08 18:50:05 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Smrtk2ssrfcWindow::on_inputFilesButton_clicked() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	inputFiles = QFileDialog::getOpenFileNames(this, tr("Open SmartTrak files"), lastUsedDir(), | 
					
						
							| 
									
										
										
										
											2017-10-27 14:52:27 +02:00
										 |  |  | 		tr("SmartTrak files") + " (*.slg);;" + tr("All files") + " (*.*)"); | 
					
						
							| 
									
										
										
										
											2015-11-08 18:50:05 +01:00
										 |  |  | 	if (inputFiles.isEmpty()) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	updateLastUsedDir(QFileInfo(inputFiles[0]).dir().path()); | 
					
						
							|  |  |  | 	ui->inputLine->setText(inputFiles.join(" ")); | 
					
						
							|  |  |  | 	ui->progressBar->setEnabled(true); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Smrtk2ssrfcWindow::on_outputFileButton_clicked() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	outputFile = QFileDialog::getSaveFileName(this, tr("Open Subsurface files"), lastUsedDir(), | 
					
						
							| 
									
										
										
										
											2017-10-27 14:52:27 +02:00
										 |  |  | 		tr("Subsurface files") + " (*.ssrf *.xml);;" + tr("All files") + " (*.*)"); | 
					
						
							| 
									
										
										
										
											2015-11-08 18:50:05 +01:00
										 |  |  | 	if (outputFile.isEmpty()) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	updateLastUsedDir(QFileInfo(outputFile).dir().path()); | 
					
						
							|  |  |  | 	ui->outputLine->setText(outputFile); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Smrtk2ssrfcWindow::on_importButton_clicked() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	if (inputFiles.isEmpty()) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	QByteArray fileNamePtr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ui->plainTextEdit->setDisabled(false); | 
					
						
							|  |  |  | 	ui->progressBar->setRange(0, inputFiles.size()); | 
					
						
							| 
									
										
										
										
											2018-02-11 22:41:06 +01:00
										 |  |  | 	set_error_cb(&getErrorFromC); | 
					
						
							| 
									
										
										
										
											2015-11-08 18:50:05 +01:00
										 |  |  | 	for (int i = 0; i < inputFiles.size(); ++i) { | 
					
						
							|  |  |  | 		ui->progressBar->setValue(i); | 
					
						
							|  |  |  | 		fileNamePtr = QFile::encodeName(inputFiles.at(i)); | 
					
						
							|  |  |  | 		smartrak_import(fileNamePtr.data(), &dive_table); | 
					
						
							| 
									
										
										
										
											2018-02-11 22:41:06 +01:00
										 |  |  | 		ui->plainTextEdit->appendPlainText(error_buf); | 
					
						
							| 
									
										
										
										
											2015-11-08 18:50:05 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	ui->progressBar->setValue(inputFiles.size()); | 
					
						
							| 
									
										
										
										
											2018-09-10 14:47:56 +02:00
										 |  |  | 	save_dives_logic(qPrintable(outputFile), false, false); | 
					
						
							| 
									
										
										
										
											2015-11-08 18:50:05 +01:00
										 |  |  | 	ui->progressBar->setDisabled(true); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Smrtk2ssrfcWindow::on_exitButton_clicked() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	this->close(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Smrtk2ssrfcWindow::on_outputLine_textEdited() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	outputFile = ui->outputLine->text(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Smrtk2ssrfcWindow::on_inputLine_textEdited() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	inputFiles = ui->inputLine->text().split(" "); | 
					
						
							|  |  |  | } |