lib Library API Documentation

koRect.h

00001 /* This file is part of the KDE project
00002    Copyright (C) 2001 David Faure <faure@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License version 2, as published by the Free Software Foundation.
00007 
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012 
00013    You should have received a copy of the GNU Library General Public License
00014    along with this library; see the file COPYING.LIB.  If not, write to
00015    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00016    Boston, MA 02111-1307, USA.
00017 */
00018 
00019 #ifndef koRect_h
00020 #define koRect_h
00021 
00022 #include "koPoint.h"
00023 #include "koSize.h"
00024 #include <qrect.h>
00025 
00030 class KoRect {
00031 
00032 public:
00033     KoRect()
00034         : m_tl(), m_br() {}
00035     KoRect(const KoPoint &topleft, const KoPoint &bottomright)
00036         : m_tl(topleft), m_br(bottomright) {}
00037     KoRect(const KoPoint &topleft, const KoSize &size)
00038         {m_tl = topleft; setSize(size);}
00039     KoRect(const double &left, const double &top, const double &width, const double &height)
00040         : m_tl(left,top), m_br(left+width,top+height) {}
00041     ~KoRect() {}
00042 
00043     bool isNull() const { return m_tl == m_br; }
00044     // Like QRect, a null KoRect is empty.
00045     bool isEmpty() const { return m_tl.x() > m_br.x() || m_tl.y() > m_br.y() || isNull(); }
00046     // Unlike QRect, a null KoRect is valid (0-sized).
00047     bool isValid() const { return m_tl.x() <= m_br.x() && m_tl.y() <= m_br.y(); }
00048     KoRect normalize() const;
00049 
00050     double left() const { return m_tl.x(); }
00051     double top() const { return m_tl.y(); }
00052     double right() const { return m_br.x(); }
00053     double bottom() const { return m_br.y(); }
00054 
00055     double& rLeft() { return m_tl.rx(); }
00056     double& rTop() { return m_tl.ry(); }
00057     double& rRight() { return m_br.rx(); }
00058     double& rBottom() { return m_br.ry(); }
00059 
00060     double x() const { return left(); }
00061     double y() const { return top(); }
00062 
00063     void setLeft(const double &left) { m_tl.setX(left); }
00064     void setTop(const double &top) { m_tl.setY(top); }
00065     void setRight(const double &right) { m_br.setX(right); }
00066     void setBottom(const double &bottom) { m_br.setY(bottom); }
00067 
00068     void setX(const double &x) { m_tl.setX(x); } //same as setLeft()
00069     void setY(const double &y) { m_tl.setY(y); } //same as setTop()
00070 
00071     KoPoint topLeft() const { return m_tl; }
00072     KoPoint bottomRight() const { return m_br; }
00073     KoPoint topRight() const { return KoPoint(m_br.x(), m_tl.y()); }
00074     KoPoint bottomLeft() const { return KoPoint(m_tl.x(), m_br.y()); }
00075     KoPoint center() const;
00076 
00077     void moveTopLeft(const KoPoint &topleft);
00078     void moveBottomRight(const KoPoint &bottomright);
00079     void moveTopRight(const KoPoint &topright);
00080     void moveBottomLeft(const KoPoint &bottomleft);
00081     //void moveCenter(const KoPoint &center);
00082     void moveBy(const double &dx, const double &dy);
00083 
00084     void setRect(const double &x, const double &y, const double &width, const double &height);
00085     void setRect(const KoRect &rect);
00086     void setCoords(const double &x1, const double &y1, const double &x2, const double &y2);
00087 
00088     KoSize size() const;
00089     double width() const { return m_br.x()-m_tl.x(); }
00090     double height() const { return m_br.y()-m_tl.y(); }
00091     void setWidth(const double &width) { m_br.setX(m_tl.x()+width); }
00092     void setHeight(const double &height) { m_br.setY(m_tl.y()+height); }
00093     void setSize(const KoSize &size);
00094 
00095     KoRect &operator|=(const KoRect &rhs);
00096     KoRect &operator&=(const KoRect &rhs);
00097     bool contains(const KoPoint &p, bool proper=false) const;
00098     bool contains(const double &x, const double &y, bool proper=false) const;
00099     bool contains(const KoRect &r, bool proper=false) const;
00100     KoRect unite(const KoRect &r) const;
00101     KoRect intersect(const KoRect &r) const;
00102     bool intersects(const KoRect &r) const;
00103 
00104     KoRect transform(const QWMatrix &m) const;
00105     KoRect translate(double dx, double dy) const;
00106 
00107     QRect toQRect() const;
00108     static KoRect fromQRect( const QRect &rect );
00109 
00110 private:
00111     KoPoint m_tl, m_br;
00112 };
00113 
00114 KoRect operator|(const KoRect &lhs, const KoRect &rhs);
00115 KoRect operator&(const KoRect &lhs, const KoRect &rhs);
00116 bool operator==(const KoRect &lhs, const KoRect &rhs);
00117 bool operator!=(const KoRect &lhs, const KoRect &rhs);
00118 
00119 
00121 #define DEBUGRECT(rc) (rc).x() << "," << (rc).y() << " " << (rc).width() << "x" << (rc).height()
00122 
00123 //inline kdbgstream operator<<( kdbgstream str, const KoRect & r )  { str << "[" << r.left() << ", " << r.top() << " - " << r.right() << ", " << r.bottom() << "]"; return str; }
00124 inline kdbgstream operator<<( kdbgstream str, const KoRect & r )  { str << "[" << r.left() << "," << r.top() << " " << r.width() << "x" << r.height() << "]"; return str; }
00125 inline kndbgstream operator<<( kndbgstream str, const KoRect & )  { return str; }
00126 
00128 #define DEBUGREGION(reg) { QMemArray<QRect>rs=reg.rects(); for (int i=0;i<rs.size();++i) \
00129                            kdDebug()<<"  "<<DEBUGRECT(rs[i] )<<endl; }
00130 // You can now use kdDebug() << theregion << endl; (kdebug.h)
00131 
00132 #endif
KDE Logo
This file is part of the documentation for lib Library Version 1.3.5.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Nov 17 13:19:24 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2003