KoRect.h
1/* This file is part of the KDE project
2 Copyright (C) 2001 David Faure <faure@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2, as published by the Free Software Foundation.
7
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
12
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 * Boston, MA 02110-1301, USA.
17*/
18
19#ifndef koRect_h
20#define koRect_h
21
22#include "KoPoint.h"
23#include "KoSize.h"
24#include <tqrect.h>
25#include "koffice_export.h"
26
31class KOFFICEUI_EXPORT KoRect {
32
33public:
34 KoRect()
35 : m_tl(), m_br() {}
36 KoRect(const KoPoint &topleft, const KoPoint &bottomright)
37 : m_tl(topleft), m_br(bottomright) {}
38 KoRect(const KoPoint &topleft, const KoSize &size)
39 {m_tl = topleft; setSize(size);}
40 KoRect(const double &left, const double &top, const double &width, const double &height)
41 : m_tl(left,top), m_br(left+width,top+height) {}
42 ~KoRect() {}
43
44 bool isNull() const { return m_tl == m_br; }
45 // Like TQRect, a null KoRect is empty.
46 bool isEmpty() const { return m_tl.x() > m_br.x() || m_tl.y() > m_br.y() || isNull(); }
47 // Unlike TQRect, a null KoRect is valid (0-sized).
48 bool isValid() const { return m_tl.x() <= m_br.x() && m_tl.y() <= m_br.y(); }
49 KoRect normalize() const;
50
51 double left() const { return m_tl.x(); }
52 double top() const { return m_tl.y(); }
53 double right() const { return m_br.x(); }
54 double bottom() const { return m_br.y(); }
55
56 double& rLeft() { return m_tl.rx(); }
57 double& rTop() { return m_tl.ry(); }
58 double& rRight() { return m_br.rx(); }
59 double& rBottom() { return m_br.ry(); }
60
61 double x() const { return left(); }
62 double y() const { return top(); }
63
64 void setLeft(const double &left) { m_tl.setX(left); }
65 void setTop(const double &top) { m_tl.setY(top); }
66 void setRight(const double &right) { m_br.setX(right); }
67 void setBottom(const double &bottom) { m_br.setY(bottom); }
68
69 void setX(const double &x) { m_tl.setX(x); } //same as setLeft()
70 void setY(const double &y) { m_tl.setY(y); } //same as setTop()
71
72 KoPoint topLeft() const { return m_tl; }
73 KoPoint bottomRight() const { return m_br; }
74 KoPoint topRight() const { return KoPoint(m_br.x(), m_tl.y()); }
75 KoPoint bottomLeft() const { return KoPoint(m_tl.x(), m_br.y()); }
76 KoPoint center() const;
77
78 void setTopLeft(const KoPoint &topleft);
79 void setBottomRight(const KoPoint &bottomright);
80 void setTopRight(const KoPoint &topright);
81 void setBottomLeft(const KoPoint &bottomleft);
82
83 void moveTopLeft(const KoPoint &topleft);
84 void moveBottomRight(const KoPoint &bottomright);
85 void moveTopRight(const KoPoint &topright);
86 void moveBottomLeft(const KoPoint &bottomleft);
87 //void moveCenter(const KoPoint &center);
88 void moveBy(const double &dx, const double &dy);
89
90 void setRect(const double &x, const double &y, const double &width, const double &height);
91 void setRect(const KoRect &rect);
92 void setCoords(const double &x1, const double &y1, const double &x2, const double &y2);
93
94 KoSize size() const;
95 double width() const { return m_br.x()-m_tl.x(); }
96 double height() const { return m_br.y()-m_tl.y(); }
97 void setWidth(const double &width) { m_br.setX(m_tl.x()+width); }
98 void setHeight(const double &height) { m_br.setY(m_tl.y()+height); }
99 void setSize(const KoSize &size);
100
101 KoRect &operator|=(const KoRect &rhs);
102 KoRect &operator&=(const KoRect &rhs);
111 bool contains(const KoPoint &p) const;
113 bool contains(const double &x, const double &y) const;
115 bool contains(const KoRect &r) const;
116 KoRect unite(const KoRect &r) const;
117 KoRect intersect(const KoRect &r) const;
118 bool intersects(const KoRect &r) const;
119
120 KoRect transform(const TQWMatrix &m) const;
121 KoRect translate(double dx, double dy) const;
122
123 TQRect toTQRect() const;
124 static KoRect fromTQRect( const TQRect &rect );
125
126private:
127 KoPoint m_tl, m_br;
128};
129
130KoRect operator|(const KoRect &lhs, const KoRect &rhs);
131KoRect operator&(const KoRect &lhs, const KoRect &rhs);
132KOFFICEUI_EXPORT bool operator==(const KoRect &lhs, const KoRect &rhs);
133KOFFICEUI_EXPORT bool operator!=(const KoRect &lhs, const KoRect &rhs);
134
135
137#define DEBUGRECT(rc) (rc).x() << "," << (rc).y() << " " << (rc).width() << "x" << (rc).height()
138
139//inline kdbgstream operator<<( kdbgstream str, const KoRect & r ) { str << "[" << r.left() << ", " << r.top() << " - " << r.right() << ", " << r.bottom() << "]"; return str; }
140inline kdbgstream operator<<( kdbgstream str, const KoRect & r ) { str << "[" << r.left() << "," << r.top() << " " << r.width() << "x" << r.height() << "]"; return str; }
141inline kndbgstream operator<<( kndbgstream str, const KoRect & ) { return str; }
142
144#define DEBUGREGION(reg) { TQMemArray<TQRect>rs=reg.rects(); for (int i=0;i<rs.size();++i) \
145 kdDebug()<<" "<<DEBUGRECT(rs[i] )<<endl; }
146// You can now use kdDebug() << theregion << endl; (kdebug.h)
147
148#endif
A point whose coordinates are floating-point values ( "double"s ).
Definition KoPoint.h:30
A rect whose coordinates are floating-point values ( "double"s ).
Definition KoRect.h:31
A size whose coordinates are floating-point values ( "double"s ).
Definition KoSize.h:34