[gimp/soc-2011-gimpunitentry] GimpUnitEntry: Removed UI dependence from parsing



commit 07350bdf38c6a42898afdd6a4e47249309f61cd7
Author: Enrico SchroÌ?der <enni schroeder gmail com>
Date:   Wed Jun 1 19:18:45 2011 +0200

    GimpUnitEntry: Removed UI dependence from parsing
    
    Added a new layer to separate everything parsing-related from the UI. Parsing and unit-determination is now done in GimpUnitParser.

 libgimpwidgets/Makefile.am      |    2 +
 libgimpwidgets/gimpunitentry.c  |  129 +++++++----------------------------
 libgimpwidgets/gimpunitentry.h  |    1 -
 libgimpwidgets/gimpunitparser.c |  143 +++++++++++++++++++++++++++++++++++++++
 libgimpwidgets/gimpunitparser.h |   40 +++++++++++
 5 files changed, 209 insertions(+), 106 deletions(-)
---
diff --git a/libgimpwidgets/Makefile.am b/libgimpwidgets/Makefile.am
index 0cdc590..05e8314 100644
--- a/libgimpwidgets/Makefile.am
+++ b/libgimpwidgets/Makefile.am
@@ -169,6 +169,8 @@ libgimpwidgets_2_0_la_sources = \
 	gimpunitentry.h			\
 	gimpunitmenu.c			\
 	gimpunitmenu.h			\
+	gimpunitparser.c   \
+	gimpunitparser.h   \
 	gimpunitstore.c			\
 	gimpunitstore.h			\
 	gimpwidgets-error.c		\
diff --git a/libgimpwidgets/gimpunitentry.c b/libgimpwidgets/gimpunitentry.c
index 077e925..a0af704 100644
--- a/libgimpwidgets/gimpunitentry.c
+++ b/libgimpwidgets/gimpunitentry.c
@@ -34,17 +34,12 @@
 
 #include "gimpwidgets.h"
 
-#include "gimpeevl.h"
+#include "gimpunitparser.h"
 #include "gimpunitentry.h"
 #include "gimpunitadjustment.h"
 
 G_DEFINE_TYPE (GimpUnitEntry, gimp_unit_entry, GTK_TYPE_SPIN_BUTTON);
 
-/* unit resolver for GimpEevl */
-static gboolean unit_resolver (const gchar      *ident,
-                               GimpEevlQuantity *result,
-                               gpointer          data);
-
 /* read and parse entered text */
 static gboolean gimp_unit_entry_parse (GimpUnitEntry *unitEntry);
 
@@ -161,68 +156,50 @@ gimp_unit_entry_connect (GimpUnitEntry *entry, GimpUnitEntry *target)
 static gboolean
 gimp_unit_entry_parse (GimpUnitEntry *entry)
 {
-  gdouble           newValue;
-  /* GimpEevl related stuff */
-  GimpEevlQuantity  result;
-  GError            *error    = NULL;
-  const gchar       *errorpos = 0;
-  /* text to parse */
-  const gchar       *str      = gtk_entry_get_text (GTK_ENTRY (entry));
-
-  if (strlen (str) <= 0)
-    return FALSE;
-  
-  /** 
-   * purpose of enteredUnit: use first unit unit_resolver will be called with  
-   * as entered unit. enteredUnit is reset now to know which unit was the first one
-   **/
-  entry->enteredUnit = -1;
-
-  g_debug ("%i parsing: %s", entry->id, str);
-
-  /* parse text via GimpEevl */
-  gimp_eevl_evaluate (str,
-                      unit_resolver,
-                      &result,
-                      (gpointer) entry,
-                      &errorpos,
-                      &error);
-
-  if (error || errorpos)
+  GimpUnitParserResult result; 
+  gboolean             success;
+  const gchar          *str = gtk_entry_get_text (GTK_ENTRY (entry));
+
+  /* set resolution (important for correct calculation of px) */
+  result.resolution = entry->unitAdjustment->resolution;
+
+  /* parse string of entry */
+  success = gimp_unit_parser_parse (str, &result);
+
+  if (!success)
   {
+    /* paint entry red */
     GdkColor color;
     gdk_color_parse ("LightSalmon", &color);
     gtk_widget_modify_base (GTK_WIDGET (entry), GTK_STATE_NORMAL, &color);
 
-    g_debug ("gimpeevl parsing error \n");
     return FALSE;
   }
   else
   {
+    /* reset color */
     gtk_widget_modify_base (GTK_WIDGET (entry), GTK_STATE_NORMAL, NULL);
-    g_debug ("%i gimpeevl parser result: %s = %lg (%d)", entry->id, str, result.value, result.dimension);
-    g_debug ("%i determined unit: %s\n", entry->id, gimp_unit_get_abbreviation (entry->enteredUnit));
 
     /* set new unit */  
-    if (entry->enteredUnit != entry->unitAdjustment->unit)
+    if (result.unit != entry->unitAdjustment->unit)
     {
-      gimp_unit_adjustment_set_unit (entry->unitAdjustment, entry->enteredUnit);
+      gimp_unit_adjustment_set_unit (entry->unitAdjustment, result.unit);
     }
 
     /* set new value */
     if (gimp_unit_adjustment_get_value (entry->unitAdjustment) != result.value)
     {
       /* result from parser is in inch, so convert to desired unit */
-      newValue = gimp_units_to_pixels (result.value,
-                                       GIMP_UNIT_INCH,
-                                       entry->unitAdjustment->resolution);
-      newValue = gimp_pixels_to_units (newValue,
-                                       entry->unitAdjustment->unit, 
-                                       entry->unitAdjustment->resolution);
+      result.value = gimp_units_to_pixels (result.value,
+                                            GIMP_UNIT_INCH,
+                                            entry->unitAdjustment->resolution);
+      result.value = gimp_pixels_to_units (result.value,
+                                            entry->unitAdjustment->unit, 
+                                            entry->unitAdjustment->resolution);
 
-      gimp_unit_adjustment_set_value (entry->unitAdjustment, newValue);
+      gimp_unit_adjustment_set_value (entry->unitAdjustment, result.value);
 
-      g_object_notify (G_OBJECT ( GTK_SPIN_BUTTON (entry)), "value");
+      //g_object_notify (G_OBJECT ( GTK_SPIN_BUTTON (entry)), "value");
     }
   }
 
@@ -296,64 +273,6 @@ void on_text_changed (GtkEditable *editable, gpointer user_data)
   gimp_unit_entry_parse (entry);
 }
 
