QT6 源,十章绘图(5):矩形 QRect 与 QRectF 的源代码带注释

(1) 本矩形是支持流运算操作符的

在这里插入图片描述

(2)对于 QRect 与 QRectF 来讲, 浮点类型 QRectF 是更好的,更容易理解的类型

在这里插入图片描述

(3) 给出 QRectF 的源代码 ,不再举例测试了,因为还是好理解的,好多都是内联函数 :

#ifndef QRECT_H
#define QRECT_H

#include <QtCore/qhashfunctions.h>
#include <QtCore/qmargins.h>
#include <QtCore/qsize.h>
#include <QtCore/qpoint.h>

#ifdef topLeft
#error qrect.h must be included before any header file that defines topLeft
#endif

#if defined(Q_OS_DARWIN) || defined(Q_QDOC)
struct CGRect;
#endif

QT_BEGIN_NAMESPACE

Q_CORE_EXPORT QDataStream & operator<<(QDataStream &, const QRectF &);
Q_CORE_EXPORT QDataStream & operator>>(QDataStream &,       QRectF &);
Q_CORE_EXPORT QDebug        operator<<(QDebug       , const QRectF &);

class Q_CORE_EXPORT QRectF
{
private:
    qreal xp;
    qreal yp;
    qreal w ;
    qreal h ;

public:
    constexpr QRectF() noexcept : xp(0.), yp(0.), w(0.), h(0.) {}

    constexpr QRectF(qreal left, qreal top, qreal width, qreal height) noexcept
        :   xp(left), yp(top), w(width), h(height) {  }

    constexpr QRectF(const QPointF & topleft, const QSizeF &  size       ) noexcept
        :   xp(topLeft.x()), yp(topLeft.y()), w(size.width()), h(size.height()) { }

    constexpr QRectF(const QPointF & topleft, const QPointF & bottomRight) noexcept
        :   xp(topLeft.x()), yp(topLeft.y()),
            w(bottomRight.x() - topLeft.x()), h(bottomRight.y() - topLeft.y())  { }

    constexpr QRectF(const QRect    & rect ) noexcept // copy构造函数
        :   xp(rect.x()), yp(rect.y()), w(rect.width()), h(rect.height())       { }


    constexpr inline bool isNull () const noexcept { return w == 0. && h == 0.; }
    constexpr inline bool isEmpty() const noexcept { return w <= 0. || h <= 0.; }
    constexpr inline bool isValid() const noexcept { return w >  0. && h >  0.; }

    //返回一个归一化的矩形;即,具有非负宽度和高度的矩形。
    //如果width()<0,函数交换左角和右角;如果height()<0,它还交换上角和下角。
    [[nodiscard]] QRectF normalized() const noexcept;

    constexpr inline qreal    x     ()    const noexcept { return xp;     }
    constexpr inline qreal    y     ()    const noexcept { return yp;     }
    constexpr inline void  setX     (qreal pos) noexcept { setLeft(pos);  }
    constexpr inline void  setY     (qreal pos) noexcept { setTop (pos);  }

    constexpr inline qreal    left  ()    const noexcept { return xp;     }
    constexpr inline qreal    top   ()    const noexcept { return yp;     }
    constexpr inline qreal    right ()    const noexcept { return xp + w; }
    constexpr inline qreal    bottom()    const noexcept { return yp + h; }

    constexpr inline void  setLeft  (qreal pos) noexcept
    {   qreal diff = pos - xp; xp += diff; w -= diff; }

    constexpr inline void  setTop   (qreal pos) noexcept
    {   qreal diff = pos - yp; yp += diff; h -= diff; }

    constexpr inline void  setRight (qreal pos) noexcept { w = pos - xp; }
    constexpr inline void  setBottom(qreal pos) noexcept { h = pos - yp; }

    constexpr inline QPointF  topLeft    () const noexcept { return QPointF(xp  , yp  ); }
    constexpr inline QPointF  topRight   () const noexcept { return QPointF(xp+w, yp  ); }
    constexpr inline QPointF  bottomLeft () const noexcept { return QPointF(xp  , yp+h); }
    constexpr inline QPointF  bottomRight() const noexcept { return QPointF(xp+w, yp+h); }
    constexpr inline QPointF  center     () const noexcept
    {   return QPointF(xp + w/2, yp + h/2); }

