2017-04-27 18:24:53 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2015-06-16 13:34:40 +00:00
|
|
|
#include <QString>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QTextStream>
|
|
|
|
#include "divelogexportlogic.h"
|
2019-08-05 17:41:15 +00:00
|
|
|
#include "errorhelper.h"
|
2018-06-03 20:15:19 +00:00
|
|
|
#include "qthelper.h"
|
2015-06-16 13:34:40 +00:00
|
|
|
#include "units.h"
|
|
|
|
#include "statistics.h"
|
2022-09-03 08:37:58 +00:00
|
|
|
#include "string-format.h"
|
2015-06-16 13:34:40 +00:00
|
|
|
#include "save-html.h"
|
|
|
|
|
2019-03-22 20:33:17 +00:00
|
|
|
static void file_copy_and_overwrite(const QString &fileName, const QString &newName)
|
2015-06-16 13:34:40 +00:00
|
|
|
{
|
|
|
|
QFile file(newName);
|
|
|
|
if (file.exists())
|
|
|
|
file.remove();
|
|
|
|
QFile::copy(fileName, newName);
|
|
|
|
}
|
|
|
|
|
2019-03-22 20:33:17 +00:00
|
|
|
static void exportHTMLsettings(const QString &filename, struct htmlExportSetting &hes)
|
2015-06-16 13:34:40 +00:00
|
|
|
{
|
|
|
|
QString fontSize = hes.fontSize;
|
|
|
|
QString fontFamily = hes.fontFamily;
|
|
|
|
QFile file(filename);
|
|
|
|
file.open(QIODevice::WriteOnly | QIODevice::Text);
|
|
|
|
QTextStream out(&file);
|
|
|
|
out << "settings = {\"fontSize\":\"" << fontSize << "\",\"fontFamily\":\"" << fontFamily << "\",\"listOnly\":\""
|
|
|
|
<< hes.listOnly << "\",\"subsurfaceNumbers\":\"" << hes.subsurfaceNumbers << "\",";
|
|
|
|
//save units preferences
|
|
|
|
if (prefs.unit_system == METRIC) {
|
|
|
|
out << "\"unit_system\":\"Meteric\"";
|
|
|
|
} else if (prefs.unit_system == IMPERIAL) {
|
|
|
|
out << "\"unit_system\":\"Imperial\"";
|
|
|
|
} else {
|
2022-09-03 11:54:52 +00:00
|
|
|
QString length = prefs.units.length == units::METERS ? "METER" : "FEET";
|
|
|
|
QString pressure = prefs.units.pressure == units::BAR ? "BAR" : "PSI";
|
|
|
|
QString volume = prefs.units.volume == units::LITER ? "LITER" : "CUFT";
|
|
|
|
QString temperature = prefs.units.temperature == units::CELSIUS ? "CELSIUS" : "FAHRENHEIT";
|
|
|
|
QString weight = prefs.units.weight == units::KG ? "KG" : "LBS";
|
2015-06-16 13:34:40 +00:00
|
|
|
out << "\"unit_system\":\"Personalize\",";
|
|
|
|
out << "\"units\":{\"depth\":\"" << length << "\",\"pressure\":\"" << pressure << "\",\"volume\":\"" << volume << "\",\"temperature\":\"" << temperature << "\",\"weight\":\"" << weight << "\"}";
|
|
|
|
}
|
|
|
|
out << "}";
|
|
|
|
file.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void exportHTMLstatisticsTotal(QTextStream &out, stats_t *total_stats)
|
|
|
|
{
|
|
|
|
out << "{";
|
|
|
|
out << "\"YEAR\":\"Total\",";
|
|
|
|
out << "\"DIVES\":\"" << total_stats->selection_size << "\",";
|
2017-05-11 20:43:36 +00:00
|
|
|
out << "\"TOTAL_TIME\":\"" << get_dive_duration_string(total_stats->total_time.seconds,
|
2018-07-03 14:52:20 +00:00
|
|
|
gettextFromC::tr("h"), gettextFromC::tr("min"), gettextFromC::tr("sec"), " ") << "\",";
|
2015-06-16 13:34:40 +00:00
|
|
|
out << "\"AVERAGE_TIME\":\"--\",";
|
|
|
|
out << "\"SHORTEST_TIME\":\"--\",";
|
|
|
|
out << "\"LONGEST_TIME\":\"--\",";
|
|
|
|
out << "\"AVG_DEPTH\":\"--\",";
|
|
|
|
out << "\"MIN_DEPTH\":\"--\",";
|
|
|
|
out << "\"MAX_DEPTH\":\"--\",";
|
|
|
|
out << "\"AVG_SAC\":\"--\",";
|
|
|
|
out << "\"MIN_SAC\":\"--\",";
|
|
|
|
out << "\"MAX_SAC\":\"--\",";
|
|
|
|
out << "\"AVG_TEMP\":\"--\",";
|
|
|
|
out << "\"MIN_TEMP\":\"--\",";
|
|
|
|
out << "\"MAX_TEMP\":\"--\",";
|
|
|
|
out << "},";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void exportHTMLstatistics(const QString filename, struct htmlExportSetting &hes)
|
|
|
|
{
|
|
|
|
QFile file(filename);
|
|
|
|
file.open(QIODevice::WriteOnly | QIODevice::Text);
|
|
|
|
QTextStream out(&file);
|
|
|
|
|
|
|
|
stats_t total_stats;
|
|
|
|
|
2024-05-01 21:16:00 +00:00
|
|
|
stats_summary stats = calculate_stats_summary(hes.selectedOnly);
|
2015-06-16 13:34:40 +00:00
|
|
|
total_stats.selection_size = 0;
|
2024-09-03 15:04:48 +00:00
|
|
|
total_stats.total_time = 0_sec;
|
2015-06-16 13:34:40 +00:00
|
|
|
|
|
|
|
out << "divestat=[";
|
|
|
|
if (hes.yearlyStatistics) {
|
2024-05-01 21:16:00 +00:00
|
|
|
for (const auto &s: stats.stats_yearly) {
|
2015-06-16 13:34:40 +00:00
|
|
|
out << "{";
|
2024-05-01 21:16:00 +00:00
|
|
|
out << "\"YEAR\":\"" << s.period << "\",";
|
|
|
|
out << "\"DIVES\":\"" << s.selection_size << "\",";
|
|
|
|
out << "\"TOTAL_TIME\":\"" << get_dive_duration_string(s.total_time.seconds,
|
2018-07-03 14:52:20 +00:00
|
|
|
gettextFromC::tr("h"), gettextFromC::tr("min"), gettextFromC::tr("sec"), " ") << "\",";
|
2024-05-01 21:16:00 +00:00
|
|
|
out << "\"AVERAGE_TIME\":\"" << formatMinutes(s.total_time.seconds / s.selection_size) << "\",";
|
|
|
|
out << "\"SHORTEST_TIME\":\"" << formatMinutes(s.shortest_time.seconds) << "\",";
|
|
|
|
out << "\"LONGEST_TIME\":\"" << formatMinutes(s.longest_time.seconds) << "\",";
|
|
|
|
out << "\"AVG_DEPTH\":\"" << get_depth_string(s.avg_depth) << "\",";
|
|
|
|
out << "\"MIN_DEPTH\":\"" << get_depth_string(s.min_depth) << "\",";
|
|
|
|
out << "\"MAX_DEPTH\":\"" << get_depth_string(s.max_depth) << "\",";
|
|
|
|
out << "\"AVG_SAC\":\"" << get_volume_string(s.avg_sac) << "\",";
|
|
|
|
out << "\"MIN_SAC\":\"" << get_volume_string(s.min_sac) << "\",";
|
|
|
|
out << "\"MAX_SAC\":\"" << get_volume_string(s.max_sac) << "\",";
|
|
|
|
if (s.combined_count) {
|
2018-02-18 20:55:57 +00:00
|
|
|
temperature_t avg_temp;
|
2024-05-01 21:16:00 +00:00
|
|
|
avg_temp.mkelvin = s.combined_temp.mkelvin / s.combined_count;
|
2018-02-18 20:55:57 +00:00
|
|
|
out << "\"AVG_TEMP\":\"" << get_temperature_string(avg_temp) << "\",";
|
|
|
|
} else {
|
2015-06-16 13:34:40 +00:00
|
|
|
out << "\"AVG_TEMP\":\"0.0\",";
|
2018-02-18 20:55:57 +00:00
|
|
|
}
|
2024-05-01 21:16:00 +00:00
|
|
|
out << "\"MIN_TEMP\":\"" << (s.min_temp.mkelvin == 0 ? 0 : get_temperature_string(s.min_temp)) << "\",";
|
|
|
|
out << "\"MAX_TEMP\":\"" << (s.max_temp.mkelvin == 0 ? 0 : get_temperature_string(s.max_temp)) << "\",";
|
2015-06-16 13:34:40 +00:00
|
|
|
out << "},";
|
2024-05-01 21:16:00 +00:00
|
|
|
total_stats.selection_size += s.selection_size;
|
2024-09-02 18:42:05 +00:00
|
|
|
total_stats.total_time += s.total_time;
|
2015-06-16 13:34:40 +00:00
|
|
|
}
|
|
|
|
exportHTMLstatisticsTotal(out, &total_stats);
|
|
|
|
}
|
|
|
|
out << "]";
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void exportHtmlInitLogic(const QString &filename, struct htmlExportSetting &hes)
|
|
|
|
{
|
|
|
|
QString photosDirectory;
|
|
|
|
QFile file(filename);
|
|
|
|
QFileInfo info(file);
|
|
|
|
QDir mainDir = info.absoluteDir();
|
2019-03-22 20:33:17 +00:00
|
|
|
QString exportFiles = file.fileName() + "_files" + QDir::separator();
|
|
|
|
mainDir.mkdir(exportFiles);
|
2015-06-16 13:34:40 +00:00
|
|
|
|
2019-03-22 20:33:17 +00:00
|
|
|
QString json_dive_data = exportFiles + "file.js";
|
|
|
|
QString json_settings = exportFiles + "settings.js";
|
|
|
|
QString translation = exportFiles + "translation.js";
|
|
|
|
QString stat_file = exportFiles + "stat.js";
|
2015-06-16 13:34:40 +00:00
|
|
|
|
|
|
|
if (hes.exportPhotos) {
|
2019-03-22 20:33:17 +00:00
|
|
|
photosDirectory = exportFiles + "photos" + QDir::separator();
|
2015-06-16 13:34:40 +00:00
|
|
|
mainDir.mkdir(photosDirectory);
|
|
|
|
}
|
|
|
|
|
|
|
|
exportHTMLsettings(json_settings, hes);
|
|
|
|
exportHTMLstatistics(stat_file, hes);
|
2018-02-25 12:51:41 +00:00
|
|
|
export_translation(qPrintable(translation));
|
2015-06-16 13:34:40 +00:00
|
|
|
|
|
|
|
export_HTML(qPrintable(json_dive_data), qPrintable(photosDirectory), hes.selectedOnly, hes.listOnly);
|
|
|
|
|
|
|
|
QString searchPath = getSubsurfaceDataPath("theme");
|
2018-08-15 21:22:32 +00:00
|
|
|
if (searchPath.isEmpty()) {
|
2024-03-12 08:17:50 +00:00
|
|
|
report_error("%s", qPrintable(gettextFromC::tr("Cannot find a folder called 'theme' in the standard locations")));
|
2015-06-16 13:34:40 +00:00
|
|
|
return;
|
2018-08-15 21:22:32 +00:00
|
|
|
}
|
2015-06-16 13:34:40 +00:00
|
|
|
|
|
|
|
searchPath += QDir::separator();
|
|
|
|
|
|
|
|
file_copy_and_overwrite(searchPath + "dive_export.html", filename);
|
|
|
|
file_copy_and_overwrite(searchPath + "list_lib.js", exportFiles + "list_lib.js");
|
|
|
|
file_copy_and_overwrite(searchPath + "poster.png", exportFiles + "poster.png");
|
|
|
|
file_copy_and_overwrite(searchPath + "jqplot.highlighter.min.js", exportFiles + "jqplot.highlighter.min.js");
|
|
|
|
file_copy_and_overwrite(searchPath + "jquery.jqplot.min.js", exportFiles + "jquery.jqplot.min.js");
|
|
|
|
file_copy_and_overwrite(searchPath + "jqplot.canvasAxisTickRenderer.min.js", exportFiles + "jqplot.canvasAxisTickRenderer.min.js");
|
|
|
|
file_copy_and_overwrite(searchPath + "jqplot.canvasTextRenderer.min.js", exportFiles + "jqplot.canvasTextRenderer.min.js");
|
|
|
|
file_copy_and_overwrite(searchPath + "jquery.min.js", exportFiles + "jquery.min.js");
|
|
|
|
file_copy_and_overwrite(searchPath + "jquery.jqplot.css", exportFiles + "jquery.jqplot.css");
|
|
|
|
file_copy_and_overwrite(searchPath + hes.themeFile, exportFiles + "theme.css");
|
|
|
|
}
|