Skip to content

RFC: core: Introduce Box and BoxF

Vlad Zahorodnii requested to merge work/zzag/box into master

The rectangle type is one of the cornerstones for a well functioning compositor. Its behavior must be intuitive and it is critical to have no any pitfalls in the api. Unfortunately, it is not the case with QRect and QRectF, e.g. the famous issue of QRect::right() being off by one, QRectF::contains() not working as expected, QRect::moveCenter() being broken, etc.

The rectangle type is expected to have some origin and size. In addition to that, the right and the bottom edges are exclusive.

This change introduces two types to represent rectangles: Box and BoxF. Box is a counterpart for QRect, and BoxF is a counterpart for QRectF. The API of Box and BoxF is very similar to QRect and QRectF, respectively, except a few things:

  • Box and BoxF must be normalized, rectangles with negative width and height are considered as invalid, and functions such as contains() won't handle them specially
  • no isValid() and isNull() functions
  • BoxF has the rounded() function instead of QRectF::toRect()
  • BoxF::rounded() rounds the coordinates of the left, the top, the right, and the bottom edge. QRectF::toRect() has some clever logic to preserve the rect size, which usually results in unexpected results when rendering
  • BoxF has the roundedOut() function instead of QRectF::toAlignedRect()
  • Box::right() is equivalent to x() + width()
  • Box::bottom() is equivalent to y() + height()

  • add tests
  • need to port at least something to Box and BoxF
Edited by Vlad Zahorodnii

Merge request reports