[recipes/unit_convert_test: 4/10] Got volume conversion working for those who prefer imperial
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [recipes/unit_convert_test: 4/10] Got volume conversion working for those who prefer imperial
- Date: Wed, 12 Jul 2017 22:34:49 +0000 (UTC)
commit 0a50834bd498ab3a336751703f48fa4086820e68
Author: Paxana Amanda Xander <VeganBikePunk Gmail com>
Date: Thu Jun 29 02:42:39 2017 -0700
Got volume conversion working for those who prefer imperial
data/org.gnome.Recipes.gschema.xml | 15 +++++++++++++
src/gr-convert-units.c | 40 +++++++++++++++++++++++++++++------
src/gr-convert-units.h | 4 ++-
src/gr-ingredients-list.c | 7 +-----
src/gr-ingredients-viewer.c | 36 ++++++++++++++++++--------------
src/gr-recipe-formatter.c | 13 ++++++++++-
src/gr-unit.c | 9 ++++++++
src/gr-unit.h | 1 +
8 files changed, 94 insertions(+), 31 deletions(-)
---
diff --git a/data/org.gnome.Recipes.gschema.xml b/data/org.gnome.Recipes.gschema.xml
index c822a6d..40c2391 100644
--- a/data/org.gnome.Recipes.gschema.xml
+++ b/data/org.gnome.Recipes.gschema.xml
@@ -7,6 +7,12 @@
<value nick="locale" value="2"/>
</enum>
+ <enum id="org.gnome.recipes.VolumeUnit">
+ <value nick="metric" value="0"/>
+ <value nick="imperial" value="1"/>
+ <value nick="locale" value="2"/>
+ </enum>
+
<schema path="/org/gnome/recipes/" id="org.gnome.Recipes" gettext-domain="gnome-recipes">
<key type="s" name="user">
@@ -78,6 +84,15 @@
</description>
</key>
+ <key name="volume-unit" enum="org.gnome.recipes.VolumeUnit">
+ <default>'locale'</default>
+ <summary>The setting for which unit temperatures should be displayed in. </summary>
+ <description>
+ The setting for which unit temperatures should be displayed in. Default is 'locale',
+ which means to use the LC_MEASUREMENT category of the current locale to decide.
+ </description>
+ </key>
+
</schema>
</schemalist>
diff --git a/src/gr-convert-units.c b/src/gr-convert-units.c
index da8e712..a759186 100644
--- a/src/gr-convert-units.c
+++ b/src/gr-convert-units.c
@@ -82,20 +82,20 @@ convert_temp (int *num, int *unit, int user_unit)
int num1 = *num;
int unit1 = *unit;
- if (unit1 == user_unit) {
+ /* if (unit1 == user_unit) {
// no conversion needed
}
- else if (unit1 == GR_TEMPERATURE_UNIT_CELSIUS &&
+ else */ if (unit1 == GR_TEMPERATURE_UNIT_CELSIUS &&
user_unit == GR_TEMPERATURE_UNIT_FAHRENHEIT) {
num1 = (num1 * 1.8) + 32;
unit1 = user_unit;
- g_message("temp should be: %i", num1);
+ //g_message("temp should be: %i", num1);
}
else if (unit1 == GR_TEMPERATURE_UNIT_FAHRENHEIT &&
user_unit == GR_TEMPERATURE_UNIT_CELSIUS) {
num1 = (num1 - 32) / 1.8;
unit1 = user_unit;
- g_message("temp should be: %i", num1);
+ //g_message("temp should be: %i", num1);
}
@@ -107,10 +107,36 @@ convert_temp (int *num, int *unit, int user_unit)
}
-/*
+
void
-convert_volume ()
+convert_volume (double *amount, char **unit)
{
+ double amount1 = *amount;
+ char *unit1 = *unit;
+
+ g_message("%f is the amount in convert-unit", amount1);
+ g_message("%s is the unit in convert-unit", unit1);
+
+ int user_volume_unit = get_volume_unit();
+
+ if (user_volume_unit == 1) {
+ if (strcmp(unit1, "ml") == 0)
+ {
+ amount1 = (amount1 / 4.92892);
+ unit1 = "tsp";
+ }
+ else if (strcmp(unit1, "dl") == 0)
+ {
+ amount1 = (amount1 / 0.422675);
+ unit1 = "cup";
+ }
+ else if (strcmp(unit1, "l") == 0)
+ {
+ amount1 = (amount1 * 4.22675);
+ unit1 = "cup";
+ }
+ }
+ *amount = amount1;
+ *unit = unit1;
}
-*/
\ No newline at end of file
diff --git a/src/gr-convert-units.h b/src/gr-convert-units.h
index d6d61d5..72906a4 100644
--- a/src/gr-convert-units.h
+++ b/src/gr-convert-units.h
@@ -41,7 +41,9 @@ typedef enum {
} GrVolumeUnit;
int get_temperature_unit (void);
-void convert_temp (int *num, int *unit, int user_unit);
+void convert_temp (int *num, int *unit, int user_unit);
+void convert_volume (double *amount, char **unit);
+
diff --git a/src/gr-ingredients-list.c b/src/gr-ingredients-list.c
index 53b7549..2a20034 100644
--- a/src/gr-ingredients-list.c
+++ b/src/gr-ingredients-list.c
@@ -164,17 +164,12 @@ gr_ingredients_list_validate (const char *text,
}
static void
-<<<<<<< fd7e9ce6bc998a11c0b79f4b509fea298bf54776
-ingredient_scale_unit (Ingredient *ing, double scale, GString *s)
-=======
-ingredient_scale_unit (IngredientObj *ing, int num, int denom, GString *s)
->>>>>>> Attempt at moving amount to double, unit failing
+ingredient_scale_unit (IngredientObj *ing, double scale, GString *s)
{
g_autofree char *scaled = NULL;
scaled = gr_number_format (scale * ing->amount);
- g_string_append (s, scaled);
if (ing->unit) {
g_string_append (s, " ");
g_string_append (s, ing->unit);
diff --git a/src/gr-ingredients-viewer.c b/src/gr-ingredients-viewer.c
index fdb4a3c..7ed4bd0 100644
--- a/src/gr-ingredients-viewer.c
+++ b/src/gr-ingredients-viewer.c
@@ -28,6 +28,8 @@
#include "gr-ingredient.h"
#include "gr-utils.h"
#include "gr-number.h"
+#include "gr-convert-units.h"
+#include "gr-unit.h"
#ifdef ENABLE_GSPELL
#include <gspell/gspell.h>
@@ -320,27 +322,29 @@ gr_ingredients_viewer_set_ingredients (GrIngredientsViewer *viewer,
g_autofree char *s = NULL;
g_auto(GStrv) strv = NULL;
double amount;
- const char *unit;
+ char *unit;
GtkWidget *row;
+ const char *measure;
-<<<<<<< 5304691ced802de6f16064e89f48e6c38b5de0df
- s = gr_ingredients_list_scale_unit (ingredients, viewer->title, ings[i], viewer->scale);
- //strv = g_strsplit (s, " ", 2);
- //amount = strv[0];
- unit = strv[1] ? strv[1] : "";
-=======
- s = gr_ingredients_list_scale_unit (ingredients, viewer->title, ings[i], viewer->scale_num,
viewer->scale_denom);
->>>>>>> got convert-temp working, need to work on scaling the display
- unit = gr_ingredients_list_get_unit(ingredients, viewer->title, ings[i]);
- amount = gr_ingredients_list_get_amount(ingredients, viewer->title, ings[i]);
-
- //strv = g_strsplit (s, " ", 2);
- //amount = strv[0];
- //unit = strv[1] ? strv[1] : "";
+ double scale = viewer->scale;
+ unit = gr_ingredients_list_get_unit(ingredients, viewer->title, ings[i]);
+ amount = (gr_ingredients_list_get_amount(ingredients, viewer->title, ings[i]) * scale);
+ measure = gr_unit_get_measure(unit);
+ //gr_ingredients_list_scale_unit (&amount, viewer->scale);
+ g_message("%f is the amount in the viewer", amount);
+
+
+ if (measure) {
+ if (strcmp(measure, "volume") == 0) {
+ g_message ("measure is %s", measure);
+ convert_volume(&amount, &unit);
+ }
+ }
g_message("segment is %s", viewer->title);
- g_message("amount is %f", amount);
+
g_message("unit is %s", unit);
+ //g_message ("measure is %s", measure);
row = g_object_new (GR_TYPE_INGREDIENTS_VIEWER_ROW,
"amount", amount,
diff --git a/src/gr-recipe-formatter.c b/src/gr-recipe-formatter.c
index 6265ae4..1430221 100644
--- a/src/gr-recipe-formatter.c
+++ b/src/gr-recipe-formatter.c
@@ -176,7 +176,7 @@ gr_recipe_parse_instructions (const char *instructions,
unit = GR_TEMPERATURE_UNIT_CELSIUS;
}
num = atoi (p + strlen ("[temperature:"));
- g_message("num before convert: %i \n unit before convert: %s", num, unit);
+ g_message("num before convert: %i \n unit before convert: %i", num, unit);
convert_temp(&num, &unit, user_unit);
g_message("num after convert: %i \n unit after convert: %i", num, unit);
@@ -198,6 +198,17 @@ gr_recipe_parse_instructions (const char *instructions,
prefix = g_strndup (step, p - step);
q = strstr (p, "]");
+ }
+
+ p = strstr (step, "[image:");
+ if (p) {
+ g_autofree char *prefix = NULL;
+ char *tmp;
+
+ image = atoi (p + strlen ("[image:"));
+
+ prefix = g_strndup (step, p - step);
+ q = strstr (p, "]");
tmp = g_strconcat (prefix, q + 1, NULL);
g_free (step);
step = tmp;
diff --git a/src/gr-unit.c b/src/gr-unit.c
index b9b95c1..1630c17 100644
--- a/src/gr-unit.c
+++ b/src/gr-unit.c
@@ -112,6 +112,15 @@ gr_unit_get_abbreviation (const char *name)
}
const char *
+gr_unit_get_measure (const char *name)
+{
+ GrUnit *unit = find_unit (name);
+ if (unit)
+ return g_dpgettext2 (NULL, "unit measure", unit->measure);
+ return NULL;
+}
+
+const char *
gr_unit_parse (char **input,
GError **error)
{
diff --git a/src/gr-unit.h b/src/gr-unit.h
index 46d8278..372e750 100644
--- a/src/gr-unit.h
+++ b/src/gr-unit.h
@@ -29,5 +29,6 @@ const char **gr_unit_get_names (void);
const char *gr_unit_get_abbreviation (const char *name);
const char *gr_unit_get_display_name (const char *name);
const char *gr_unit_get_plural (const char *name);
+const char *gr_unit_get_measure(const char *name);
G_END_DECLS
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]