00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "KoRect.h"
00020
00021 KoRect KoRect::normalize() const
00022 {
00023 KoRect r;
00024 if ( right() < left() ) {
00025 r.m_tl.setX( right() );
00026 r.m_br.setX( left() );
00027 } else {
00028 r.m_tl.setX( left() );
00029 r.m_br.setX( right() );
00030 }
00031 if ( bottom() < top() ) {
00032 r.m_tl.setY( bottom() );
00033 r.m_br.setY( top() );
00034 } else {
00035 r.m_tl.setY( top() );
00036 r.m_br.setY( bottom() );
00037 }
00038 return r;
00039 }
00040
00041 void KoRect::setTopLeft(const KoPoint &topleft)
00042 {
00043 m_tl = topleft;
00044 }
00045
00046 void KoRect::setBottomRight(const KoPoint &bottomright)
00047 {
00048 m_br = bottomright;
00049 }
00050
00051 void KoRect::setTopRight(const KoPoint &topright)
00052 {
00053 m_br.rx() = topright.x();
00054 m_tl.ry() = topright.y();
00055 }
00056
00057 void KoRect::setBottomLeft(const KoPoint &bottomleft)
00058 {
00059 m_tl.rx() = bottomleft.x();
00060 m_br.ry() = bottomleft.y();
00061 }
00062
00063
00064 KoPoint KoRect::center() const
00065 {
00066 return KoPoint((left() + right()) / 2, (top() + bottom()) / 2);
00067 }
00068
00069 void KoRect::moveTopLeft(const KoPoint &topleft)
00070 {
00071 m_br.rx() += topleft.x() - m_tl.x();
00072 m_br.ry() += topleft.y() - m_tl.y();
00073 m_tl = topleft;
00074 }
00075
00076 void KoRect::moveBottomRight(const KoPoint &bottomright)
00077 {
00078 m_tl.rx() += bottomright.x() - m_br.x();
00079 m_tl.ry() += bottomright.y() - m_br.y();
00080 m_br = bottomright;
00081 }
00082
00083 void KoRect::moveTopRight(const KoPoint &topright)
00084 {
00085 m_tl.rx() += topright.x() - m_br.x();
00086 m_br.ry() += topright.y() - m_tl.y();
00087 m_br.rx() = topright.x();
00088 m_tl.ry() = topright.y();
00089 }
00090
00091 void KoRect::moveBottomLeft(const KoPoint &bottomleft)
00092 {
00093 m_br.rx() += bottomleft.x() - m_tl.x();
00094 m_tl.ry() += bottomleft.y() - m_br.y();
00095 m_tl.rx() = bottomleft.x();
00096 m_br.ry() = bottomleft.y();
00097 }
00098
00099 void KoRect::moveBy(const double &dx, const double &dy)
00100 {
00101 m_tl.rx() += dx;
00102 m_tl.ry() += dy;
00103 m_br.rx() += dx;
00104 m_br.ry() += dy;
00105 }
00106
00107 void KoRect::setRect(const double &x, const double &y, const double &width, const double &height)
00108 {
00109 m_tl.setCoords( x, y );
00110 m_br.setCoords( x + width, y + height );
00111 }
00112
00113 void KoRect::setRect(const KoRect &rect)
00114 {
00115 m_tl = rect.m_tl;
00116 m_br = rect.m_br;
00117 }
00118
00119 void KoRect::setCoords(const double &x1, const double &y1, const double &x2, const double &y2)
00120 {
00121 m_tl.setCoords( x1, y1 );
00122 m_br.setCoords( x2, y2 );
00123 }
00124
00125 void KoRect::setSize(const KoSize &size)
00126 {
00127 setWidth(size.width());
00128 setHeight(size.height());
00129 }
00130
00131 KoSize KoRect::size() const
00132 {
00133 return KoSize(width(), height());
00134 }
00135
00136 KoRect &KoRect::operator|=(const KoRect &rhs) {
00137
00138 if(rhs.isEmpty())
00139 return *this;
00140 if(isEmpty())
00141 {
00142 *this = rhs;
00143 return *this;
00144 }
00145 if(m_tl.x() > rhs.left())
00146 m_tl.setX(rhs.left());
00147 if(m_tl.y() > rhs.top())
00148 m_tl.setY(rhs.top());
00149 if(m_br.x() < rhs.right())
00150 m_br.setX(rhs.right());
00151 if(m_br.y() < rhs.bottom())
00152 m_br.setY(rhs.bottom());
00153 return *this;
00154 }
00155
00156 KoRect &KoRect::operator&=(const KoRect &rhs) {
00157
00158 if(m_tl.x() < rhs.left())
00159 m_tl.setX(rhs.left());
00160 if(m_tl.y() < rhs.top())
00161 m_tl.setY(rhs.top());
00162 if(m_br.x() > rhs.right())
00163 m_br.setX(rhs.right());
00164 if(m_br.y() > rhs.bottom())
00165 m_br.setY(rhs.bottom());
00166 return *this;
00167 }
00168
00169 bool KoRect::contains(const KoPoint &p) const {
00170 return (p.x() >= m_tl.x() && p.x() <= m_br.x() && p.y() >= m_tl.y() && p.y() <= m_br.y());
00171 }
00172
00173 bool KoRect::contains(const double &x, const double &y) const {
00174 return (x >= m_tl.x() && x <= m_br.x() && y >= m_tl.y() && y <= m_br.y());
00175 }
00176
00177 bool KoRect::contains(const KoRect &r) const {
00178 return (r.left() >= m_tl.x() && r.right() <= m_br.x() && r.top() >= m_tl.y() && r.bottom() <= m_br.y());
00179 }
00180
00181
00182 KoRect KoRect::unite(const KoRect &r) const {
00183 return *this | r;
00184 }
00185
00186 KoRect KoRect::intersect(const KoRect &r) const {
00187 return *this & r;
00188 }
00189
00190 bool KoRect::intersects(const KoRect &r) const {
00191 return ( qMax(m_tl.x(), r.left()) <= qMin(m_br.x(), r.right()) &&
00192 qMax(m_tl.y(), r.top()) <= qMin(m_br.y(), r.bottom()) );
00193 }
00194
00195 KoRect operator|(const KoRect &lhs, const KoRect &rhs) {
00196
00197 if(lhs.isEmpty())
00198 return rhs;
00199 if(rhs.isEmpty())
00200 return lhs;
00201 KoRect tmp;
00202 tmp.setCoords( (lhs.left() < rhs.left() ? lhs.left() : rhs.left()),
00203 (lhs.top() < rhs.top() ? lhs.top() : rhs.top()),
00204 (lhs.right() > rhs.right() ? lhs.right() : rhs.right()),
00205 (lhs.bottom() > rhs.bottom() ? lhs.bottom() : rhs.bottom()) );
00206 return tmp;
00207 }
00208
00209 KoRect operator&(const KoRect &lhs, const KoRect &rhs) {
00210
00211 KoRect tmp;
00212 tmp.setCoords( (lhs.left() > rhs.left() ? lhs.left() : rhs.left()),
00213 (lhs.top() > rhs.top() ? lhs.top() : rhs.top()),
00214 (lhs.right() < rhs.right() ? lhs.right() : rhs.right()),
00215 (lhs.bottom() < rhs.bottom() ? lhs.bottom() : rhs.bottom()) );
00216 return tmp;
00217 }
00218
00219 bool operator==(const KoRect &lhs, const KoRect &rhs) {
00220 return ( lhs.topLeft()==rhs.topLeft() &&
00221 lhs.bottomRight()==rhs.bottomRight() );
00222 }
00223
00224 bool operator!=(const KoRect &lhs, const KoRect &rhs) {
00225 return ( lhs.topLeft()!=rhs.topLeft() ||
00226 lhs.bottomRight()!=rhs.bottomRight() );
00227 }
00228
00229 KoRect KoRect::transform(const QMatrix &m) const
00230 {
00231 KoRect result;
00232 if(m.m12() == 0.0F && m.m21() == 0.0F)
00233 {
00234 result = KoRect(topLeft().transform(m), bottomRight().transform(m));
00235 }
00236 else
00237 {
00238 int i;
00239 KoPoint p[4] = { KoPoint(m_tl.x(), m_tl.y()), KoPoint(m_tl.x(), m_br.x()),
00240 KoPoint(m_br.x(), m_br.x()), KoPoint(m_br.x(), m_tl.y()) };
00241 for(i = 0; i < 4; i++)
00242 p[i] = p[i].transform(m);
00243
00244 result.setLeft(p[0].x());
00245 result.setTop(p[0].y());
00246 result.setRight(p[0].x());
00247 result.setBottom(p[0].y());
00248
00249 for(int i = 1; i < 4; i++)
00250 {
00251 result.setLeft(qMin(p[i].x(), result.left()));
00252 result.setTop(qMin(p[i].y(), result.top()));
00253 result.setRight(qMax(p[i].x(), result.right()));
00254 result.setBottom(qMax(p[i].y(), result.bottom()));
00255 }
00256 }
00257 return result;
00258 }
00259
00260 KoRect KoRect::translate(double dx, double dy) const
00261 {
00262 return KoRect(left() + dx, top() + dy, width(), height());
00263 }
00264
00265 QRect KoRect::toQRect() const
00266 {
00267 return QRect( qRound( left() ), qRound( top() ), qRound( width() ), qRound( height() ) );
00268 }
00269
00270
00271 KoRect KoRect::fromQRect( const QRect &rect )
00272 {
00273 return KoRect( rect.left(), rect.top(), rect.width(), rect.height() );
00274 }