KoClipMask.cpp 3.94 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
 *  Copyright (c) 2016 Dmitry Kazakov <dimula73@gmail.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "KoClipMask.h"

#include <QRectF>
#include <QTransform>
#include <QPainter>
#include <KoShape.h>
#include "kis_algebra_2d.h"

#include <KoViewConverter.h>
#include <KoShapePainter.h>

30
struct Q_DECL_HIDDEN KoClipMask::Private {
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
    Private() {}
    Private(const Private &rhs)
        : coordinates(rhs.coordinates),
          contentCoordinates(rhs.contentCoordinates),
          maskRect(rhs.maskRect),
          extraShapeTransform(rhs.extraShapeTransform)
    {
        Q_FOREACH (KoShape *shape, rhs.shapes) {
            KoShape *clonedShape = shape->cloneShape();
            KIS_ASSERT_RECOVER(clonedShape) { continue; }

            shapes << clonedShape;
        }
    }

    ~Private() {
        qDeleteAll(shapes);
        shapes.clear();
    }

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166

    CoordinateSystem coordinates = ObjectBoundingBox;
    CoordinateSystem contentCoordinates = UserSpaceOnUse;

    QRectF maskRect = QRectF(-0.1, -0.1, 1.2, 1.2);

    QList<KoShape*> shapes;
    QTransform extraShapeTransform;

};

KoClipMask::KoClipMask()
    : m_d(new Private)
{
}

KoClipMask::~KoClipMask()
{
}

KoClipMask::KoClipMask(const KoClipMask &rhs)
    : m_d(new Private(*rhs.m_d))
{
}

KoClipMask *KoClipMask::clone() const
{
    return new KoClipMask(*this);
}

KoClipMask::CoordinateSystem KoClipMask::coordinates() const
{
    return m_d->coordinates;
}

void KoClipMask::setCoordinates(KoClipMask::CoordinateSystem value)
{
    m_d->coordinates = value;
}

KoClipMask::CoordinateSystem KoClipMask::contentCoordinates() const
{
    return m_d->contentCoordinates;
}

void KoClipMask::setContentCoordinates(KoClipMask::CoordinateSystem value)
{
    m_d->contentCoordinates = value;
}

QRectF KoClipMask::maskRect() const
{
    return m_d->maskRect;
}

void KoClipMask::setMaskRect(const QRectF &value)
{
    m_d->maskRect = value;
}

QList<KoShape *> KoClipMask::shapes() const
{
    return m_d->shapes;
}

void KoClipMask::setShapes(const QList<KoShape *> &value)
{
    m_d->shapes = value;
}

bool KoClipMask::isEmpty() const
{
    return m_d->shapes.isEmpty();
}

QTransform KoClipMask::extraShapeTransform() const
{
    return m_d->extraShapeTransform;
}

void KoClipMask::setExtraShapeTransform(const QTransform &value)
{
    m_d->extraShapeTransform = value;
}

void KoClipMask::drawMask(QPainter *painter, KoShape *shape)
{
    painter->save();

    QPainterPath clipPathInShapeSpace;

    if (m_d->coordinates == ObjectBoundingBox) {
        QTransform relativeToShape = KisAlgebra2D::mapToRect(shape->outlineRect());
        clipPathInShapeSpace.addPolygon(relativeToShape.map(m_d->maskRect));
    } else {
        clipPathInShapeSpace.addRect(m_d->maskRect);
        clipPathInShapeSpace = m_d->extraShapeTransform.map(clipPathInShapeSpace);
    }

    painter->setClipPath(clipPathInShapeSpace, Qt::IntersectClip);

    if (m_d->contentCoordinates == ObjectBoundingBox) {
        QTransform relativeToShape = KisAlgebra2D::mapToRect(shape->outlineRect());

        painter->setTransform(relativeToShape, true);
    } else {
        painter->setTransform(m_d->extraShapeTransform, true);
    }

    KoViewConverter converter;
    KoShapePainter p;
    p.setShapes(m_d->shapes);
    p.paint(*painter, converter);

    painter->restore();
}