From 5eb62abb259ea721ca9a15d3dc2d99f1f493b5ca Mon Sep 17 00:00:00 2001 From: t-bltg Date: Sun, 4 Jul 2021 15:07:09 +0200 Subject: [PATCH] simplify build_layout logic --- src/layouts.jl | 79 +++++++++++++++----------------------------------- 1 file changed, 24 insertions(+), 55 deletions(-) diff --git a/src/layouts.jl b/src/layouts.jl index a34c94a85..5b79b8f6e 100644 --- a/src/layouts.jl +++ b/src/layouts.jl @@ -494,34 +494,42 @@ layout_args(huh) = error("unhandled layout type $(typeof(huh)): $huh") function build_layout(args...) layout, n = layout_args(args...) - build_layout(layout, n) + build_layout(layout, n, Array{Plot}(undef, 0)) end -# # just a single subplot -# function build_layout(sp::Subplot, n::Integer) -# sp, Subplot[sp], SubplotMap(gensym() => sp) -# end -# n is the number of subplots... build a grid and initialize the inner subplots recursively -function build_layout(layout::GridLayout, n::Integer) +# n is the number of subplots... +function build_layout(layout::GridLayout, n::Integer, plts::AVec{Plot}) nr, nc = size(layout) subplots = Subplot[] spmap = SubplotMap() + empty = isempty(plts) i = 0 - for r=1:nr, c=1:nc - l = layout[r,c] + for r = 1:nr, c = 1:nc + l = layout[r, c] if isa(l, EmptyLayout) && !get(l.attr, :blank, false) - sp = Subplot(backend(), parent=layout) - layout[r,c] = sp - push!(subplots, sp) - spmap[attr(l,:label,gensym())] = sp + if empty + # initialize the inner subplots recursively + sp = Subplot(backend(), parent=layout) + layout[r, c] = sp + push!(subplots, sp) + spmap[attr(l,:label,gensym())] = sp + inc = 1 + else + # build a layout from a list of existing Plot objects + plt = popfirst!(plts) # grab the first plot out of the list + layout[r, c] = plt.layout + append!(subplots, plt.subplots) + merge!(spmap, plt.spmap) + inc = length(plt.subplots) + end if get(l.attr, :width, :auto) != :auto layout.widths[c] = attr(l,:width) end if get(l.attr, :height, :auto) != :auto layout.heights[r] = attr(l,:height) end - i += 1 + i += inc elseif isa(l, GridLayout) # sub-grid if get(l.attr, :width, :auto) != :auto @@ -530,11 +538,11 @@ function build_layout(layout::GridLayout, n::Integer) if get(l.attr, :height, :auto) != :auto layout.heights[r] = attr(l,:height) end - l, sps, m = build_layout(l, n-i) + l, sps, m = build_layout(l, n - i, plts) append!(subplots, sps) merge!(spmap, m) i += length(sps) - elseif isa(l, Subplot) + elseif isa(l, Subplot) && empty error("Subplot exists. Cannot re-use existing layout. Please make a new one.") end i >= n && break # only add n subplots @@ -543,45 +551,6 @@ function build_layout(layout::GridLayout, n::Integer) layout, subplots, spmap end -# build a layout from a list of existing Plot objects -# TODO... much of the logic overlaps with the method above... can we merge? -function build_layout(layout::GridLayout, numsp::Integer, plts::AVec{Plot}) - nr, nc = size(layout) - subplots = Subplot[] - spmap = SubplotMap() - i = 0 - for r=1:nr, c=1:nc - l = layout[r,c] - if isa(l, EmptyLayout) && !get(l.attr, :blank, false) - plt = popfirst!(plts) # grab the first plot out of the list - layout[r,c] = plt.layout - append!(subplots, plt.subplots) - merge!(spmap, plt.spmap) - if get(l.attr, :width, :auto) != :auto - layout.widths[c] = attr(l,:width) - end - if get(l.attr, :height, :auto) != :auto - layout.heights[r] = attr(l,:height) - end - i += length(plt.subplots) - elseif isa(l, GridLayout) - # sub-grid - if get(l.attr, :width, :auto) != :auto - layout.widths[c] = attr(l,:width) - end - if get(l.attr, :height, :auto) != :auto - layout.heights[r] = attr(l,:height) - end - l, sps, m = build_layout(l, numsp-i, plts) - append!(subplots, sps) - merge!(spmap, m) - i += length(sps) - end - i >= numsp && break # only add n subplots - end - layout, subplots, spmap -end - # ---------------------------------------------------------------------- # @layout macro