    constexpr inline void  setTopLeft    (const QPointF & p) noexcept
    {   setLeft (p.x()); setTop   (p.y()); }

    constexpr inline void  setTopRight   (const QPointF & p) noexcept
    {   setRight(p.x()); setTop   (p.y()); }

    constexpr inline void  setBottomLeft (const QPointF & p) noexcept
    {   setLeft (p.x()); setBottom(p.y()); }

    constexpr inline void  setBottomRight(const QPointF & p) noexcept
    {   setRight(p.x()); setBottom(p.y()); }

    constexpr inline qreal    width ()  const noexcept { return w;            }
    constexpr inline qreal    height()  const noexcept { return h;            }
    constexpr inline QSizeF   size  ()  const noexcept { return QSizeF(w, h); }
    constexpr inline void  setWidth (qreal w) noexcept { this->w = w;         }
    constexpr inline void  setHeight(qreal h) noexcept { this->h = h;         }
    constexpr inline void  setSize  (const QSizeF & s) noexcept
    {   w = s.width();   h = s.height();   }

    constexpr inline void  getRect  (qreal * x, qreal * y, qreal * w, qreal * h) const
    {
        * x = this->xp;
        * y = this->yp;
        * w = this->w ;
        * h = this->h ;
    }
    constexpr inline void  setRect  (qreal   x, qreal   y, qreal   w, qreal   h) noexcept
    {
        this->xp = x;
        this->yp = y;
        this->w =  w;
        this->h =  h;
    }

    constexpr inline void  getCoords(qreal *x1, qreal *y1, qreal *x2, qreal *y2) const
    {                                        //按左上右下对角顶点,来设置矩形
        * x1 = xp;
        * y1 = yp;
        * x2 = xp + w;
        * y2 = yp + h;
    }
    constexpr inline void  setCoords(qreal x1, qreal y1, qreal x2, qreal y2) noexcept
    {
        xp = x1;
        yp = y1;
        w = x2 - x1;
        h = y2 - y1;
    }

    [[nodiscard]] constexpr inline
    QRect toRect() const noexcept
    {   // This rounding is designed to minimize the maximum possible difference
        // in topLeft(), bottomRight(), and size() after rounding.
        // All dimensions are at most off by 0.75, and topLeft by at most 0.5.
        //这种舍入设计旨在在舍入后最小化topLeft()、bottomRight()和size()之间的最大可能差异。
        //所有尺寸的最大偏差为0.75,其中topLeft的偏差最大为0.5。
        const int nxp = qRound(xp);
        const int nyp = qRound(yp);
        const int nw  = qRound(w + (xp - nxp) / 2);
        const int nh  = qRound(h + (yp - nyp) / 2);
        return QRect(nxp, nyp, nw, nh);
    }

    [[nodiscard]]
    QRect toAlignedRect() const noexcept;
    //根据此矩形的值返回一个QRect,该矩形是最小可能的整数矩形,完全包含此矩形。

    inline     //如果给定点(x, y) 在矩形内部或边界上,则返回true;否则返回false。
    bool       contains(qreal x, qreal  y) const noexcept
    {   return contains(QPointF(x, y));   }
    bool       contains(const QPointF & p) const noexcept;
    bool       contains(const QRectF  & r) const noexcept;
    //如果给定的矩形 r 在该矩形内部,则返回true;否则返回false。

    //返回一个由 margins扩展的矩形。注意其形参类型,矩形因增加边距而增大了
    //class QMarginsF { qreal m_left; qreal m_top; qreal m_right; qreal m_bottom; };
    constexpr inline QRectF marginsAdded  (const QMarginsF & margins) const noexcept
    {
        return  QRectF(QPointF(xp - margins.left(), yp - margins.top()),
                      QSizeF(w + margins.left() + margins.right(),
                             h + margins.top() + margins.bottom()));
    }
    constexpr inline QRectF marginsRemoved(const QMarginsF & margins) const noexcept
    {
        return  QRectF(QPointF(xp + margins.left(), yp + margins.top()), //矩形变小
                      QSizeF(w - margins.left() - margins.right(),
                             h - margins.top() - margins.bottom()));
    }

