1 /****************************************************************************
2 ** $Id: frect.h 23705 2020-05-05 22:44:11Z craig $
3 **
4 ** Definition of FRect class
5 **
6 ** Created : 931028
7 **
8 ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
9 **
10 ** This file is part of the kernel module of the Qt GUI Toolkit.
11 **
12 ** This file may be distributed under the terms of the Q Public License
13 ** as defined by Trolltech AS of Norway and appearing in the file
14 ** LICENSE.QPL included in the packaging of this file.
15 **
16 ** This file may be distributed and/or modified under the terms of the
17 ** GNU General Public License version 2 as published by the Free Software
18 ** Foundation and appearing in the file LICENSE.GPL included in the
19 ** packaging of this file.
20 **
21 ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
22 ** licenses may use this file in accordance with the Qt Commercial License
23 ** Agreement provided with the Software.
24 **
25 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
26 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27 **
28 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
29 ** information about Qt Commercial License Agreements.
30 ** See http://www.trolltech.com/qpl/ for QPL licensing information.
31 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
32 **
33 ** Contact info@trolltech.com if any conditions of this licensing are
34 ** not clear to you.
35 **
36 **********************************************************************/
37
38 #ifndef FRECT_H
39 #define FRECT_H
40
41 #include "fsize.h"
42
43 #if defined(topLeft)
44 #error "Macro definition of topLeft conflicts with FRect"
45 // don't just silently undo people's defines: #undef topLeft
46 #endif
47
48 class FRect // rectangle class
49 {
50 public:
FRect()51 FRect() {}
52 FRect(FPoint &topleft, FPoint &bottomright);
53 FRect(FPoint &topleft, FSize &size);
54 FRect(qreal left, qreal top, qreal width, qreal height);
55
56 bool isNull() const;
57 bool isEmpty() const;
58 bool isValid() const;
59 FRect normalize() const;
60
61 qreal left() const;
62 qreal top() const;
63 qreal right() const;
64 qreal bottom() const;
65
66 qreal &rLeft();
67 qreal &rTop();
68 qreal &rRight();
69 qreal &rBottom();
70
71 qreal x() const;
72 qreal y() const;
73 void setLeft(qreal pos);
74 void setTop(qreal pos);
75 void setRight(qreal pos);
76 void setBottom(qreal pos);
77 void setX(qreal x);
78 void setY(qreal y);
79
80 void setTopLeft(FPoint &p);
81 void setBottomRight(FPoint &p);
82 void setTopRight(FPoint &p);
83 void setBottomLeft(FPoint &p);
84
85 FPoint topLeft() const;
86 FPoint bottomRight() const;
87 FPoint topRight() const;
88 FPoint bottomLeft() const;
89 FPoint center() const;
90
91 void rect(qreal *x, qreal *y, qreal *w, qreal *h) const;
92 void coords(qreal *xp1, qreal *yp1, qreal *xp2, qreal *yp2) const;
93
94 void moveLeft(qreal pos);
95 void moveTop(qreal pos);
96 void moveRight(qreal pos);
97 void moveBottom(qreal pos);
98 void moveTopLeft(FPoint &p);
99 void moveBottomRight(FPoint &p);
100 void moveTopRight(FPoint &p);
101 void moveBottomLeft(FPoint &p);
102 void moveCenter(FPoint &p);
103 void moveBy(qreal dx, qreal dy);
104
105 void setRect(qreal x, qreal y, qreal w, qreal h);
106 void setCoords(qreal xp1, qreal yp1, qreal xp2, qreal yp);
107 void addCoords(qreal xp1, qreal yp1, qreal xp2, qreal yp2);
108
109 FSize size() const;
110 qreal width() const;
111 qreal height() const;
112 void setWidth(qreal w);
113 void setHeight(qreal h);
114 void setSize(const FSize &s);
115
116 FRect operator|(const FRect &r) const;
117 FRect operator&(const FRect &r) const;
118 FRect& operator|=(const FRect &r);
119 FRect& operator&=(const FRect &r);
120
121 bool contains(FPoint &p, bool proper=false) const;
122 bool contains(qreal x, qreal y) const; // inline methods, _don't_ merge these
123 bool contains(qreal x, qreal y, bool proper) const;
124 bool contains(const FRect &r, bool proper=false) const;
125 FRect unite(const FRect &r) const;
126 FRect intersect(const FRect &r) const;
127 bool intersects(const FRect &r) const;
128
129 friend bool operator==(const FRect &, const FRect &);
130 friend bool operator!=(const FRect &, const FRect &);
131
132 private:
133 #if defined(Q_OS_LINUX) || defined(Q_OS_TEMP)
134 friend void qt_setCoords(FRect *r, qreal xp1, qreal yp1, qreal xp2, qreal yp2);
135 #endif
136 qreal x1 {0.0};
137 qreal y1 {0.0};
138 qreal x2 {-1.0};
139 qreal y2 {-1.0};
140 };
141
142 bool operator==(const FRect &, const FRect &);
143 bool operator!=(const FRect &, const FRect &);
144
145
146 /*****************************************************************************
147 FRect stream functions
148 *****************************************************************************/
149 // #ifndef QT_NO_DATASTREAM
150 // Q_EXPORT QDataStream &operator<<(QDataStream &, const FRect &);
151 // Q_EXPORT QDataStream &operator>>(QDataStream &, FRect &);
152 // #endif
153
154 /*****************************************************************************
155 FRect inline member functions
156 *****************************************************************************/
157
FRect(qreal left,qreal top,qreal width,qreal height)158 inline FRect::FRect(qreal left, qreal top, qreal width, qreal height)
159 {
160 x1 = (qreal)left;
161 y1 = (qreal)top;
162 x2 = (qreal)(left+width-1);
163 y2 = (qreal)(top+height-1);
164 }
165
isNull()166 inline bool FRect::isNull() const
167 { return x2 == x1-1 && y2 == y1-1; }
168
isEmpty()169 inline bool FRect::isEmpty() const
170 { return x1 > x2 || y1 > y2; }
171
isValid()172 inline bool FRect::isValid() const
173 { return x1 <= x2 && y1 <= y2; }
174
left()175 inline qreal FRect::left() const
176 { return x1; }
177
top()178 inline qreal FRect::top() const
179 { return y1; }
180
right()181 inline qreal FRect::right() const
182 { return x2; }
183
bottom()184 inline qreal FRect::bottom() const
185 { return y2; }
186
rLeft()187 inline qreal &FRect::rLeft()
188 { return x1; }
189
rTop()190 inline qreal & FRect::rTop()
191 { return y1; }
192
rRight()193 inline qreal & FRect::rRight()
194 { return x2; }
195
rBottom()196 inline qreal & FRect::rBottom()
197 { return y2; }
198
x()199 inline qreal FRect::x() const
200 { return x1; }
201
y()202 inline qreal FRect::y() const
203 { return y1; }
204
setLeft(qreal pos)205 inline void FRect::setLeft(qreal pos)
206 { x1 = (qreal)pos; }
207
setTop(qreal pos)208 inline void FRect::setTop(qreal pos)
209 { y1 = (qreal)pos; }
210
setRight(qreal pos)211 inline void FRect::setRight(qreal pos)
212 { x2 = (qreal)pos; }
213
setBottom(qreal pos)214 inline void FRect::setBottom(qreal pos)
215 { y2 = (qreal)pos; }
216
setX(qreal x)217 inline void FRect::setX(qreal x)
218 { x1 = (qreal)x; }
219
setY(qreal y)220 inline void FRect::setY(qreal y)
221 { y1 = (qreal)y; }
222
topLeft()223 inline FPoint FRect::topLeft() const
224 { return FPoint(x1, y1); }
225
bottomRight()226 inline FPoint FRect::bottomRight() const
227 { return FPoint(x2, y2); }
228
topRight()229 inline FPoint FRect::topRight() const
230 { return FPoint(x2, y1); }
231
bottomLeft()232 inline FPoint FRect::bottomLeft() const
233 { return FPoint(x1, y2); }
234
center()235 inline FPoint FRect::center() const
236 { return FPoint((x1+x2)/2, (y1+y2)/2); }
237
width()238 inline qreal FRect::width() const
239 { return x2 - x1 + 1; }
240
height()241 inline qreal FRect::height() const
242 { return y2 - y1 + 1; }
243
size()244 inline FSize FRect::size() const
245 { return FSize(x2-x1+1, y2-y1+1); }
246
contains(qreal x,qreal y,bool proper)247 inline bool FRect::contains(qreal x, qreal y, bool proper) const
248 {
249 if (proper)
250 return x > x1 && x < x2 &&
251 y > y1 && y < y2;
252 else
253 return x >= x1 && x <= x2 &&
254 y >= y1 && y <= y2;
255 }
256
contains(qreal x,qreal y)257 inline bool FRect::contains(qreal x, qreal y) const
258 {
259 return x >= x1 && x <= x2 &&
260 y >= y1 && y <= y2;
261 }
262
263 #endif // FRECT_H
264