UBGraphicsCache.cpp 5.36 KB
Newer Older
shibakaneki's avatar
shibakaneki committed
1
/*
2
 * Copyright (C) 2015-2018 Département de l'Instruction Publique (DIP-SEM)
Craig Watson's avatar
Craig Watson committed
3
 *
Claudio Valerio's avatar
Claudio Valerio committed
4
 * Copyright (C) 2013 Open Education Foundation
shibakaneki's avatar
shibakaneki committed
5
 *
Claudio Valerio's avatar
Claudio Valerio committed
6 7
 * Copyright (C) 2010-2013 Groupement d'Intérêt Public pour
 * l'Education Numérique en Afrique (GIP ENA)
8
 *
Claudio Valerio's avatar
Claudio Valerio committed
9 10 11
 * This file is part of OpenBoard.
 *
 * OpenBoard is free software: you can redistribute it and/or modify
Claudio Valerio's avatar
Claudio Valerio committed
12 13
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License,
14 15 16 17
 * with a specific linking exception for the OpenSSL project's
 * "OpenSSL" library (or with modified versions of it that use the
 * same license as the "OpenSSL" library).
 *
Claudio Valerio's avatar
Claudio Valerio committed
18
 * OpenBoard is distributed in the hope that it will be useful,
shibakaneki's avatar
shibakaneki committed
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Claudio Valerio's avatar
Claudio Valerio committed
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Claudio Valerio's avatar
Claudio Valerio committed
21
 * GNU General Public License for more details.
shibakaneki's avatar
shibakaneki committed
22
 *
Claudio Valerio's avatar
Claudio Valerio committed
23
 * You should have received a copy of the GNU General Public License
Claudio Valerio's avatar
Claudio Valerio committed
24
 * along with OpenBoard. If not, see <http://www.gnu.org/licenses/>.
shibakaneki's avatar
shibakaneki committed
25
 */
26 27


Claudio Valerio's avatar
Claudio Valerio committed
28

Claudio Valerio's avatar
Claudio Valerio committed
29

shibakaneki's avatar
shibakaneki committed
30 31 32 33 34 35 36 37 38 39
#include <QDebug>

#include "UBGraphicsCache.h"

#include "core/UBApplication.h"

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

40
#include "core/memcheck.h"
shibakaneki's avatar
shibakaneki committed
41

42 43 44 45 46 47 48 49 50 51
QMap<UBGraphicsScene*, UBGraphicsCache*> UBGraphicsCache::sInstances;

UBGraphicsCache* UBGraphicsCache::instance(UBGraphicsScene *scene)
{
    if (!sInstances.contains(scene))
        sInstances.insert(scene, new UBGraphicsCache(scene));
    return sInstances[scene];
}

UBGraphicsCache::UBGraphicsCache(UBGraphicsScene *scene) : QGraphicsRectItem()
shibakaneki's avatar
shibakaneki committed
52 53 54 55
  , mMaskColor(Qt::black)
  , mMaskShape(eMaskShape_Circle)
  , mShapeWidth(100)
  , mDrawMask(false)
56
  , mScene(scene)
shibakaneki's avatar
shibakaneki committed
57 58 59 60
{
    // 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());
61
    setData(Qt::UserRole, QVariant("Cache"));
shibakaneki's avatar
shibakaneki committed
62
    setData(UBGraphicsItemData::itemLayerType, QVariant(itemLayerType::Cache)); //Necessary to set if we want z value to be assigned correctly
shibakaneki's avatar
shibakaneki committed
63 64 65 66
}

UBGraphicsCache::~UBGraphicsCache()
{
67
    sInstances.remove(mScene);
shibakaneki's avatar
shibakaneki committed
68 69 70 71
}

UBItem* UBGraphicsCache::deepCopy() const
{
72
    UBGraphicsCache* copy = new UBGraphicsCache(mScene);
shibakaneki's avatar
shibakaneki committed
73

74
    copyItemParameters(copy);
shibakaneki's avatar
shibakaneki committed
75 76 77 78 79 80

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

    return copy;
}

81 82 83 84 85 86 87 88 89 90 91
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
92 93 94 95 96 97 98 99
QColor UBGraphicsCache::maskColor()
{
    return mMaskColor;
}

void UBGraphicsCache::setMaskColor(QColor color)
{
    mMaskColor = color;
100
    update();
shibakaneki's avatar
shibakaneki committed
101 102 103 104 105 106 107 108 109 110
}

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

void UBGraphicsCache::setMaskShape(eMaskShape shape)
{
    mMaskShape = shape;
111
    update();
shibakaneki's avatar
shibakaneki committed
112 113 114 115 116 117 118 119 120 121 122
}

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)
{
123
    Q_UNUSED(option);
shibakaneki's avatar
shibakaneki committed
124 125 126 127 128
    Q_UNUSED(widget);

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

129
    // Draw the hole
shibakaneki's avatar
shibakaneki committed
130 131 132 133 134 135 136 137 138 139 140
    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
141
            path.addRect(mShapePos.x() - mShapeWidth, mShapePos.y() - mShapeWidth, 2*mShapeWidth, 2*mShapeWidth);
shibakaneki's avatar
shibakaneki committed
142 143 144 145 146 147 148 149 150 151 152 153
        }
        path.setFillRule(Qt::OddEvenFill);
    }

    painter->drawPath(path);
}

void UBGraphicsCache::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    Q_UNUSED(event);
    mShapePos = event->pos();
    mDrawMask = true;
154 155 156 157 158

    // 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
159 160 161 162 163
}

void UBGraphicsCache::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    mShapePos = event->pos();
164 165 166 167 168

    // 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
169 170 171 172 173 174
}

void UBGraphicsCache::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    Q_UNUSED(event);
    mDrawMask = false;
175 176 177 178 179

    // 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
180 181 182 183 184 185 186 187 188 189
}

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

void UBGraphicsCache::setShapeWidth(int width)
{
    mShapeWidth = width;
190
    update();
shibakaneki's avatar
shibakaneki committed
191
}
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

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;
}