    constexpr inline void moveLeft       (      qreal     pos) noexcept { xp = pos;     }
    constexpr inline void moveTop        (      qreal     pos) noexcept { yp = pos;     }
    constexpr inline void moveRight      (      qreal     pos) noexcept { xp = pos - w; }
    constexpr inline void moveBottom     (      qreal     pos) noexcept { yp = pos - h; }

    constexpr inline void moveCenter     (const QPointF & p  ) noexcept
    {   xp = p.x() - w/2; yp = p.y() - h/2; }

    constexpr inline void moveTopLeft    (const QPointF & p  ) noexcept
    {   moveLeft (p.x()); moveTop   (p.y()); }
    constexpr inline void moveTopRight   (const QPointF & p  ) noexcept
    {   moveRight(p.x()); moveTop   (p.y()); }
    constexpr inline void moveBottomLeft (const QPointF & p  ) noexcept
    {   moveLeft (p.x()); moveBottom(p.y()); }
    constexpr inline void moveBottomRight(const QPointF & p  ) noexcept
    {   moveRight(p.x()); moveBottom(p.y()); }

    constexpr inline void moveTo(qreal x, qreal  y) noexcept { xp = x;     yp = y;     }
    constexpr inline void moveTo(const QPointF & p) noexcept { xp = p.x(); yp = p.y(); }

    //平移操作: 函数 moveTo(..)的形参是坐标, 平移 translate(..)的形参是增量
    constexpr inline   //平移操作
    void   translate (qreal dx, qreal dy) noexcept { xp += dx   ;  yp += dy   ; }
    constexpr inline
    void   translate (const QPointF & p ) noexcept { xp += p.x();  yp += p.y(); }

    [[nodiscard]] constexpr inline //返回本矩形如此平移后的新矩形
    QRectF translated(qreal dx, qreal dy) const noexcept
    {   return QRectF(xp + dx, yp + dy, w, h);    }
    [[nodiscard]] constexpr inline
    QRectF translated(const QPointF & p ) const noexcept
    {   return QRectF(xp + p.x(), yp + p.y(), w, h); }

    [[nodiscard]] constexpr inline     //原点不变,只交换宽高的数值
    QRectF transposed() const noexcept //转置, 返回一个交换了宽度和高度的矩形副本:
    {   return QRectF(topLeft(), size().transposed()); }

    constexpr inline  //这个函数不好,不易于理解
    void   adjust    (qreal x1, qreal y1, qreal x2, qreal y2) noexcept
    {
        xp += x1;
        yp += y1;
        w  += x2 - x1;
        h  += y2 - y1;
    }
    [[nodiscard]] constexpr inline
    QRectF adjusted  (qreal x1, qreal y1, qreal x2, qreal y2) const noexcept
    {
        return QRectF(xp + x1, yp + y1, w + x2 - x1, h + y2 - y1);
    }

    friend constexpr inline bool operator==(const QRectF & r1, const QRectF & r2) noexcept
    {
        return r1.topLeft() == r2.topLeft() && r1.size() == r2.size();
    }
    friend constexpr inline bool operator!=(const QRectF & r1, const QRectF & r2) noexcept
    {
        return r1.topLeft() != r2.topLeft() || r1.size() != r2.size();
    }

    [[nodiscard]] inline  //并集
    QRectF united     (const QRectF & other) const noexcept { return * this | other;  }
    [[nodiscard]] inline
    QRectF intersected(const QRectF & other) const noexcept { return * this & other;  }
    bool   intersects (const QRectF & other) const noexcept; //交集

    QRectF   operator | (const QRectF & r) const noexcept;   //无注释
    QRectF   operator & (const QRectF & r) const noexcept;

