gimp/app/display/gimpdisplayshell-utils.c
Jehan d493f0537f Issue #8900 and #9923: reimplementing GimpUnit as a proper class.
This fixes all our GObject Introspection issues with GimpUnit which was
both an enum and an int-derived type of user-defined units *completing*
the enum values. GIR clearly didn't like this!

Now GimpUnit is a proper class and units are unique objects, allowing to
compare them with an identity test (i.e. `unit == gimp_unit_pixel ()`
tells us if unit is the pixel unit or not), which makes it easy to use,
just like with int, yet adding also methods, making for nicer
introspected API.

As an aside, this also fixes #10738, by having all the built-in units
retrievable even if libgimpbase had not been properly initialized with
gimp_base_init().
I haven't checked in details how GIR works to introspect, but it looks
like it loads the library to inspect and runs functions, hence
triggering some CRITICALS because virtual methods (supposed to be
initialized with gimp_base_init() run by libgimp) are not set. This new
code won't trigger any critical because the vtable method are now not
necessary, at least for all built-in units.

Note that GimpUnit is still in libgimpbase. It could have been moved to
libgimp in order to avoid any virtual method table (since we need to
keep core and libgimp side's units in sync, PDB is required), but too
many libgimpwidgets widgets were already using GimpUnit. And technically
most of GimpUnit logic doesn't require PDB (only the creation/sync
part). This is one of the reasons why user-created GimpUnit list is
handled and stored differently from other types of objects.

Globally this simplifies the code a lot too and we don't need separate
implementations of various utils for core and libgimp, which means less
prone to errors.
2024-08-02 10:46:38 +02:00

220 lines
6.7 KiB
C

/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#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"
void
gimp_display_shell_get_constrained_line_params (GimpDisplayShell *shell,
gdouble *offset_angle,
gdouble *xres,
gdouble *yres)
{
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
g_return_if_fail (offset_angle != NULL);
g_return_if_fail (xres != NULL);
g_return_if_fail (yres != NULL);
if (shell->flip_horizontally ^ shell->flip_vertically)
*offset_angle = +shell->rotate_angle;
else
*offset_angle = -shell->rotate_angle;
*xres = 1.0;
*yres = 1.0;
if (! shell->dot_for_dot)
{
GimpImage *image = gimp_display_get_image (shell->display);
if (image)
gimp_image_get_resolution (image, xres, yres);
}
}
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 offset_angle;
gdouble xres, yres;
g_return_if_fail (GIMP_IS_DISPLAY_SHELL (shell));
g_return_if_fail (end_x != NULL);
g_return_if_fail (end_y != NULL);
gimp_display_shell_get_constrained_line_params (shell,
&offset_angle,
&xres, &yres);
gimp_constrain_line (start_x, start_y,
end_x, end_y,
n_snap_lines,
offset_angle,
xres, yres);
}
gdouble
gimp_display_shell_constrain_angle (GimpDisplayShell *shell,
gdouble angle,
gint n_snap_lines)
{
gdouble x, y;
g_return_val_if_fail (GIMP_IS_DISPLAY_SHELL (shell), 0.0);
x = cos (angle);
y = sin (angle);
gimp_display_shell_constrain_line (shell,
0.0, 0.0,
&x, &y,
n_snap_lines);
return atan2 (y, x);
}
/**
* 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.
*
* Returns: 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;
}