Minor changes here and there.
[aesalon.git] / include / artisan / gviewport / Rect.h
blob5db81a8f85f9e43c0d576f8f78b72096530f5ffc
1 /** Aesalon, a tool to visualize program behaviour in real time.
2 Copyright (C) 2009-2011, Aesalon development team.
4 Aesalon is distributed under the terms of the GNU GPLv3. See
5 the included file LICENSE for more information.
7 @file include/artisan/gviewport/Rect.h
8 */
10 #ifndef AesalonArtisan_GViewport_Rect_H
11 #define AesalonArtisan_GViewport_Rect_H
13 #include <QRect>
14 #include <QSize>
16 #include "TreeType.h"
17 #include "Point.h"
19 #include "util/StreamAsString.h"
21 namespace Artisan {
22 namespace GViewport {
24 class Rect {
25 private:
26 double m_left, m_right;
27 double m_top, m_bottom;
28 public:
29 Rect() : m_left(0), m_right(0), m_top(0), m_bottom(0) {}
30 Rect(double width, double height) : m_left(0), m_right(width), m_top(0), m_bottom(height) { normalize(); }
31 Rect(QSizeF size) : m_left(0), m_right(size.width()), m_top(0), m_bottom(size.height()) { normalize(); }
32 Rect(double left, double right, double top, double bottom)
33 : m_left(left), m_right(right), m_top(top), m_bottom(bottom) { normalize(); }
34 Rect(Point ul, Point lr) :
35 m_left(ul.x()), m_right(lr.x()), m_top(ul.y()), m_bottom(lr.y()) { normalize(); }
36 Rect(Point ul, double width, double height)
37 : m_left(ul.x()), m_right(ul.x() + width), m_top(ul.y()), m_bottom(ul.y() + height) { normalize(); }
39 double &left() { return m_left; }
40 double left() const { return m_left; }
42 double &right() { return m_right; }
43 double right() const { return m_right; }
45 double width() const { return m_right - m_left; }
47 double &top() { return m_top; }
48 double top() const { return m_top; }
50 double &bottom() { return m_bottom; }
51 double bottom() const { return m_bottom; }
53 double height() const { return m_bottom - m_top; }
55 Point topLeft() const { return Point(m_left, m_top); }
56 Point topRight() const { return Point(m_right, m_top); }
57 Point bottomLeft() const { return Point(m_left, m_bottom); }
58 Point bottomRight() const { return Point(m_right, m_bottom); }
60 void normalize() {
61 double x1 = m_left, x2 = m_right;
62 double y1 = m_top, y2 = m_bottom;
63 m_left = std::min(x1, x2);
64 m_right = std::max(x1, x2);
65 m_top = std::min(y1, y2);
66 m_bottom = std::max(y1, y2);
69 TreeType::Bound toTreeBound() const {
70 TreeType::Bound b;
71 b.range(0) = TreeType::Range(m_left, m_right);
72 b.range(1) = TreeType::Range(m_top, m_bottom);
73 return b;
76 QRectF toQRect() const {
77 return QRectF(m_left, m_top, width(), height());
80 std::string toString() const {
81 return Util::StreamAsString() << "(" << m_left << ", "
82 << m_top << "), (" << m_right << ", " << m_bottom << ")";
85 Rect operator+(const Point &point) const {
86 return Rect(m_left + point.x(), m_right + point.x(), m_top + point.y(), m_bottom + point.y());
89 void scaleWidth(double factor) {
90 double w = width() / 2;
91 double centreX = m_left + w;
92 w *= factor;
93 m_left = centreX - w;
94 m_right = centreX + w;
97 void scaleHeight(double factor) {
98 double h = height()/2;
99 double centreY = m_top + h;
100 h *= factor;
101 m_top = centreY - h;
102 m_bottom = centreY + h;
106 } // namespace GViewport
107 } // namespace Artisan
109 #endif