    inline
    QRectF & operator |=(const QRectF & r) noexcept
    {
        *this = *this | r;
        return  *this;
    }
    inline
    QRectF & operator &=(const QRectF & r) noexcept
    {
        *this = *this & r;
        return  *this;
    }

    constexpr inline
    QRectF & operator +=(const QMarginsF & margins) noexcept
    {
        *this = marginsAdded(margins);
        return *this;
    }
    constexpr inline
    QRectF & operator -=(const QMarginsF & margins) noexcept
    {
        *this = marginsRemoved(margins);
        return *this;
    }


}; //完结 class Q_CORE_EXPORT QRectF
Q_DECLARE_TYPEINFO(QRectF, Q_RELOCATABLE_TYPE);



constexpr inline  //复杂的全局函数
QRectF operator + (const QRectF & lhs, const QMarginsF & rhs) noexcept
{
    return  QRectF(QPointF(lhs.left() - rhs.left(), lhs.top() - rhs.top()),
                    QSizeF(lhs.width() + rhs.left() + rhs.right(),
                            lhs.height() + rhs.top() + rhs.bottom())    );
}

constexpr inline
QRectF operator + (const QMarginsF & lhs, const QRectF & rhs) noexcept
{
    return  QRectF(QPointF(rhs.left() - lhs.left(), rhs.top() - lhs.top()),
                    QSizeF(rhs.width() + lhs.left() + lhs.right(),
                            rhs.height() + lhs.top() + lhs.bottom())    );
}

constexpr inline
QRectF operator - (const QRectF & lhs, const QMarginsF & rhs) noexcept
{
    return  QRectF(QPointF(lhs.left() + rhs.left(), lhs.top() + rhs.top()),
                    QSizeF(lhs.width() - rhs.left() - rhs.right(),
                            lhs.height() - rhs.top() - rhs.bottom())    );
}


QT_END_NAMESPACE

#endif // QRECT_H




(4) QRect 由于历史遗留原因,定义的不好,里面的有些成员函数是有 1 像素的误差的,只可以通过左上角和宽高来操作与访问整数矩形,这时,这些成员函数的功能,形参,与语义是等价于 QRectF 的

Q_CORE_EXPORT QDataStream & operator<<(QDataStream &, const QRect & );
Q_CORE_EXPORT QDataStream & operator>>(QDataStream &,       QRect & );
Q_CORE_EXPORT QDebug        operator<<(QDebug       , const QRect & );

/*
The QRect class defines a rectangle in the plane using integer precision.

Detailed Description :
矩形通常被表示为左上角和尺寸。QRect的尺寸(宽度和高度)始终与其染所基于的数学矩形相对应。

-个QRect可以通过一组左、上、宽和高的整数来构造,或者通过一个QPoint和一个QSize来构造。
下面的代码创建了两个相同的矩形。
    QRect r1(100, 200, 11, 16);
    QRect r2(QPoint(100, 200), QSize(11, 16));

存在第三个构造函数,它使用左上角和右下角坐标来创建 QRect,但我们建议您避免使用该构造函数。
原因在于,出于历史原因,由bottom()和right()函数返回的值与矩形的确切右下角位置存在偏差。

QRect类提供了一组函数,这些函数返回不同的矩形坐标,并允许对这些坐标进行操作。
QRect还提供了相对于不同坐标移动矩形的函数。
此外,还有moveTo()函数,用于移动矩形,使其左上角位于给定的坐标处。
另外,translate()函数用于相对于当前位置移动矩形,
而translated()函数则返回经过移动变换后的矩形副本。

size()、函数返回矩形的尺寸,以`QSize`类型表示。这些尺寸也可以通过`width()'和`height()函数单独获取。
要调整尺寸,可以使用`setSize()、setWidth()或`setHeight()'函数。
此外,通过调用设置矩形坐标(例如`setBottom()`或`setRight()')的函数,也可以改变其尺寸。

contains()、函数用于判断给定的点是否在矩形内,
而`intersects()'函数则返回一个布尔值,表示该矩形是否与给定的矩形相交。
`QRect~类还提供了`intersected()'函数,用于返回相交的矩形, 即交集。
以及'united()'函数,用于返回包含给定矩形及该矩形的矩形。包含这两个矩形。即并集。

isEmpty()`函数会在`left()`大于`right()`或`top()'大于`bottom()'时返回true。
请注意,空矩形是不合法的。
`isValid()、函数会在`left()<=`right()'和`top()<=`bottom()时返回 true。
另一方面,空矩形 isNull() == true 的宽度和高度都被设为 0。

请注意,由于QRect和QRectF的定义方式,空QRect基本上以与QRectF相同的方式定义。

最后,QRect对象可以被流式传输和比较。

Rendering  :
在使用抗锯齿绘图器时,QRect的边界线将在数学矩形边界线的两侧被对称渲染。
但使用默认的未抗锯齿绘图器时,其他规则将适用。
然后,当使用宽度为一像素的笔绘制时,QRect的边界线将被绘制在数学矩形边界线的右侧和下方。

当使用两个像素宽的笔绘制时,边界线会在中间被数学矩形分割。
这将是每当笔被设置为偶数像素的情况下,
而用钢笔与奇数像素渲染,多余的像素将染到数学矩形的右边和下面的一个像素的情况。

Coordinates :
QRect类提供了一组函数,这些函数返回各种矩形坐标,并允许对这些坐标进行操作.
QRect还提供了相对于各种坐标移动矩形的函数。
例如,`left() setLeft()`和`moveLeft()'函数如下:`left()`函数返回矩形方边缘的x坐标,
`setLeft()、函数将矩形的左边缘设置为给定的x坐标(这可能会改变宽度,但绝不会改变矩形的右边缘),
而`moveLeft()、函数则横向移动整个矩形,使矩形的左边缘位于给定的x坐标处,而其大小保持不变。

请注意,由于历史原因,`bottom()、和`right()、函数返回的值与矩形的确切底右角存在偏差:
`right、函数返回的是`eft()'加上`width()'减1,
而`bottom()、函数返回的是`top()'加上`height、减1。
同样,`bottomRight()、便捷函数返回的点也存在相同的偏差。
此外,`topRight()、和bottomLeft()函数中的 x和 y坐标分别包含与真实右边缘和底边缘相同的偏差。

我们建议您使用x()+width()和y()+height()来找到真正的右下角位置,避免使用right()和bottom()。
另一个解决方案是使用QRectF类:QRectF类使用浮点数精度定义平面上的矩形,
而QRectF::right()和QRectF::bottom()函数确实返回右和底坐标。

此外,还可通过使用`adiust()、函数来为这个矩形的坐标添加偏移量,
以及使用adjusted()`函数根据对原始矩形的调整来获取一个新的矩形。
如果宽度和高度中任一个为负值,则应使用`normalized()、函数来获取一个角位置互换的矩形。

此外,QRect提供了getCoords()函数,用于提取矩形左上角的坐标和右下角的坐标,
以及getRect()函数,用于提取矩形的左上角坐标、宽度和高度。
使用setCoords()和setRect()函数可以一次性操作矩形的坐标和大小。

Constraints :
QRect仅限于int类型的最小值和最大值。对QRect执行的操作如果可能产生超出此范围的结果,将导致未定义行为。

*/

class Q_CORE_EXPORT QRect
{
private:
    int x1;
    int y1;
    int x2; //由于历史原因, x2 = x1 + 宽度 - 1
    int y2; //由于历史原因, y2 = y1 + 高度 - 1,所以很别扭。

public:
    constexpr QRect() noexcept : x1(0), y1(0), x2(-1), y2(-1) {} //构建一个空矩形。

    constexpr QRect(int left, int top, int width, int height) noexcept
        : x1(left), y1(top), x2(left + width - 1), y2(top + height - 1) {}

    constexpr QRect(const QPoint & topleft, const QPoint & bottomright) noexcept
        : x1(topLeft.x()), y1(topLeft.y()), x2(bottomRight.x()), y2(bottomRight.y()) {}

    constexpr QRect(const QPoint & topleft, const QSize  & size) noexcept
        : x1(topLeft.x()), y1(topLeft.y()),
            x2(topLeft.x() + size.width() - 1), y2(topLeft.y() + size.height() - 1) {}

    //返回一个包含两点p1和p2的矩形,包括这两点及其之间的所有点。
    [[nodiscard]] static constexpr inline
    QRect span(const QPoint & p1, const QPoint & p2) noexcept
    {
        return  QRect(QPoint(qMin(p1.x(), p2.x()), qMin(p1.y(), p2.y())),
                     QPoint(qMax(p1.x(), p2.x()), qMax(p1.y(), p2.y())));
    }


    constexpr inline bool isNull () const noexcept { return x2 == x1 - 1 && y2 == y1 - 1; }
    constexpr inline bool isEmpty() const noexcept { return x1 >  x2 || y1 >  y2; }
    constexpr inline bool isValid() const noexcept { return x1 <= x2 && y1 <= y2; }

    constexpr inline int     x() const noexcept { return x1; }
    constexpr inline int     y() const noexcept { return y1; }
    constexpr inline void setX(int x)  noexcept { x1 = x;    }
    constexpr inline void setY(int y)  noexcept { y1 = y;    }

    constexpr inline int     left  () const  noexcept { return x1; }
    constexpr inline int     top   () const  noexcept { return y1; }
    constexpr inline int     right () const  noexcept { return x2; }
    constexpr inline int     bottom() const  noexcept { return y2; }
    constexpr inline void setLeft  (int pos) noexcept { x1 = pos;  }
    constexpr inline void setTop   (int pos) noexcept { y1 = pos;  }
    constexpr inline void setRight (int pos) noexcept { x2 = pos;  }
    constexpr inline void setBottom(int pos) noexcept { y2 = pos;  }
    //从这里就看出来了,由于历史原因。数据成员的设置不合理。赋值时的形参不合理,虽然只差了一像素。


    [[nodiscard]] QRect normalized() const noexcept;

    constexpr inline QPoint topLeft    () const noexcept { return QPoint(x1, y1); }
    constexpr inline QPoint bottomRight() const noexcept { return QPoint(x2, y2); }
    constexpr inline QPoint topRight   () const noexcept { return QPoint(x2, y1); }
    constexpr inline QPoint bottomLeft () const noexcept { return QPoint(x1, y2); }

    constexpr inline QPoint center() const noexcept // cast avoids overflow on addition
    { return QPoint(int((qint64(x1)+x2)/2), int((qint64(y1)+y2)/2)); } //也有误差

    constexpr inline void setTopLeft    (const QPoint & p) noexcept
    {   x1 = p.x(); y1 = p.y(); }
    constexpr inline void setBottomRight(const QPoint & p) noexcept
    {   x2 = p.x(); y2 = p.y(); }
    constexpr inline void setTopRight   (const QPoint & p) noexcept
    {   x2 = p.x(); y1 = p.y(); }
    constexpr inline void setBottomLeft (const QPoint & p) noexcept
    {   x1 = p.x(); y2 = p.y(); }


    constexpr inline void setRect(int x, int y, int w, int h) noexcept //形参还是正确的
    {   //设置矩形的左上角起点与宽高
        x1 = x;
        y1 = y;  //整数矩形内部还是采用了很别扭的存储
        x2 = (x + w - 1);
        y2 = (y + h - 1);
    }
    constexpr inline void getRect(int * x, int * y, int * w, int * h) const
    {   //得到矩形的左上角起点与宽高
        * x = x1;
        * y = y1;
        * w = x2 - x1 + 1;
        * h = y2 - y1 + 1;
    }


    constexpr inline void setCoords(int xp1, int yp1, int xp2, int yp2) noexcept
    {   //根据左上角与右下角的点坐标设置矩形,很不好
        x1 = xp1;
        y1 = yp1;
        x2 = xp2;
        y2 = yp2;
    }
    constexpr inline void getCoords(int * xp1, int * yp1, int * xp2, int * yp2) const
    {
        * xp1 = x1;
        * yp1 = y1;
        * xp2 = x2;
        * yp2 = y2;
    }


