simplify build_layout logic

This commit is contained in:
t-bltg 2021-07-04 15:07:09 +02:00
parent 3f110d6a6c
commit 5eb62abb25

View File

@ -494,34 +494,42 @@ layout_args(huh) = error("unhandled layout type $(typeof(huh)): $huh")
function build_layout(args...) function build_layout(args...)
layout, n = layout_args(args...) layout, n = layout_args(args...)
build_layout(layout, n) build_layout(layout, n, Array{Plot}(undef, 0))
end 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 # n is the number of subplots...
function build_layout(layout::GridLayout, n::Integer) function build_layout(layout::GridLayout, n::Integer, plts::AVec{Plot})
nr, nc = size(layout) nr, nc = size(layout)
subplots = Subplot[] subplots = Subplot[]
spmap = SubplotMap() spmap = SubplotMap()
empty = isempty(plts)
i = 0 i = 0
for r=1:nr, c=1:nc for r = 1:nr, c = 1:nc
l = layout[r,c] l = layout[r, c]
if isa(l, EmptyLayout) && !get(l.attr, :blank, false) if isa(l, EmptyLayout) && !get(l.attr, :blank, false)
if empty
# initialize the inner subplots recursively
sp = Subplot(backend(), parent=layout) sp = Subplot(backend(), parent=layout)
layout[r,c] = sp layout[r, c] = sp
push!(subplots, sp) push!(subplots, sp)
spmap[attr(l,:label,gensym())] = 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 if get(l.attr, :width, :auto) != :auto
layout.widths[c] = attr(l,:width) layout.widths[c] = attr(l,:width)
end end
if get(l.attr, :height, :auto) != :auto if get(l.attr, :height, :auto) != :auto
layout.heights[r] = attr(l,:height) layout.heights[r] = attr(l,:height)
end end
i += 1 i += inc
elseif isa(l, GridLayout) elseif isa(l, GridLayout)
# sub-grid # sub-grid
if get(l.attr, :width, :auto) != :auto 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 if get(l.attr, :height, :auto) != :auto
layout.heights[r] = attr(l,:height) layout.heights[r] = attr(l,:height)
end end
l, sps, m = build_layout(l, n-i) l, sps, m = build_layout(l, n - i, plts)
append!(subplots, sps) append!(subplots, sps)
merge!(spmap, m) merge!(spmap, m)
i += length(sps) 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.") error("Subplot exists. Cannot re-use existing layout. Please make a new one.")
end end
i >= n && break # only add n subplots i >= n && break # only add n subplots
@ -543,45 +551,6 @@ function build_layout(layout::GridLayout, n::Integer)
layout, subplots, spmap layout, subplots, spmap
end 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 # @layout macro