valentina/src/app/puzzle/scene/vpgraphicspiececontrols.cpp

935 lines
32 KiB
C++
Raw Normal View History

2021-08-09 14:09:10 +02:00
/************************************************************************
**
** @file vpgraphicspiececontrols.cpp
** @author Roman Telezhynskyi <dismine(at)gmail.com>
** @date 2 8, 2021
**
** @brief
** @copyright
** This source code is part of the Valentina project, a pattern making
** program, whose allow create and modeling patterns of clothing.
** Copyright (C) 2021 Valentina project
** <https://gitlab.com/smart-pattern/valentina> All Rights Reserved.
**
** Valentina 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 3 of the License, or
** (at your option) any later version.
**
** Valentina 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 Valentina. If not, see <http://www.gnu.org/licenses/>.
**
*************************************************************************/
#include "vpgraphicspiececontrols.h"
#include <QCursor>
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QIcon>
#include <QPainter>
#include <QtDebug>
#include <QGuiApplication>
#include <QScreen>
2021-08-09 14:09:10 +02:00
#include "../vmisc/compatibility.h"
#include "../vwidgets/global.h"
#include "../layout/vplayout.h"
2021-08-25 15:58:50 +02:00
#include "../layout/vppiece.h"
2022-08-12 17:50:13 +02:00
#include "../layout/vpsheet.h"
2021-08-18 19:33:47 +02:00
#include "../undocommands/vpundopiecerotate.h"
2021-08-19 11:36:39 +02:00
#include "../undocommands/vpundooriginmove.h"
#include "qgraphicsscene.h"
#include "qgraphicsview.h"
#include "qnamespace.h"
2021-08-09 14:09:10 +02:00
namespace
{
constexpr qreal penWidth = 2;
const qreal centerRadius1 = 5;
const qreal centerRadius2 = 10;
2022-08-12 17:50:13 +02:00
Q_GLOBAL_STATIC_WITH_ARGS(const QColor, defaultColor, (Qt::black)) // NOLINT
Q_GLOBAL_STATIC_WITH_ARGS(const QColor, hoverColor, (Qt::green)) // NOLINT
2021-08-09 14:09:10 +02:00
2021-08-26 18:04:24 +02:00
auto TransformationOrigin(const VPLayoutPtr &layout, const QRectF &boundingRect) -> VPTransformationOrigon
2021-08-09 14:09:10 +02:00
{
SCASSERT(layout != nullptr)
2021-08-18 19:33:47 +02:00
VPSheetPtr sheet = layout->GetFocusedSheet();
if (not sheet.isNull())
2021-08-09 14:09:10 +02:00
{
2021-08-26 18:04:24 +02:00
return sheet->TransformationOrigin();
2021-08-09 14:09:10 +02:00
}
2021-09-27 10:47:09 +02:00
QT_WARNING_PUSH
QT_WARNING_DISABLE_GCC("-Wnoexcept")
2021-08-26 18:04:24 +02:00
VPTransformationOrigon origin;
origin.origin = boundingRect.center();
origin.custom = false;
2022-08-08 14:25:14 +02:00
// cppcheck-suppress unknownMacro
2021-09-27 10:47:09 +02:00
QT_WARNING_POP
2021-08-26 18:04:24 +02:00
return origin;
2021-08-09 14:09:10 +02:00
}
} // namespace
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
VPGraphicsTransformationOrigin::VPGraphicsTransformationOrigin(const VPLayoutPtr &layout, QGraphicsItem *parent)
2021-08-09 14:09:10 +02:00
: QGraphicsObject(parent),
m_layout(layout),
2022-08-12 17:50:13 +02:00
m_color(*defaultColor)
2021-08-09 14:09:10 +02:00
{
SCASSERT(m_layout != nullptr)
setCursor(Qt::OpenHandCursor);
setZValue(1);
setAcceptHoverEvents(true);
2021-08-19 11:36:39 +02:00
2021-09-13 17:19:39 +02:00
connect(layout.data(), &VPLayout::TransformationOriginChanged, this,
2021-08-19 11:36:39 +02:00
&VPGraphicsTransformationOrigin::SetTransformationOrigin);
2021-08-09 14:09:10 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
void VPGraphicsTransformationOrigin::SetTransformationOrigin()
{
prepareGeometryChange();
2021-08-19 11:36:39 +02:00
update();
2021-08-09 14:09:10 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
void VPGraphicsTransformationOrigin::on_HideHandles(bool hide)
{
m_originVisible = not hide;
prepareGeometryChange();
}
//---------------------------------------------------------------------------------------------------------------------
void VPGraphicsTransformationOrigin::on_ShowOrigin(bool show)
{
setVisible(show);
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
auto VPGraphicsTransformationOrigin::boundingRect() const -> QRectF
2021-08-09 14:09:10 +02:00
{
constexpr qreal halfPenWidth = penWidth/2.;
return Center2().boundingRect().adjusted(-halfPenWidth, -halfPenWidth, halfPenWidth, halfPenWidth);
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
auto VPGraphicsTransformationOrigin::shape() const -> QPainterPath
2021-08-09 14:09:10 +02:00
{
return Center2();
}
//---------------------------------------------------------------------------------------------------------------------
void VPGraphicsTransformationOrigin::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(widget);
Q_UNUSED(option);
const qreal scale = SceneScale(scene());
QPen pen(m_color, penWidth/scale, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
painter->setPen(pen);
if (m_originVisible)
{
painter->save();
painter->setBrush(QBrush(m_color));
painter->drawPath(Center1());
painter->restore();
painter->save();
painter->setBrush(QBrush());
painter->drawPath(Center2());
painter->restore();
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPGraphicsTransformationOrigin::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
// change the cursor when clicking the left button
if((event->button() == Qt::LeftButton))
{
setCursor(Qt::ClosedHandCursor);
event->accept();
}
else
{
//perform the default behaviour
QGraphicsObject::mousePressEvent(event);
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPGraphicsTransformationOrigin::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
2021-08-18 19:33:47 +02:00
VPLayoutPtr layout = m_layout.toStrongRef();
if (not layout.isNull())
2021-08-09 14:09:10 +02:00
{
2021-08-18 19:33:47 +02:00
VPSheetPtr sheet = layout->GetFocusedSheet();
if (not sheet.isNull())
{
VPTransformationOrigon origin = sheet->TransformationOrigin();
origin.origin = event->scenePos();
origin.custom = true;
2021-08-19 11:36:39 +02:00
auto *command = new VPUndoOriginMove(sheet, origin, m_allowChangeMerge);
layout->UndoStack()->push(command);
2021-08-18 19:33:47 +02:00
}
prepareGeometryChange();
2021-08-09 14:09:10 +02:00
}
2021-08-19 11:36:39 +02:00
m_allowChangeMerge = true;
2021-08-09 14:09:10 +02:00
QGraphicsObject::mouseMoveEvent(event);
}
//---------------------------------------------------------------------------------------------------------------------
void VPGraphicsTransformationOrigin::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
//perform the default behaviour
QGraphicsItem::mouseReleaseEvent(event);
// change the cursor when clicking left button
if (event->button() == Qt::LeftButton)
{
setCursor(Qt::OpenHandCursor);
2021-08-19 11:36:39 +02:00
m_allowChangeMerge = false;
2021-08-09 14:09:10 +02:00
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPGraphicsTransformationOrigin::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
2022-08-12 17:50:13 +02:00
m_color = *hoverColor;
2021-08-09 14:09:10 +02:00
QGraphicsObject::hoverEnterEvent(event);
}
//---------------------------------------------------------------------------------------------------------------------
void VPGraphicsTransformationOrigin::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
2022-08-12 17:50:13 +02:00
m_color = *defaultColor;
2021-08-09 14:09:10 +02:00
QGraphicsObject::hoverEnterEvent(event);
}
//---------------------------------------------------------------------------------------------------------------------
auto VPGraphicsTransformationOrigin::RotationCenter(QPainter *painter) const -> QPainterPath
{
QPainterPath path;
const qreal scale = SceneScale(scene());
qreal radius = centerRadius1/scale;
2021-08-26 18:04:24 +02:00
VPTransformationOrigon transformationOrigin = TransformationOrigin(m_layout, QRectF());
QRectF rect(transformationOrigin.origin.x()-radius, transformationOrigin.origin.y()-radius, radius*2., radius*2.);
2021-08-09 14:09:10 +02:00
QPainterPath center1;
center1.addEllipse(rect);
if (painter != nullptr)
{
painter->save();
painter->setBrush(QBrush(m_color));
painter->drawPath(Center1());
painter->restore();
}
path.addPath(center1);
radius = centerRadius2/scale;
2021-08-26 18:04:24 +02:00
rect = QRectF(transformationOrigin.origin.x()-radius, transformationOrigin.origin.y()-radius, radius*2., radius*2.);
2021-08-09 14:09:10 +02:00
QPainterPath center2;
center2.addEllipse(rect);
if (painter != nullptr)
{
painter->save();
painter->setBrush(QBrush());
painter->drawPath(Center2());
painter->restore();
}
path.addPath(center2);
return path;
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
auto VPGraphicsTransformationOrigin::Center1() const -> QPainterPath
2021-08-09 14:09:10 +02:00
{
const qreal scale = SceneScale(scene());
qreal radius = centerRadius1/scale;
2021-08-26 18:04:24 +02:00
VPTransformationOrigon transformationOrigin = TransformationOrigin(m_layout, QRectF());
QRectF rect(transformationOrigin.origin.x()-radius, transformationOrigin.origin.y()-radius, radius*2., radius*2.);
2021-08-09 14:09:10 +02:00
QPainterPath center1;
center1.addEllipse(rect);
return center1;
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
auto VPGraphicsTransformationOrigin::Center2() const -> QPainterPath
2021-08-09 14:09:10 +02:00
{
const qreal scale = SceneScale(scene());
qreal radius = centerRadius2/scale;
2021-08-26 18:04:24 +02:00
VPTransformationOrigon transformationOrigin = TransformationOrigin(m_layout, QRectF());
QRectF rect = QRectF(transformationOrigin.origin.x()-radius, transformationOrigin.origin.y()-radius, radius*2.,
radius*2.);
2021-08-09 14:09:10 +02:00
QPainterPath center2;
center2.addEllipse(rect);
return center2;
}
// VPGraphicsPieceControls
//---------------------------------------------------------------------------------------------------------------------
2021-08-18 19:33:47 +02:00
VPGraphicsPieceControls::VPGraphicsPieceControls(const VPLayoutPtr &layout, QGraphicsItem *parent)
2021-08-09 14:09:10 +02:00
: QGraphicsObject(parent),
m_layout(layout)
{
SCASSERT(m_layout != nullptr)
setCursor(QCursor(Qt::OpenHandCursor));
setZValue(100);
setFlag(QGraphicsItem::ItemIgnoresTransformations);
2021-08-09 14:09:10 +02:00
setAcceptHoverEvents(true);
InitPixmaps();
2021-08-09 14:09:10 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
void VPGraphicsPieceControls::on_UpdateControls()
{
2021-08-18 19:33:47 +02:00
if (m_ignorePieceTransformation)
{
return;
}
m_selectedPieces = SelectedPieces();
m_pieceRect = PiecesBoundingRect(m_selectedPieces);
m_controlsVisible = not m_pieceRect.isNull();
2021-08-09 14:09:10 +02:00
if (not m_pieceRect.isNull())
{
2021-08-18 19:33:47 +02:00
VPLayoutPtr layout = m_layout.toStrongRef();
if (not layout.isNull())
2021-08-09 14:09:10 +02:00
{
2021-08-18 19:33:47 +02:00
VPSheetPtr sheet = layout->GetFocusedSheet();
if (not sheet.isNull())
2021-08-09 14:09:10 +02:00
{
2021-08-18 19:33:47 +02:00
VPTransformationOrigon origin = sheet->TransformationOrigin();
if (not origin.custom)
{
origin.origin = m_pieceRect.center();
sheet->SetTransformationOrigin(origin);
emit TransformationOriginChanged();
}
2021-08-09 14:09:10 +02:00
}
}
}
emit ShowOrigin(not m_pieceRect.isNull());
prepareGeometryChange();
}
//---------------------------------------------------------------------------------------------------------------------
void VPGraphicsPieceControls::on_HideHandles(bool hide)
{
m_controlsVisible = not hide;
2021-08-18 19:33:47 +02:00
update();
2021-08-09 14:09:10 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
auto VPGraphicsPieceControls::boundingRect() const -> QRectF
{
QRectF boundingRect;
auto HandlerBoundingRect = [this, &boundingRect](VPHandleCorner corner, VPHandleCornerType type, QPointF pos)
{
QPixmap handler = HandlerPixmap(m_handleCorner == corner, type);
boundingRect = boundingRect.united(QRectF(pos, handler.size() / handler.devicePixelRatio()));
};
HandlerBoundingRect(VPHandleCorner::TopLeft, VPHandleCornerType::TopLeft, TopLeftHandlerPosition());
HandlerBoundingRect(VPHandleCorner::TopRight, VPHandleCornerType::TopRight, TopRightHandlerPosition());
HandlerBoundingRect(VPHandleCorner::BottomRight, VPHandleCornerType::BottomRight, BottomRightHandlerPosition());
HandlerBoundingRect(VPHandleCorner::BottomLeft, VPHandleCornerType::BottomLeft, BottomLeftHandlerPosition());
return boundingRect;
2021-08-09 14:09:10 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
auto VPGraphicsPieceControls::shape() const -> QPainterPath
{
return Handles();
}
//---------------------------------------------------------------------------------------------------------------------
void VPGraphicsPieceControls::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(widget);
Q_UNUSED(option);
if (m_controlsVisible)
{
painter->drawPixmap(TopLeftHandlerPosition(),
HandlerPixmap(m_handleCorner == VPHandleCorner::TopLeft,
VPHandleCornerType::TopLeft));
2021-08-09 14:09:10 +02:00
painter->drawPixmap(TopRightHandlerPosition(),
HandlerPixmap(m_handleCorner == VPHandleCorner::TopRight,
VPHandleCornerType::TopRight));
2021-08-09 14:09:10 +02:00
painter->drawPixmap(BottomRightHandlerPosition(),
HandlerPixmap(m_handleCorner == VPHandleCorner::BottomRight,
VPHandleCornerType::BottomRight));
2021-08-09 14:09:10 +02:00
painter->drawPixmap(BottomLeftHandlerPosition(),
HandlerPixmap(m_handleCorner == VPHandleCorner::BottomLeft,
VPHandleCornerType::BottomLeft));
2021-08-09 14:09:10 +02:00
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPGraphicsPieceControls::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
if(event->button() == Qt::LeftButton && event->type() != QEvent::GraphicsSceneMouseDoubleClick)
2021-08-09 14:09:10 +02:00
{
m_rotationStartPoint = event->scenePos();
2021-08-26 18:04:24 +02:00
m_rotationSum = 0;
2021-08-09 14:09:10 +02:00
m_controlsVisible = false;
m_handleCorner = SelectedHandleCorner(event->pos());
2021-08-18 19:33:47 +02:00
m_ignorePieceTransformation = true;
2021-08-09 14:09:10 +02:00
prepareGeometryChange();
if (m_handleCorner != VPHandleCorner::Invalid)
{
setCursor(QCursor(Qt::ClosedHandCursor));
event->accept();
}
else
{
QGraphicsView *view = ItemView();
if (view != nullptr)
{
2022-08-12 17:50:13 +02:00
setCursor(view->viewport()->cursor()); // clazy:exclude=clazy-qt6-deprecated-api-fixes
}
event->ignore();
}
2021-08-09 14:09:10 +02:00
}
else
{
QGraphicsObject::mousePressEvent(event);
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPGraphicsPieceControls::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
PrepareTransformationOrigin(event->modifiers() & Qt::ShiftModifier);
2021-08-09 14:09:10 +02:00
QPointF rotationNewPoint = event->scenePos();
// get the angle from the center to the initial click point
2021-08-26 18:04:24 +02:00
VPTransformationOrigon rotationOrigin = TransformationOrigin(m_layout, m_pieceRect);
QLineF initPosition(rotationOrigin.origin, m_rotationStartPoint);
QLineF initRotationPosition(rotationOrigin.origin, rotationNewPoint);
qreal rotateOn = initPosition.angleTo(initRotationPosition);
2021-08-09 14:09:10 +02:00
2021-08-26 18:04:24 +02:00
if (rotateOn > 180)
{
rotateOn = rotateOn - 360.;
}
2021-08-09 14:09:10 +02:00
2021-08-26 18:04:24 +02:00
if (not qFuzzyIsNull(rotateOn))
2021-08-09 14:09:10 +02:00
{
QList<VPPiecePtr> pieces = SelectedPieces();
2021-08-18 19:33:47 +02:00
VPLayoutPtr layout = m_layout.toStrongRef();
if (not layout.isNull())
{
CorrectRotationSum(layout, rotationOrigin, rotateOn);
2021-08-26 18:04:24 +02:00
2021-08-18 19:33:47 +02:00
if (pieces.size() == 1)
{
2022-01-29 09:59:02 +01:00
auto *command = new VPUndoPieceRotate(ConstFirst(pieces), rotationOrigin, rotateOn, m_rotationSum,
2021-08-26 18:04:24 +02:00
allowChangeMerge);
2021-08-18 19:33:47 +02:00
layout->UndoStack()->push(command);
}
else if (pieces.size() > 1)
{
2021-08-26 18:04:24 +02:00
auto *command = new VPUndoPiecesRotate(pieces, rotationOrigin, rotateOn, m_rotationSum,
allowChangeMerge);
2021-08-18 19:33:47 +02:00
layout->UndoStack()->push(command);
}
}
2021-08-09 14:09:10 +02:00
}
if (m_originSaved && m_savedOrigin.custom)
{
2021-08-26 18:04:24 +02:00
QLineF line(rotationOrigin.origin, m_savedOrigin.origin);
line.setAngle(line.angle()+rotateOn);
2021-08-09 14:09:10 +02:00
m_savedOrigin.origin = line.p2();
}
m_rotationStartPoint = rotationNewPoint;
2021-08-18 19:33:47 +02:00
allowChangeMerge = true;
2021-08-09 14:09:10 +02:00
QGraphicsObject::mouseMoveEvent(event);
}
//---------------------------------------------------------------------------------------------------------------------
void VPGraphicsPieceControls::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
m_controlsVisible = true;
2021-08-18 19:33:47 +02:00
m_ignorePieceTransformation = false;
2021-08-09 14:09:10 +02:00
if (SelectedHandleCorner(event->pos()) != VPHandleCorner::Invalid)
{
setCursor(QCursor(Qt::OpenHandCursor));
}
else
{
QGraphicsView *view = ItemView();
if (view != nullptr)
{
2022-08-12 17:50:13 +02:00
setCursor(view->viewport()->cursor()); // clazy:exclude=clazy-qt6-deprecated-api-fixes
}
}
2021-08-09 14:09:10 +02:00
if (m_originSaved)
{
2021-08-18 19:33:47 +02:00
VPLayoutPtr layout = m_layout.toStrongRef();
if (not layout.isNull())
2021-08-09 14:09:10 +02:00
{
2021-08-18 19:33:47 +02:00
VPSheetPtr sheet = layout->GetFocusedSheet();
if (not sheet.isNull())
2021-08-09 14:09:10 +02:00
{
2021-08-18 19:33:47 +02:00
if (not m_savedOrigin.custom)
{
m_pieceRect = PiecesBoundingRect(m_selectedPieces);
m_savedOrigin.origin = m_pieceRect.center();
}
sheet->SetTransformationOrigin(m_savedOrigin);
emit TransformationOriginChanged();
2021-08-09 14:09:10 +02:00
}
2021-08-18 19:33:47 +02:00
m_originSaved = false;
2021-08-09 14:09:10 +02:00
}
}
on_UpdateControls();
2021-08-18 19:33:47 +02:00
allowChangeMerge = false;
2021-08-09 14:09:10 +02:00
}
QGraphicsObject::mouseReleaseEvent(event);
}
2021-08-18 19:33:47 +02:00
//---------------------------------------------------------------------------------------------------------------------
void VPGraphicsPieceControls::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
2021-08-18 19:33:47 +02:00
{
UpdateCursor(SelectedHandleCorner(event->pos()));
QGraphicsObject::hoverEnterEvent(event);
2021-08-18 19:33:47 +02:00
}
2021-08-09 14:09:10 +02:00
//---------------------------------------------------------------------------------------------------------------------
void VPGraphicsPieceControls::hoverMoveEvent(QGraphicsSceneHoverEvent *event)
2021-08-09 14:09:10 +02:00
{
UpdateCursor(SelectedHandleCorner(event->pos()));
QGraphicsObject::hoverMoveEvent(event);
2021-08-09 14:09:10 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
void VPGraphicsPieceControls::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
2021-08-09 14:09:10 +02:00
{
UpdateCursor(VPHandleCorner::Invalid);
QGraphicsObject::hoverLeaveEvent(event);
2021-08-09 14:09:10 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
void VPGraphicsPieceControls::InitPixmaps()
2021-08-09 14:09:10 +02:00
{
m_handlePixmaps.clear();
m_handleHoverPixmaps.clear();
m_handlePaths.clear();
auto InitPixmap = [this](VPHandleCornerType type, const QString &fileName)
{
const QFileInfo fileInfo(fileName);
const QString imageName = fileInfo.baseName();
const QString fileNameHover = QStringLiteral("%1/%2-hover.%3")
.arg(fileInfo.absolutePath(), imageName, fileInfo.suffix());
#if QT_VERSION >= QT_VERSION_CHECK(5, 5, 0)
if (QGuiApplication::primaryScreen()->devicePixelRatio() >= 2 )
{
const QString fileName2x = QStringLiteral("%1/%2@2x.%3")
.arg(fileInfo.absolutePath(), imageName, fileInfo.suffix());
const QString fileName2xHover = QStringLiteral("%1/%2-hover@2x.%3")
.arg(fileInfo.absolutePath(), imageName, fileInfo.suffix());
2021-08-09 14:09:10 +02:00
m_handlePixmaps.insert(type, QPixmap(fileName2x));
m_handleHoverPixmaps.insert(type, QPixmap(fileName2xHover));
}
else
{
m_handlePixmaps.insert(type, QPixmap(fileName));
m_handleHoverPixmaps.insert(type, QPixmap(fileNameHover));
}
#else
m_handlePixmaps.insert(type, QPixmap(fileName));
m_handleHoverPixmaps.insert(type, QPixmap(fileNameHover));
#endif
QPainterPath p = PixmapToPainterPath(m_handlePixmaps.value(type));
p.setFillRule(Qt::WindingFill);
p.closeSubpath();
m_handlePaths.insert(type, p);
};
InitPixmap(VPHandleCornerType::TopLeft, QStringLiteral("://icon/32x32/rotate-top-left.png"));
InitPixmap(VPHandleCornerType::TopRight, QStringLiteral("://icon/32x32/rotate-top-right.png"));
InitPixmap(VPHandleCornerType::BottomRight, QStringLiteral("://icon/32x32/rotate-bottom-right.png"));
InitPixmap(VPHandleCornerType::BottomLeft, QStringLiteral("://icon/32x32/rotate-bottom-left.png"));
2021-08-09 14:09:10 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
auto VPGraphicsPieceControls::TopLeftHandlerPosition() const -> QPointF
2021-08-09 14:09:10 +02:00
{
return ControllersRect().topLeft();
2021-08-09 14:09:10 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
auto VPGraphicsPieceControls::TopRightHandlerPosition() const -> QPointF
2021-08-09 14:09:10 +02:00
{
QRectF rect = ControllersRect();
QPixmap handler = m_handlePixmaps.value(VPHandleCornerType::TopRight);
QSize size = handler.size() / handler.devicePixelRatio();
return {rect.topLeft().x() + (rect.width() - size.width()), rect.topLeft().y()};
}
2021-08-09 14:09:10 +02:00
//---------------------------------------------------------------------------------------------------------------------
auto VPGraphicsPieceControls::BottomRightHandlerPosition() const -> QPointF
{
QRectF rect = ControllersRect();
QPixmap handler = m_handlePixmaps.value(VPHandleCornerType::BottomRight);
QSize size = handler.size() / handler.devicePixelRatio();
return {rect.topLeft().x() + (rect.width() - size.width()),
rect.topLeft().y() + (rect.height() - size.height())};
}
2021-08-09 14:09:10 +02:00
//---------------------------------------------------------------------------------------------------------------------
auto VPGraphicsPieceControls::BottomLeftHandlerPosition() const -> QPointF
{
QRectF rect = ControllersRect();
QPixmap handler = m_handlePixmaps.value(VPHandleCornerType::BottomLeft);
QSize size = handler.size() / handler.devicePixelRatio();
return {rect.topLeft().x(), rect.topLeft().y() + (rect.height() - size.height())};
2021-08-09 14:09:10 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
auto VPGraphicsPieceControls::ControllerPath(VPHandleCornerType type, QPointF pos) const -> QPainterPath
2021-08-09 14:09:10 +02:00
{
QTransform t;
t.translate(pos.x(), pos.y());
2021-08-09 14:09:10 +02:00
QPainterPath controller = m_handlePaths.value(type);
2021-08-09 14:09:10 +02:00
controller = t.map(controller);
return controller;
}
//---------------------------------------------------------------------------------------------------------------------
auto VPGraphicsPieceControls::TopLeftControl() const -> QPainterPath
2021-08-09 14:09:10 +02:00
{
return ControllerPath(VPHandleCornerType::TopLeft, TopLeftHandlerPosition());
2021-08-09 14:09:10 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
auto VPGraphicsPieceControls::TopRightControl() const -> QPainterPath
2021-08-09 14:09:10 +02:00
{
return ControllerPath(VPHandleCornerType::TopRight, TopRightHandlerPosition());
}
2021-08-09 14:09:10 +02:00
//---------------------------------------------------------------------------------------------------------------------
auto VPGraphicsPieceControls::BottomRightControl() const -> QPainterPath
{
return ControllerPath(VPHandleCornerType::BottomRight, BottomRightHandlerPosition());
}
2021-08-09 14:09:10 +02:00
//---------------------------------------------------------------------------------------------------------------------
auto VPGraphicsPieceControls::BottomLeftControl() const -> QPainterPath
{
return ControllerPath(VPHandleCornerType::BottomLeft, BottomLeftHandlerPosition());
}
2021-08-09 14:09:10 +02:00
//---------------------------------------------------------------------------------------------------------------------
void VPGraphicsPieceControls::SetIgnorePieceTransformation(bool newIgnorePieceTransformation)
{
m_ignorePieceTransformation = newIgnorePieceTransformation;
}
2021-08-09 14:09:10 +02:00
//---------------------------------------------------------------------------------------------------------------------
auto VPGraphicsPieceControls::Handles() const -> QPainterPath
{
QPainterPath path;
2021-08-09 14:09:10 +02:00
path.addPath(TopLeftControl());
path.addPath(TopRightControl());
path.addPath(BottomLeftControl());
path.addPath(BottomRightControl());
2021-08-09 14:09:10 +02:00
return path;
}
2021-08-09 14:09:10 +02:00
//---------------------------------------------------------------------------------------------------------------------
auto VPGraphicsPieceControls::ControllersRect() const -> QRectF
{
const qreal scale = SceneScale(scene());
QPixmap handler = m_handlePixmaps.value(VPHandleCornerType::TopLeft);
QRectF pieceRect = m_pieceRect;
2021-08-09 14:09:10 +02:00
pieceRect = QRectF(pieceRect.topLeft()*scale, QSizeF(pieceRect.width()*scale, pieceRect.height()*scale));
QRectF rect = pieceRect;
2021-08-09 14:09:10 +02:00
if (pieceRect.width() < handler.width())
{
qreal diff = handler.width() - pieceRect.width();
rect.adjust(0, 0, diff, 0);
}
2021-08-09 14:09:10 +02:00
if (pieceRect.height() < handler.height())
{
qreal diff = handler.height() - pieceRect.height();
rect.adjust(0, 0, 0, diff);
}
2021-08-09 14:09:10 +02:00
const qreal gap = 2;
rect.adjust(- (handler.width() + gap), - (handler.height() + gap), handler.width() + gap, handler.height() + gap);
2021-08-09 14:09:10 +02:00
return rect;
2021-08-09 14:09:10 +02:00
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-25 15:58:50 +02:00
auto VPGraphicsPieceControls::SelectedPieces() const -> QList<VPPiecePtr>
2021-08-09 14:09:10 +02:00
{
2021-08-25 15:58:50 +02:00
QList<VPPiecePtr> pieces;
VPLayoutPtr layout = m_layout.toStrongRef();
if (not layout.isNull())
2021-08-09 14:09:10 +02:00
{
2021-08-25 15:58:50 +02:00
VPSheetPtr sheet = layout->GetFocusedSheet();
if (not sheet.isNull())
2021-08-09 14:09:10 +02:00
{
2021-08-25 15:58:50 +02:00
pieces = sheet->GetSelectedPieces();
2021-08-09 14:09:10 +02:00
}
}
2021-08-18 19:33:47 +02:00
return pieces;
}
//---------------------------------------------------------------------------------------------------------------------
2021-08-25 15:58:50 +02:00
auto VPGraphicsPieceControls::PiecesBoundingRect(const QList<VPPiecePtr> &selectedPieces) -> QRectF
2021-08-18 19:33:47 +02:00
{
QRectF rect;
2021-08-25 15:58:50 +02:00
for (const auto& item : selectedPieces)
2021-08-18 19:33:47 +02:00
{
2021-08-25 15:58:50 +02:00
if (not item.isNull())
{
rect = rect.united(item->MappedDetailBoundingRect());
}
2021-08-18 19:33:47 +02:00
}
2021-08-09 14:09:10 +02:00
return rect;
}
//---------------------------------------------------------------------------------------------------------------------
auto VPGraphicsPieceControls::ItemView() -> QGraphicsView *
2021-08-09 14:09:10 +02:00
{
QGraphicsScene *scene = this->scene();
if (scene != nullptr)
2021-08-09 14:09:10 +02:00
{
QList<QGraphicsView *> views = scene->views();
if (not views.isEmpty())
{
return views.at(0);
}
2021-08-09 14:09:10 +02:00
}
return nullptr;
}
//---------------------------------------------------------------------------------------------------------------------
void VPGraphicsPieceControls::UpdateCursor(VPHandleCorner corner)
{
m_handleCorner = corner;
if (m_handleCorner != VPHandleCorner::Invalid)
2021-08-09 14:09:10 +02:00
{
setCursor(QCursor(Qt::OpenHandCursor));
2021-08-09 14:09:10 +02:00
}
else
2021-08-09 14:09:10 +02:00
{
QGraphicsView *view = ItemView();
if (view != nullptr)
{
2022-08-12 17:50:13 +02:00
setCursor(view->viewport()->cursor()); // clazy:exclude=clazy-qt6-deprecated-api-fixes
}
2021-08-09 14:09:10 +02:00
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPGraphicsPieceControls::PrepareTransformationOrigin(bool shiftPressed)
{
if(shiftPressed && m_handleCorner != VPHandleCorner::Invalid)
{
if (m_originSaved)
{
return;
}
VPLayoutPtr layout = m_layout.toStrongRef();
if (layout.isNull())
{
return;
}
VPSheetPtr sheet = layout->GetFocusedSheet();
if (sheet.isNull())
{
return;
}
m_savedOrigin = sheet->TransformationOrigin();
m_originSaved = true;
m_pieceRect = PiecesBoundingRect(m_selectedPieces);
VPTransformationOrigon origin;
origin.custom = true;
switch (m_handleCorner)
{
case VPHandleCorner::TopLeft:
origin.origin = m_pieceRect.bottomRight();
break;
case VPHandleCorner::TopRight:
origin.origin = m_pieceRect.bottomLeft();
break;
case VPHandleCorner::BottomRight:
origin.origin = m_pieceRect.topLeft();
break;
case VPHandleCorner::BottomLeft:
origin.origin = m_pieceRect.topRight();
break;
case VPHandleCorner::Invalid:
default:
break;
}
sheet->SetTransformationOrigin(origin);
emit TransformationOriginChanged();
}
else
{
if (not m_originSaved)
{
return;
}
VPLayoutPtr layout = m_layout.toStrongRef();
if (layout.isNull())
{
return;
}
VPSheetPtr sheet = layout->GetFocusedSheet();
if (sheet.isNull())
{
return;
}
if (not m_savedOrigin.custom)
{
m_pieceRect = PiecesBoundingRect(m_selectedPieces);
m_savedOrigin.origin = m_pieceRect.center();
}
sheet->SetTransformationOrigin(m_savedOrigin);
emit TransformationOriginChanged();
m_originSaved = false;
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPGraphicsPieceControls::CorrectRotationSum(const VPLayoutPtr &layout,
const VPTransformationOrigon &rotationOrigin, qreal rotateOn)
{
if (layout.isNull())
{
return;
}
if (layout->LayoutSettings().GetFollowGrainline() && not rotationOrigin.custom)
{
if (m_rotationSum > 90 || m_rotationSum < -90)
{
m_rotationSum = rotateOn;
}
else
{
m_rotationSum += rotateOn;
}
}
else
{
m_rotationSum = rotateOn;
}
}
//---------------------------------------------------------------------------------------------------------------------
auto VPGraphicsPieceControls::SelectedHandleCorner(const QPointF &pos) const -> VPHandleCorner
{
QMap<VPHandleCorner, QPainterPath> corners
{
{VPHandleCorner::TopLeft, TopLeftControl()},
{VPHandleCorner::TopRight, TopRightControl()},
{VPHandleCorner::BottomRight, BottomRightControl()},
{VPHandleCorner::BottomLeft, BottomLeftControl()},
};
QPainterPath circle;
circle.addEllipse(pos.x()-4, pos.y()-4, 8, 8);
2021-08-09 14:09:10 +02:00
auto CheckCorner = [circle](const QPainterPath &handler)
2021-08-09 14:09:10 +02:00
{
return handler.intersects(circle) || handler.contains(circle);
};
auto i = corners.constBegin();
while (i != corners.constEnd())
{
if (CheckCorner(i.value()))
{
return i.key();
}
++i;
2021-08-09 14:09:10 +02:00
}
return VPHandleCorner::Invalid;
}
//---------------------------------------------------------------------------------------------------------------------
auto VPGraphicsPieceControls::HandlerPixmap(bool hover, VPHandleCornerType type) const -> QPixmap
{
return hover ? m_handleHoverPixmaps.value(type) : m_handlePixmaps.value(type);
2021-08-09 14:09:10 +02:00
}