diff options
author | Dirk Hohndel <dirk@hohndel.org> | 2015-06-16 06:34:40 -0700 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2015-06-16 14:04:41 -0700 |
commit | 6a0e1cd9f6da893a7da4fa82de6ef47bb645f617 (patch) | |
tree | d38e7f4c7b4ff8c3758c9300e3bdfcaf55695a66 | |
parent | a2c546152ebfd0e01dd7021433aa04b94150bf4a (diff) | |
download | subsurface-6a0e1cd9f6da893a7da4fa82de6ef47bb645f617.tar.gz |
HTML export: separate UI and logic code
In order to be able to initiate an HTML export from the core library we
need to separate the actual logic from the UI.
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
-rw-r--r-- | CMakeLists.txt | 1 | ||||
-rw-r--r-- | divelogexportlogic.cpp | 161 | ||||
-rw-r--r-- | divelogexportlogic.h | 20 | ||||
-rw-r--r-- | qt-ui/divelogexportdialog.cpp | 153 | ||||
-rw-r--r-- | qt-ui/divelogexportdialog.h | 2 |
5 files changed, 195 insertions, 142 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 22eb08937..8ac86f726 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -276,6 +276,7 @@ set(SUBSURFACE_CORE_LIB_SRCS divesitehelpers.cpp checkcloudconnection.cpp windowtitleupdate.cpp + divelogexportlogic.cpp ${SUBSURFACE_PRINTING_SRCS} ${PLATFORM_SRC} ) diff --git a/divelogexportlogic.cpp b/divelogexportlogic.cpp new file mode 100644 index 000000000..af5157f4a --- /dev/null +++ b/divelogexportlogic.cpp @@ -0,0 +1,161 @@ +#include <QString> +#include <QFile> +#include <QFileInfo> +#include <QDir> +#include <QSettings> +#include <QTextStream> +#include "divelogexportlogic.h" +#include "helpers.h" +#include "units.h" +#include "statistics.h" +#include "save-html.h" + +void file_copy_and_overwrite(const QString &fileName, const QString &newName) +{ + QFile file(newName); + if (file.exists()) + file.remove(); + QFile::copy(fileName, newName); +} + +void exportHTMLsettings(const QString &filename, struct htmlExportSetting &hes) +{ + 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 { + QVariant v; + QString length, pressure, volume, temperature, weight; + length = prefs.units.length == units::METERS ? "METER" : "FEET"; + pressure = prefs.units.pressure == units::BAR ? "BAR" : "PSI"; + volume = prefs.units.volume == units::LITER ? "LITER" : "CUFT"; + temperature = prefs.units.temperature == units::CELSIUS ? "CELSIUS" : "FAHRENHEIT"; + weight = prefs.units.weight == units::KG ? "KG" : "LBS"; + 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 << "\","; + out << "\"TOTAL_TIME\":\"" << get_time_string(total_stats->total_time.seconds, 0) << "\","; + 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; + + total_stats.selection_size = 0; + total_stats.total_time.seconds = 0; + + int i = 0; + out << "divestat=["; + if (hes.yearlyStatistics) { + while (stats_yearly != NULL && stats_yearly[i].period) { + out << "{"; + out << "\"YEAR\":\"" << stats_yearly[i].period << "\","; + out << "\"DIVES\":\"" << stats_yearly[i].selection_size << "\","; + out << "\"TOTAL_TIME\":\"" << get_time_string(stats_yearly[i].total_time.seconds, 0) << "\","; + out << "\"AVERAGE_TIME\":\"" << get_minutes(stats_yearly[i].total_time.seconds / stats_yearly[i].selection_size) << "\","; + out << "\"SHORTEST_TIME\":\"" << get_minutes(stats_yearly[i].shortest_time.seconds) << "\","; + out << "\"LONGEST_TIME\":\"" << get_minutes(stats_yearly[i].longest_time.seconds) << "\","; + out << "\"AVG_DEPTH\":\"" << get_depth_string(stats_yearly[i].avg_depth) << "\","; + out << "\"MIN_DEPTH\":\"" << get_depth_string(stats_yearly[i].min_depth) << "\","; + out << "\"MAX_DEPTH\":\"" << get_depth_string(stats_yearly[i].max_depth) << "\","; + out << "\"AVG_SAC\":\"" << get_volume_string(stats_yearly[i].avg_sac) << "\","; + out << "\"MIN_SAC\":\"" << get_volume_string(stats_yearly[i].min_sac) << "\","; + out << "\"MAX_SAC\":\"" << get_volume_string(stats_yearly[i].max_sac) << "\","; + if ( stats_yearly[i].combined_count ) + out << "\"AVG_TEMP\":\"" << QString::number(stats_yearly[i].combined_temp / stats_yearly[i].combined_count, 'f', 1) << "\","; + else + out << "\"AVG_TEMP\":\"0.0\","; + out << "\"MIN_TEMP\":\"" << ( stats_yearly[i].min_temp == 0 ? 0 : get_temp_units(stats_yearly[i].min_temp, NULL)) << "\","; + out << "\"MAX_TEMP\":\"" << ( stats_yearly[i].max_temp == 0 ? 0 : get_temp_units(stats_yearly[i].max_temp, NULL)) << "\","; + out << "},"; + total_stats.selection_size += stats_yearly[i].selection_size; + total_stats.total_time.seconds += stats_yearly[i].total_time.seconds; + i++; + } + 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(); + mainDir.mkdir(file.fileName() + "_files"); + QString exportFiles = file.fileName() + "_files"; + + QString json_dive_data = exportFiles + QDir::separator() + "file.js"; + QString json_settings = exportFiles + QDir::separator() + "settings.js"; + QString translation = exportFiles + QDir::separator() + "translation.js"; + QString stat_file = exportFiles + QDir::separator() + "stat.js"; + exportFiles += "/"; + + if (hes.exportPhotos) { + photosDirectory = exportFiles + QDir::separator() + "photos" + QDir::separator(); + mainDir.mkdir(photosDirectory); + } + + + exportHTMLsettings(json_settings, hes); + exportHTMLstatistics(stat_file, hes); + export_translation(translation.toUtf8().data()); + + export_HTML(qPrintable(json_dive_data), qPrintable(photosDirectory), hes.selectedOnly, hes.listOnly); + + QString searchPath = getSubsurfaceDataPath("theme"); + if (searchPath.isEmpty()) + return; + + 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"); +} diff --git a/divelogexportlogic.h b/divelogexportlogic.h new file mode 100644 index 000000000..84f09c362 --- /dev/null +++ b/divelogexportlogic.h @@ -0,0 +1,20 @@ +#ifndef DIVELOGEXPORTLOGIC_H +#define DIVELOGEXPORTLOGIC_H + +struct htmlExportSetting { + bool exportPhotos; + bool selectedOnly; + bool listOnly; + QString fontFamily; + QString fontSize; + int themeSelection; + bool subsurfaceNumbers; + bool yearlyStatistics; + QString themeFile; +}; + +void file_copy_and_overwrite(const QString &fileName, const QString &newName); +void exportHtmlInitLogic(const QString &filename, struct htmlExportSetting &hes); + +#endif // DIVELOGEXPORTLOGIC_H + diff --git a/qt-ui/divelogexportdialog.cpp b/qt-ui/divelogexportdialog.cpp index b88ebda70..cf48a6226 100644 --- a/qt-ui/divelogexportdialog.cpp +++ b/qt-ui/divelogexportdialog.cpp @@ -4,6 +4,7 @@ #include <QtConcurrent> #include "divelogexportdialog.h" +#include "divelogexportlogic.h" #include "diveshareexportdialog.h" #include "ui_divelogexportdialog.h" #include "subsurfacewebservices.h" @@ -88,56 +89,20 @@ void DiveLogExportDialog::showExplanation() } } -void DiveLogExportDialog::copy_and_overwrite(const QString &fileName, const QString &newName) -{ - QFile file(newName); - if (file.exists()) - file.remove(); - QFile::copy(fileName, newName); -} - void DiveLogExportDialog::exportHtmlInit(const QString &filename) { - QFile file(filename); - QFileInfo info(file); - QDir mainDir = info.absoluteDir(); - mainDir.mkdir(file.fileName() + "_files"); - QString exportFiles = file.fileName() + "_files"; - - QString json_dive_data = exportFiles + QDir::separator() + "file.js"; - QString json_settings = exportFiles + QDir::separator() + "settings.js"; - QString translation = exportFiles + QDir::separator() + "translation.js"; - QString stat_file = exportFiles + QDir::separator() + "stat.js"; - - QString photos_directory; - if (ui->exportPhotos->isChecked()) { - photos_directory = exportFiles + QDir::separator() + "photos" + QDir::separator(); - mainDir.mkdir(photos_directory); - } - exportFiles += "/"; - - exportHTMLsettings(json_settings); - exportHTMLstatistics(stat_file); - export_translation(translation.toUtf8().data()); - - export_HTML(json_dive_data.toUtf8().data(), photos_directory.toUtf8().data(), ui->exportSelectedDives->isChecked(), ui->exportListOnly->isChecked()); - - QString searchPath = getSubsurfaceDataPath("theme"); - if (searchPath.isEmpty()) - return; - - searchPath += QDir::separator(); - - copy_and_overwrite(searchPath + "dive_export.html", filename); - copy_and_overwrite(searchPath + "list_lib.js", exportFiles + "list_lib.js"); - copy_and_overwrite(searchPath + "poster.png", exportFiles + "poster.png"); - copy_and_overwrite(searchPath + "jqplot.highlighter.min.js", exportFiles + "jqplot.highlighter.min.js"); - copy_and_overwrite(searchPath + "jquery.jqplot.min.js", exportFiles + "jquery.jqplot.min.js"); - copy_and_overwrite(searchPath + "jqplot.canvasAxisTickRenderer.min.js", exportFiles + "jqplot.canvasAxisTickRenderer.min.js"); - copy_and_overwrite(searchPath + "jqplot.canvasTextRenderer.min.js", exportFiles + "jqplot.canvasTextRenderer.min.js"); - copy_and_overwrite(searchPath + "jquery.min.js", exportFiles + "jquery.min.js"); - copy_and_overwrite(searchPath + "jquery.jqplot.css", exportFiles + "jquery.jqplot.css"); - copy_and_overwrite(searchPath + (ui->themeSelection->currentText() == tr("Light") ? "light.css" : "sand.css"), exportFiles + "theme.css"); + struct htmlExportSetting hes; + hes.themeFile = (ui->themeSelection->currentText() == tr("Light")) ? "light.css" : "sand.css"; + hes.exportPhotos = ui->exportPhotos->isChecked(); + hes.selectedOnly = ui->exportSelectedDives->isChecked(); + hes.listOnly = ui->exportListOnly->isChecked(); + hes.fontFamily = ui->fontSelection->itemData(ui->fontSelection->currentIndex()).toString(); + hes.fontSize = ui->fontSizeSelection->currentText(); + hes.themeSelection = ui->themeSelection->currentIndex(); + hes.subsurfaceNumbers = ui->exportSubsurfaceNumber->isChecked(); + hes.yearlyStatistics = ui->exportStatistics->isChecked(); + + exportHtmlInitLogic(filename, hes); } void DiveLogExportDialog::exportHTMLsettings(const QString &filename) @@ -153,100 +118,8 @@ void DiveLogExportDialog::exportHTMLsettings(const QString &filename) settings.setValue("exportPhotos", ui->exportPhotos->isChecked()); settings.endGroup(); - QString fontSize = ui->fontSizeSelection->currentText(); - QString fontFamily = ui->fontSelection->itemData(ui->fontSelection->currentIndex()).toString(); - QFile file(filename); - file.open(QIODevice::WriteOnly | QIODevice::Text); - QTextStream out(&file); - out << "settings = {\"fontSize\":\"" << fontSize << "\",\"fontFamily\":\"" << fontFamily << "\",\"listOnly\":\"" - << ui->exportListOnly->isChecked() << "\",\"subsurfaceNumbers\":\"" << ui->exportSubsurfaceNumber->isChecked() << "\","; - //save units preferences - settings.beginGroup("Units"); - if (settings.value("unit_system").toString() == "metric") { - out << "\"unit_system\":\"Meteric\""; - } else if (settings.value("unit_system").toString() == "imperial") { - out << "\"unit_system\":\"Imperial\""; - } else { - QVariant v; - QString length, pressure, volume, temperature, weight; - GET_UNIT("length", length, "FEET", "METER"); - GET_UNIT("pressure", pressure, "PSI", "BAR"); - GET_UNIT("volume", volume, "CUFT", "LITER"); - GET_UNIT("temperature", temperature, "FAHRENHEIT", "CELSIUS"); - GET_UNIT("weight", weight, "LBS", "KG"); - out << "\"unit_system\":\"Personalize\","; - out << "\"units\":{\"depth\":\"" << length << "\",\"pressure\":\"" << pressure << "\",\"volume\":\"" << volume << "\",\"temperature\":\"" << temperature << "\",\"weight\":\"" << weight << "\"}"; - } - out << "}"; - settings.endGroup(); - file.close(); -} - -void DiveLogExportDialog::exportHTMLstatistics(const QString &filename) -{ - QFile file(filename); - file.open(QIODevice::WriteOnly | QIODevice::Text); - QTextStream out(&file); - - stats_t total_stats; - - total_stats.selection_size = 0; - total_stats.total_time.seconds = 0; - - int i = 0; - out << "divestat=["; - if (ui->exportStatistics->isChecked()) { - while (stats_yearly != NULL && stats_yearly[i].period) { - out << "{"; - out << "\"YEAR\":\"" << stats_yearly[i].period << "\","; - out << "\"DIVES\":\"" << stats_yearly[i].selection_size << "\","; - out << "\"TOTAL_TIME\":\"" << get_time_string(stats_yearly[i].total_time.seconds, 0) << "\","; - out << "\"AVERAGE_TIME\":\"" << get_minutes(stats_yearly[i].total_time.seconds / stats_yearly[i].selection_size) << "\","; - out << "\"SHORTEST_TIME\":\"" << get_minutes(stats_yearly[i].shortest_time.seconds) << "\","; - out << "\"LONGEST_TIME\":\"" << get_minutes(stats_yearly[i].longest_time.seconds) << "\","; - out << "\"AVG_DEPTH\":\"" << get_depth_string(stats_yearly[i].avg_depth) << "\","; - out << "\"MIN_DEPTH\":\"" << get_depth_string(stats_yearly[i].min_depth) << "\","; - out << "\"MAX_DEPTH\":\"" << get_depth_string(stats_yearly[i].max_depth) << "\","; - out << "\"AVG_SAC\":\"" << get_volume_string(stats_yearly[i].avg_sac) << "\","; - out << "\"MIN_SAC\":\"" << get_volume_string(stats_yearly[i].min_sac) << "\","; - out << "\"MAX_SAC\":\"" << get_volume_string(stats_yearly[i].max_sac) << "\","; - if ( stats_yearly[i].combined_count ) - out << "\"AVG_TEMP\":\"" << QString::number(stats_yearly[i].combined_temp / stats_yearly[i].combined_count, 'f', 1) << "\","; - else - out << "\"AVG_TEMP\":\"0.0\","; - out << "\"MIN_TEMP\":\"" << ( stats_yearly[i].min_temp == 0 ? 0 : get_temp_units(stats_yearly[i].min_temp, NULL)) << "\","; - out << "\"MAX_TEMP\":\"" << ( stats_yearly[i].max_temp == 0 ? 0 : get_temp_units(stats_yearly[i].max_temp, NULL)) << "\","; - out << "},"; - total_stats.selection_size += stats_yearly[i].selection_size; - total_stats.total_time.seconds += stats_yearly[i].total_time.seconds; - i++; - } - exportHTMLstatisticsTotal(out, &total_stats); - } - out << "]"; - file.close(); } -void exportHTMLstatisticsTotal(QTextStream &out, stats_t *total_stats) -{ - out << "{"; - out << "\"YEAR\":\"Total\","; - out << "\"DIVES\":\"" << total_stats->selection_size << "\","; - out << "\"TOTAL_TIME\":\"" << get_time_string(total_stats->total_time.seconds, 0) << "\","; - 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 << "},"; -} void DiveLogExportDialog::on_exportGroup_buttonClicked(QAbstractButton *button) { diff --git a/qt-ui/divelogexportdialog.h b/qt-ui/divelogexportdialog.h index 14f8f4539..a5b5cc770 100644 --- a/qt-ui/divelogexportdialog.h +++ b/qt-ui/divelogexportdialog.h @@ -33,8 +33,6 @@ private: void showExplanation(); void exportHtmlInit(const QString &filename); void exportHTMLsettings(const QString &filename); - void exportHTMLstatistics(const QString &filename); - void copy_and_overwrite(const QString &fileName, const QString &newName); void export_depths(const char *filename, const bool selected_only); }; |