I attached three patches, so the HTML exported files now support imperial and metric units based on subsurface selected preferences.
The main Javascript file still needs some re-factoring and cleaning that I will be working on them for more consistency. Regards, Gehad
From 1dd3d36716e88eb294cd639f8d68631d3d9be3c3 Mon Sep 17 00:00:00 2001 From: Gehad elrobey <[email protected]> Date: Mon, 20 Oct 2014 03:56:06 +0200 Subject: [PATCH 3/3] HTML: Fix the Javascript library to support multi units. -make the dive profile dynamic to check user selected units from the settings file. -some of the code needs to be refactored and copied to a more appropriate location. Signed-off-by: Gehad elrobey <[email protected]> --- theme/dive_export.html | 2 + theme/list_lib.js | 115 +++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 105 insertions(+), 12 deletions(-) diff --git a/theme/dive_export.html b/theme/dive_export.html index 0b5b6dc..6f3708c 100644 --- a/theme/dive_export.html +++ b/theme/dive_export.html @@ -132,6 +132,8 @@ window.onload=function(){ document.onkeydown = switchDives; + set_units(); + //translate Page translate_page(); getDefaultColor(); diff --git a/theme/list_lib.js b/theme/list_lib.js index c547b09..b2dc13b 100644 --- a/theme/list_lib.js +++ b/theme/list_lib.js @@ -814,7 +814,7 @@ function lastNonZero() */ function get_weight_HTML(weight) { - return '<tr><td class="Cyl">' + gram_to_km(weight.weight) + ' kg ' + '</td><td class="Cyl">' + weight.description + '</td></tr>'; + return '<tr><td class="Cyl">' + weight.weight + '</td><td class="Cyl">' + weight.description + '</td></tr>'; } /** @@ -843,12 +843,12 @@ function get_cylinder_HTML(cylinder) var cEPressure = cylinder.EPressure; if (cSPressure === "--") { - cSPressure = Math.round(mbar_to_bar(items[dive_id].samples[0][2])).toFixed(1) + " bar"; + cSPressure = Math.round(put_pressure_unit(items[dive_id].samples[0][2])).toFixed(1) + " " + pressure_unit; } if (cEPressure === "--") { var nonZeroCEPressure = lastNonZero(); - cEPressure = Math.round(mbar_to_bar(nonZeroCEPressure)).toFixed(1) + " bar"; + cEPressure = Math.round(put_pressure_unit(nonZeroCEPressure)).toFixed(1) + " " + pressure_unit; } return '<tr><td class="Cyl">' + cylinder.Type + '</td><td class="Cyl">' + cylinder.Size + '</td><td class="Cyl">' + cylinder.WPressure + '</td>' + '<td class="Cyl">' + cSPressure + '</td><td class="Cyl">' + cEPressure + '</td><td class="Cyl">' + cylinder.O2 + '</td></tr>'; @@ -969,8 +969,8 @@ function put_divecomputer_details(dc) */ function get_status_HTML(dive) { - return '<h2 class="det_hed">' + translate.Dive_Status + '</h2><table><tr><td class="words">SAC: </td><td>' + ml_to_litre(dive.sac) + - ' l/min' + '</td><td class="words"> OTU: </td><td>' + dive.otu + + return '<h2 class="det_hed">' + translate.Dive_Status + '</h2><table><tr><td class="words">SAC: </td><td>' + put_volume_unit(dive.sac).toFixed(2) + ' ' + volume_unit + + '/min' + '</td><td class="words"> OTU: </td><td>' + dive.otu + '</td><td class="words"> CNS: </td><td>' + dive.cns + '</td></tr></table>'; }; @@ -1016,16 +1016,31 @@ function mkelvin_to_C(mkelvin) return (mkelvin - ZERO_C_IN_MKELVIN) / 1000.0; } +function mkelvin_to_F(mkelvin) +{ + return mkelvin * 9 / 5000.0 - 459.670; +} + function mbar_to_bar(mbar) { return mbar / 1000; } +function mbar_to_psi(mbar) +{ + return (mbar * 0.0145037738); +} + function mm_to_meter(mm) { return mm / (1000); } +function mm_to_feet(mm) +{ + return mm * 0.00328084; +} + function gram_to_km(gram) { return (gram / 1000).toFixed(1); @@ -1036,6 +1051,11 @@ function ml_to_litre(ml) return ml / (1000); } +function ml_to_cuft(ml) +{ + return ml / 28316.8466; +} + function format_two_digit(n) { return n > 9 ? "" + n : "0" + n; @@ -1054,6 +1074,11 @@ function float_to_deg(flt){ return deg + "° " + min + "' " + sec + "\""; } +var depth_unit = "m"; +var temperature_unit = "C"; +var pressure_unit = "bar"; +var volume_unit = "l"; + /** *Main canvas draw function *this calls the axis and grid initialization functions. @@ -1069,25 +1094,25 @@ function canvas_draw() for (var i = 0; i < items[dive_id].samples.length; i++) { depthData.push([ items[dive_id].samples[i][0] / 60, - -1 * mm_to_meter(items[dive_id].samples[i][1]) + -1 * put_depth_unit(items[dive_id].samples[i][1]) ]); if (items[dive_id].samples[i][2] !== 0) { pressureData.push([ items[dive_id].samples[i][0] / 60, - mbar_to_bar(items[dive_id].samples[i][2]) + put_pressure_unit(items[dive_id].samples[i][2]) ]); } if (items[dive_id].samples[i][3] !== 0) { temperatureData.push([ items[dive_id].samples[i][0] / 60, - mkelvin_to_C(items[dive_id].samples[i][3]) + put_temperature_unit(items[dive_id].samples[i][3]) ]); last = items[dive_id].samples[i][3]; } else { if (last !== 0) { temperatureData.push([ items[dive_id].samples[i][0] / 60, - mkelvin_to_C(last) + put_temperature_unit(last) ]); } } @@ -1174,7 +1199,7 @@ function canvas_draw() max : 0, tickRenderer : $.jqplot.CanvasAxisTickRenderer, tickOptions : { - formatter: function(format, value) { return -1 * value + "m"; }, + formatter: function(format, value) { return -1 * value + " " + depth_unit;}, formatString : '%.2fm' }, pad : 2.05 @@ -1183,7 +1208,7 @@ function canvas_draw() min : 0, tickRenderer : $.jqplot.CanvasAxisTickRenderer, tickOptions : { - formatString : '%ibar' + formatString : '%i '+pressure_unit }, pad : 3.05 }, @@ -1195,7 +1220,7 @@ function canvas_draw() showMark: false, showLabel: false, shadow: false, - formatString : '%i C' + formatString : '%i ' + temperature_unit } } } @@ -1310,3 +1335,69 @@ function translate_page() document.getElementById("bk_to_ls_lbl").innerHTML = translate.Back_to_List; document.getElementById("bk_to_ls_lbl2").innerHTML = translate.Back_to_List; } + +function set_units() +{ + if (settings.unit_system == "Imperial") { + depth_unit = "ft"; + temperature_unit = "F"; + pressure_unit = "psi"; + volume_unit = "cuft"; + } else if (settings.unit_system == "Meteric") { + depth_unit = "m"; + temperature_unit = "C"; + pressure_unit = "bar"; + volume_unit = "l"; + } else if (settings.unit_system == "Personalize") { + depth_unit = settings.units.depth == "METER"?"m":"ft"; + pressure_unit = settings.units.pressure == "BAR"?"bar":"psi"; + temperature_unit = settings.units.temperature == "CELSIUS"?"C":"F"; + volume_unit = settings.units.volume == "CUFT"?"cuft":"l"; + } + // meteric by default +} + +function put_volume_unit(ml) +{ + if (settings.unit_system == "Imperial") + return ml_to_cuft(ml); + else if (settings.unit_system == "Personalize" && settings.units.volume == "CUFT") + return ml_to_cuft(ml); + return ml_to_litre(ml); +} + +function put_weight_unit(grams) +{ + if (settings.unit_system == "Imperial") + return grams_to_lbs(ml); + else if (settings.unit_system == "Personalize" && settings.units.weight == "LBS") + return grams_to_lbs(ml); + return grams_to_kgs(ml); +} + +function put_temperature_unit(mkelvin) +{ + if (settings.unit_system == "Imperial") + return mkelvin_to_F(mkelvin); + else if (settings.unit_system == "Personalize" && settings.units.temperature == "FAHRENHEIT") + return mkelvin_to_F(mkelvin); + return mkelvin_to_C(mkelvin); +} + +function put_depth_unit(mm) +{ + if (settings.unit_system == "Imperial") + return mm_to_feet(mm).toFixed(1); + else if (settings.unit_system == "Personalize" && settings.units.depth == "FEET") + return mm_to_feet(mm).toFixed(1); + return mm_to_meter(mm); +} + +function put_pressure_unit(mbar) +{ + if (settings.unit_system == "Imperial") + return mbar_to_psi(mbar); + else if (settings.unit_system == "Personalize" && settings.units.pressure == "PSI") + return mbar_to_psi(mbar); + return mbar_to_bar(mbar); +} -- 1.9.1
From 10d4e5b8c0f17c0360d8a0545d18bbec20023335 Mon Sep 17 00:00:00 2001 From: Gehad elrobey <[email protected]> Date: Sun, 19 Oct 2014 19:30:11 +0200 Subject: [PATCH 2/3] HTML: Save data with user selected units Edit the HTML exporter to export data fields with user selected units. Signed-off-by: Gehad elrobey <[email protected]> --- save-html.c | 44 +++++++++++++++++++++++++++++++++++++++----- save-html.h | 3 +++ 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/save-html.c b/save-html.c index 2befb41..f8ef172 100644 --- a/save-html.c +++ b/save-html.c @@ -99,7 +99,7 @@ static void put_weightsystem_HTML(struct membuffer *b, struct dive *dive) put_string(b, separator); separator = ", "; put_string(b, "{"); - put_format(b, "\"weight\":\"%d\",", grams); + put_HTML_weight_units(b, grams, "\"weight\":\"", "\","); write_attribute(b, "description", description, " "); put_string(b, "}"); } @@ -121,20 +121,20 @@ static void put_cylinder_HTML(struct membuffer *b, struct dive *dive) separator = ", "; write_attribute(b, "Type", cylinder->type.description, ", "); if (cylinder->type.size.mliter) { - put_milli(b, "\"Size\":\"", cylinder->type.size.mliter, " l\", "); + put_HTML_volume_units(b, cylinder->type.size.mliter, "\"Size\":\"", " \", "); } else { write_attribute(b, "Size", "--", ", "); } - put_pressure(b, cylinder->type.workingpressure, "\"WPressure\":\"", " bar\", "); + put_HTML_pressure_units(b, cylinder->type.workingpressure, "\"WPressure\":\"", " \", "); if (cylinder->start.mbar) { - put_milli(b, "\"SPressure\":\"", cylinder->start.mbar, " bar\", "); + put_HTML_pressure_units(b, cylinder->start, "\"SPressure\":\"", " \", "); } else { write_attribute(b, "SPressure", "--", ", "); } if (cylinder->end.mbar) { - put_milli(b, "\"EPressure\":\"", cylinder->end.mbar, " bar\", "); + put_HTML_pressure_units(b, cylinder->end, "\"EPressure\":\"", " \", "); } else { write_attribute(b, "EPressure", "--", ", "); } @@ -209,6 +209,40 @@ void put_HTML_notes(struct membuffer *b, struct dive *dive, const char *pre, con put_string(b, post); } +void put_HTML_pressure_units(struct membuffer *b, pressure_t pressure, const char *pre, const char *post) +{ + const char *unit; + double value; + + if (!pressure.mbar) { + put_format(b, "%s%s", pre, post); + return; + } + + value = get_pressure_units(pressure.mbar, &unit); + put_format(b, "%s%.1f %s%s", pre, value, unit, post); +} + +void put_HTML_volume_units(struct membuffer *b, unsigned int ml, const char *pre, const char *post) +{ + const char *unit; + double value; + int frac; + + value = get_volume_units(ml, &frac, &unit); + put_format(b, "%s%.1f %s%s", pre, value, unit, post); +} + +void put_HTML_weight_units(struct membuffer *b, unsigned int grams, const char *pre, const char *post) +{ + const char *unit; + double value; + int frac; + + value = get_weight_units(grams, &frac, &unit); + put_format(b, "%s%.1f %s%s", pre, value, unit, post); +} + void put_HTML_time(struct membuffer *b, struct dive *dive, const char *pre, const char *post) { struct tm tm; diff --git a/save-html.h b/save-html.h index fddbf26..3ed757f 100644 --- a/save-html.h +++ b/save-html.h @@ -14,6 +14,9 @@ void put_HTML_watertemp(struct membuffer *b, struct dive *dive, const char *pre, void put_HTML_time(struct membuffer *b, struct dive *dive, const char *pre, const char *post); void put_HTML_notes(struct membuffer *b, struct dive *dive, const char *pre, const char *post); void put_HTML_quoted(struct membuffer *b, const char *text); +void put_HTML_pressure_units(struct membuffer *b, pressure_t pressure, const char *pre, const char *post); +void put_HTML_weight_units(struct membuffer *b, unsigned int grams, const char *pre, const char *post); +void put_HTML_volume_units(struct membuffer *b, unsigned int ml, const char *pre, const char *post); void export_HTML(const char *file_name, const char *photos_dir, const bool selected_only, const bool list_only); void export_list(struct membuffer *b, const char *photos_dir, bool selected_only, const bool list_only); -- 1.9.1
From 36234b515c479b0b24b4dc53c00e5e020d18a001 Mon Sep 17 00:00:00 2001 From: Gehad elrobey <[email protected]> Date: Fri, 17 Oct 2014 22:46:33 +0200 Subject: [PATCH 1/3] HTML: Export Units preferences to settings file Working on HTML exports to support imperial and metric units and also custom selected units based on subsurface preferences. User selected units is exported to settings file that will be mainly used by listlib javascript file. Signed-off-by: Gehad elrobey <[email protected]> --- qt-ui/divelogexportdialog.cpp | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/qt-ui/divelogexportdialog.cpp b/qt-ui/divelogexportdialog.cpp index 6b847a4..56bc8eb 100644 --- a/qt-ui/divelogexportdialog.cpp +++ b/qt-ui/divelogexportdialog.cpp @@ -17,6 +17,13 @@ #include "helpers.h" #include "statistics.h" +#define GET_UNIT(name, field, f, t) \ + v = settings.value(QString(name)); \ + if (v.isValid()) \ + field = (v.toInt() == 0) ? (t) : (f); \ + else \ + field = default_prefs.units.field + DiveLogExportDialog::DiveLogExportDialog(QWidget *parent) : QDialog(parent), ui(new Ui::DiveLogExportDialog) { @@ -149,7 +156,26 @@ void DiveLogExportDialog::exportHTMLsettings(const QString &filename) file.open(QIODevice::WriteOnly | QIODevice::Text); QTextStream out(&file); out << "settings = {\"fontSize\":\"" << fontSize << "\",\"fontFamily\":\"" << fontFamily << "\",\"listOnly\":\"" - << ui->exportListOnly->isChecked() << "\",\"subsurfaceNumbers\":\"" << ui->exportSubsurfaceNumber->isChecked() << "\",}"; + << 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(); } -- 1.9.1
_______________________________________________ subsurface mailing list [email protected] http://lists.subsurface-divelog.org/cgi-bin/mailman/listinfo/subsurface