    constexpr inline int       width () const noexcept { return  x2 - x1 + 1; }
    constexpr inline int       height() const noexcept { return  y2 - y1 + 1; }
    constexpr inline QSize     size  () const noexcept { return QSize(width(), height()); }
    constexpr inline void   setWidth (int w)  noexcept { x2 = (x1 + w - 1); }
    constexpr inline void   setHeight(int h)  noexcept { y2 = (y1 + h - 1); }
    constexpr inline void   setSize  (const QSize & s) noexcept
    {
        x2 = (s.width () + x1 - 1);  //又是转化为如此别扭的内部存储
        y2 = (s.height() + y1 - 1);
    }


    inline
    bool contains(int x, int y) const noexcept
    {
        return contains(QPoint(x, y), false);
    }
    inline
    bool contains(int x, int y, bool proper) const noexcept
    {
        return contains(QPoint(x, y), proper);
    }
    //如果给定的矩形 r 位于此矩形内部,则返回true;否则返回false。
    //如果 proper为 true,则只有当该矩形完全位于此矩形内部(而非位于边缘)时,此函数才会返回 true。
    bool contains(const QRect  & r, bool proper = false) const noexcept;
    bool contains(const QPoint & p, bool proper = false) const noexcept;


    constexpr inline void moveLeft  (int pos) noexcept //整体移动矩形,使左边界位于 pos位置
    {   x2 += (pos - x1); x1 = pos; }
    constexpr inline void moveTop   (int pos) noexcept
    {   y2 += (pos - y1); y1 = pos; }
    constexpr inline void moveRight (int pos) noexcept
    {   x1 += (pos - x2); x2 = pos; }
    constexpr inline void moveBottom(int pos) noexcept
    {   y1 += (pos - y2); y2 = pos; }

    constexpr inline void moveTopLeft    (const QPoint & p) noexcept
    {   moveLeft(p.x());     moveTop(p.y());    }
    constexpr inline void moveBottomRight(const QPoint & p) noexcept
    {   moveRight(p.x());    moveBottom(p.y()); }
    constexpr inline void moveTopRight   (const QPoint & p) noexcept
    {   moveRight(p.x());    moveTop(p.y());    }
    constexpr inline void moveBottomLeft (const QPoint & p) noexcept
    {   moveLeft(p.x());     moveBottom(p.y()); }
    constexpr inline void moveCenter     (const QPoint & p) noexcept //把矩形中心点挪到 p位置
    {
        int w = x2 - x1;
        int h = y2 - y1;
        x1 = p.x() - w/2;
        y1 = p.y() - h/2;
        x2 = x1 + w;
        y2 = y1 + h;
    }

    constexpr inline
    void     translate (int dx, int    dy) noexcept
    {
        x1 += dx;
        y1 += dy;
        x2 += dx;
        y2 += dy;
    }
    constexpr inline
    void     translate (const QPoint & p ) noexcept
    {
        x1 += p.x();
        y1 += p.y();
        x2 += p.x();
        y2 += p.y();
    }
    [[nodiscard]] constexpr inline
    QRect    translated(int dx,  int   dy) const noexcept
    {   return QRect(QPoint(x1 + dx, y1 + dy), QPoint(x2 + dx, y2 + dy));   }
    [[nodiscard]] constexpr inline
    QRect    translated(const QPoint & p ) const noexcept
    {   return QRect(QPoint(x1 + p.x(), y1 + p.y()), QPoint(x2 + p.x(), y2 + p.y())); }
    [[nodiscard]] constexpr inline
    QRect    transposed() const noexcept //转置是左上角的原点不变,转置矩形
    {   return QRect(topLeft(), size().transposed()); }


    constexpr inline void moveTo(int ax, int ay) noexcept
    {
        x2 += ax - x1; //先计算(x2, y2), 后计算(x1, y1)的顺序是对的
        y2 += ay - y1;
        x1  = ax;
        y1  = ay;
    }

