Commit b6932690 authored by Michael Natterer's avatar Michael Natterer 😴

app: simplify creating of stroke and fill groups a lot

- add gimp_draw_tool_push_group()/pop_group() which manage a stack
  of groups; all items automatically get added to the stack's top group
- use push_group()/pop_group() all over the place, which saves a lot
  of code in most cases
- return GimpCanvasGroup not GimpCanvasItem pointers from
  gimp_draw_tool_add_stroke_group() and fill_group()

Unrelated:

- add GipmCanvasGroup parameter to gimp_rectangle_tool_draw()
- put rect select's round corners into the stroke group to
  avoid ugly overdrawing (the mis-alignment of arcs becomes
  very visible now however, will fix that soon)
parent 64d9d767
......@@ -85,6 +85,7 @@ static void gimp_crop_tool_cursor_update (GimpTool
const GimpCoords *coords,
GdkModifierType state,
GimpDisplay *display);
static void gimp_crop_tool_draw (GimpDrawTool *draw_tool);
static gboolean gimp_crop_tool_execute (GimpRectangleTool *rectangle,
gint x,
gint y,
......@@ -154,7 +155,7 @@ gimp_crop_tool_class_init (GimpCropToolClass *klass)
tool_class->oper_update = gimp_rectangle_tool_oper_update;
tool_class->cursor_update = gimp_crop_tool_cursor_update;
draw_tool_class->draw = gimp_rectangle_tool_draw;
draw_tool_class->draw = gimp_crop_tool_draw;
}
static void
......@@ -302,6 +303,12 @@ gimp_crop_tool_cursor_update (GimpTool *tool,
GIMP_TOOL_CLASS (parent_class)->cursor_update (tool, coords, state, display);
}
static void
gimp_crop_tool_draw (GimpDrawTool *draw_tool)
{
gimp_rectangle_tool_draw (draw_tool, NULL);
}
static gboolean
gimp_crop_tool_execute (GimpRectangleTool *rectangle,
gint x,
......
......@@ -198,6 +198,17 @@ gimp_draw_tool_draw (GimpDrawTool *draw_tool)
GIMP_DRAW_TOOL_GET_CLASS (draw_tool)->draw (draw_tool);
if (draw_tool->group_stack)
{
g_warning ("%s: draw_tool->group_stack not empty after calling "
"GimpDrawTool::draw() of %s",
G_STRFUNC,
g_type_name (G_TYPE_FROM_INSTANCE (draw_tool)));
while (draw_tool->group_stack)
gimp_draw_tool_pop_group (draw_tool);
}
if (draw_tool->item)
{
gimp_display_shell_add_item (shell, draw_tool->item);
......@@ -343,13 +354,20 @@ void
gimp_draw_tool_add_item (GimpDrawTool *draw_tool,
GimpCanvasItem *item)
{
GimpCanvasGroup *group;
g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));
g_return_if_fail (GIMP_IS_CANVAS_ITEM (item));
if (! draw_tool->item)
draw_tool->item = gimp_canvas_group_new (gimp_display_get_shell (draw_tool->display));
gimp_canvas_group_add_item (GIMP_CANVAS_GROUP (draw_tool->item), item);
group = GIMP_CANVAS_GROUP (draw_tool->item);
if (draw_tool->group_stack)
group = draw_tool->group_stack->data;
gimp_canvas_group_add_item (group, item);
}
void
......@@ -363,7 +381,7 @@ gimp_draw_tool_remove_item (GimpDrawTool *draw_tool,
gimp_canvas_group_remove_item (GIMP_CANVAS_GROUP (draw_tool->item), item);
}
GimpCanvasItem *
GimpCanvasGroup *
gimp_draw_tool_add_stroke_group (GimpDrawTool *draw_tool)
{
GimpCanvasItem *item;
......@@ -376,10 +394,10 @@ gimp_draw_tool_add_stroke_group (GimpDrawTool *draw_tool)
gimp_draw_tool_add_item (draw_tool, item);
g_object_unref (item);
return item;
return GIMP_CANVAS_GROUP (item);
}
GimpCanvasItem *
GimpCanvasGroup *
gimp_draw_tool_add_fill_group (GimpDrawTool *draw_tool)
{
GimpCanvasItem *item;
......@@ -392,7 +410,27 @@ gimp_draw_tool_add_fill_group (GimpDrawTool *draw_tool)
gimp_draw_tool_add_item (draw_tool, item);
g_object_unref (item);
return item;
return GIMP_CANVAS_GROUP (item);
}
void
gimp_draw_tool_push_group (GimpDrawTool *draw_tool,
GimpCanvasGroup *group)
{
g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));
g_return_if_fail (GIMP_IS_CANVAS_GROUP (group));
draw_tool->group_stack = g_list_prepend (draw_tool->group_stack, group);
}
void
gimp_draw_tool_pop_group (GimpDrawTool *draw_tool)
{
g_return_if_fail (GIMP_IS_DRAW_TOOL (draw_tool));
g_return_if_fail (draw_tool->group_stack != NULL);
draw_tool->group_stack = g_list_remove (draw_tool->group_stack,
draw_tool->group_stack->data);
}
/**
......
......@@ -43,6 +43,7 @@ struct _GimpDrawTool
gint paused_count; /* count to keep track of multiple pauses */
GimpCanvasItem *item;
GList *group_stack;
};
struct _GimpDrawToolClass
......@@ -84,8 +85,12 @@ void gimp_draw_tool_add_item (GimpDrawTool *draw_too
void gimp_draw_tool_remove_item (GimpDrawTool *draw_tool,
GimpCanvasItem *item);
GimpCanvasItem * gimp_draw_tool_add_stroke_group (GimpDrawTool *draw_tool);
GimpCanvasItem * gimp_draw_tool_add_fill_group (GimpDrawTool *draw_tool);
GimpCanvasGroup* gimp_draw_tool_add_stroke_group (GimpDrawTool *draw_tool);
GimpCanvasGroup* gimp_draw_tool_add_fill_group (GimpDrawTool *draw_tool);
void gimp_draw_tool_push_group (GimpDrawTool *draw_tool,
GimpCanvasGroup *group);
void gimp_draw_tool_pop_group (GimpDrawTool *draw_tool);
GimpCanvasItem * gimp_draw_tool_add_line (GimpDrawTool *draw_tool,
gdouble x1,
......
......@@ -1529,8 +1529,7 @@ gimp_free_select_tool_draw (GimpDrawTool *draw_tool)
GimpFreeSelectTool *fst = GIMP_FREE_SELECT_TOOL (draw_tool);
GimpFreeSelectToolPrivate *priv = GET_PRIVATE (fst);
GimpTool *tool = GIMP_TOOL (draw_tool);
GimpCanvasItem *stroke_group;
GimpCanvasItem *item;
GimpCanvasGroup *stroke_group;
gboolean hovering_first_point = FALSE;
gboolean handles_wants_to_show = FALSE;
GimpCoords coords = { priv->last_coords.x,
......@@ -1547,15 +1546,11 @@ gimp_free_select_tool_draw (GimpDrawTool *draw_tool)
stroke_group = gimp_draw_tool_add_stroke_group (draw_tool);
item = gimp_draw_tool_add_lines (draw_tool,
priv->points, priv->n_points,
FALSE);
if (item)
{
gimp_canvas_group_add_item (GIMP_CANVAS_GROUP (stroke_group), item);
gimp_draw_tool_remove_item (draw_tool, item);
}
gimp_draw_tool_push_group (draw_tool, stroke_group);
gimp_draw_tool_add_lines (draw_tool,
priv->points, priv->n_points,
FALSE);
gimp_draw_tool_pop_group (draw_tool);
/* We always show the handle for the first point, even with button1
* down, since releasing the button on the first point will close
......@@ -1616,14 +1611,13 @@ gimp_free_select_tool_draw (GimpDrawTool *draw_tool)
{
GimpVector2 last = priv->points[priv->n_points - 1];
item = gimp_draw_tool_add_line (draw_tool,
last.x,
last.y,
priv->pending_point.x,
priv->pending_point.y);
gimp_canvas_group_add_item (GIMP_CANVAS_GROUP (stroke_group), item);
gimp_draw_tool_remove_item (draw_tool, item);
gimp_draw_tool_push_group (draw_tool, stroke_group);
gimp_draw_tool_add_line (draw_tool,
last.x,
last.y,
priv->pending_point.x,
priv->pending_point.y);
gimp_draw_tool_pop_group (draw_tool);
}
}
......
......@@ -655,8 +655,7 @@ gimp_measure_tool_draw (GimpDrawTool *draw_tool)
{
GimpMeasureTool *measure = GIMP_MEASURE_TOOL (draw_tool);
GimpTool *tool = GIMP_TOOL (draw_tool);
GimpCanvasItem *stroke_group;
GimpCanvasItem *item;
GimpCanvasGroup *stroke_group;
gint i;
gint draw_arc = 0;
......@@ -687,14 +686,15 @@ gimp_measure_tool_draw (GimpDrawTool *draw_tool)
if (i > 0)
{
item = gimp_draw_tool_add_line (draw_tool,
measure->x[0],
measure->y[0],
measure->x[i],
measure->y[i]);
gimp_draw_tool_push_group (draw_tool, stroke_group);
gimp_canvas_group_add_item (GIMP_CANVAS_GROUP (stroke_group), item);
gimp_draw_tool_remove_item (draw_tool, item);
gimp_draw_tool_add_line (draw_tool,
measure->x[0],
measure->y[0],
measure->x[i],
measure->y[i]);
gimp_draw_tool_pop_group (draw_tool);
/* only draw the arc if the lines are long enough */
if (gimp_draw_tool_calc_distance (draw_tool, tool->display,
......@@ -721,6 +721,10 @@ gimp_measure_tool_draw (GimpDrawTool *draw_tool)
if (angle2 != 0.0)
{
GimpCanvasItem *item;
gimp_draw_tool_push_group (draw_tool, stroke_group);
item = gimp_draw_tool_add_handle (draw_tool,
GIMP_HANDLE_CIRCLE,
measure->x[0],
......@@ -732,9 +736,6 @@ gimp_measure_tool_draw (GimpDrawTool *draw_tool)
gimp_canvas_handle_set_angles (GIMP_CANVAS_HANDLE (item),
angle1, angle2);
gimp_canvas_group_add_item (GIMP_CANVAS_GROUP (stroke_group), item);
gimp_draw_tool_remove_item (draw_tool, item);
if (measure->num_points == 2)
{
GimpDisplayShell *shell;
......@@ -746,17 +747,16 @@ gimp_measure_tool_draw (GimpDrawTool *draw_tool)
target = FUNSCALEX (shell, (TARGET >> 1));
arc_radius = FUNSCALEX (shell, ARC_RADIUS);
item = gimp_draw_tool_add_line (draw_tool,
measure->x[0],
measure->y[0],
(measure->x[1] >= measure->x[0] ?
measure->x[0] + arc_radius + target :
measure->x[0] - arc_radius - target),
measure->y[0]);
gimp_canvas_group_add_item (GIMP_CANVAS_GROUP (stroke_group), item);
gimp_draw_tool_remove_item (draw_tool, item);
gimp_draw_tool_add_line (draw_tool,
measure->x[0],
measure->y[0],
(measure->x[1] >= measure->x[0] ?
measure->x[0] + arc_radius + target :
measure->x[0] - arc_radius - target),
measure->y[0]);
}
gimp_draw_tool_pop_group (draw_tool);
}
}
}
......
......@@ -747,35 +747,27 @@ gimp_perspective_clone_tool_draw (GimpDrawTool *draw_tool)
if (clone_tool->use_handles)
{
GimpCanvasItem *stroke_group;
GimpCanvasItem *item;
GimpCanvasGroup *stroke_group;
stroke_group = gimp_draw_tool_add_stroke_group (draw_tool);
/* draw the bounding box */
item = gimp_draw_tool_add_line (draw_tool,
clone_tool->tx1, clone_tool->ty1,
clone_tool->tx2, clone_tool->ty2);
gimp_canvas_group_add_item (GIMP_CANVAS_GROUP (stroke_group), item);
gimp_draw_tool_remove_item (draw_tool, item);
item = gimp_draw_tool_add_line (draw_tool,
clone_tool->tx2, clone_tool->ty2,
clone_tool->tx4, clone_tool->ty4);
gimp_canvas_group_add_item (GIMP_CANVAS_GROUP (stroke_group), item);
gimp_draw_tool_remove_item (draw_tool, item);
item = gimp_draw_tool_add_line (draw_tool,
clone_tool->tx3, clone_tool->ty3,
clone_tool->tx4, clone_tool->ty4);
gimp_canvas_group_add_item (GIMP_CANVAS_GROUP (stroke_group), item);
gimp_draw_tool_remove_item (draw_tool, item);
item = gimp_draw_tool_add_line (draw_tool,
clone_tool->tx3, clone_tool->ty3,
clone_tool->tx1, clone_tool->ty1);
gimp_canvas_group_add_item (GIMP_CANVAS_GROUP (stroke_group), item);
gimp_draw_tool_remove_item (draw_tool, item);
gimp_draw_tool_push_group (draw_tool, stroke_group);
gimp_draw_tool_add_line (draw_tool,
clone_tool->tx1, clone_tool->ty1,
clone_tool->tx2, clone_tool->ty2);
gimp_draw_tool_add_line (draw_tool,
clone_tool->tx2, clone_tool->ty2,
clone_tool->tx4, clone_tool->ty4);
gimp_draw_tool_add_line (draw_tool,
clone_tool->tx3, clone_tool->ty3,
clone_tool->tx4, clone_tool->ty4);
gimp_draw_tool_add_line (draw_tool,
clone_tool->tx3, clone_tool->ty3,
clone_tool->tx1, clone_tool->ty1);
gimp_draw_tool_pop_group (draw_tool);
/* draw the tool handles */
gimp_draw_tool_add_handle (draw_tool,
......
......@@ -41,6 +41,7 @@
#include "widgets/gimpviewabledialog.h"
#include "widgets/gimpwidgets-utils.h"
#include "display/gimpcanvasgroup.h"
#include "display/gimpdisplay.h"
#include "display/gimpdisplayshell.h"
#include "display/gimpdisplayshell-transform.h"
......@@ -288,15 +289,17 @@ gimp_rectangle_select_tool_draw (GimpDrawTool *draw_tool)
{
GimpRectangleSelectTool *rect_sel_tool;
GimpRectangleSelectToolPrivate *priv;
GimpCanvasGroup *stroke_group = NULL;
rect_sel_tool = GIMP_RECTANGLE_SELECT_TOOL (draw_tool);
priv = GIMP_RECTANGLE_SELECT_TOOL_GET_PRIVATE (rect_sel_tool);
if (priv->round_corners)
{
gint x1, y1, x2, y2;
gdouble radius;
gint square_size;
GimpCanvasItem *item;
gint x1, y1, x2, y2;
gdouble radius;
gint square_size;
g_object_get (rect_sel_tool,
"x1", &x1,
......@@ -310,28 +313,39 @@ gimp_rectangle_select_tool_draw (GimpDrawTool *draw_tool)
square_size = (int) (radius * 2);
gimp_draw_tool_add_arc (draw_tool, FALSE,
x1, y1,
square_size, square_size,
G_PI / 2.0, G_PI / 2.0);
gimp_draw_tool_add_arc (draw_tool, FALSE,
x2 - square_size, y1,
square_size, square_size,
0.0, G_PI / 2.0);
gimp_draw_tool_add_arc (draw_tool, FALSE,
x2 - square_size, y2 - square_size,
square_size, square_size,
G_PI * 1.5, G_PI / 2.0);
gimp_draw_tool_add_arc (draw_tool, FALSE,
x1, y2 - square_size,
square_size, square_size,
G_PI, G_PI / 2.0);
stroke_group =
GIMP_CANVAS_GROUP (gimp_draw_tool_add_stroke_group (draw_tool));
item = gimp_draw_tool_add_arc (draw_tool, FALSE,
x1, y1,
square_size, square_size,
G_PI / 2.0, G_PI / 2.0);
gimp_canvas_group_add_item (stroke_group, item);
gimp_draw_tool_remove_item (draw_tool, item);
item = gimp_draw_tool_add_arc (draw_tool, FALSE,
x2 - square_size, y1,
square_size, square_size,
0.0, G_PI / 2.0);
gimp_canvas_group_add_item (stroke_group, item);
gimp_draw_tool_remove_item (draw_tool, item);
item = gimp_draw_tool_add_arc (draw_tool, FALSE,
x2 - square_size, y2 - square_size,
square_size, square_size,
G_PI * 1.5, G_PI / 2.0);
gimp_canvas_group_add_item (stroke_group, item);
gimp_draw_tool_remove_item (draw_tool, item);
item = gimp_draw_tool_add_arc (draw_tool, FALSE,
x1, y2 - square_size,
square_size, square_size,
G_PI, G_PI / 2.0);
gimp_canvas_group_add_item (stroke_group, item);
gimp_draw_tool_remove_item (draw_tool, item);
}
gimp_rectangle_tool_draw (draw_tool);
gimp_rectangle_tool_draw (draw_tool, stroke_group);
}
static gboolean
......
This diff is collapsed.
......@@ -120,7 +120,8 @@ void gimp_rectangle_tool_cursor_update (GimpTool *to
const GimpCoords *coords,
GdkModifierType state,
GimpDisplay *display);
void gimp_rectangle_tool_draw (GimpDrawTool *draw);
void gimp_rectangle_tool_draw (GimpDrawTool *draw,
GimpCanvasGroup *stroke_group);
gboolean gimp_rectangle_tool_execute (GimpRectangleTool *rect_tool);
void gimp_rectangle_tool_cancel (GimpRectangleTool *rect_tool);
void gimp_rectangle_tool_set_constraint (GimpRectangleTool *rectangle,
......
......@@ -783,7 +783,7 @@ gimp_text_tool_draw (GimpDrawTool *draw_tool)
"narrow-mode", TRUE,
NULL);
gimp_rectangle_tool_draw (draw_tool);
gimp_rectangle_tool_draw (draw_tool, NULL);
if (! text_tool->text ||
! text_tool->layer ||
......@@ -823,7 +823,7 @@ gimp_text_tool_draw_selection (GimpDrawTool *draw_tool)
{
GimpTextTool *text_tool = GIMP_TEXT_TOOL (draw_tool);
GtkTextBuffer *buffer = GTK_TEXT_BUFFER (text_tool->buffer);
GimpCanvasItem *fill_group;
GimpCanvasGroup *fill_group;
PangoLayout *layout;
gint offset_x;
gint offset_y;
......@@ -850,6 +850,8 @@ gimp_text_tool_draw_selection (GimpDrawTool *draw_tool)
iter = pango_layout_get_iter (layout);
gimp_draw_tool_push_group (draw_tool, fill_group);
do
{
if (! pango_layout_iter_get_run (iter))
......@@ -859,9 +861,8 @@ gimp_text_tool_draw_selection (GimpDrawTool *draw_tool)
if (i >= min && i < max)
{
GimpCanvasItem *item;
PangoRectangle rect;
gint ytop, ybottom;
PangoRectangle rect;
gint ytop, ybottom;
pango_layout_iter_get_char_extents (iter, &rect);
pango_layout_iter_get_line_yrange (iter, &ytop, &ybottom);
......@@ -876,16 +877,15 @@ gimp_text_tool_draw_selection (GimpDrawTool *draw_tool)
rect.x += offset_x;
rect.y += offset_y;
item = gimp_draw_tool_add_rectangle (draw_tool, TRUE,
rect.x, rect.y,
rect.width, rect.height);
gimp_canvas_group_add_item (GIMP_CANVAS_GROUP (fill_group), item);
gimp_draw_tool_remove_item (draw_tool, item);
gimp_draw_tool_add_rectangle (draw_tool, TRUE,
rect.x, rect.y,
rect.width, rect.height);
}
}
while (pango_layout_iter_next_char (iter));
gimp_draw_tool_pop_group (draw_tool);
pango_layout_iter_free (iter);
}
......
......@@ -781,14 +781,16 @@ gimp_transform_tool_draw (GimpDrawTool *draw_tool)
GimpTool *tool = GIMP_TOOL (draw_tool);
GimpTransformTool *tr_tool = GIMP_TRANSFORM_TOOL (draw_tool);
GimpImage *image = gimp_display_get_image (tool->display);
GimpCanvasItem *stroke_group;
GimpCanvasItem *item;
gdouble z1, z2, z3, z4;
stroke_group = gimp_draw_tool_add_stroke_group (draw_tool);
if (tr_tool->use_grid)
{
GimpCanvasGroup *stroke_group;
stroke_group = gimp_draw_tool_add_stroke_group (draw_tool);
gimp_draw_tool_push_group (draw_tool, stroke_group);
/* We test if the transformed polygon is convex.
* if z1 and z2 have the same sign as well as z3 and z4
* the polygon is convex.
......@@ -814,41 +816,29 @@ gimp_transform_tool_draw (GimpDrawTool *draw_tool)
for (i = 0, gci = 0; i < k; i++, gci += 4)
{
item = gimp_draw_tool_add_line (draw_tool,
tr_tool->tgrid_coords[gci],
tr_tool->tgrid_coords[gci + 1],
tr_tool->tgrid_coords[gci + 2],
tr_tool->tgrid_coords[gci + 3]);
gimp_canvas_group_add_item (GIMP_CANVAS_GROUP (stroke_group), item);
gimp_draw_tool_remove_item (draw_tool, item);
gimp_draw_tool_add_line (draw_tool,
tr_tool->tgrid_coords[gci],
tr_tool->tgrid_coords[gci + 1],
tr_tool->tgrid_coords[gci + 2],
tr_tool->tgrid_coords[gci + 3]);
}
}
/* draw the bounding box */
item = gimp_draw_tool_add_line (draw_tool,
tr_tool->tx1, tr_tool->ty1,
tr_tool->tx2, tr_tool->ty2);
gimp_canvas_group_add_item (GIMP_CANVAS_GROUP (stroke_group), item);
gimp_draw_tool_remove_item (draw_tool, item);
item = gimp_draw_tool_add_line (draw_tool,
tr_tool->tx2, tr_tool->ty2,
tr_tool->tx4, tr_tool->ty4);
gimp_canvas_group_add_item (GIMP_CANVAS_GROUP (stroke_group), item);
gimp_draw_tool_remove_item (draw_tool, item);
item = gimp_draw_tool_add_line (draw_tool,
tr_tool->tx3, tr_tool->ty3,
tr_tool->tx4, tr_tool->ty4);
gimp_canvas_group_add_item (GIMP_CANVAS_GROUP (stroke_group), item);
gimp_draw_tool_remove_item (draw_tool, item);
item = gimp_draw_tool_add_line (draw_tool,
tr_tool->tx3, tr_tool->ty3,
tr_tool->tx1, tr_tool->ty1);
gimp_canvas_group_add_item (GIMP_CANVAS_GROUP (stroke_group), item);
gimp_draw_tool_remove_item (draw_tool, item);
gimp_draw_tool_add_line (draw_tool,
tr_tool->tx1, tr_tool->ty1,
tr_tool->tx2, tr_tool->ty2);
gimp_draw_tool_add_line (draw_tool,
tr_tool->tx2, tr_tool->ty2,
tr_tool->tx4, tr_tool->ty4);
gimp_draw_tool_add_line (draw_tool,
tr_tool->tx3, tr_tool->ty3,
tr_tool->tx4, tr_tool->ty4);
gimp_draw_tool_add_line (draw_tool,
tr_tool->tx3, tr_tool->ty3,
tr_tool->tx1, tr_tool->ty1);
gimp_draw_tool_pop_group (draw_tool);
}
gimp_transform_tool_handles_recalc (tr_tool, tool->display);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment