2016-09-10 18:30:03 +02:00
|
|
|
/************************************************************************
|
|
|
|
**
|
2016-10-16 15:00:53 +02:00
|
|
|
** @file vgrainlineitem.cpp
|
2016-09-10 18:30:03 +02:00
|
|
|
** @author Bojan Kverh
|
|
|
|
** @date September 10, 2016
|
|
|
|
**
|
|
|
|
** @brief
|
|
|
|
** @copyright
|
2017-10-05 11:20:01 +02:00
|
|
|
** This source code is part of the Valentina project, a pattern making
|
2016-09-10 18:30:03 +02:00
|
|
|
** program, whose allow create and modeling patterns of clothing.
|
|
|
|
** Copyright (C) 2013-2015 Valentina project
|
2020-01-31 07:00:05 +01:00
|
|
|
** <https://gitlab.com/smart-pattern/valentina> All Rights Reserved.
|
2016-09-10 18:30:03 +02:00
|
|
|
**
|
|
|
|
** 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/>.
|
|
|
|
**
|
|
|
|
*************************************************************************/
|
|
|
|
|
2023-05-02 16:38:02 +02:00
|
|
|
#include <cmath>
|
2016-09-10 18:30:03 +02:00
|
|
|
|
|
|
|
#include <QPainter>
|
2016-09-11 12:18:09 +02:00
|
|
|
#include <QGraphicsSceneMouseEvent>
|
2016-09-14 01:15:01 +02:00
|
|
|
#include <QStyleOptionGraphicsItem>
|
2016-09-11 12:18:09 +02:00
|
|
|
#include <QDebug>
|
2017-02-24 18:47:13 +01:00
|
|
|
#include <QGraphicsScene>
|
|
|
|
#include <QGraphicsView>
|
2016-09-10 18:30:03 +02:00
|
|
|
|
2016-09-14 01:15:01 +02:00
|
|
|
#include "../vmisc/def.h"
|
2023-05-02 16:38:02 +02:00
|
|
|
#include "../vmisc/vabstractapplication.h"
|
2022-02-18 09:36:38 +01:00
|
|
|
#include "../vmisc/literals.h"
|
2021-01-23 17:54:07 +01:00
|
|
|
#include "global.h"
|
2023-05-02 16:38:02 +02:00
|
|
|
#include "vpiecegrainline.h"
|
2016-09-14 01:15:01 +02:00
|
|
|
|
2016-09-10 18:30:03 +02:00
|
|
|
#include "vgrainlineitem.h"
|
|
|
|
|
2023-05-02 16:38:02 +02:00
|
|
|
constexpr int rectWidth = 30;
|
|
|
|
constexpr int resizeRectSize = 10;
|
|
|
|
constexpr int rotateCircR = 7;
|
|
|
|
constexpr int activeZ = 10;
|
2016-09-10 18:30:03 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-09-11 12:18:09 +02:00
|
|
|
/**
|
|
|
|
* @brief VGrainlineItem::VGrainlineItem constructor
|
|
|
|
* @param pParent pointer to the parent item
|
|
|
|
*/
|
2016-09-10 18:30:03 +02:00
|
|
|
VGrainlineItem::VGrainlineItem(QGraphicsItem* pParent)
|
2017-01-19 11:07:26 +01:00
|
|
|
: VPieceItem(pParent),
|
2021-02-06 14:52:21 +01:00
|
|
|
m_penWidth(VAbstractApplication::VApp()->Settings()->WidthMainLine())
|
2016-09-10 18:30:03 +02:00
|
|
|
{
|
2017-02-28 16:33:53 +01:00
|
|
|
setAcceptHoverEvents(true);
|
2017-01-19 11:07:26 +01:00
|
|
|
m_inactiveZ = 5;
|
2016-09-11 12:18:09 +02:00
|
|
|
Reset();
|
|
|
|
UpdateRectangle();
|
2016-09-10 18:30:03 +02:00
|
|
|
}
|
|
|
|
|
2017-02-27 10:59:08 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-05-02 16:38:02 +02:00
|
|
|
auto VGrainlineItem::shape() const -> QPainterPath
|
2017-02-27 10:59:08 +01:00
|
|
|
{
|
|
|
|
if (m_eMode == mNormal)
|
|
|
|
{
|
2017-02-28 16:33:53 +01:00
|
|
|
return MainShape();
|
2017-02-27 10:59:08 +01:00
|
|
|
}
|
2023-05-02 16:38:02 +02:00
|
|
|
|
|
|
|
QPainterPath path;
|
|
|
|
path.addPolygon(m_polyBound);
|
|
|
|
return path;
|
2017-02-27 10:59:08 +01:00
|
|
|
}
|
|
|
|
|
2016-09-10 18:30:03 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-09-11 12:18:09 +02:00
|
|
|
/**
|
|
|
|
* @brief VGrainlineItem::paint paints the item content
|
|
|
|
* @param pP pointer to the painter object
|
|
|
|
* @param pOption not used
|
|
|
|
* @param pWidget not used
|
|
|
|
*/
|
2016-09-10 18:30:03 +02:00
|
|
|
void VGrainlineItem::paint(QPainter* pP, const QStyleOptionGraphicsItem* pOption, QWidget* pWidget)
|
|
|
|
{
|
2016-12-20 20:19:21 +01:00
|
|
|
Q_UNUSED(pOption)
|
|
|
|
Q_UNUSED(pWidget)
|
2016-09-11 12:18:09 +02:00
|
|
|
pP->save();
|
2016-09-10 18:30:03 +02:00
|
|
|
QColor clr = Qt::black;
|
2021-01-23 17:54:07 +01:00
|
|
|
|
2021-02-06 14:52:21 +01:00
|
|
|
const qreal width = ScaleWidth(VAbstractApplication::VApp()->Settings()->WidthHairLine(), SceneScale(scene()));
|
2021-01-23 17:54:07 +01:00
|
|
|
pP->setPen(QPen(clr, width, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin));
|
2016-09-10 18:30:03 +02:00
|
|
|
pP->setRenderHints(QPainter::Antialiasing);
|
2023-05-02 16:38:02 +02:00
|
|
|
|
|
|
|
VPieceGrainline grainline(QPointF(), m_dLength, m_dRotation, m_eArrowType);
|
|
|
|
// main line
|
|
|
|
const QLineF mainLine = grainline.GetMainLine();
|
2017-02-27 10:59:08 +01:00
|
|
|
pP->drawLine(mainLine.p1(), mainLine.p2());
|
2016-09-10 18:30:03 +02:00
|
|
|
|
2016-10-25 00:53:01 +02:00
|
|
|
pP->setBrush(clr);
|
2017-02-24 18:47:13 +01:00
|
|
|
|
2023-05-02 16:38:02 +02:00
|
|
|
if (grainline.IsArrowUpEnabled())
|
2016-10-25 00:53:01 +02:00
|
|
|
{
|
2023-05-02 16:38:02 +02:00
|
|
|
pP->drawPolygon(grainline.ArrowUp());
|
2016-10-25 00:53:01 +02:00
|
|
|
}
|
2023-05-02 16:38:02 +02:00
|
|
|
|
|
|
|
if (grainline.IsArrowDownEnabled())
|
2016-10-25 00:53:01 +02:00
|
|
|
{
|
2023-05-02 16:38:02 +02:00
|
|
|
pP->drawPolygon(grainline.ArrowDown());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (grainline.IsFourWays())
|
|
|
|
{
|
|
|
|
// secondary line
|
|
|
|
const QLineF secondaryLine = grainline.SecondaryLine();
|
|
|
|
pP->drawLine(secondaryLine.p1(), secondaryLine.p2());
|
|
|
|
|
|
|
|
if (grainline.IsArrowLeftEnabled())
|
|
|
|
{
|
|
|
|
pP->drawPolygon(grainline.ArrowLeft());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (grainline.IsArrowRightEnabled())
|
|
|
|
{
|
|
|
|
pP->drawPolygon(grainline.ArrowRight());
|
2023-04-07 09:05:29 +02:00
|
|
|
}
|
2016-10-25 00:53:01 +02:00
|
|
|
}
|
2016-09-10 18:30:03 +02:00
|
|
|
|
|
|
|
if (m_eMode != mNormal)
|
|
|
|
{
|
|
|
|
pP->setPen(QPen(clr, 2, Qt::DashLine));
|
|
|
|
pP->setBrush(Qt::NoBrush);
|
2016-09-14 01:15:01 +02:00
|
|
|
// bounding polygon
|
2016-09-11 12:18:09 +02:00
|
|
|
pP->drawPolygon(m_polyBound);
|
2016-10-12 02:45:01 +02:00
|
|
|
pP->setPen(QPen(clr, 3));
|
2016-10-11 00:27:14 +02:00
|
|
|
|
2016-10-12 02:45:01 +02:00
|
|
|
if (m_eMode != mRotate)
|
|
|
|
{
|
|
|
|
pP->setBrush(clr);
|
2017-03-14 12:19:34 +01:00
|
|
|
UpdatePolyResize();
|
2016-10-12 02:45:01 +02:00
|
|
|
pP->drawPolygon(m_polyResize);
|
|
|
|
}
|
2016-10-11 00:27:14 +02:00
|
|
|
|
|
|
|
pP->setBrush(Qt::NoBrush);
|
|
|
|
if (m_eMode == mResize)
|
|
|
|
{
|
2016-10-12 02:45:01 +02:00
|
|
|
pP->setPen(clr);
|
2016-10-11 00:27:14 +02:00
|
|
|
pP->drawLine(m_polyBound.at(0), m_polyBound.at(2));
|
|
|
|
pP->drawLine(m_polyBound.at(1), m_polyBound.at(3));
|
|
|
|
}
|
|
|
|
|
2016-10-12 02:45:01 +02:00
|
|
|
if (m_eMode == mRotate)
|
|
|
|
{
|
|
|
|
QPointF ptC = (m_polyBound.at(0) + m_polyBound.at(2))/2;
|
2023-05-02 16:38:02 +02:00
|
|
|
qreal dRad = rotateCircR;
|
2016-10-12 02:45:01 +02:00
|
|
|
pP->setBrush(clr);
|
|
|
|
pP->drawEllipse(ptC, dRad, dRad);
|
2016-10-13 23:33:32 +02:00
|
|
|
|
|
|
|
pP->setBrush(Qt::NoBrush);
|
|
|
|
pP->save();
|
|
|
|
pP->translate(ptC);
|
|
|
|
pP->rotate(qRadiansToDegrees(-m_dRotation));
|
2023-05-02 16:38:02 +02:00
|
|
|
int iX = qRound(m_dLength/2 - 0.5*dRad);
|
|
|
|
int iY = grainline.IsFourWays() ? qRound((m_dLength/2) - 0.8*dRad) : qRound(rectWidth - 0.5*dRad);
|
|
|
|
int iR = qRound(dRad*3);
|
2016-10-13 23:33:32 +02:00
|
|
|
pP->drawArc(iX - iR, iY - iR, iR, iR, 0*16, -90*16);
|
|
|
|
pP->drawArc(-iX, iY - iR, iR, iR, 270*16, -90*16);
|
|
|
|
pP->drawArc(-iX, -iY, iR, iR, 180*16, -90*16);
|
|
|
|
pP->drawArc(iX - iR, -iY, iR, iR, 90*16, -90*16);
|
|
|
|
pP->restore();
|
2016-10-12 02:45:01 +02:00
|
|
|
}
|
2016-09-10 18:30:03 +02:00
|
|
|
}
|
2016-09-11 12:18:09 +02:00
|
|
|
pP->restore();
|
2016-09-10 18:30:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-09-11 12:18:09 +02:00
|
|
|
/**
|
|
|
|
* @brief VGrainlineItem::UpdateGeometry updates the item with grainline parameters
|
|
|
|
* @param ptPos position of one grainline's end
|
|
|
|
* @param dRotation rotation of the grainline in [degrees]
|
|
|
|
* @param dLength length of the grainline in user's units
|
|
|
|
*/
|
2020-01-07 12:54:23 +01:00
|
|
|
void VGrainlineItem::UpdateGeometry(const QPointF& ptPos, qreal dRotation, qreal dLength, GrainlineArrowDirection eAT)
|
2016-09-10 18:30:03 +02:00
|
|
|
{
|
|
|
|
m_dRotation = qDegreesToRadians(dRotation);
|
|
|
|
m_dLength = dLength;
|
2023-05-02 16:38:02 +02:00
|
|
|
m_eArrowType = eAT;
|
2016-09-11 12:18:09 +02:00
|
|
|
|
2023-05-02 16:38:02 +02:00
|
|
|
VPieceGrainline grainline(ptPos, m_dLength, m_dRotation, m_eArrowType);
|
2016-09-14 01:15:01 +02:00
|
|
|
qreal dX;
|
|
|
|
qreal dY;
|
|
|
|
QPointF pt = ptPos;
|
2023-05-02 16:38:02 +02:00
|
|
|
if (not grainline.IsContained(parentItem()->boundingRect(), dX, dY))
|
2016-09-14 01:15:01 +02:00
|
|
|
{
|
|
|
|
pt.setX(pt.x() + dX);
|
|
|
|
pt.setY(pt.y() + dY);
|
|
|
|
}
|
|
|
|
setPos(pt);
|
2016-10-25 00:53:01 +02:00
|
|
|
|
2016-09-11 12:18:09 +02:00
|
|
|
UpdateRectangle();
|
2017-01-19 11:07:26 +01:00
|
|
|
Update();
|
2016-09-11 12:18:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-05-02 16:38:02 +02:00
|
|
|
auto VGrainlineItem::Grainline() const -> VPieceGrainline
|
2016-09-11 12:18:09 +02:00
|
|
|
{
|
2023-05-02 16:38:02 +02:00
|
|
|
return {QLineF(m_ptStart, m_ptFinish), m_eArrowType};
|
2021-01-23 14:24:44 +01:00
|
|
|
}
|
|
|
|
|
2016-09-11 12:18:09 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief VGrainlineItem::mousePressEvent handles left button mouse press events
|
|
|
|
* @param pME pointer to QGraphicsSceneMouseEvent object
|
|
|
|
*/
|
|
|
|
void VGrainlineItem::mousePressEvent(QGraphicsSceneMouseEvent* pME)
|
|
|
|
{
|
2017-10-28 16:31:18 +02:00
|
|
|
if (pME->button() == Qt::LeftButton && pME->type() != QEvent::GraphicsSceneMouseDoubleClick
|
|
|
|
&& (flags() & QGraphicsItem::ItemIsMovable))
|
2016-09-11 12:18:09 +02:00
|
|
|
{
|
2017-02-27 21:06:48 +01:00
|
|
|
if (m_moveType == NotMovable)
|
|
|
|
{
|
|
|
|
pME->ignore();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-31 09:31:50 +01:00
|
|
|
pME->accept();
|
|
|
|
|
2016-09-11 12:18:09 +02:00
|
|
|
m_ptStartPos = pos();
|
|
|
|
m_ptStartMove = pME->scenePos();
|
2016-10-11 00:27:14 +02:00
|
|
|
m_dStartLength = m_dLength;
|
2016-10-13 23:33:32 +02:00
|
|
|
m_dStartRotation = m_dRotation;
|
2016-10-12 02:45:01 +02:00
|
|
|
m_dAngle = GetAngle(mapToParent(pME->pos()));
|
2016-10-13 23:33:32 +02:00
|
|
|
m_ptRotCenter = m_ptCenter;
|
2016-10-11 00:27:14 +02:00
|
|
|
|
2017-03-10 15:22:19 +01:00
|
|
|
if ((m_moveType & AllModifications ) == AllModifications)
|
2016-10-11 00:27:14 +02:00
|
|
|
{
|
2017-03-10 15:22:19 +01:00
|
|
|
AllUserModifications(pME->pos());
|
2023-05-02 16:38:02 +02:00
|
|
|
setZValue(activeZ);
|
2017-03-10 15:22:19 +01:00
|
|
|
Update();
|
|
|
|
}
|
|
|
|
else if (m_moveType & IsRotatable)
|
|
|
|
{
|
|
|
|
if (m_moveType & IsResizable)
|
2016-10-12 02:45:01 +02:00
|
|
|
{
|
2017-03-10 15:22:19 +01:00
|
|
|
AllUserModifications(pME->pos());
|
|
|
|
}
|
|
|
|
else if (m_moveType & IsMovable)
|
|
|
|
{
|
|
|
|
UserRotateAndMove();
|
2016-10-12 02:45:01 +02:00
|
|
|
}
|
2017-03-01 06:39:13 +01:00
|
|
|
else
|
|
|
|
{
|
2017-03-10 15:22:19 +01:00
|
|
|
m_eMode = mRotate;
|
2017-08-01 13:35:48 +02:00
|
|
|
SetItemOverrideCursor(this, cursorArrowCloseHand, 1, 1);
|
2017-03-01 06:39:13 +01:00
|
|
|
}
|
2023-05-02 16:38:02 +02:00
|
|
|
setZValue(activeZ);
|
2017-03-01 06:10:04 +01:00
|
|
|
Update();
|
2016-10-11 00:27:14 +02:00
|
|
|
}
|
2017-03-10 15:22:19 +01:00
|
|
|
else if (m_moveType & IsResizable)
|
2016-10-11 00:27:14 +02:00
|
|
|
{
|
2017-03-10 15:22:19 +01:00
|
|
|
if (m_moveType & IsRotatable)
|
|
|
|
{
|
|
|
|
AllUserModifications(pME->pos());
|
|
|
|
}
|
|
|
|
else if (m_moveType & IsMovable)
|
|
|
|
{
|
|
|
|
UserMoveAndResize(pME->pos());
|
|
|
|
}
|
2023-05-02 16:38:02 +02:00
|
|
|
setZValue(activeZ);
|
2017-03-01 06:10:04 +01:00
|
|
|
Update();
|
2016-10-11 00:27:14 +02:00
|
|
|
}
|
2017-03-10 15:22:19 +01:00
|
|
|
else if (m_moveType & IsMovable)
|
2017-02-27 21:06:48 +01:00
|
|
|
{
|
2017-03-10 15:22:19 +01:00
|
|
|
if (m_moveType & IsRotatable)
|
2017-02-27 21:06:48 +01:00
|
|
|
{
|
2017-03-10 15:22:19 +01:00
|
|
|
UserRotateAndMove();
|
|
|
|
}
|
|
|
|
else if (m_moveType & IsResizable)
|
|
|
|
{
|
|
|
|
UserMoveAndResize(pME->pos());
|
2017-02-27 21:06:48 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-03-10 15:22:19 +01:00
|
|
|
m_eMode = mMove;
|
2017-08-01 13:35:48 +02:00
|
|
|
SetItemOverrideCursor(this, cursorArrowCloseHand, 1, 1);
|
2017-02-27 21:06:48 +01:00
|
|
|
}
|
|
|
|
|
2023-05-02 16:38:02 +02:00
|
|
|
setZValue(activeZ);
|
2017-02-27 21:06:48 +01:00
|
|
|
Update();
|
|
|
|
}
|
2017-03-10 15:22:19 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
pME->ignore();
|
|
|
|
}
|
2016-09-11 12:18:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief VGrainlineItem::mouseMoveEvent handles mouse move events, making sure that the item is moved properly
|
|
|
|
* @param pME pointer to QGraphicsSceneMouseEvent object
|
|
|
|
*/
|
|
|
|
void VGrainlineItem::mouseMoveEvent(QGraphicsSceneMouseEvent* pME)
|
|
|
|
{
|
2016-10-11 00:27:14 +02:00
|
|
|
QPointF ptDiff = pME->scenePos() - m_ptStartMove;
|
2016-10-12 02:45:01 +02:00
|
|
|
qreal dX;
|
|
|
|
qreal dY;
|
2017-03-10 15:22:19 +01:00
|
|
|
if (m_eMode == mMove && m_moveType & IsMovable)
|
2016-09-11 12:18:09 +02:00
|
|
|
{
|
2016-09-14 01:15:01 +02:00
|
|
|
QPointF pt = m_ptStartPos + ptDiff;
|
2023-05-02 16:38:02 +02:00
|
|
|
VPieceGrainline grainline(pt, m_dLength, m_dRotation, m_eArrowType);
|
|
|
|
if (not grainline.IsContained(parentItem()->boundingRect(), dX, dY))
|
2016-09-11 12:18:09 +02:00
|
|
|
{
|
2016-09-14 01:15:01 +02:00
|
|
|
pt.setX(pt.x() + dX);
|
|
|
|
pt.setY(pt.y() + dY);
|
2016-09-11 12:18:09 +02:00
|
|
|
}
|
2016-09-14 01:15:01 +02:00
|
|
|
setPos(pt);
|
2017-01-19 11:07:26 +01:00
|
|
|
Update();
|
2016-10-11 00:27:14 +02:00
|
|
|
}
|
2017-03-10 15:22:19 +01:00
|
|
|
else if (m_eMode == mResize && m_moveType & IsResizable)
|
2016-10-11 00:27:14 +02:00
|
|
|
{
|
|
|
|
qreal dLen = qSqrt(ptDiff.x()*ptDiff.x() + ptDiff.y()*ptDiff.y());
|
2016-10-13 23:33:32 +02:00
|
|
|
qreal dAng = qAtan2(-ptDiff.y(), ptDiff.x());
|
2017-03-14 11:24:59 +01:00
|
|
|
dLen = -dLen*qCos(dAng - m_dRotation);
|
2016-10-11 00:27:14 +02:00
|
|
|
qreal dPrevLen = m_dLength;
|
|
|
|
// try with new length
|
2017-03-10 16:07:23 +01:00
|
|
|
if (not (m_moveType & IsMovable))
|
|
|
|
{
|
|
|
|
dLen *= 2;
|
|
|
|
}
|
2018-08-01 18:22:43 +02:00
|
|
|
|
|
|
|
if (m_dStartLength + dLen < ToPixel(5, Unit::Mm))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-11 00:27:14 +02:00
|
|
|
m_dLength = m_dStartLength + dLen;
|
2017-03-10 16:07:23 +01:00
|
|
|
|
|
|
|
QPointF pos;
|
|
|
|
|
|
|
|
if (m_moveType & IsMovable)
|
|
|
|
{
|
2017-03-14 11:24:59 +01:00
|
|
|
QLineF grainline(this->pos().x(), this->pos().y(),
|
|
|
|
this->pos().x() + dPrevLen, this->pos().y());
|
|
|
|
grainline.setAngle(qRadiansToDegrees(m_dRotation));
|
|
|
|
grainline = QLineF(grainline.p2(), grainline.p1());
|
|
|
|
grainline.setLength(m_dLength);
|
|
|
|
pos = grainline.p2();
|
2017-03-10 16:07:23 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
QLineF grainline(m_ptCenter.x(), m_ptCenter.y(),
|
|
|
|
m_ptCenter.x() + m_dLength / 2.0, m_ptCenter.y());
|
|
|
|
|
|
|
|
grainline.setAngle(qRadiansToDegrees(m_dRotation));
|
|
|
|
grainline = QLineF(grainline.p2(), grainline.p1());
|
|
|
|
grainline.setLength(m_dLength);
|
|
|
|
|
|
|
|
pos = grainline.p2();
|
|
|
|
}
|
|
|
|
|
2016-10-11 00:27:14 +02:00
|
|
|
qreal dX;
|
|
|
|
qreal dY;
|
2023-05-02 16:38:02 +02:00
|
|
|
VPieceGrainline grainline(pos, m_dLength, m_dRotation, m_eArrowType);
|
|
|
|
if (not grainline.IsContained(parentItem()->boundingRect(), dX, dY))
|
2016-10-11 00:27:14 +02:00
|
|
|
{
|
|
|
|
m_dLength = dPrevLen;
|
|
|
|
}
|
2017-03-10 16:07:23 +01:00
|
|
|
else
|
|
|
|
{
|
2017-03-14 11:24:59 +01:00
|
|
|
setPos(pos);
|
2017-03-10 16:07:23 +01:00
|
|
|
}
|
|
|
|
|
2016-09-14 01:15:01 +02:00
|
|
|
UpdateRectangle();
|
2017-01-19 11:07:26 +01:00
|
|
|
Update();
|
2016-09-11 12:18:09 +02:00
|
|
|
}
|
2017-03-10 15:22:19 +01:00
|
|
|
else if (m_eMode == mRotate && m_moveType & IsRotatable)
|
2016-10-12 02:45:01 +02:00
|
|
|
{
|
2016-10-13 23:33:32 +02:00
|
|
|
// prevent strange angle changes due to singularities
|
|
|
|
qreal dLen = qSqrt(ptDiff.x()*ptDiff.x() + ptDiff.y()*ptDiff.y());
|
|
|
|
if (dLen < 2)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-12 02:45:01 +02:00
|
|
|
if (fabs(m_dAngle) < 0.01)
|
|
|
|
{
|
|
|
|
m_dAngle = GetAngle(mapToParent(pME->pos()));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-13 23:33:32 +02:00
|
|
|
qreal dAng = GetAngle(mapToParent(pME->pos())) - m_dAngle;
|
|
|
|
QPointF ptNewPos = Rotate(m_ptStartPos, m_ptRotCenter, dAng);
|
2023-05-02 16:38:02 +02:00
|
|
|
VPieceGrainline grainline(ptNewPos, m_dLength, m_dStartRotation + dAng, m_eArrowType);
|
|
|
|
if (grainline.IsContained(parentItem()->boundingRect(), dX, dY))
|
2016-10-13 23:33:32 +02:00
|
|
|
{
|
|
|
|
setPos(ptNewPos);
|
|
|
|
m_dRotation = m_dStartRotation + dAng;
|
|
|
|
UpdateRectangle();
|
2017-01-19 11:07:26 +01:00
|
|
|
Update();
|
2016-10-13 23:33:32 +02:00
|
|
|
}
|
2016-10-12 02:45:01 +02:00
|
|
|
}
|
2016-09-11 12:18:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief VGrainlineItem::mouseReleaseEvent handles mouse release events and emits the proper signal if the item was
|
|
|
|
* moved
|
|
|
|
* @param pME pointer to QGraphicsSceneMouseEvent object
|
|
|
|
*/
|
|
|
|
void VGrainlineItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* pME)
|
|
|
|
{
|
2023-05-02 16:38:02 +02:00
|
|
|
if (pME->button() != Qt::LeftButton)
|
2016-09-11 12:18:09 +02:00
|
|
|
{
|
2023-05-02 16:38:02 +02:00
|
|
|
VPieceItem::mouseReleaseEvent(pME);
|
|
|
|
return;
|
|
|
|
}
|
2016-10-11 00:27:14 +02:00
|
|
|
|
2023-05-02 16:38:02 +02:00
|
|
|
if ((m_eMode == mMove || m_eMode == mRotate || m_eMode == mResize) && (flags() & QGraphicsItem::ItemIsMovable))
|
|
|
|
{
|
|
|
|
SetItemOverrideCursor(this, cursorArrowOpenHand, 1, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
QPointF ptDiff = pME->scenePos() - m_ptStartMove;
|
|
|
|
qreal dLen = qSqrt(ptDiff.x()*ptDiff.x() + ptDiff.y()*ptDiff.y());
|
|
|
|
bool bShort = (dLen < 2);
|
2016-10-11 00:27:14 +02:00
|
|
|
|
2023-05-02 16:38:02 +02:00
|
|
|
if (m_eMode == mMove || m_eMode == mResize)
|
|
|
|
{
|
|
|
|
if (bShort)
|
2016-10-11 00:27:14 +02:00
|
|
|
{
|
2023-05-02 16:38:02 +02:00
|
|
|
if (m_bReleased && m_moveType & IsRotatable)
|
2016-09-11 12:18:09 +02:00
|
|
|
{
|
2023-05-02 16:38:02 +02:00
|
|
|
m_eMode = mRotate;
|
2017-01-19 11:07:26 +01:00
|
|
|
Update();
|
2016-09-11 12:18:09 +02:00
|
|
|
}
|
|
|
|
}
|
2016-10-12 02:45:01 +02:00
|
|
|
else
|
|
|
|
{
|
2023-05-02 16:38:02 +02:00
|
|
|
if (m_eMode == mMove && m_moveType & IsMovable)
|
2016-10-12 02:45:01 +02:00
|
|
|
{
|
2023-05-02 16:38:02 +02:00
|
|
|
emit SignalMoved(pos());
|
2016-10-12 02:45:01 +02:00
|
|
|
}
|
2023-05-02 16:38:02 +02:00
|
|
|
else if (m_moveType & IsResizable)
|
2016-10-12 02:45:01 +02:00
|
|
|
{
|
2023-05-02 16:38:02 +02:00
|
|
|
emit SignalResized(m_dLength);
|
2016-10-12 02:45:01 +02:00
|
|
|
}
|
2017-01-19 11:07:26 +01:00
|
|
|
Update();
|
2016-10-12 02:45:01 +02:00
|
|
|
}
|
2016-09-11 12:18:09 +02:00
|
|
|
}
|
2023-05-02 16:38:02 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (bShort)
|
|
|
|
{
|
|
|
|
m_eMode = mMove;
|
|
|
|
}
|
|
|
|
else if (m_moveType & IsRotatable)
|
|
|
|
{
|
|
|
|
emit SignalRotated(m_dRotation, m_ptStart);
|
|
|
|
}
|
|
|
|
Update();
|
|
|
|
}
|
|
|
|
m_bReleased = true;
|
2016-09-11 12:18:09 +02:00
|
|
|
}
|
|
|
|
|
2017-02-28 16:33:53 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VGrainlineItem::hoverEnterEvent(QGraphicsSceneHoverEvent *pME)
|
|
|
|
{
|
2017-10-28 16:31:18 +02:00
|
|
|
if (flags() & QGraphicsItem::ItemIsMovable)
|
|
|
|
{
|
|
|
|
SetItemOverrideCursor(this, cursorArrowOpenHand, 1, 1);
|
2021-02-06 14:52:21 +01:00
|
|
|
m_penWidth = VAbstractApplication::VApp()->Settings()->WidthMainLine() + 1;
|
2017-10-28 16:31:18 +02:00
|
|
|
}
|
2017-02-28 16:33:53 +01:00
|
|
|
VPieceItem::hoverEnterEvent(pME);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VGrainlineItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *pME)
|
|
|
|
{
|
2017-10-28 16:31:18 +02:00
|
|
|
if (flags() & QGraphicsItem::ItemIsMovable)
|
|
|
|
{
|
2021-02-06 14:52:21 +01:00
|
|
|
m_penWidth = VAbstractApplication::VApp()->Settings()->WidthMainLine();
|
2017-10-28 16:31:18 +02:00
|
|
|
}
|
2017-02-28 16:33:53 +01:00
|
|
|
VPieceItem::hoverLeaveEvent(pME);
|
|
|
|
}
|
|
|
|
|
2016-09-11 12:18:09 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief VGrainlineItem::UpdateBox updates the item
|
|
|
|
*/
|
2017-01-19 11:07:26 +01:00
|
|
|
void VGrainlineItem::Update()
|
2016-09-11 12:18:09 +02:00
|
|
|
{
|
|
|
|
update(m_rectBoundingBox);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief VGrainlineItem::UpdateRectangle updates the polygon for the box around active item
|
|
|
|
* and the bounding rectangle
|
|
|
|
*/
|
|
|
|
void VGrainlineItem::UpdateRectangle()
|
|
|
|
{
|
2023-05-02 16:38:02 +02:00
|
|
|
VPieceGrainline grainline(QPointF(), m_dLength, m_dRotation, m_eArrowType);
|
|
|
|
const QLineF mainLine = grainline.GetMainLine();
|
2016-10-12 02:45:01 +02:00
|
|
|
|
2023-05-02 16:38:02 +02:00
|
|
|
m_ptStart = mapToParent(mainLine.p1());
|
|
|
|
m_ptFinish = mapToParent(mainLine.p2());
|
2016-10-12 02:45:01 +02:00
|
|
|
m_ptCenter = (m_ptStart + m_ptFinish)/2;
|
2016-09-11 12:18:09 +02:00
|
|
|
|
2017-03-10 15:45:41 +01:00
|
|
|
m_polyBound.clear();
|
2023-05-02 16:38:02 +02:00
|
|
|
|
|
|
|
if (grainline.IsFourWays())
|
|
|
|
{
|
|
|
|
m_polyBound << QPointF(mainLine.p1().x() + (m_dLength/2)*cos(m_dRotation + M_PI/2),
|
|
|
|
mainLine.p1().y() - (m_dLength/2)*sin(m_dRotation + M_PI/2));
|
|
|
|
m_polyBound << QPointF(mainLine.p1().x() + (m_dLength/2)*cos(m_dRotation - M_PI/2),
|
|
|
|
mainLine.p1().y() - (m_dLength/2)*sin(m_dRotation - M_PI/2));
|
|
|
|
m_polyBound << QPointF(mainLine.p2().x() + (m_dLength/2)*cos(m_dRotation - M_PI/2),
|
|
|
|
mainLine.p2().y() - (m_dLength/2)*sin(m_dRotation - M_PI/2));
|
|
|
|
m_polyBound << QPointF(mainLine.p2().x() + (m_dLength/2)*cos(m_dRotation + M_PI/2),
|
|
|
|
mainLine.p2().y() - (m_dLength/2)*sin(m_dRotation + M_PI/2));
|
|
|
|
|
|
|
|
const QLineF secondaryLine = grainline.SecondaryLine();
|
|
|
|
m_ptSecondaryStart = mapToParent(secondaryLine.p1());
|
|
|
|
m_ptSecondaryFinish = mapToParent(secondaryLine.p2());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_polyBound << QPointF(mainLine.p1().x() + rectWidth*cos(m_dRotation + M_PI/2),
|
|
|
|
mainLine.p1().y() - rectWidth*sin(m_dRotation + M_PI/2));
|
|
|
|
m_polyBound << QPointF(mainLine.p1().x() + rectWidth*cos(m_dRotation - M_PI/2),
|
|
|
|
mainLine.p1().y() - rectWidth*sin(m_dRotation - M_PI/2));
|
|
|
|
m_polyBound << QPointF(mainLine.p2().x() + rectWidth*cos(m_dRotation - M_PI/2),
|
|
|
|
mainLine.p2().y() - rectWidth*sin(m_dRotation - M_PI/2));
|
|
|
|
m_polyBound << QPointF(mainLine.p2().x() + rectWidth*cos(m_dRotation + M_PI/2),
|
|
|
|
mainLine.p2().y() - rectWidth*sin(m_dRotation + M_PI/2));
|
|
|
|
}
|
2017-07-11 11:32:07 +02:00
|
|
|
m_rectBoundingBox = m_polyBound.boundingRect().adjusted(-2, -2, 2, 2);
|
2016-09-14 01:15:01 +02:00
|
|
|
setTransformOriginPoint(m_rectBoundingBox.center());
|
2016-10-11 00:27:14 +02:00
|
|
|
|
2017-03-14 12:19:34 +01:00
|
|
|
UpdatePolyResize();
|
2016-09-14 01:15:01 +02:00
|
|
|
prepareGeometryChange();
|
2016-09-11 12:18:09 +02:00
|
|
|
}
|
2016-10-12 02:45:01 +02:00
|
|
|
|
2017-03-01 06:38:43 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-05-02 16:38:02 +02:00
|
|
|
auto VGrainlineItem::GetAngle(const QPointF &pt) const -> double
|
2017-03-01 06:38:43 +01:00
|
|
|
{
|
|
|
|
return -VPieceItem::GetAngle(pt);
|
|
|
|
}
|
|
|
|
|
2016-10-13 23:33:32 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief VGrainlineItem::Rotate rotates point pt around ptCenter by angle dAng [rad]
|
|
|
|
* and returns the resulting point
|
|
|
|
* @param pt point to rotate
|
|
|
|
* @param ptCenter center of rotation
|
|
|
|
* @param dAng angle of rotation
|
|
|
|
* @return point, which is a result of rotating pt around ptCenter by angle dAng
|
|
|
|
*/
|
2023-05-02 16:38:02 +02:00
|
|
|
auto VGrainlineItem::Rotate(const QPointF& pt, const QPointF& ptCenter, qreal dAng) -> QPointF
|
2016-10-13 23:33:32 +02:00
|
|
|
{
|
|
|
|
QPointF ptRel = pt - ptCenter;
|
|
|
|
QPointF ptFinal;
|
|
|
|
ptFinal.setX(ptRel.x()*qCos(dAng) + ptRel.y()*qSin(dAng));
|
|
|
|
ptFinal.setY(-ptRel.x()*qSin(dAng) + ptRel.y()*qCos(dAng));
|
|
|
|
ptFinal += ptCenter;
|
|
|
|
return ptFinal;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief VGrainlineItem::GetInsideCorner calculates a point inside the bounding polygon,
|
|
|
|
* dDist away of i-th point in each direction
|
|
|
|
* @param i index of corner
|
|
|
|
* @param dDist distance
|
|
|
|
* @return resulting point
|
|
|
|
*/
|
2023-05-02 16:38:02 +02:00
|
|
|
auto VGrainlineItem::GetInsideCorner(int i, qreal dDist) const -> QPointF
|
2016-10-13 23:33:32 +02:00
|
|
|
{
|
|
|
|
QPointF pt1 = m_polyBound.at((i + 1) % m_polyBound.count()) - m_polyBound.at(i);
|
|
|
|
QPointF pt2 = m_polyBound.at((i + m_polyBound.count() - 1) % m_polyBound.count()) - m_polyBound.at(i);
|
|
|
|
|
|
|
|
pt1 = dDist*pt1/qSqrt(pt1.x()*pt1.x() + pt1.y()*pt1.y());
|
|
|
|
pt2 = dDist*pt2/qSqrt(pt2.x()*pt2.x() + pt2.y()*pt2.y());
|
|
|
|
|
|
|
|
return m_polyBound.at(i) + pt1 + pt2;
|
|
|
|
}
|
2017-02-24 18:47:13 +01:00
|
|
|
|
2017-02-27 10:59:08 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2023-05-02 16:38:02 +02:00
|
|
|
auto VGrainlineItem::MainShape() const -> QPainterPath
|
2017-02-27 10:59:08 +01:00
|
|
|
{
|
2023-05-02 16:38:02 +02:00
|
|
|
VPieceGrainline grainline(QPointF(), m_dLength, m_dRotation, m_eArrowType);
|
|
|
|
const QLineF mainLine = grainline.GetMainLine();
|
2017-02-28 16:33:53 +01:00
|
|
|
QPainterPath linePath;
|
|
|
|
linePath.moveTo(mainLine.p1());
|
|
|
|
linePath.lineTo(mainLine.p2());
|
|
|
|
linePath.closeSubpath();
|
|
|
|
|
|
|
|
QPainterPathStroker stroker;
|
|
|
|
stroker.setWidth(m_penWidth);
|
2023-05-02 16:38:02 +02:00
|
|
|
QPainterPath path;
|
2017-02-28 16:33:53 +01:00
|
|
|
path.addPath((stroker.createStroke(linePath) + linePath).simplified());
|
|
|
|
path.closeSubpath();
|
|
|
|
|
2023-05-02 16:38:02 +02:00
|
|
|
if (grainline.IsArrowUpEnabled())
|
2017-02-28 16:33:53 +01:00
|
|
|
{
|
|
|
|
QPainterPath polyPath;
|
2023-05-02 16:38:02 +02:00
|
|
|
polyPath.addPolygon(grainline.ArrowUp());
|
2023-04-07 09:05:29 +02:00
|
|
|
|
2017-02-28 16:33:53 +01:00
|
|
|
path.addPath((stroker.createStroke(polyPath) + polyPath).simplified());
|
|
|
|
path.closeSubpath();
|
|
|
|
}
|
|
|
|
|
2023-05-02 16:38:02 +02:00
|
|
|
if (grainline.IsArrowDownEnabled())
|
2017-02-28 16:33:53 +01:00
|
|
|
{
|
|
|
|
QPainterPath polyPath;
|
2023-05-02 16:38:02 +02:00
|
|
|
polyPath.addPolygon(grainline.ArrowDown());
|
2023-04-07 09:05:29 +02:00
|
|
|
|
2017-02-28 16:33:53 +01:00
|
|
|
path.addPath((stroker.createStroke(polyPath) + polyPath).simplified());
|
|
|
|
path.closeSubpath();
|
|
|
|
}
|
2023-05-02 16:38:02 +02:00
|
|
|
|
|
|
|
if (grainline.IsFourWays())
|
|
|
|
{
|
|
|
|
const QLineF secondaryLine = grainline.SecondaryLine();
|
|
|
|
QPainterPath secondaryLinePath;
|
|
|
|
secondaryLinePath.moveTo(secondaryLine.p1());
|
|
|
|
secondaryLinePath.lineTo(secondaryLine.p2());
|
|
|
|
secondaryLinePath.closeSubpath();
|
|
|
|
|
|
|
|
path.addPath((stroker.createStroke(secondaryLinePath) + secondaryLinePath).simplified());
|
|
|
|
path.closeSubpath();
|
|
|
|
|
|
|
|
if (grainline.IsArrowLeftEnabled())
|
|
|
|
{
|
|
|
|
QPainterPath polyPath;
|
|
|
|
polyPath.addPolygon(grainline.ArrowLeft());
|
|
|
|
|
|
|
|
path.addPath((stroker.createStroke(polyPath) + polyPath).simplified());
|
|
|
|
path.closeSubpath();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (grainline.IsArrowRightEnabled())
|
|
|
|
{
|
|
|
|
QPainterPath polyPath;
|
|
|
|
polyPath.addPolygon(grainline.ArrowRight());
|
|
|
|
|
|
|
|
path.addPath((stroker.createStroke(polyPath) + polyPath).simplified());
|
|
|
|
path.closeSubpath();
|
|
|
|
}
|
|
|
|
}
|
2017-02-28 16:33:53 +01:00
|
|
|
return path;
|
|
|
|
}
|
2017-03-10 15:22:19 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VGrainlineItem::AllUserModifications(const QPointF &pos)
|
|
|
|
{
|
|
|
|
if (m_eMode != mRotate)
|
|
|
|
{
|
|
|
|
UserMoveAndResize(pos);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-08-01 13:35:48 +02:00
|
|
|
SetItemOverrideCursor(this, cursorArrowCloseHand, 1, 1);
|
2017-03-10 15:22:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VGrainlineItem::UserRotateAndMove()
|
|
|
|
{
|
|
|
|
if (m_eMode != mRotate)
|
|
|
|
{
|
|
|
|
m_eMode = mMove;
|
|
|
|
}
|
2017-08-01 13:35:48 +02:00
|
|
|
SetItemOverrideCursor(this, cursorArrowCloseHand, 1, 1);
|
2017-03-10 15:22:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VGrainlineItem::UserMoveAndResize(const QPointF &pos)
|
|
|
|
{
|
2023-05-02 16:38:02 +02:00
|
|
|
if (m_polyResize.containsPoint(pos, Qt::OddEvenFill))
|
2017-03-10 15:22:19 +01:00
|
|
|
{
|
|
|
|
m_eMode = mResize;
|
2017-08-01 13:35:48 +02:00
|
|
|
setCursor(Qt::SizeFDiagCursor);
|
2017-03-10 15:22:19 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_eMode = mMove; // block later if need
|
2017-08-01 13:35:48 +02:00
|
|
|
SetItemOverrideCursor(this, cursorArrowCloseHand, 1, 1);
|
2017-03-10 15:22:19 +01:00
|
|
|
}
|
|
|
|
}
|
2017-03-14 12:19:34 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VGrainlineItem::UpdatePolyResize()
|
|
|
|
{
|
|
|
|
m_polyResize.clear();
|
|
|
|
QPointF ptA = m_polyBound.at(1);
|
|
|
|
m_polyResize << ptA;
|
2023-05-02 16:38:02 +02:00
|
|
|
const double dSize = resizeRectSize;
|
2017-03-14 12:19:34 +01:00
|
|
|
|
|
|
|
ptA.setX(ptA.x() - dSize*cos(m_dRotation - M_PI/2));
|
|
|
|
ptA.setY(ptA.y() + dSize*sin(m_dRotation - M_PI/2));
|
|
|
|
m_polyResize << ptA;
|
|
|
|
|
|
|
|
ptA.setX(ptA.x() + dSize*cos(m_dRotation));
|
|
|
|
ptA.setY(ptA.y() - dSize*sin(m_dRotation));
|
|
|
|
m_polyResize << ptA;
|
|
|
|
|
|
|
|
ptA.setX(ptA.x() - dSize*cos(m_dRotation + M_PI/2));
|
|
|
|
ptA.setY(ptA.y() + dSize*sin(m_dRotation + M_PI/2));
|
|
|
|
m_polyResize << ptA;
|
|
|
|
}
|