File indexing completed on 2024-05-26 04:24:49

0001 
0002 /*
0003    Copyright (c) 2003-2007 Clarence Dang <dang@kde.org>
0004    All rights reserved.
0005 
0006    Redistribution and use in source and binary forms, with or without
0007    modification, are permitted provided that the following conditions
0008    are met:
0009 
0010    1. Redistributions of source code must retain the above copyright
0011       notice, this list of conditions and the following disclaimer.
0012    2. Redistributions in binary form must reproduce the above copyright
0013       notice, this list of conditions and the following disclaimer in the
0014       documentation and/or other materials provided with the distribution.
0015 
0016    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0017    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0018    OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0019    IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0020    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0021    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0022    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0023    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0024    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0025    THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0026 */
0027 
0028 
0029 #define DEBUG_KP_SELECTION 0
0030 
0031 
0032 #include "layers/selections/image/kpEllipticalImageSelection.h"
0033 
0034 #include <QPainter>
0035 #include <QRegion>
0036 #include <QPainterPath>
0037 
0038 
0039 struct kpEllipticalImageSelectionPrivate
0040 {
0041 };
0042 
0043 
0044 kpEllipticalImageSelection::kpEllipticalImageSelection (
0045         const kpImageSelectionTransparency &transparency)
0046     : kpAbstractImageSelection (transparency),
0047       d (new kpEllipticalImageSelectionPrivate ())
0048 {
0049 }
0050 
0051 kpEllipticalImageSelection::kpEllipticalImageSelection (const QRect &rect,
0052         const kpImage &baseImage,
0053         const kpImageSelectionTransparency &transparency)
0054     : kpAbstractImageSelection (rect, baseImage, transparency),
0055       d (new kpEllipticalImageSelectionPrivate ())
0056 {
0057 }
0058 
0059 kpEllipticalImageSelection::kpEllipticalImageSelection (const QRect &rect,
0060         const kpImageSelectionTransparency &transparency)
0061     : kpAbstractImageSelection (rect, transparency),
0062       d (new kpEllipticalImageSelectionPrivate ())
0063 {
0064 }
0065 
0066 kpEllipticalImageSelection::kpEllipticalImageSelection (const kpEllipticalImageSelection &rhs)
0067     : kpAbstractImageSelection (),
0068       d (new kpEllipticalImageSelectionPrivate ())
0069 {
0070     *this = rhs;
0071 }
0072 
0073 kpEllipticalImageSelection &kpEllipticalImageSelection::operator= (
0074         const kpEllipticalImageSelection &rhs)
0075 {
0076     kpAbstractImageSelection::operator= (rhs);
0077 
0078     return *this;
0079 }
0080 
0081 kpEllipticalImageSelection *kpEllipticalImageSelection::clone () const
0082 {
0083     kpEllipticalImageSelection *sel = new kpEllipticalImageSelection ();
0084     *sel = *this;
0085     return sel;
0086 }
0087 
0088 kpEllipticalImageSelection::~kpEllipticalImageSelection ()
0089 {
0090     delete d;
0091 }
0092 
0093 //---------------------------------------------------------------------
0094 
0095 // public virtual [kpAbstractSelection]
0096 int kpEllipticalImageSelection::serialID () const
0097 {
0098     return SerialID;
0099 }
0100 
0101 //---------------------------------------------------------------------
0102 
0103 // public virtual [kpAbstractSelection]
0104 bool kpEllipticalImageSelection::isRectangular () const
0105 {
0106     return false;
0107 }
0108 
0109 //---------------------------------------------------------------------
0110 
0111 // public virtual [kpAbstractSelection]
0112 QPolygon kpEllipticalImageSelection::calculatePoints () const
0113 {
0114     Q_ASSERT (boundingRect ().isValid ());
0115 
0116     if (width () == 1 && height () == 1)
0117     {
0118         QPolygon ret;
0119         ret.append (topLeft ());
0120         return ret;
0121     }
0122 
0123     QPainterPath path;
0124     if (width () == 1 || height () == 1)
0125     {
0126         path.moveTo (x (), y ());
0127         // This does not work when the width _and_ height are 1 since lineTo()
0128         // would not move at all.  This is why we have a separate case for that
0129         // at the top of the method.
0130         path.lineTo (x () + width () - 1, y () + height () - 1);
0131     }
0132     else
0133     {
0134         // The adjusting is to fight QPainterPath::addEllipse() making
0135         // the ellipse 1 pixel higher and wider than specified.
0136         path.addEllipse (boundingRect ().adjusted (0, 0, -1, -1));
0137     }
0138 
0139     const QList <QPolygonF> polygons = path.toSubpathPolygons ();
0140     Q_ASSERT (polygons.size () == 1);
0141 
0142     const QPolygonF& firstPolygonF = polygons.first ();
0143     return firstPolygonF.toPolygon ();
0144 }
0145 
0146 //---------------------------------------------------------------------
0147 
0148 
0149 // protected virtual [kpAbstractImageSelection]
0150 QRegion kpEllipticalImageSelection::shapeRegion () const
0151 {
0152     QRegion reg(calculatePoints());
0153     return reg;
0154 }
0155 
0156 //---------------------------------------------------------------------
0157 
0158 
0159 // public virtual [kpAbstractSelection]
0160 bool kpEllipticalImageSelection::contains (const QPoint &point) const
0161 {
0162     if (!boundingRect ().contains (point)) {
0163         return false;
0164     }
0165 
0166     return shapeRegion ().contains (point);
0167 }
0168 
0169 //---------------------------------------------------------------------
0170 
0171 
0172 // public virtual [kpAbstractSelection]
0173 void kpEllipticalImageSelection::paintBorder (QImage *destPixmap, const QRect &docRect,
0174         bool selectionFinished) const
0175 {
0176     if ( !boundingRect().isValid() ) {
0177       return;
0178     }
0179 
0180     paintPolygonalBorder (calculatePoints (),
0181         destPixmap, docRect,
0182         selectionFinished);
0183 }
0184 
0185 #include "moc_kpEllipticalImageSelection.cpp"