[gimp] app: new gimp_display_shell_get_line_status().



commit 0116d7316ab957adbed2b73916b4caaf724ec3a5
Author: Jehan <jehan girinstud io>
Date:   Fri Dec 22 22:23:33 2017 +0100

    app: new gimp_display_shell_get_line_status().
    
    Factorize the code to show status for tools needing to show line
    information.

 app/display/gimpdisplayshell-utils.c |  115 ++++++++++++++++++++++++++++++++++
 app/display/gimpdisplayshell-utils.h |   17 ++++--
 2 files changed, 127 insertions(+), 5 deletions(-)
---
diff --git a/app/display/gimpdisplayshell-utils.c b/app/display/gimpdisplayshell-utils.c
index ea0f3eb..057a669 100644
--- a/app/display/gimpdisplayshell-utils.c
+++ b/app/display/gimpdisplayshell-utils.c
@@ -20,15 +20,20 @@
 #include <gegl.h>
 #include <gtk/gtk.h>
 
+#include "libgimpbase/gimpbase.h"
 #include "libgimpmath/gimpmath.h"
 
 #include "display-types.h"
 
 #include "core/gimp-utils.h"
+#include "core/gimpimage.h"
+#include "core/gimpunit.h"
 
+#include "gimpdisplay.h"
 #include "gimpdisplayshell.h"
 #include "gimpdisplayshell-utils.h"
 
+#include "gimp-intl.h"
 
 gdouble
 gimp_display_shell_get_constrained_line_offset_angle (GimpDisplayShell *shell)
@@ -58,3 +63,113 @@ gimp_display_shell_constrain_line (GimpDisplayShell *shell,
                        n_snap_lines,
                        gimp_display_shell_get_constrained_line_offset_angle (shell));
 }
+
+/**
+ * gimp_display_shell_get_line_status:
+ * @status:    initial status text.
+ * @separator: separator text between the line information and @status.
+ * @shell:     #GimpDisplayShell this status text will be displayed for.
+ * @x1:        abscissa of first point.
+ * @y1:        ordinate of first point.
+ * @x2:        abscissa of second point.
+ * @y2:        ordinate of second point.
+ *
+ * Utility function to prepend the status message with a distance and
+ * angle value. Obviously this is only to be used for tools when it
+ * makes sense, and in particular when there is a concept of line. For
+ * instance when shift-clicking a painting tool or in the blend tool,
+ * etc.
+ * This utility prevents code duplication but also ensures a common
+ * display for every tool where such a status is needed. It will take
+ * into account the shell unit settings and will use the ideal digit
+ * precision according to current image resolution.
+ *
+ * Return value: a newly allocated string containing the enhanced status.
+ **/
+gchar *
+gimp_display_shell_get_line_status (GimpDisplayShell *shell,
+                                    const gchar      *status,
+                                    const gchar      *separator,
+                                    gdouble           x1,
+                                    gdouble           y1,
+                                    gdouble           x2,
+                                    gdouble           y2)
+{
+  GimpImage *image;
+  gchar     *enhanced_status;
+  gdouble    xres;
+  gdouble    yres;
+  gdouble    dx, dy, pixel_dist;
+  gdouble    angle;
+
+  image = gimp_display_get_image (shell->display);
+  if (! image)
+    {
+      /* This makes no sense to add line information when no image is
+       * attached to the display. */
+      return g_strdup (status);
+    }
+
+  if (shell->unit == GIMP_UNIT_PIXEL)
+    xres = yres = 1.0;
+  else
+    gimp_image_get_resolution (image, &xres, &yres);
+
+  dx = x2 - x1;
+  dy = y2 - y1;
+  pixel_dist = sqrt (SQR (dx) + SQR (dy));
+
+  if (dx)
+    {
+      angle = gimp_rad_to_deg (atan ((dy/yres) / (dx/xres)));
+      if (dx > 0)
+        {
+          if (dy > 0)
+            angle = 360.0 - angle;
+          else if (dy < 0)
+            angle = -angle;
+        }
+      else
+        {
+          angle = 180.0 - angle;
+        }
+    }
+  else if (dy)
+    {
+      angle = dy > 0 ? 270.0 : 90.0;
+    }
+  else
+    {
+      angle = 0.0;
+    }
+
+  if (shell->unit == GIMP_UNIT_PIXEL)
+    {
+      enhanced_status = g_strdup_printf ("%.1f %s, %.2f\302\260%s%s",
+                                         pixel_dist, _("pixels"), angle,
+                                         separator, status);
+    }
+  else
+    {
+      gdouble inch_dist;
+      gdouble unit_dist;
+      gint    digits = 0;
+
+      /* The distance in unit. */
+      inch_dist = sqrt (SQR (dx / xres) + SQR (dy / yres));
+      unit_dist = gimp_unit_get_factor (shell->unit) * inch_dist;
+
+      /* The ideal digit precision for unit in current resolution. */
+      if (inch_dist)
+        digits = gimp_unit_get_scaled_digits (shell->unit,
+                                              pixel_dist / inch_dist);
+
+      enhanced_status = g_strdup_printf ("%.*f %s, %.2f\302\260%s%s",
+                                         digits, unit_dist,
+                                         gimp_unit_get_symbol (shell->unit),
+                                         angle, separator, status);
+
+    }
+
+  return enhanced_status;
+}
diff --git a/app/display/gimpdisplayshell-utils.h b/app/display/gimpdisplayshell-utils.h
index a9a2f83..1e4a55b 100644
--- a/app/display/gimpdisplayshell-utils.h
+++ b/app/display/gimpdisplayshell-utils.h
@@ -21,11 +21,18 @@
 
 gdouble   gimp_display_shell_get_constrained_line_offset_angle (GimpDisplayShell *shell);
 void      gimp_display_shell_constrain_line                    (GimpDisplayShell *shell,
-                                                                gdouble          start_x,
-                                                                gdouble          start_y,
-                                                                gdouble         *end_x,
-                                                                gdouble         *end_y,
-                                                                gint             n_snap_lines);
+                                                                gdouble           start_x,
+                                                                gdouble           start_y,
+                                                                gdouble          *end_x,
+                                                                gdouble          *end_y,
+                                                                gint              n_snap_lines);
+gchar   * gimp_display_shell_get_line_status                   (GimpDisplayShell *shell,
+                                                                const gchar      *status,
+                                                                const gchar      *separator,
+                                                                gdouble           x1,
+                                                                gdouble           y1,
+                                                                gdouble           x2,
+                                                                gdouble           y2);
 
 
 #endif  /*  __GIMP_DISPLAY_SHELL_UTILS_H__  */


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