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 #include <koffice_export.h>
00026 
00031 class KOFFICEUI_EXPORT KoRect {
00032 
00033 public:
00034     KoRect()
00035         : m_tl(), m_br() {}
00036     KoRect(const KoPoint &topleft, const KoPoint &bottomright)
00037         : m_tl(topleft), m_br(bottomright) {}
00038     KoRect(const KoPoint &topleft, const KoSize &size)
00039         {m_tl = topleft; setSize(size);}
00040     KoRect(const double &left, const double &top, const double &width, const double &height)
00041         : m_tl(left,top), m_br(left+width,top+height) {}
00042     ~KoRect() {}
00043 
00044     bool isNull() const { return m_tl == m_br; }
00045     // Like QRect, a null KoRect is empty.
00046     bool isEmpty() const { return m_tl.x() > m_br.x() || m_tl.y() > m_br.y() || isNull(); }
00047     // Unlike QRect, a null KoRect is valid (0-sized).
00048     bool isValid() const { return m_tl.x() <= m_br.x() && m_tl.y() <= m_br.y(); }
00049     KoRect normalize() const;
00050 
00051     double left() const { return m_tl.x(); }
00052     double top() const { return m_tl.y(); }
00053     double right() const { return m_br.x(); }
00054     double bottom() const { return m_br.y(); }
00055 
00056     double& rLeft() { return m_tl.rx(); }
00057     double& rTop() { return m_tl.ry(); }
00058     double& rRight() { return m_br.rx(); }
00059     double& rBottom() { return m_br.ry(); }
00060 
00061     double x() const { return left(); }
00062     double y() const { return top(); }
00063 
00064     void setLeft(const double &left) { m_tl.setX(left); }
00065     void setTop(const double &top) { m_tl.setY(top); }
00066     void setRight(const double &right) { m_br.setX(right); }
00067     void setBottom(const double &bottom) { m_br.setY(bottom); }
00068 
00069     void setX(const double &x) { m_tl.setX(x); } //same as setLeft()
00070     void setY(const double &y) { m_tl.setY(y); } //same as setTop()
00071 
00072     KoPoint topLeft() const { return m_tl; }
00073     KoPoint bottomRight() const { return m_br; }
00074     KoPoint topRight() const { return KoPoint(m_br.x(), m_tl.y()); }
00075     KoPoint bottomLeft() const { return KoPoint(m_tl.x(), m_br.y()); }
00076     KoPoint center() const;
00077 
00078     void moveTopLeft(const KoPoint &topleft);
00079     void moveBottomRight(const KoPoint &bottomright);
00080     void moveTopRight(const KoPoint &topright);
00081     void moveBottomLeft(const KoPoint &bottomleft);
00082     //void moveCenter(const KoPoint &center);
00083     void moveBy(const double &dx, const double &dy);
00084 
00085     void setRect(const double &x, const double &y, const double &width, const double &height);
00086     void setRect(const KoRect &rect);
00087     void setCoords(const double &x1, const double &y1, const double &x2, const double &y2);
00088 
00089     KoSize size() const;
00090     double width() const { return m_br.x()-m_tl.x(); }
00091     double height() const { return m_br.y()-m_tl.y(); }
00092     void setWidth(const double &width) { m_br.setX(m_tl.x()+width); }
00093     void setHeight(const double &height) { m_br.setY(m_tl.y()+height); }
00094     void setSize(const KoSize &size);
00095 
00096     KoRect &operator|=(const KoRect &rhs);
00097     KoRect &operator&=(const KoRect &rhs);
00098     bool contains(const KoPoint &p, bool proper=false) const;
00099     bool contains(const double &x, const double &y, bool proper=false) const;
00100     bool contains(const KoRect &r, bool proper=false) const;
00101     KoRect unite(const KoRect &r) const;
00102     KoRect intersect(const KoRect &r) const;
00103     bool intersects(const KoRect &r) const;
00104 
00105     KoRect transform(const QWMatrix &m) const;
00106     KoRect translate(double dx, double dy) const;
00107 
00108     QRect toQRect() const;
00109     static KoRect fromQRect( const QRect &rect );
00110 
00111 private:
00112     KoPoint m_tl, m_br;
00113 };
00114 
00115 KoRect operator|(const KoRect &lhs, const KoRect &rhs);
00116 KoRect operator&(const KoRect &lhs, const KoRect &rhs);
00117 KOFFICEUI_EXPORT bool operator==(const KoRect &lhs, const KoRect &rhs);
00118 KOFFICEUI_EXPORT bool operator!=(const KoRect &lhs, const KoRect &rhs);
00119 
00120 
00122 #define DEBUGRECT(rc) (rc).x() << "," << (rc).y() << " " << (rc).width() << "x" << (rc).height()
00123 
00124 //inline kdbgstream operator<<( kdbgstream str, const KoRect & r )  { str << "[" << r.left() << ", " << r.top() << " - " << r.right() << ", " << r.bottom() << "]"; return str; }
00125 inline kdbgstream operator<<( kdbgstream str, const KoRect & r )  { str << "[" << r.left() << "," << r.top() << " " << r.width() << "x" << r.height() << "]"; return str; }
00126 inline kndbgstream operator<<( kndbgstream str, const KoRect & )  { return str; }
00127 
00129 #define DEBUGREGION(reg) { QMemArray<QRect>rs=reg.rects(); for (int i=0;i<rs.size();++i) \
00130                            kdDebug()<<"  "<<DEBUGRECT(rs[i] )<<endl; }
00131 // You can now use kdDebug() << theregion << endl; (kdebug.h)
00132 
00133 #endif
KDE Logo
This file is part of the documentation for lib Library Version 1.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Feb 13 09:40:04 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003