Skip to content

generateLayout()

John Brooks edited this page Aug 27, 2014 · 1 revision

generateLayout() is a function in printLabel.py that is responsible for laying out elements on the label.

#Process

generateLayout() is a recursive function. This is because container objects (boxes) can contain other elements within them that need to be positioned relative to its container's position and dimensions.

generateLayout() is first called by printLabel(), with the root element (which is a "container" representing the whole label at the top level) as the parent.

Layout of elements is based on a system of rows. Every element contained within the parent is assigned to a row. Elements are added to a row until there isn't enough space left to add the next element. When this happens, the element is added to the next row. This continues until all the elements are placed. Each row is horizontally centered within the container. elementSpacing is added before every element except the first one

A special case is "absolute" positioning, which is really when items are positioned relative to the bounding box of their parent. This type of positioning ignores rows; one just specifies how far from the top, bottom, left, or right edge of the parent the element is to be placed. If an element contains elements inside it, this whole process is repeated again with that element as the parent, hence the recursive function.

##Handling of special cases

  • If an element is wider or taller than its parent, or its width or height was not defined in the XML (and is therefore set to 0), then its width or height is set to the parents' width or height (minus the border if any) respectively.
  • If an element has a width/height specified as a percent, then its dot width/height will be that percentage of the parent's width/height.
  • If an image had to have its width or height modified here due to being too large, then the image will be converted again with a different resulting size.

##Equations ###Vertical position element.y = heightUsed+rownum*elementSpacing+parent.y+parent.border

  • heightUsed = How far down we are on the label
  • rownum*elementSpacing

elementSpacing is how far apart things will be, both vertically and horizontally. If there is only 1 row, rownum is 0 and so no spacing is added. But for each additional row, elementSpacing will be added.

  • parent.y+parent.border is added so that the element is positioned inside its parent

###Horizontal position element.x = widthUsed-element.width+parent.x

  • widthUsed is the amount of dots currently occupied in the row, including the current element and including spacing. element.width is subtracted from this so that it does not include the current element.
  • parent.x is added so that the element is positioned inside its parent

###Absolute ####Vertical position if element.top is not None: element.y = parent.y+element.top+parent.border

  • Simply places the element element.top dots underneath the top edge of its parent

elif element.bottom is not None: element.y = parent.y+parent.height-element.bottom-parent.border-element.height

  • After adding parent.y+parent.height, the top edge is now at the bottom edge of the parent
  • -element.bottom-parent.border-element.height = Move the element up by element.bottom, and then above the parent's border, and then move the origin to the bottom edge (by moving it up by its height)

####Horizontal position Works the same way as vertical, but with horizontal positions and dimensions instead.

Clone this wiki locally