UBGraphicsCache.cpp 4.63 KB
Newer Older
shibakaneki's avatar
shibakaneki committed
1 2 3
/*
 * 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
Claudio Valerio's avatar
Claudio Valerio committed
4
 * the Free Software Foundation, either version 2 of the License, or
shibakaneki's avatar
shibakaneki committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * (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, see <http://www.gnu.org/licenses/>.
 */
#include <QDebug>

#include "UBGraphicsCache.h"

#include "core/UBApplication.h"

#include "board/UBBoardController.h"
#include "board/UBBoardView.h"
#include "domain/UBGraphicsScene.h"

25
#include "core/memcheck.h"
shibakaneki's avatar
shibakaneki committed
26 27 28 29 30 31 32 33 34 35

UBGraphicsCache::UBGraphicsCache():QGraphicsRectItem()
  , mMaskColor(Qt::black)
  , mMaskShape(eMaskShape_Circle)
  , mShapeWidth(100)
  , mDrawMask(false)
{
    // Get the board size and pass it to the shape
    QRect boardRect = UBApplication::boardController->displayView()->rect();
    setRect(-15*boardRect.width(), -15*boardRect.height(), 30*boardRect.width(), 30*boardRect.height());
36
    setData(Qt::UserRole, QVariant("Cache"));
shibakaneki's avatar
shibakaneki committed
37
    setData(UBGraphicsItemData::itemLayerType, QVariant(itemLayerType::Cache)); //Necessary to set if we want z value to be assigned correctly
shibakaneki's avatar
shibakaneki committed
38 39 40 41 42 43 44 45 46 47
}

UBGraphicsCache::~UBGraphicsCache()
{
}

UBItem* UBGraphicsCache::deepCopy() const
{
    UBGraphicsCache* copy = new UBGraphicsCache();

48
    copyItemParameters(copy);
shibakaneki's avatar
shibakaneki committed
49 50 51 52 53 54

    // TODO UB 4.7 ... complete all members ?

    return copy;
}

55 56 57 58 59 60 61 62 63 64 65
void UBGraphicsCache::copyItemParameters(UBItem *copy) const
{
    UBGraphicsCache *cp = dynamic_cast<UBGraphicsCache*>(copy);
    if (cp)
    {
        cp->setPos(this->pos());
        cp->setRect(this->rect());
        cp->setTransform(this->transform());
    }
}

shibakaneki's avatar
shibakaneki committed
66 67 68 69 70 71 72 73
QColor UBGraphicsCache::maskColor()
{
    return mMaskColor;
}

void UBGraphicsCache::setMaskColor(QColor color)
{
    mMaskColor = color;
74
    update();
shibakaneki's avatar
shibakaneki committed
75 76 77 78 79 80 81 82 83 84
}

eMaskShape UBGraphicsCache::maskshape()
{
    return mMaskShape;
}

void UBGraphicsCache::setMaskShape(eMaskShape shape)
{
    mMaskShape = shape;
85
    update();
shibakaneki's avatar
shibakaneki committed
86 87 88 89 90 91 92 93 94 95 96
}

void UBGraphicsCache::init()
{
    setFlag(QGraphicsItem::ItemIsMovable, true);
    setFlag(QGraphicsItem::ItemIsSelectable, true);
    setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
}

void UBGraphicsCache::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
97
    Q_UNUSED(option);
shibakaneki's avatar
shibakaneki committed
98 99 100 101 102
    Q_UNUSED(widget);

    painter->setBrush(mMaskColor);
    painter->setPen(mMaskColor);

103
    // Draw the hole
shibakaneki's avatar
shibakaneki committed
104 105 106 107 108 109 110 111 112 113 114
    QPainterPath path;
    path.addRect(rect());

    if(mDrawMask)
    {
        if(eMaskShape_Circle == mMaskShape)
        {
            path.addEllipse(mShapePos, mShapeWidth, mShapeWidth);
        }
        else if(eMaskShap_Rectangle == mMaskShape)
        {
shibakaneki's avatar
shibakaneki committed
115
            path.addRect(mShapePos.x() - mShapeWidth, mShapePos.y() - mShapeWidth, 2*mShapeWidth, 2*mShapeWidth);
shibakaneki's avatar
shibakaneki committed
116 117 118 119 120 121 122 123 124 125 126 127
        }
        path.setFillRule(Qt::OddEvenFill);
    }

    painter->drawPath(path);
}

void UBGraphicsCache::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    Q_UNUSED(event);
    mShapePos = event->pos();
    mDrawMask = true;
128 129 130 131 132

    // Note: if refresh issues occure, replace the following 3 lines by: update();
    update(updateRect(event->pos()));
    mOldShapeWidth = mShapeWidth;
    mOldShapePos = event->pos();
shibakaneki's avatar
shibakaneki committed
133 134 135 136 137
}

void UBGraphicsCache::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    mShapePos = event->pos();
138 139 140 141 142

    // Note: if refresh issues occure, replace the following 3 lines by: update();
    update(updateRect(event->pos()));
    mOldShapeWidth = mShapeWidth;
    mOldShapePos = event->pos();
shibakaneki's avatar
shibakaneki committed
143 144 145 146 147 148
}

void UBGraphicsCache::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    Q_UNUSED(event);
    mDrawMask = false;
149 150 151 152 153

    // Note: if refresh issues occure, replace the following 3 lines by: update();
    update(updateRect(event->pos()));
    mOldShapeWidth = mShapeWidth;
    mOldShapePos = event->pos();
shibakaneki's avatar
shibakaneki committed
154 155 156 157 158 159 160 161 162 163
}

int UBGraphicsCache::shapeWidth()
{
    return mShapeWidth;
}

void UBGraphicsCache::setShapeWidth(int width)
{
    mShapeWidth = width;
164
    update();
shibakaneki's avatar
shibakaneki committed
165
}
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

QRectF UBGraphicsCache::updateRect(QPointF currentPoint)
{
    QRectF r;
    int x;
    int y;

    x = qMin(currentPoint.x() - mShapeWidth, mOldShapePos.x() - mOldShapeWidth);
    y = qMin(currentPoint.y() - mShapeWidth, mOldShapePos.y() - mOldShapeWidth);
    r = QRect(  x,
                y,
                qAbs(currentPoint.x() - mOldShapePos.x()) + 2*mShapeWidth,
                qAbs(currentPoint.y() - mOldShapePos.y()) + 2*mShapeWidth);
    return r;
}