Back to index

scribus-ng  1.3.4.dfsg+svn20071115
frect.cpp
Go to the documentation of this file.
00001 /****************************************************************************
00002 ** $Id: frect.cpp 6144 2006-08-15 23:30:58Z avox $
00003 **
00004 ** Implementation of FRect class
00005 **
00006 ** Created : 931028
00007 **
00008 ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
00009 **
00010 ** This file is part of the kernel module of the Qt GUI Toolkit.
00011 **
00012 ** This file may be distributed under the terms of the Q Public License
00013 ** as defined by Trolltech AS of Norway and appearing in the file
00014 ** LICENSE.QPL included in the packaging of this file.
00015 **
00016 ** This file may be distributed and/or modified under the terms of the
00017 ** GNU General Public License version 2 as published by the Free Software
00018 ** Foundation and appearing in the file LICENSE.GPL included in the
00019 ** packaging of this file.
00020 **
00021 ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
00022 ** licenses may use this file in accordance with the Qt Commercial License
00023 ** Agreement provided with the Software.
00024 **
00025 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00026 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00027 **
00028 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
00029 **   information about Qt Commercial License Agreements.
00030 ** See http://www.trolltech.com/qpl/ for QPL licensing information.
00031 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00032 **
00033 ** Contact info@trolltech.com if any conditions of this licensing are
00034 ** not clear to you.
00035 **
00036 **********************************************************************/
00037 
00038 #define        QRECT_C
00039 #include "frect.h"
00040 //#include "qdatastream.h"
00041 
00091 /*****************************************************************************
00092   FRect member functions
00093  *****************************************************************************/
00094 
00106 FRect::FRect( FPoint &topLeft, FPoint &bottomRight )
00107 {
00108     x1 = (double)topLeft.x();
00109     y1 = (double)topLeft.y();
00110     x2 = (double)bottomRight.x();
00111     y2 = (double)bottomRight.y();
00112 }
00113 
00119 FRect::FRect( FPoint &topLeft, FSize &size )
00120 {
00121     x1 = (double)topLeft.x();
00122     y1 = (double)topLeft.y();
00123     x2 = (double)(x1+size.width());
00124     y2 = (double)(y1+size.height());
00125 }
00126 
00199 FRect FRect::normalize() const
00200 {
00201     FRect r;
00202     if ( x2 < x1 ) {                      // swap bad x values
00203        r.x1 = x2;
00204        r.x2 = x1;
00205     } else {
00206        r.x1 = x1;
00207        r.x2 = x2;
00208     }
00209     if ( y2 < y1 ) {                      // swap bad y values
00210        r.y1 = y2;
00211        r.y2 = y1;
00212     } else {
00213        r.y1 = y1;
00214        r.y2 = y2;
00215     }
00216     return r;
00217 }
00218 
00219 
00371 void FRect::setTopLeft( FPoint &p )
00372 {
00373     setLeft( p.x() );
00374     setTop( p.y() );
00375 }
00376 
00384 void FRect::setBottomRight( FPoint &p )
00385 {
00386     setRight( p.x() );
00387     setBottom( p.y() );
00388 }
00389 
00397 void FRect::setTopRight( FPoint &p )
00398 {
00399     setRight( p.x() );
00400     setTop( p.y() );
00401 }
00402 
00410 void FRect::setBottomLeft( FPoint &p )
00411 {
00412     setLeft( p.x() );
00413     setBottom( p.y() );
00414 }
00415 
00464 void FRect::rect( double *x, double *y, double *w, double *h ) const
00465 {
00466     *x = x1;
00467     *y = y1;
00468     *w = x2-x1;
00469     *h = y2-y1;
00470 }
00471 
00479 void FRect::coords( double *xp1, double *yp1, double *xp2, double *yp2 ) const
00480 {
00481     *xp1 = x1;
00482     *yp1 = y1;
00483     *xp2 = x2;
00484     *yp2 = y2;
00485 }
00486 
00487 
00494 void FRect::moveLeft( double pos )
00495 {
00496     x2 += (double)(pos - x1);
00497     x1 = (double)pos;
00498 }
00499 
00507 void FRect::moveTop( double pos )
00508 {
00509     y2 += (double)(pos - y1);
00510     y1 = (double)pos;
00511 }
00512 
00520 void FRect::moveRight( double pos )
00521 {
00522     x1 += (double)(pos - x2);
00523     x2 = (double)pos;
00524 }
00525 
00533 void FRect::moveBottom( double pos )
00534 {
00535     y1 += (double)(pos - y2);
00536     y2 = (double)pos;
00537 }
00538 
00546 void FRect::moveTopLeft( FPoint &p )
00547 {
00548     moveLeft( p.x() );
00549     moveTop( p.y() );
00550 }
00551 
00559 void FRect::moveBottomRight( FPoint &p )
00560 {
00561     moveRight( p.x() );
00562     moveBottom( p.y() );
00563 }
00564 
00572 void FRect::moveTopRight( FPoint &p )
00573 {
00574     moveRight( p.x() );
00575     moveTop( p.y() );
00576 }
00577 
00585 void FRect::moveBottomLeft( FPoint &p )
00586 {
00587     moveLeft( p.x() );
00588     moveBottom( p.y() );
00589 }
00590 
00591 
00599 void FRect::moveCenter( FPoint &p )
00600 {
00601     double w = x2 - x1;
00602     double h = y2 - y1;
00603     x1 = (double)(p.x() - w/2);
00604     y1 = (double)(p.y() - h/2);
00605     x2 = x1 + w;
00606     y2 = y1 + h;
00607 }
00608 
00609 
00618 void FRect::moveBy( double dx, double dy )
00619 {
00620     x1 += (double)dx;
00621     y1 -= (double)dy;
00622     x2 += (double)dx;
00623     y2 -= (double)dy;
00624 }
00625 
00633 void FRect::setRect( double x, double y, double w, double h )
00634 {
00635     x1 = (double)x;
00636     y1 = (double)y;
00637     x2 = (double)(x+w);
00638     y2 = (double)(y+h);
00639 }
00640 
00649 void FRect::setCoords( double xp1, double yp1, double xp2, double yp2 )
00650 {
00651     x1 = (double)xp1;
00652     y1 = (double)yp1;
00653     x2 = (double)xp2;
00654     y2 = (double)yp2;
00655 }
00656 
00662 void FRect::addCoords( double xp1, double yp1, double xp2, double yp2 )
00663 {
00664     x1 += (double)xp1;
00665     y1 += (double)yp1;
00666     x2 += (double)xp2;
00667     y2 += (double)yp2;
00668 }
00669 
00703 void FRect::setWidth( double w )
00704 {
00705     x2 = (double)(x1 + w);
00706 }
00707 
00715 void FRect::setHeight( double h )
00716 {
00717     y2 = (double)(y1 + h);
00718 }
00719 
00727 void FRect::setSize( const FSize &s )
00728 {
00729     x2 = (double)(s.width() +x1);
00730     y2 = (double)(s.height()+y1);
00731 }
00732 
00741 bool FRect::contains( FPoint &p, bool proper ) const
00742 {
00743     if ( proper )
00744        return p.x() > x1 && p.x() < x2 &&
00745               p.y() > y1 && p.y() < y2;
00746     else
00747        return p.x() >= x1 && p.x() <= x2 &&
00748               p.y() >= y1 && p.y() <= y2;
00749 }
00750 
00780 bool FRect::contains( const FRect &r, bool proper ) const
00781 {
00782     if ( proper )
00783        return r.x1 > x1 && r.x2 < x2 && r.y1 > y1 && r.y2 < y2;
00784     else
00785        return r.x1 >= x1 && r.x2 <= x2 && r.y1 >= y1 && r.y2 <= y2;
00786 }
00787 
00791 FRect& FRect::operator|=(const FRect &r)
00792 {
00793     *this = *this | r;
00794     return *this;
00795 }
00796 
00800 FRect& FRect::operator&=(const FRect &r)
00801 {
00802     *this = *this & r;
00803     return *this;
00804 }
00805 
00806 
00817 FRect FRect::operator|(const FRect &r) const
00818 {
00819     if ( isValid() ) {
00820        if ( r.isValid() ) {
00821            FRect tmp;
00822            tmp.setLeft(   QMIN( x1, r.x1 ) );
00823            tmp.setRight(  QMAX( x2, r.x2 ) );
00824            tmp.setTop(         QMIN( y1, r.y1 ) );
00825            tmp.setBottom( QMAX( y2, r.y2 ) );
00826            return tmp;
00827        } else {
00828            return *this;
00829        }
00830     } else {
00831        return r;
00832     }
00833 }
00834 
00839 FRect FRect::unite( const FRect &r ) const
00840 {
00841     return *this | r;
00842 }
00843 
00844 
00853 FRect FRect::operator&( const FRect &r ) const
00854 {
00855     FRect tmp;
00856     tmp.x1 = QMAX( x1, r.x1 );
00857     tmp.x2 = QMIN( x2, r.x2 );
00858     tmp.y1 = QMAX( y1, r.y1 );
00859     tmp.y2 = QMIN( y2, r.y2 );
00860     return tmp;
00861 }
00862 
00867 FRect FRect::intersect( const FRect &r ) const
00868 {
00869     return *this & r;
00870 }
00871 
00880 bool FRect::intersects( const FRect &r ) const
00881 {
00882     return ( QMAX( x1, r.x1 ) <= QMIN( x2, r.x2 ) &&
00883             QMAX( y1, r.y1 ) <= QMIN( y2, r.y2 ) );
00884 }
00885 
00886 
00893 bool operator==( const FRect &r1, const FRect &r2 )
00894 {
00895     return r1.x1==r2.x1 && r1.x2==r2.x2 && r1.y1==r2.y1 && r1.y2==r2.y2;
00896 }
00897 
00904 bool operator!=( const FRect &r1, const FRect &r2 )
00905 {
00906     return r1.x1!=r2.x1 || r1.x2!=r2.x2 || r1.y1!=r2.y1 || r1.y2!=r2.y2;
00907 }
00908 
00909 
00910 // /*****************************************************************************
00911 //   FRect stream functions
00912 //  *****************************************************************************/
00913 // #ifndef QT_NO_DATASTREAM
00914 // /*!
00915 //     \relates FRect
00916 //
00917 //     Writes the FRect, \a r, to the stream \a s, and returns a
00918 //     reference to the stream.
00919 //
00920 //     \sa \link datastreamformat.html Format of the QDataStream operators \endlink
00921 // */
00922 //
00923 // QDataStream &operator<<( QDataStream &s, const FRect &r )
00924 // {
00925 //     if ( s.version() == 1 )
00926 //     s << (Q_INT16)r.left() << (Q_INT16)r.top()
00927 //       << (Q_INT16)r.right() << (Q_INT16)r.bottom();
00928 //     else
00929 //     s << (Q_INT32)r.left() << (Q_INT32)r.top()
00930 //       << (Q_INT32)r.right() << (Q_INT32)r.bottom();
00931 //     return s;
00932 // }
00933 //
00934 // /*!
00935 //     \relates FRect
00936 //
00937 //     Reads a FRect from the stream \a s into rect \a r and returns a
00938 //     reference to the stream.
00939 //
00940 //     \sa \link datastreamformat.html Format of the QDataStream operators \endlink
00941 // */
00942 //
00943 // QDataStream &operator>>( QDataStream &s, FRect &r )
00944 // {
00945 //     if ( s.version() == 1 ) {
00946 //     Q_INT16 x1, y1, x2, y2;
00947 //     s >> x1; s >> y1; s >> x2; s >> y2;
00948 //     r.setCoords( x1, y1, x2, y2 );
00949 //     }
00950 //     else {
00951 //     Q_INT32 x1, y1, x2, y2;
00952 //     s >> x1; s >> y1; s >> x2; s >> y2;
00953 //     r.setCoords( x1, y1, x2, y2 );
00954 //     }
00955 //     return s;
00956 // }
00957 // #endif // QT_NO_DATASTREAM