Implemented basic scaling in BasicViewport.
[aesalon.git] / include / artisan / gviewport / Rect.h
blob8c3d57ab91535c1a3e7bc4e05b53c72e7b34b566
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(); }
37 double &left() { return m_left; }
38 double left() const { return m_left; }
40 double &right() { return m_right; }
41 double right() const { return m_right; }
43 double width() const { return m_right - m_left; }
45 double &top() { return m_top; }
46 double top() const { return m_top; }
48 double &bottom() { return m_bottom; }
49 double bottom() const { return m_bottom; }
51 double height() const { return m_bottom - m_top; }
53 Point topLeft() const { return Point(m_left, m_top); }
54 Point topRight() const { return Point(m_right, m_top); }
55 Point bottomLeft() const { return Point(m_left, m_bottom); }
56 Point bottomRight() const { return Point(m_right, m_bottom); }
58 void normalize() {
59 double x1 = m_left, x2 = m_right;
60 double y1 = m_top, y2 = m_bottom;
61 m_left = std::min(x1, x2);
62 m_right = std::max(x1, x2);
63 m_top = std::min(y1, y2);
64 m_bottom = std::max(y1, y2);
67 TreeType::Bound toTreeBound() const {
68 TreeType::Bound b;
69 b.range(0) = TreeType::Range(m_left, m_right);
70 b.range(1) = TreeType::Range(m_top, m_bottom);
71 return b;
74 QRectF toQRect() const {
75 return QRectF(m_left, m_top, width(), height());
78 std::string toString() const {
79 return Util::StreamAsString() << "(" << m_left << ", "
80 << m_top << "), (" << m_right << ", " << m_bottom << ")";
83 Rect operator+(const Point &point) const {
84 return Rect(m_left + point.x(), m_right + point.x(), m_top + point.y(), m_bottom + point.y());
87 void scaleWidth(double factor) {
88 double w = width() / 2;
89 double centreX = m_left + w;
90 w *= factor;
91 m_left = centreX - w;
92 m_right = centreX + w;
95 void scaleHeight(double factor) {
96 double h = height()/2;
97 double centreY = m_top + h;
98 h *= factor;
99 m_top = centreY - h;
100 m_bottom = centreY + h;
104 } // namespace GViewport
105 } // namespace Artisan
107 #endif