новые иконки в OpenBoard
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
OpenBoard/src/tools/UBGraphicsCache.cpp

201 lines
5.2 KiB

13 years ago
/*
* Copyright (C) 2012 Webdoc SA
13 years ago
*
* This file is part of Open-Sankoré.
*
12 years ago
* Open-Sankoré 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, version 3 of the License,
* 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).
*
* Open-Sankoré is distributed in the hope that it will be useful,
13 years ago
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12 years ago
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
13 years ago
*
12 years ago
* You should have received a copy of the GNU General Public License
* along with Open-Sankoré. If not, see <http://www.gnu.org/licenses/>.
13 years ago
*/
12 years ago
13 years ago
#include <QDebug>
#include "UBGraphicsCache.h"
#include "core/UBApplication.h"
#include "board/UBBoardController.h"
#include "board/UBBoardView.h"
#include "domain/UBGraphicsScene.h"
#include "core/memcheck.h"
13 years ago
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()
13 years ago
, mMaskColor(Qt::black)
, mMaskShape(eMaskShape_Circle)
, mShapeWidth(100)
, mDrawMask(false)
, mScene(scene)
13 years ago
{
// 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());
setData(Qt::UserRole, QVariant("Cache"));
13 years ago
setData(UBGraphicsItemData::itemLayerType, QVariant(itemLayerType::Cache)); //Necessary to set if we want z value to be assigned correctly
13 years ago
}
UBGraphicsCache::~UBGraphicsCache()
{
sInstances.remove(mScene);
13 years ago
}
UBItem* UBGraphicsCache::deepCopy() const
{
UBGraphicsCache* copy = new UBGraphicsCache(mScene);
13 years ago
copyItemParameters(copy);
13 years ago
// TODO UB 4.7 ... complete all members ?
return copy;
}
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());
}
}
13 years ago
QColor UBGraphicsCache::maskColor()
{
return mMaskColor;
}
void UBGraphicsCache::setMaskColor(QColor color)
{
mMaskColor = color;
update();
13 years ago
}
eMaskShape UBGraphicsCache::maskshape()
{
return mMaskShape;
}
void UBGraphicsCache::setMaskShape(eMaskShape shape)
{
mMaskShape = shape;
update();
13 years ago
}
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)
{
Q_UNUSED(option);
13 years ago
Q_UNUSED(widget);
painter->setBrush(mMaskColor);
painter->setPen(mMaskColor);
// Draw the hole
13 years ago
QPainterPath path;
path.addRect(rect());
if(mDrawMask)
{
if(eMaskShape_Circle == mMaskShape)
{
path.addEllipse(mShapePos, mShapeWidth, mShapeWidth);
}
else if(eMaskShap_Rectangle == mMaskShape)
{
path.addRect(mShapePos.x() - mShapeWidth, mShapePos.y() - mShapeWidth, 2*mShapeWidth, 2*mShapeWidth);
13 years ago
}
path.setFillRule(Qt::OddEvenFill);
}
painter->drawPath(path);
}
void UBGraphicsCache::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
Q_UNUSED(event);
mShapePos = event->pos();
mDrawMask = true;
// Note: if refresh issues occure, replace the following 3 lines by: update();
update(updateRect(event->pos()));
mOldShapeWidth = mShapeWidth;
mOldShapePos = event->pos();
13 years ago
}
void UBGraphicsCache::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
mShapePos = event->pos();
// Note: if refresh issues occure, replace the following 3 lines by: update();
update(updateRect(event->pos()));
mOldShapeWidth = mShapeWidth;
mOldShapePos = event->pos();
13 years ago
}
void UBGraphicsCache::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
Q_UNUSED(event);
mDrawMask = false;
// Note: if refresh issues occure, replace the following 3 lines by: update();
update(updateRect(event->pos()));
mOldShapeWidth = mShapeWidth;
mOldShapePos = event->pos();
13 years ago
}
int UBGraphicsCache::shapeWidth()
{
return mShapeWidth;
}
void UBGraphicsCache::setShapeWidth(int width)
{
mShapeWidth = width;
update();
13 years ago
}
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;
}