[recipes/unit_convert_test: 8/10] Added unit ID numbers, moved to switch-case instead of if-thens. human-readability function working
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [recipes/unit_convert_test: 8/10] Added unit ID numbers, moved to switch-case instead of if-thens. human-readability function working
- Date: Wed, 12 Jul 2017 22:35:09 +0000 (UTC)
commit 6a9f2fe2d2dd8488d90751bc104f8f21edec84ff
Author: Paxana Amanda Xander <VeganBikePunk Gmail com>
Date: Wed Jul 12 14:29:12 2017 -0700
Added unit ID numbers, moved to switch-case instead of if-thens. human-readability function working for
some units not others
.vscode/settings.json | 10 ++
...getting-the-double-to-display-and-scale-p.patch | 149 ++++++++++++++++++++
src/gr-convert-units.c | 89 +++++++++++-
src/gr-ingredients-viewer.c | 8 +-
src/gr-number.c | 2 +-
src/gr-unit.c | 60 +++++----
src/gr-unit.h | 28 ++++
7 files changed, 310 insertions(+), 36 deletions(-)
---
diff --git a/.vscode/settings.json b/.vscode/settings.json
new file mode 100644
index 0000000..18beda3
--- /dev/null
+++ b/.vscode/settings.json
@@ -0,0 +1,10 @@
+{
+ "files.associations": {
+ "gr-ingredients-viewer.c": "cpp",
+ "gr-convert-units.c": "cpp",
+ "gr-unit.c": "cpp",
+ "gr-ingredients-viewer-row.c": "cpp",
+ "gr-shopping-page.c": "cpp",
+ "gr-ingredients-list.c": "cpp"
+ }
+}
\ No newline at end of file
diff --git a/0001-working-on-getting-the-double-to-display-and-scale-p.patch
b/0001-working-on-getting-the-double-to-display-and-scale-p.patch
new file mode 100644
index 0000000..3856899
--- /dev/null
+++ b/0001-working-on-getting-the-double-to-display-and-scale-p.patch
@@ -0,0 +1,149 @@
+From 36daa31ab2032b63402c8501bcebbb249fc9355a Mon Sep 17 00:00:00 2001
+From: Paxana Amanda Xander <VeganBikePunk Gmail com>
+Date: Mon, 26 Jun 2017 14:07:31 -0700
+Subject: [PATCH] working on getting the double to display and scale properly
+
+---
+ src/gr-ingredients-viewer-row.c | 38 +++++++++++++++++++-------------------
+ src/gr-ingredients-viewer.c | 4 ++++
+ 2 files changed, 23 insertions(+), 19 deletions(-)
+
+diff --git a/src/gr-ingredients-viewer-row.c b/src/gr-ingredients-viewer-row.c
+index 821afa0..5151650 100644
+--- a/src/gr-ingredients-viewer-row.c
++++ b/src/gr-ingredients-viewer-row.c
+@@ -50,7 +50,7 @@ struct _GrIngredientsViewerRow
+ GtkEntryCompletion *unit_completion;
+ GtkCellRenderer *unit_cell;
+
+- char *amount;
++ double amount;
+ char *unit;
+ char *ingredient;
+
+@@ -88,7 +88,7 @@ gr_ingredients_viewer_row_finalize (GObject *object)
+ {
+ GrIngredientsViewerRow *self = GR_INGREDIENTS_VIEWER_ROW (object);
+
+- g_free (self->amount);
++ //g_free (self->amount);
+ g_free (self->unit);
+ g_free (self->ingredient);
+
+@@ -108,7 +108,7 @@ gr_ingredients_viewer_row_get_property (GObject *object,
+ switch (prop_id)
+ {
+ case PROP_AMOUNT:
+- g_value_set_string (value, self->amount);
++ g_value_set_double (value, self->amount);
+ break;
+
+ case PROP_UNIT:
+@@ -140,14 +140,14 @@ static void
+ update_unit (GrIngredientsViewerRow *row)
+ {
+ g_autofree char *tmp = NULL;
+- const char *amount;
++ double amount;
+ const char *space;
+ const char *unit;
+
+- amount = row->amount ? row->amount : "";
+- space = amount[0] ? " " : "";
++ amount = row->amount ? row->amount : 0;
++ //space = amount[0] ? " " : "";
+ unit = row->unit ? row->unit : "";
+- tmp = g_strdup_printf ("%s%s%s", amount, space, unit);
++ tmp = g_strdup_printf ("%f %s", amount, unit);
+ if (tmp[0] == '\0' && row->editable) {
+ gtk_style_context_add_class (gtk_widget_get_style_context (row->unit_label), "dim-label");
+ gtk_label_set_label (GTK_LABEL (row->unit_label), _("Amount…"));
+@@ -173,10 +173,10 @@ update_ingredient (GrIngredientsViewerRow *row)
+
+ static void
+ gr_ingredients_viewer_row_set_amount (GrIngredientsViewerRow *row,
+- const char *amount)
++ double amount)
+ {
+- g_free (row->amount);
+- row->amount = g_strdup (amount);
++ //g_free (row->amount);
++ row->amount = amount;
+ update_unit (row);
+ }
+
+@@ -245,7 +245,7 @@ gr_ingredients_viewer_row_set_property (GObject *object,
+ switch (prop_id)
+ {
+ case PROP_AMOUNT:
+- gr_ingredients_viewer_row_set_amount (self, g_value_get_string (value));
++ gr_ingredients_viewer_row_set_amount (self, g_value_get_double (value));
+ break;
+
+ case PROP_UNIT:
+@@ -306,14 +306,14 @@ static void
+ edit_unit (GrIngredientsViewerRow *row)
+ {
+ g_autofree char *tmp = NULL;
+- const char *amount;
++ double amount;
+ const char *space;
+ const char *unit;
+
+- amount = row->amount ? row->amount : "";
+- space = amount[0] ? " " : "";
++ amount = row->amount ? row->amount : 0;
++ //space = amount[0] ? " " : "";
+ unit = row->unit ? row->unit : "";
+- tmp = g_strdup_printf ("%s%s%s", amount, space, unit);
++ tmp = g_strdup_printf ("%f%s%s", amount, space, unit);
+
+ save_ingredient (row);
+
+@@ -356,7 +356,7 @@ parse_unit (const char *text,
+ return FALSE;
+ }
+
+- *amount = gr_number_format (number);
++ amount = gr_number_format (number);
+ skip_whitespace (&tmp);
+ if (tmp)
+ *unit = g_strdup (tmp);
+@@ -481,8 +481,8 @@ gr_ingredients_viewer_row_class_init (GrIngredientsViewerRowClass *klass)
+ object_class->get_property = gr_ingredients_viewer_row_get_property;
+ object_class->set_property = gr_ingredients_viewer_row_set_property;
+
+- pspec = g_param_spec_string ("amount", NULL, NULL,
+- NULL,
++ pspec = g_param_spec_double ("amount", NULL, NULL,
++ 0.0, G_MAXDOUBLE, 1.0,
+ G_PARAM_READWRITE);
+ g_object_class_install_property (object_class, PROP_AMOUNT, pspec);
+
+@@ -812,7 +812,7 @@ match_selected (GtkEntryCompletion *completion,
+
+ parse_unit (gtk_entry_get_text (GTK_ENTRY (row->unit_entry)), &amount, &unit);
+
+- tmp = g_strdup_printf ("%s %s", amount, abbrev);
++ tmp = g_strdup_printf ("%f %s", amount, abbrev);
+
+ gtk_entry_set_text (GTK_ENTRY (row->unit_entry), tmp);
+
+diff --git a/src/gr-ingredients-viewer.c b/src/gr-ingredients-viewer.c
+index 9f5a42d..d49a468 100644
+--- a/src/gr-ingredients-viewer.c
++++ b/src/gr-ingredients-viewer.c
+@@ -330,6 +330,10 @@ gr_ingredients_viewer_set_ingredients (GrIngredientsViewer *viewer,
+ //s = gr_ingredients_list_scale_unit (ingredients, viewer->title, ings[i],
viewer->scale_num, viewer->scale_denom);
+ 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] : "";
+
+ g_message("segment is %s", viewer->title);
+ g_message("amount is %f", amount);
+--
+2.9.3
+
diff --git a/src/gr-convert-units.c b/src/gr-convert-units.c
index 60bb941..5c71029 100644
--- a/src/gr-convert-units.c
+++ b/src/gr-convert-units.c
@@ -190,7 +190,6 @@ convert_volume (double *amount, char **unit)
unit1 = "ml";
}
}
-
*amount = amount1;
*unit = unit1;
}
@@ -208,7 +207,7 @@ convert_weight (double *amount, char **unit)
if (strcmp(unit1, "g") == 0)
{
amount1 = (amount1 * 0.035274);
- unit1 = "oz";
+ unit1 = "oz";
}
else if (strcmp(unit1, "kg") == 0)
{
@@ -246,31 +245,109 @@ human_readable (double *amount, char **unit)
double amount1 = *amount;
char *unit1 = *unit;
+ int id = gr_unit_get_id_number(*unit);
+ g_message("UNIT is %s", unit1);
+ g_message("ID is %i", id);
+ g_message("amount is %f", amount1);
+
+ switch (id) {
+ case GR_UNIT_GRAM:
+ if (amount1 >= 1000) {
+ amount1 = (amount1 / 1000);
+ unit1 = "kg";
+ }
+ break;
+
+ case GR_UNIT_OUNCE:
+ if (amount1 >= 16) {
+ amount1 = (amount1 / 16);
+ unit1 = "lb";
+ }
+ break;
+
+ case GR_UNIT_TEASPOON:
+ if (amount1 >= 3) {
+ amount1 = (amount1 / 3);
+ unit1 = "tbsp";
+ }
+ break;
+
+ case GR_UNIT_TABLESPOON:
+ if (amount1 >= 16) {
+ amount1 = (amount1 / 16);
+ unit1 = "cup";
+ }
+
+ else if (amount1 < 3) {
+ g_message("Here I am");
+ amount1 = (amount1 * 3);
+ unit1 = "tsp";
+ }
+
+ break;
+
+ case GR_UNIT_CUP:
+ if (amount1 >= 4) {
+ amount1 = (amount1 / 4);
+ unit1 = "qt";
+ }
+
+ else if (amount1 < 1) {
+ amount1 = amount1 / 16;
+ unit1 = "tbsp";
+ }
+ break;
+
+ default:
+ g_message("default");
+ }
+
+ *amount = amount1;
+ *unit = unit1;
+}
+
+/*
if ((strcmp(unit1, "g") == 0) && (amount1 >= 1000) )
{
amount1 = (amount1 / 1000);
unit1 = "kg";
}
- else if ((strcmp(unit1, "oz") == 0) && (amount1 >= 16) )
+ if ((strcmp(unit1, "oz") == 0) && (amount1 >= 16) )
{
amount1 = (amount1 / 16);
unit1 = "lb";
}
- else if ((strcmp(unit1, "tsp") == 0) && (amount1 >= 3) )
+ if ((strcmp(unit1, "tsp") == 0) && (amount1 >= 3) )
{
amount1 = (amount1 / 3);
unit1 = "tbsp";
}
- else if ((strcmp(unit1, "tbsp") == 0) && (amount1 >= 16) )
+ if ((strcmp(unit1, "tbsp") == 0) && (amount1 >= 16) )
{
amount1 = (amount1 / 16);
unit1 = "cup";
}
+ if ((strcmp(unit1, "cup") == 0) && (amount1 >= 4) )
+ {
+ amount1 = (amount1 / 4);
+ unit1 = "qt";
+ }
+ if ((strcmp(unit1, "qt") == 0) && (amount1 >= 4) )
+ {
+ amount1 = (amount1 / 4);
+ unit1 = "gal";
+ }
+ if ((strcmp(unit1, "ml") == 0) && (amount1 >= 1000) )
+ {
+ amount1 = (amount1 / 1000);
+ unit1 = "l";
+ }
*amount = amount1;
*unit = unit1;
-}
+}
+ */
void
round_amount (double *amount, char **unit)
{
diff --git a/src/gr-ingredients-viewer.c b/src/gr-ingredients-viewer.c
index 7344c07..f4cec9a 100644
--- a/src/gr-ingredients-viewer.c
+++ b/src/gr-ingredients-viewer.c
@@ -328,9 +328,9 @@ gr_ingredients_viewer_set_ingredients (GrIngredientsViewer *viewer,
const char *endAmount;
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);
+ amount = gr_ingredients_list_get_amount(ingredients, viewer->title, ings[i]) * scale;
measure = gr_unit_get_measure(unit);
if (measure) {
@@ -341,9 +341,9 @@ gr_ingredients_viewer_set_ingredients (GrIngredientsViewer *viewer,
convert_weight(&amount, &unit);
}
}
+
+ human_readable(&amount, &unit);
- human_readable(&amount, &unit);
-
endAmount = gr_number_format(amount);
row = g_object_new (GR_TYPE_INGREDIENTS_VIEWER_ROW,
diff --git a/src/gr-number.c b/src/gr-number.c
index 3ccac82..d0a58e8 100644
--- a/src/gr-number.c
+++ b/src/gr-number.c
@@ -1,4 +1,4 @@
-/* gr-number.c
+ /* gr-number.c
*
* Copyright (C) 2016 Matthias Clasen <mclasen redhat com>
*
diff --git a/src/gr-unit.c b/src/gr-unit.c
index 1630c17..cf1a156 100644
--- a/src/gr-unit.c
+++ b/src/gr-unit.c
@@ -36,33 +36,34 @@ typedef struct {
const char *display_name;
const char *plural;
const char *measure;
+ int id_number;
} GrUnit;
static GrUnit units[] = {
- { "g", NC_("unit abbreviation", "g"), NC_("unit name", "gram"), NC_("unit plural", "grams"),
NC_("unit measure", "weight") },
- { "kg", NC_("unit abbreviation", "kg"), NC_("unit name", "kilogram"), NC_("unit plural",
"kilograms"), NC_("unit measure", "weight") },
- { "lb", NC_("unit abbreviation", "lb"), NC_("unit name", "pound"), NC_("unit plural",
"pounds"), NC_("unit measure", "weight") },
- { "oz", NC_("unit abbreviation", "oz"), NC_("unit name", "ounce"), NC_("unit plural",
"ounces"), NC_("unit measure", "weight") },
- { "l", NC_("unit abbreviation", "l"), NC_("unit name", "liter"), NC_("unit plural",
"liters"), NC_("unit measure", "volume") },
- { "dl", NC_("unit abbreviation", "dl"), NC_("unit name", "deciliter"), NC_("unit plural",
"deciliters"), NC_("unit measure", "volume") },
- { "ml", NC_("unit abbreviation", "ml"), NC_("unit name", "milliliter"), NC_("unit plural",
"milliliters"), NC_("unit measure", "volume") },
- { "fl oz", NC_("unit abbreviation", "fl oz"), NC_("unit name", "fluid ounce"), NC_("unit plural",
"fluid ounces"), NC_("unit measure", "volume") },
- { "fl. oz.", NC_("unit abbreviation", "fl oz"), NC_("unit name", "fluid ounce"), NC_("unit plural",
"fluid ounces"), NC_("unit measure", "volume") },
- { "pt", NC_("unit abbreviation", "pt"), NC_("unit name", "pint"), NC_("unit plural", "pints"),
NC_("unit measure", "volume") },
- { "qt", NC_("unit abbreviation", "qt"), NC_("unit name", "quart"), NC_("unit plural",
"quarts"), NC_("unit measure", "volume") },
- { "gal", NC_("unit abbreviation", "gal"), NC_("unit name", "gallon"), NC_("unit plural",
"gallons"), NC_("unit measure", "volume") },
- { "cup", NC_("unit abbreviation", "cup"), NC_("unit name", "cup"), NC_("unit plural", "cups"),
NC_("unit measure", "volume") },
- { "tbsp", NC_("unit abbreviation", "tbsp"), NC_("unit name", "tablespoon"), NC_("unit plural",
"tablespoons"), NC_("unit measure", "volume") },
- { "tsp", NC_("unit abbreviation", "tsp"), NC_("unit name", "teaspoon"), NC_("unit plural",
"teaspoons"), NC_("unit measure", "volume") },
- { "box", NC_("unit abbreviation", "box"), NC_("unit name", "box"), NC_("unit plural", "boxes"),
NC_("unit measure", "quantity") },
- { "pkg", NC_("unit abbreviation", "pkg"), NC_("unit name", "package"), NC_("unit plural",
"packages"), NC_("unit measure", "quantity") },
- { "glass", NC_("unit abbreviation", "glass"), NC_("unit name", "glass"), NC_("unit plural",
"glasses"), NC_("unit measure", "quantity") },
- { "mm", NC_("unit abbreviation", "mm"), NC_("unit name", "millimeter"), NC_("unit plural",
"millimeters"), NC_("unit measure", "length") },
- { "cm", NC_("unit abbreviation", "cm"), NC_("unit name", "centimeter"), NC_("unit plural",
"centimeters"), NC_("unit measure", "length") },
- { "m", NC_("unit abbreviation", "m"), NC_("unit name", "meter"), NC_("unit plural",
"meters"), NC_("unit measure", "length") },
- { "st", NC_("unit abbreviation", "st"), NC_("unit name", "stone"), NC_("unit plural",
"stone") , NC_("unit measure", "weight")},
- { "pinch", NC_("unit abbreviation", "pinch"), NC_("unit name", "pinch"), NC_("unit plural",
"pinches"), NC_("unit measure", "volume") },
- { "bunch", NC_("unit abbreviation", "bunch"), NC_("unit name", "bunch"), NC_("unit plural",
"bunches"), NC_("unit measure", "quantity") },
+ { "g", NC_("unit abbreviation", "g"), NC_("unit name", "gram"), NC_("unit plural", "grams"),
"weight", 1 },
+ { "kg", NC_("unit abbreviation", "kg"), NC_("unit name", "kilogram"), NC_("unit plural",
"kilograms"), "weight", 2 },
+ { "lb", NC_("unit abbreviation", "lb"), NC_("unit name", "pound"), NC_("unit plural",
"pounds"), "weight", 3 },
+ { "oz", NC_("unit abbreviation", "oz"), NC_("unit name", "ounce"), NC_("unit plural",
"ounces"), "weight", 4 },
+ { "l", NC_("unit abbreviation", "l"), NC_("unit name", "liter"), NC_("unit plural",
"liters"), "volume", 5 },
+ { "dl", NC_("unit abbreviation", "dl"), NC_("unit name", "deciliter"), NC_("unit plural",
"deciliters"), "volume", 6 },
+ { "ml", NC_("unit abbreviation", "ml"), NC_("unit name", "milliliter"), NC_("unit plural",
"milliliters"), "volume", 7 },
+ { "fl oz", NC_("unit abbreviation", "fl oz"), NC_("unit name", "fluid ounce"), NC_("unit plural",
"fluid ounces"), "volume", 8 },
+ // { "fl. oz.", NC_("unit abbreviation", "fl oz"), NC_("unit name", "fluid ounce"), NC_("unit
plural", "fluid ounces"), "volume", 9 },
+ { "pt", NC_("unit abbreviation", "pt"), NC_("unit name", "pint"), NC_("unit plural", "pints"),
"volume", 9 },
+ { "qt", NC_("unit abbreviation", "qt"), NC_("unit name", "quart"), NC_("unit plural",
"quarts"), "volume", 10 },
+ { "gal", NC_("unit abbreviation", "gal"), NC_("unit name", "gallon"), NC_("unit plural",
"gallons"), "volume", 11 },
+ { "cup", NC_("unit abbreviation", "cup"), NC_("unit name", "cup"), NC_("unit plural", "cups"),
"volume", 12 },
+ { "tbsp", NC_("unit abbreviation", "tbsp"), NC_("unit name", "tablespoon"), NC_("unit plural",
"tablespoons"), "volume", 13 },
+ { "tsp", NC_("unit abbreviation", "tsp"), NC_("unit name", "teaspoon"), NC_("unit plural",
"teaspoons"), "volume", 14 },
+ { "box", NC_("unit abbreviation", "box"), NC_("unit name", "box"), NC_("unit plural", "boxes"),
"quantity", 15 },
+ { "pkg", NC_("unit abbreviation", "pkg"), NC_("unit name", "package"), NC_("unit plural",
"packages"), "quantity", 16 },
+ { "glass", NC_("unit abbreviation", "glass"), NC_("unit name", "glass"), NC_("unit plural",
"glasses"), "quantity", 17 },
+ { "mm", NC_("unit abbreviation", "mm"), NC_("unit name", "millimeter"), NC_("unit plural",
"millimeters"), "length", 18 },
+ { "cm", NC_("unit abbreviation", "cm"), NC_("unit name", "centimeter"), NC_("unit plural",
"centimeters"), "length", 19 },
+ { "m", NC_("unit abbreviation", "m"), NC_("unit name", "meter"), NC_("unit plural",
"meters"), "length", 20 },
+ { "st", NC_("unit abbreviation", "st"), NC_("unit name", "stone"), NC_("unit plural",
"stone") , "weight", 21 },
+ { "pinch", NC_("unit abbreviation", "pinch"), NC_("unit name", "pinch"), NC_("unit plural",
"pinches"), "volume", 22 },
+ { "bunch", NC_("unit abbreviation", "bunch"), NC_("unit name", "bunch"), NC_("unit plural",
"bunches"), "quantity", 23 },
};
@@ -102,6 +103,15 @@ gr_unit_get_plural (const char *name)
}
+int
+gr_unit_get_id_number (const char *name)
+{
+ GrUnit *unit = find_unit (name);
+ if (unit)
+ return unit->id_number;
+ return NULL;
+}
+
const char *
gr_unit_get_abbreviation (const char *name)
{
@@ -116,7 +126,7 @@ gr_unit_get_measure (const char *name)
{
GrUnit *unit = find_unit (name);
if (unit)
- return g_dpgettext2 (NULL, "unit measure", unit->measure);
+ return g_dpgettext2 (NULL, "unit_measure", unit->measure);
return NULL;
}
diff --git a/src/gr-unit.h b/src/gr-unit.h
index 372e750..3b477af 100644
--- a/src/gr-unit.h
+++ b/src/gr-unit.h
@@ -22,6 +22,33 @@
G_BEGIN_DECLS
+typedef enum {
+ GR_UNIT_GRAM = 1,
+ GR_UNIT_KILOGRAM = 2,
+ GR_UNIT_POUND = 3,
+ GR_UNIT_OUNCE = 4,
+ GR_UNIT_LITER = 5,
+ GR_UNIT_DECILITER = 6,
+ GR_UNIT_MILLILITER = 7,
+ GR_UNIT_FLUID_OUNCE = 8,
+ GR_UNIT_PINT = 9,
+ GR_UNIT_QUART = 10,
+ GR_UNIT_GALLON = 11,
+ GR_UNIT_CUP = 12,
+ GR_UNIT_TABLESPOON = 13,
+ GR_UNIT_TEASPOON = 14,
+ GR_UNIT_BOX = 15,
+ GR_UNIT_PACKAGES = 16,
+ GR_UNIT_GLASS = 17,
+ GR_UNIT_MILLIMETER = 18,
+ GR_UNIT_CENTIMETER = 19,
+ GR_UNIT_METER = 20,
+ GR_UNIT_STONE = 21,
+ GR_UNIT_PINCH = 22,
+ GR_UNIT_BUNCH = 23
+
+} GrUnitEnum;
+
const char *gr_unit_parse (char **string,
GError **error);
@@ -30,5 +57,6 @@ 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);
+int gr_unit_get_id_number(const char *name);
G_END_DECLS
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]