src/gui/painting/qpolygon.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.
00004 **
00005 ** This file is part of the QtGui module of the Qt Toolkit.
00006 **
00007 ** This file may be used under the terms of the GNU General Public
00008 ** License version 2.0 as published by the Free Software Foundation
00009 ** and appearing in the file LICENSE.GPL included in the packaging of
00010 ** this file.  Please review the following information to ensure GNU
00011 ** General Public Licensing requirements will be met:
00012 ** http://www.trolltech.com/products/qt/opensource.html
00013 **
00014 ** If you are unsure which license is appropriate for your use, please
00015 ** review the following information:
00016 ** http://www.trolltech.com/products/qt/licensing.html or contact the
00017 ** sales department at sales@trolltech.com.
00018 **
00019 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00020 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00021 **
00022 ****************************************************************************/
00023 
00024 #include "qpolygon.h"
00025 #include "qrect.h"
00026 #include "qdatastream.h"
00027 #include "qmatrix.h"
00028 #include "qdebug.h"
00029 #include "qpainterpath.h"
00030 #include "qvariant.h"
00031 #include "qpainterpath_p.h"
00032 #include "qbezier_p.h"
00033 
00034 #include <stdarg.h>
00035 
00077 /*****************************************************************************
00078   QPolygon member functions
00079  *****************************************************************************/
00080 
00129 QPolygon::QPolygon(const QRect &r, bool closed)
00130 {
00131     reserve(closed ? 5 : 4);
00132     *this << QPoint(r.x(), r.y())
00133           << QPoint(r.x() + r.width(), r.y())
00134           << QPoint(r.x() + r.width(), r.y() + r.height())
00135           << QPoint(r.x(), r.y() + r.height());
00136     if (closed)
00137         *this << QPoint(r.left(), r.top());
00138 }
00139 
00148 QPolygon::QPolygon(int nPoints, const int *points)
00149 {
00150     setPoints(nPoints, points);
00151 }
00152 
00153 
00165 void QPolygon::translate(int dx, int dy)
00166 {
00167     register QPoint *p = data();
00168     register int i = size();
00169     QPoint pt(dx, dy);
00170     while (i--) {
00171         *p += pt;
00172         ++p;
00173     }
00174 }
00175 
00191 void QPolygon::point(int index, int *x, int *y) const
00192 {
00193     QPoint p = at(index);
00194     if (x)
00195         *x = (int)p.x();
00196     if (y)
00197         *y = (int)p.y();
00198 }
00199 
00238 void QPolygon::setPoints(int nPoints, const int *points)
00239 {
00240     resize(nPoints);
00241     int i = 0;
00242     while (nPoints--) {
00243         setPoint(i++, *points, *(points+1));
00244         points += 2;
00245     }
00246 }
00247 
00265 void QPolygon::setPoints(int nPoints, int firstx, int firsty, ...)
00266 {
00267     va_list ap;
00268     resize(nPoints);
00269     setPoint(0, firstx, firsty);
00270     int i = 0, x, y;
00271     va_start(ap, firsty);
00272     while (--nPoints) {
00273         x = va_arg(ap, int);
00274         y = va_arg(ap, int);
00275         setPoint(++i, x, y);
00276     }
00277     va_end(ap);
00278 }
00279 
00291 void QPolygon::putPoints(int index, int nPoints, const int *points)
00292 {
00293     if (index + nPoints > size())
00294         resize(index + nPoints);
00295     int i = index;
00296     while (nPoints--) {
00297         setPoint(i++, *points, *(points+1));
00298         points += 2;
00299     }
00300 }
00301 
00329 void QPolygon::putPoints(int index, int nPoints, int firstx, int firsty, ...)
00330 {
00331     va_list ap;
00332     if (index + nPoints > size())
00333         resize(index + nPoints);
00334     if (nPoints <= 0)
00335         return;
00336     setPoint(index, firstx, firsty);
00337     int i = index, x, y;
00338     va_start(ap, firsty);
00339     while (--nPoints) {
00340         x = va_arg(ap, int);
00341         y = va_arg(ap, int);
00342         setPoint(++i, x, y);
00343     }
00344     va_end(ap);
00345 }
00346 
00347 
00362 void QPolygon::putPoints(int index, int nPoints, const QPolygon & from, int fromIndex)
00363 {
00364     if (index + nPoints > size())
00365         resize(index + nPoints);
00366     if (nPoints <= 0)
00367         return;
00368     int n = 0;
00369     while(n < nPoints) {
00370         setPoint(index + n, from[fromIndex+n]);
00371         ++n;
00372     }
00373 }
00374 
00375 
00383 QRect QPolygon::boundingRect() const
00384 {
00385     if (isEmpty())
00386         return QRect(0, 0, 0, 0);
00387     register const QPoint *pd = constData();
00388     int minx, maxx, miny, maxy;
00389     minx = maxx = pd->x();
00390     miny = maxy = pd->y();
00391     ++pd;
00392     for (int i = 1; i < size(); ++i) {
00393         if (pd->x() < minx)
00394             minx = pd->x();
00395         else if (pd->x() > maxx)
00396             maxx = pd->x();
00397         if (pd->y() < miny)
00398             miny = pd->y();
00399         else if (pd->y() > maxy)
00400             maxy = pd->y();
00401         ++pd;
00402     }
00403     return QRect(QPoint(minx,miny), QPoint(maxx,maxy));
00404 }
00405 
00406 #ifndef QT_NO_DEBUG_STREAM
00407 QDebug operator<<(QDebug dbg, const QPolygon &a)
00408 {
00409 #ifndef Q_BROKEN_DEBUG_STREAM
00410     dbg.nospace() << "QPolygon(";
00411     for (int i = 0; i < a.count(); ++i)
00412         dbg.nospace() << a.at(i);
00413     dbg.nospace() << ')';
00414     return dbg.space();
00415 #else
00416     qWarning("This compiler doesn't support streaming QPolygon to QDebug");
00417     return dbg;
00418     Q_UNUSED(a);
00419 #endif
00420 }
00421 #endif
00422 
00423 
00459 /*****************************************************************************
00460   QPolygonF member functions
00461  *****************************************************************************/
00462 
00503 QPolygonF::QPolygonF(const QRectF &r)
00504 {
00505     reserve(5);
00506     append(QPointF(r.x(), r.y()));
00507     append(QPointF(r.x() + r.width(), r.y()));
00508     append(QPointF(r.x() + r.width(), r.y() + r.height()));
00509     append(QPointF(r.x(), r.y() + r.height()));
00510     append(QPointF(r.x(), r.y()));
00511 }
00512 
00522 QPolygonF::QPolygonF(const QPolygon &a)
00523 {
00524     reserve(a.size());
00525     for (int i=0; i<a.size(); ++i)
00526         append(a.at(i));
00527 }
00528 
00540 void QPolygonF::translate(const QPointF &offset)
00541 {
00542     register QPointF *p = data();
00543     register int i = size();
00544     while (i--) {
00545         *p += offset;
00546         ++p;
00547     }
00548 }
00549 
00574 QRectF QPolygonF::boundingRect() const
00575 {
00576     if (isEmpty())
00577         return QRectF(0, 0, 0, 0);
00578     register const QPointF *pd = constData();
00579     qreal minx, maxx, miny, maxy;
00580     minx = maxx = pd->x();
00581     miny = maxy = pd->y();
00582     ++pd;
00583     for (int i = 1; i < size(); ++i) {
00584         if (pd->x() < minx)
00585             minx = pd->x();
00586         else if (pd->x() > maxx)
00587             maxx = pd->x();
00588         if (pd->y() < miny)
00589             miny = pd->y();
00590         else if (pd->y() > maxy)
00591             maxy = pd->y();
00592         ++pd;
00593     }
00594     return QRectF(minx,miny, maxx - minx, maxy - miny);
00595 }
00596 
00604 QPolygon QPolygonF::toPolygon() const
00605 {
00606     QPolygon a;
00607     a.reserve(size());
00608     for (int i=0; i<size(); ++i)
00609         a.append(at(i).toPoint());
00610     return a;
00611 }
00612 
00616 QPolygon::operator QVariant() const
00617 {
00618     return QVariant(QVariant::Polygon, this);
00619 }
00620 
00621 /*****************************************************************************
00622   QPolygonF stream functions
00623  *****************************************************************************/
00624 #ifndef QT_NO_DATASTREAM
00625 
00635 QDataStream &operator<<(QDataStream &s, const QPolygonF &a)
00636 {
00637     quint32 len = a.size();
00638     uint i;
00639 
00640     s << len;
00641     for (i = 0; i < len; ++i)
00642         s << a.at(i);
00643     return s;
00644 }
00645 
00656 QDataStream &operator>>(QDataStream &s, QPolygonF &a)
00657 {
00658     quint32 len;
00659     uint i;
00660 
00661     s >> len;
00662     a.reserve(a.size() + (int)len);
00663     QPointF p;
00664     for (i = 0; i < len; ++i) {
00665         s >> p;
00666         a.insert(i, p);
00667     }
00668     return s;
00669 }
00670 #endif //QT_NO_DATASTREAM
00671 
00672 #ifndef QT_NO_DEBUG_STREAM
00673 QDebug operator<<(QDebug dbg, const QPolygonF &a)
00674 {
00675 #ifndef Q_BROKEN_DEBUG_STREAM
00676     dbg.nospace() << "QPolygonF(";
00677     for (int i = 0; i < a.count(); ++i)
00678         dbg.nospace() << a.at(i);
00679     dbg.nospace() << ')';
00680     return dbg.space();
00681 #else
00682     qWarning("This compiler doesn't support streaming QPolygonF to QDebug");
00683     return dbg;
00684     Q_UNUSED(a);
00685 #endif
00686 }
00687 #endif
00688 

Generated on Thu Mar 15 11:55:34 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1