-/* unit resolver for GimpEevl */
-static gboolean
-unit_resolver (const gchar      *ident,
-               GimpEevlQuantity *result,
-               gpointer          user_data)
-{
-  GimpUnitEntry   *entry       = GIMP_UNIT_ENTRY (user_data);
-  GimpUnit        *unit        = &(entry->enteredUnit);
-  gboolean        resolved     = FALSE;
-  gboolean        default_unit = (ident == NULL);
-  gint            numUnits     = gimp_unit_get_number_of_units ();
-  const gchar     *abbr; 
-  gint            i            = 0;
-
-  result->dimension = 1;
-
-  /* if no unit is specified, use default unit */
-  if (default_unit)
-  {
-    /* if default hasn't been set before, set to inch*/
-    if (*unit == -1)
-      *unit = GIMP_UNIT_INCH;
-
-    result->dimension = 1;
-
-    if (*unit == GIMP_UNIT_PIXEL) /* handle case that unit is px */
-      result->value = gimp_unit_entry_get_adjustment (entry)->resolution;
-    else                          /* otherwise use factor */
-      result->value = gimp_unit_get_factor (*unit);
-
-    resolved          = TRUE; 
-    return resolved;
-  }
-
-  /* find matching unit */
-  for (i = 0; i < numUnits; i++)
-  {
-    abbr = gimp_unit_get_abbreviation (i);
-
-    if (strcmp (abbr, ident) == 0)
-    {
-      /* handle case that unit is px */
-      if (i == GIMP_UNIT_PIXEL)
-        result->value = gimp_unit_entry_get_adjustment (entry)->resolution;
-      else
-        result->value = gimp_unit_get_factor (i);
-
-      if (*unit == -1)
-        *unit = i;
-
-      i = numUnits;
-      resolved = TRUE;
-    }
-  }
-
-  return resolved;
-}
-
 static 
 gint on_input        (GtkSpinButton *spinButton,
                       gpointer       arg1,
diff --git a/libgimpwidgets/gimpunitentry.h b/libgimpwidgets/gimpunitentry.h
index 1c28551..80a7713 100644
--- a/libgimpwidgets/gimpunitentry.h
+++ b/libgimpwidgets/gimpunitentry.h
@@ -45,7 +45,6 @@ struct _GimpUnitEntry
 
   /* private */
   GimpUnitAdjustment *unitAdjustment; /* for convinience */
-  GimpUnit           enteredUnit;     /* used to determine which unit was entered */
 
   /* set TRUE while up/down buttons/keys pressed or scrolling so that we can disable
      our parsing and display the changed value */
diff --git a/libgimpwidgets/gimpunitparser.c b/libgimpwidgets/gimpunitparser.c
new file mode 100644
index 0000000..f8991be
--- /dev/null
+++ b/libgimpwidgets/gimpunitparser.c
@@ -0,0 +1,143 @@
+/* LIBGIMP - The GIMP Library
+ * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
+ *
+ * gimpunitparser.c
+ * Copyright (C) 2011 Enrico Schröder <enni schroeder gmail com>
+ *
+ * This library is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#include <string.h>
+
+#include "gimpunitparser.h"
+#include "gimpeevl.h"
+
+//#define PARSER_DEBUG
+#ifdef  PARSER_DEBUG
+#define DEBUG(x) g_debug x 
+#else
+#define DEBUG(x) /* nothing */
+#endif
+
+/**
+ * prototypes
+ */
+
+/* unit resolver for GimpEevl */
+static gboolean unit_resolver (const gchar      *ident,
+                               GimpEevlQuantity *result,
+                               gpointer          data);
+
+
+/* parse given string */
+gboolean 
+gimp_unit_parser_parse (const char *str, GimpUnitParserResult *result)
+{
+  /* GimpEevl related stuff */
+  GimpEevlQuantity  eevlResult;
+  GError            *error    = NULL;
+  const gchar       *errorpos = 0;
+
+  if (strlen (str) <= 0)
+    return FALSE;
+  
+  /* set unit to -1 so we can determine the first unit the user entered and use that
+     as unit for our result */
+  result->unit = -1;
+
+  DEBUG (("parsing: %s", str));
+
+  /* parse text via GimpEevl */
+  gimp_eevl_evaluate (str,
+                      unit_resolver,
+                      &eevlResult,
+                      (gpointer) result,
+                      &errorpos,
+                      &error);
+
+  if (error || errorpos)
+  {
+    DEBUG (("gimpeevl parsing error \n"));
+    return FALSE;
+  }
+  else
+  {
+    DEBUG (("gimpeevl parser result: %s = %lg (%d)", str, eevlResult.value, eevlResult.dimension));
+    DEBUG (("determined unit: %s\n", gimp_unit_get_abbreviation (result->unit)));
+
+    result->value = eevlResult.value;
+  }
+
+  return TRUE;
+}
+
+/* unit resolver for GimpEevl */
+static 
+gboolean unit_resolver (const gchar      *ident,
+                        GimpEevlQuantity *result,
+                        gpointer          user_data)
+{
+  GimpUnitParserResult   *parserResult = (GimpUnitParserResult*) user_data;
+  GimpUnit               *unit        = &(parserResult->unit);
+  gboolean               resolved     = FALSE;
+  gboolean               default_unit = (ident == NULL);
+  gint                   numUnits     = gimp_unit_get_number_of_units ();
+  const gchar            *abbr; 
+  gint                   i            = 0;
+
+  result->dimension = 1;
+
+  /* if no unit is specified, use default unit */
+  if (default_unit)
+  {
+    /* if default hasn't been set before, set to inch*/
+    if (*unit == -1)
+      *unit = GIMP_UNIT_INCH;
+
+    result->dimension = 1;
+
+    if (*unit == GIMP_UNIT_PIXEL) /* handle case that unit is px */
+      result->value = parserResult->resolution;
+    else                          /* otherwise use factor */
+      result->value = gimp_unit_get_factor (*unit);
+
+    resolved          = TRUE; 
+    return resolved;
+  }
+
+  /* find matching unit */
+  for (i = 0; i < numUnits; i++)
+  {
+    abbr = gimp_unit_get_abbreviation (i);
+
+    if (strcmp (abbr, ident) == 0)
+    {
+      /* handle case that unit is px */
+      if (i == GIMP_UNIT_PIXEL)
+        result->value = parserResult->resolution;
+      else
+        result->value = gimp_unit_get_factor (i);
+
+      if (*unit == -1)
+        *unit = i;
+
+      i = numUnits;
+      resolved = TRUE;
+    }
+  }
+
+  return resolved;
+}
+                              
\ No newline at end of file
diff --git a/libgimpwidgets/gimpunitparser.h b/libgimpwidgets/gimpunitparser.h
new file mode 100644
index 0000000..18f976b
--- /dev/null
+++ b/libgimpwidgets/gimpunitparser.h
@@ -0,0 +1,40 @@
+/* LIBGIMP - The GIMP Library
+ * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
+ *
+ * gimpunitparser.h
+ * Copyright (C) 2011 Enrico Schröder <enni schroeder gmail com>
+ *
+ * This library is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __GIMP_UNIT_PARSER_H__
+#define __GIMP_UNIT_PARSER_H__
+
+#include <glib-object.h>
+#include "libgimpbase/gimpbase.h"
+
+typedef struct _GimpUnitParserResult GimpUnitParserResult;
+
+struct _GimpUnitParserResult
+{
+  gdouble   value;
+  gdouble   resolution;
+  GimpUnit  unit;
+};
+
+/* parse given string */
+gboolean gimp_unit_parser_parse (const char *str, GimpUnitParserResult *result);
+
+#endif /*__GIMP_UNIT_PARSER_H__*/



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]