    constexpr inline void moveTo(const QPoint & p) noexcept
    {
        x2 += p.x() - x1;
        y2 += p.y() - y1;
        x1  = p.x();
        y1  = p.y();
    }

    constexpr inline
    void  adjust  (int dx1, int dy1, int dx2, int dy2) noexcept //这俩函数不易理解,不好
    {
        x1 += dx1;
        y1 += dy1;
        x2 += dx2;
        y2 += dy2;
    }
    [[nodiscard]] constexpr inline
    QRect adjusted(int xp1, int yp1, int xp2, int yp2) const noexcept
    {   return QRect(QPoint(x1 + xp1, y1 + yp1), QPoint(x2 + xp2, y2 + yp2)); }


    QRect    operator |  (const QRect & r) const noexcept;
    QRect    operator &  (const QRect & r) const noexcept;
    inline
    QRect &  operator |= (const QRect & r) noexcept { *this = *this | r;  return *this; }
    inline
    QRect &  operator &= (const QRect & r) noexcept { *this = *this & r;  return *this; }


    [[nodiscard]] inline
    QRect   united     (const QRect & other) const noexcept { return *this | other; }
    [[nodiscard]] inline
    QRect   intersected(const QRect & other) const noexcept { return *this & other; }
    bool    intersects (const QRect & other) const noexcept;

    constexpr inline QRect & operator+=(const QMargins & margins) noexcept
    {
        *this = marginsAdded(margins);
        return *this;
    }
    constexpr inline QRect & operator-=(const QMargins & margins) noexcept
    {
        *this = marginsRemoved(margins);
        return *this;
    }



    constexpr inline QRect marginsAdded  (const QMargins & margins) const noexcept
    {
        return  QRect(QPoint(x1 - margins.left(), y1 - margins.top()),
                     QPoint(x2 + margins.right(), y2 + margins.bottom()));
    }
    constexpr inline QRect marginsRemoved(const QMargins & margins) const noexcept
    {
        return  QRect(QPoint(x1 + margins.left(), y1 + margins.top()),
                     QPoint(x2 - margins.right(), y2 - margins.bottom()));
    }


    friend constexpr inline
    bool operator== (const QRect & r1, const QRect & r2) noexcept
    { return r1.x1==r2.x1 && r1.x2==r2.x2 && r1.y1==r2.y1 && r1.y2==r2.y2; }

    friend constexpr inline
    bool operator!= (const QRect & r1, const QRect & r2) noexcept
    { return r1.x1!=r2.x1 || r1.x2!=r2.x2 || r1.y1!=r2.y1 || r1.y2!=r2.y2; }

    friend constexpr inline size_t qHash(const QRect &, size_t) noexcept;


}; //完结 class Q_CORE_EXPORT QRect
Q_DECLARE_TYPEINFO(QRect, Q_RELOCATABLE_TYPE);

constexpr inline
size_t qHash(const QRect & r, size_t seed = 0) noexcept
{
    return qHashMulti(seed, r.x1, r.x2, r.y1, r.y2);
}

constexpr inline
QRect operator + (const QRect    & rectangle, const QMargins & margins  ) noexcept
{
    return QRect(QPoint(rectangle.left() - margins.left(), rectangle.top() - margins.top()),
                 QPoint(rectangle.right() + margins.right(), rectangle.bottom() + margins.bottom()));
}

constexpr inline
QRect operator + (const QMargins & margins  , const QRect    & rectangle) noexcept
{
    return QRect(QPoint(rectangle.left() - margins.left(), rectangle.top() - margins.top()),
                 QPoint(rectangle.right() + margins.right(), rectangle.bottom() + margins.bottom()));
}

constexpr inline
QRect operator - (const QRect    & lhs      , const QMargins & rhs      ) noexcept
{
    return QRect(QPoint(lhs.left() + rhs.left(), lhs.top() + rhs.top()),
                 QPoint(lhs.right() - rhs.right(), lhs.bottom() - rhs.bottom()));
}


(5)

谢谢

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangzhangkeji

谢谢支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值