2013-11-15 13:41:26 +01:00
|
|
|
/************************************************************************
|
2013-09-18 21:16:19 +02:00
|
|
|
**
|
2013-11-15 13:50:05 +01:00
|
|
|
** @file vcontrolpointspline.cpp
|
2014-04-30 07:38:52 +02:00
|
|
|
** @author Roman Telezhynskyi <dismine(at)gmail.com>
|
2013-11-15 13:50:05 +01:00
|
|
|
** @date November 15, 2013
|
2013-09-18 21:16:19 +02:00
|
|
|
**
|
2013-11-15 13:41:26 +01:00
|
|
|
** @brief
|
|
|
|
** @copyright
|
2017-10-05 11:20:01 +02:00
|
|
|
** This source code is part of the Valentina project, a pattern making
|
2013-11-15 13:41:26 +01:00
|
|
|
** program, whose allow create and modeling patterns of clothing.
|
2015-02-27 11:27:48 +01:00
|
|
|
** Copyright (C) 2013-2015 Valentina project
|
2020-01-31 07:00:05 +01:00
|
|
|
** <https://gitlab.com/smart-pattern/valentina> All Rights Reserved.
|
2013-09-18 21:16:19 +02:00
|
|
|
**
|
2013-11-15 13:41:26 +01:00
|
|
|
** Valentina is free software: you can redistribute it and/or modify
|
2013-09-18 21:16:19 +02:00
|
|
|
** 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.
|
|
|
|
**
|
2013-10-27 13:36:29 +01:00
|
|
|
** Valentina is distributed in the hope that it will be useful,
|
2013-09-18 21:16:19 +02:00
|
|
|
** 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/>.
|
|
|
|
**
|
2013-11-15 13:41:26 +01:00
|
|
|
*************************************************************************/
|
2013-09-18 21:16:19 +02:00
|
|
|
|
2013-08-05 10:37:56 +02:00
|
|
|
#include "vcontrolpointspline.h"
|
|
|
|
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QBrush>
|
|
|
|
#include <QEvent>
|
|
|
|
#include <QGraphicsLineItem>
|
|
|
|
#include <QGraphicsScene>
|
2014-09-08 15:54:41 +02:00
|
|
|
#include <QGraphicsSceneMouseEvent>
|
2015-11-25 18:26:37 +01:00
|
|
|
#include <QGraphicsView>
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QLineF>
|
|
|
|
#include <QList>
|
|
|
|
#include <QMessageLogger>
|
2013-11-21 13:05:26 +01:00
|
|
|
#include <QPen>
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QPoint>
|
|
|
|
#include <QPolygonF>
|
|
|
|
#include <QRectF>
|
|
|
|
#include <Qt>
|
2013-11-21 13:05:26 +01:00
|
|
|
|
2017-06-16 13:53:08 +02:00
|
|
|
#include "../vwidgets/global.h"
|
2016-08-08 13:44:49 +02:00
|
|
|
#include "../vgeometry/vgobject.h"
|
2018-02-06 18:30:27 +01:00
|
|
|
#include "../vmisc/vabstractapplication.h"
|
2022-02-18 09:36:38 +01:00
|
|
|
#include "../vmisc/literals.h"
|
2016-01-15 13:57:47 +01:00
|
|
|
#include "vmaingraphicsscene.h"
|
2016-08-08 13:44:49 +02:00
|
|
|
#include "vmaingraphicsview.h"
|
2017-06-16 13:53:08 +02:00
|
|
|
#include "vgraphicssimpletextitem.h"
|
2017-06-23 11:25:02 +02:00
|
|
|
#include "scalesceneitems.h"
|
2016-08-08 13:44:49 +02:00
|
|
|
|
2016-02-12 19:10:01 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2017-06-16 13:53:08 +02:00
|
|
|
VControlPointSpline::VControlPointSpline(const qint32 &indexSpline, SplinePointPosition position, QGraphicsItem *parent)
|
|
|
|
: VScenePoint(parent),
|
2016-02-12 19:10:01 +01:00
|
|
|
controlLine(nullptr),
|
|
|
|
indexSpline(indexSpline),
|
|
|
|
position(position),
|
2016-02-27 09:29:32 +01:00
|
|
|
freeAngle(true),
|
|
|
|
freeLength(true)
|
2016-02-12 19:10:01 +01:00
|
|
|
{
|
|
|
|
Init();
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-13 19:02:41 +02:00
|
|
|
/**
|
|
|
|
* @brief VControlPointSpline constructor.
|
|
|
|
* @param indexSpline index spline in list.
|
|
|
|
* @param position position point in spline.
|
|
|
|
* @param controlPoint control point.
|
|
|
|
* @param parent parent object.
|
|
|
|
*/
|
2014-06-12 09:22:29 +02:00
|
|
|
VControlPointSpline::VControlPointSpline(const qint32 &indexSpline, SplinePointPosition position,
|
2018-02-06 20:05:43 +01:00
|
|
|
const QPointF &controlPoint, bool freeAngle,
|
2017-06-16 13:53:08 +02:00
|
|
|
bool freeLength, QGraphicsItem *parent)
|
|
|
|
: VScenePoint(parent),
|
2016-02-12 19:10:01 +01:00
|
|
|
controlLine(nullptr),
|
|
|
|
indexSpline(indexSpline),
|
|
|
|
position(position),
|
2016-02-27 09:29:32 +01:00
|
|
|
freeAngle(freeAngle),
|
|
|
|
freeLength(freeLength)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2016-02-12 19:10:01 +01:00
|
|
|
Init();
|
|
|
|
|
2013-08-05 10:37:56 +02:00
|
|
|
this->setFlag(QGraphicsItem::ItemIsMovable, true);
|
|
|
|
this->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
|
|
|
|
this->setAcceptHoverEvents(true);
|
|
|
|
this->setPos(controlPoint);
|
|
|
|
|
2018-02-06 20:05:43 +01:00
|
|
|
// Do not draw control point in constructor. It creates artefacts if they enabled from start.
|
2013-08-05 10:37:56 +02:00
|
|
|
}
|
|
|
|
|
2017-06-16 13:53:08 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VControlPointSpline::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|
|
|
{
|
2017-06-23 11:25:02 +02:00
|
|
|
QPen lPen = controlLine->pen();
|
|
|
|
lPen.setColor(CorrectColor(controlLine, Qt::black));
|
|
|
|
controlLine->setPen(lPen);
|
2017-06-16 13:53:08 +02:00
|
|
|
|
2018-05-08 11:04:12 +02:00
|
|
|
QPointF p1, p2;
|
|
|
|
VGObject::LineIntersectCircle(QPointF(), ScaledRadius(SceneScale(scene())),
|
|
|
|
QLineF( QPointF(), controlLine->line().p1()), p1, p2);
|
|
|
|
QLineF line(controlLine->line().p1(), p1);
|
|
|
|
controlLine->setLine(line);
|
|
|
|
|
2017-06-16 13:53:08 +02:00
|
|
|
VScenePoint::paint(painter, option, widget);
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-13 19:02:41 +02:00
|
|
|
/**
|
2014-08-19 13:16:00 +02:00
|
|
|
* @brief hoverEnterEvent handle hover enter events.
|
2014-06-13 19:02:41 +02:00
|
|
|
* @param event hover move event.
|
|
|
|
*/
|
2014-08-19 13:16:00 +02:00
|
|
|
void VControlPointSpline::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2017-10-30 10:08:25 +01:00
|
|
|
if ((flags() & QGraphicsItem::ItemIsMovable) && (freeAngle || freeLength))
|
2016-02-27 09:29:32 +01:00
|
|
|
{
|
2017-08-01 13:35:48 +02:00
|
|
|
SetItemOverrideCursor(this, cursorArrowOpenHand, 1, 1);
|
2016-02-27 09:29:32 +01:00
|
|
|
}
|
2017-06-16 13:53:08 +02:00
|
|
|
VScenePoint::hoverEnterEvent(event);
|
2013-08-05 10:37:56 +02:00
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-13 19:02:41 +02:00
|
|
|
/**
|
|
|
|
* @brief itemChange handle item change.
|
|
|
|
* @param change change.
|
|
|
|
* @param value value.
|
|
|
|
* @return value.
|
|
|
|
*/
|
2013-11-04 21:35:15 +01:00
|
|
|
QVariant VControlPointSpline::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
|
|
|
|
{
|
2016-02-27 09:29:32 +01:00
|
|
|
switch (change)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
2016-02-27 09:29:32 +01:00
|
|
|
case ItemPositionChange:
|
2015-11-25 14:45:38 +01:00
|
|
|
{
|
2016-02-27 09:29:32 +01:00
|
|
|
if (not freeAngle || not freeLength)
|
2015-11-25 18:26:37 +01:00
|
|
|
{
|
2016-02-27 09:29:32 +01:00
|
|
|
const QPointF splPoint = controlLine->line().p1() + pos();
|
|
|
|
|
|
|
|
QLineF newLine(splPoint, value.toPointF());// value - new position.
|
|
|
|
QLineF oldLine(splPoint, pos());// pos() - old position.
|
|
|
|
|
|
|
|
if (not freeAngle)
|
2015-11-25 18:26:37 +01:00
|
|
|
{
|
2016-02-27 09:29:32 +01:00
|
|
|
newLine.setAngle(oldLine.angle());
|
|
|
|
}
|
2016-01-15 13:57:47 +01:00
|
|
|
|
2016-02-27 09:29:32 +01:00
|
|
|
if (not freeLength)
|
|
|
|
{
|
|
|
|
newLine.setLength(oldLine.length());
|
|
|
|
}
|
2016-01-15 13:57:47 +01:00
|
|
|
|
2016-02-27 09:29:32 +01:00
|
|
|
return newLine.p2();
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ItemPositionHasChanged:
|
|
|
|
{
|
|
|
|
// Each time we move something we call recalculation scene rect. In some cases this can cause moving
|
|
|
|
// objects positions. And this cause infinite redrawing. That's why we wait the finish of saving the last
|
|
|
|
// move.
|
|
|
|
static bool changeFinished = true;
|
|
|
|
if (changeFinished)
|
|
|
|
{
|
|
|
|
changeFinished = false;
|
|
|
|
// value - new position.
|
|
|
|
emit ControlPointChangePosition(indexSpline, position, value.toPointF());
|
|
|
|
if (scene())
|
|
|
|
{
|
|
|
|
const QList<QGraphicsView *> viewList = scene()->views();
|
|
|
|
if (not viewList.isEmpty())
|
|
|
|
{
|
2017-10-02 15:25:18 +02:00
|
|
|
if (VMainGraphicsView *view = qobject_cast<VMainGraphicsView *>(viewList.at(0)))
|
2016-01-15 13:57:47 +01:00
|
|
|
{
|
2020-06-20 11:20:39 +02:00
|
|
|
view->EnsureItemVisibleWithDelay(this, VMainGraphicsView::scrollDelay);
|
2016-01-15 13:57:47 +01:00
|
|
|
}
|
2015-12-05 17:27:38 +01:00
|
|
|
}
|
2015-11-25 18:26:37 +01:00
|
|
|
}
|
2016-02-27 09:29:32 +01:00
|
|
|
changeFinished = true;
|
2015-11-25 18:26:37 +01:00
|
|
|
}
|
2016-02-27 09:29:32 +01:00
|
|
|
break;
|
2015-11-25 14:45:38 +01:00
|
|
|
}
|
2017-10-30 07:34:18 +01:00
|
|
|
case QGraphicsItem::ItemSelectedHasChanged:
|
|
|
|
emit Selected(value.toBool());
|
|
|
|
break;
|
2016-02-27 09:29:32 +01:00
|
|
|
default:
|
|
|
|
break;
|
2013-08-05 10:37:56 +02:00
|
|
|
}
|
2017-06-16 13:53:08 +02:00
|
|
|
return VScenePoint::itemChange(change, value);
|
2013-08-05 10:37:56 +02:00
|
|
|
}
|
|
|
|
|
2014-08-19 13:16:00 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VControlPointSpline::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
2014-08-29 16:10:47 +02:00
|
|
|
if (event->button() == Qt::LeftButton && event->type() != QEvent::GraphicsSceneMouseDoubleClick)
|
2014-08-19 13:16:00 +02:00
|
|
|
{
|
2016-02-27 09:29:32 +01:00
|
|
|
if (freeAngle || freeLength)
|
|
|
|
{
|
2017-08-01 13:35:48 +02:00
|
|
|
SetItemOverrideCursor(this, cursorArrowCloseHand, 1, 1);
|
2017-10-31 09:31:50 +01:00
|
|
|
event->accept();
|
2016-02-27 09:29:32 +01:00
|
|
|
}
|
2017-10-29 12:48:28 +01:00
|
|
|
|
|
|
|
// Somehow clicking on notselectable object do not clean previous selections.
|
|
|
|
if (not (flags() & ItemIsSelectable) && scene())
|
|
|
|
{
|
|
|
|
scene()->clearSelection();
|
|
|
|
}
|
2014-08-19 13:16:00 +02:00
|
|
|
}
|
2017-06-16 13:53:08 +02:00
|
|
|
VScenePoint::mousePressEvent(event);
|
2014-08-19 13:16:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VControlPointSpline::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
|
|
|
{
|
2014-08-29 16:10:47 +02:00
|
|
|
if (event->button() == Qt::LeftButton && event->type() != QEvent::GraphicsSceneMouseDoubleClick)
|
2014-08-19 13:16:00 +02:00
|
|
|
{
|
2016-02-27 09:29:32 +01:00
|
|
|
if (freeAngle || freeLength)
|
|
|
|
{
|
2017-08-01 13:35:48 +02:00
|
|
|
SetItemOverrideCursor(this, cursorArrowOpenHand, 1, 1);
|
2017-10-03 12:01:56 +02:00
|
|
|
emit Released();
|
2016-02-27 09:29:32 +01:00
|
|
|
}
|
2014-08-19 13:16:00 +02:00
|
|
|
}
|
2017-06-16 13:53:08 +02:00
|
|
|
VScenePoint::mouseReleaseEvent(event);
|
2014-08-19 13:16:00 +02:00
|
|
|
}
|
|
|
|
|
2015-04-22 15:13:13 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VControlPointSpline::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
|
|
|
|
{
|
|
|
|
emit ShowContextMenu(event);
|
|
|
|
}
|
|
|
|
|
2016-02-12 19:10:01 +01:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VControlPointSpline::Init()
|
|
|
|
{
|
2017-06-16 13:53:08 +02:00
|
|
|
m_baseColor = Qt::red;
|
|
|
|
SetOnlyPoint(true);
|
2016-02-12 19:10:01 +01:00
|
|
|
this->setBrush(QBrush(Qt::NoBrush));
|
|
|
|
this->setZValue(100);
|
|
|
|
|
2017-06-23 11:25:02 +02:00
|
|
|
controlLine = new VScaledLine(this);
|
2018-02-06 18:30:27 +01:00
|
|
|
controlLine->SetBoldLine(false);
|
2018-06-19 11:22:47 +02:00
|
|
|
//controlLine->setFlag(QGraphicsItem::ItemStacksBehindParent, true);
|
2017-10-29 19:12:35 +01:00
|
|
|
controlLine->setVisible(false);
|
2016-02-12 19:10:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VControlPointSpline::SetCtrlLine(const QPointF &controlPoint, const QPointF &splinePoint)
|
|
|
|
{
|
2018-06-19 12:18:38 +02:00
|
|
|
QLineF circleLine (QPointF(), splinePoint-controlPoint);
|
|
|
|
const qreal radius = ScaledRadius(SceneScale(scene()));
|
|
|
|
|
|
|
|
if (circleLine.length() > radius)
|
|
|
|
{
|
|
|
|
QPointF p1, p2;
|
|
|
|
VGObject::LineIntersectCircle(QPointF(), radius, circleLine, p1, p2);
|
|
|
|
QLineF line(splinePoint-controlPoint, p1);
|
|
|
|
controlLine->setFlag(QGraphicsItem::ItemSendsGeometryChanges, false);
|
|
|
|
controlLine->setLine(line);
|
|
|
|
controlLine->setVisible(not line.isNull());
|
|
|
|
controlLine->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
controlLine->setVisible(false);
|
|
|
|
}
|
2016-02-12 19:10:01 +01:00
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-13 19:02:41 +02:00
|
|
|
/**
|
2016-02-12 19:10:01 +01:00
|
|
|
* @brief RefreshCtrlPoint refresh the control point.
|
2014-06-13 19:02:41 +02:00
|
|
|
* @param indexSpline index spline in list.
|
|
|
|
* @param pos position point in spline.
|
|
|
|
* @param controlPoint control point.
|
|
|
|
* @param splinePoint spline point.
|
|
|
|
*/
|
2016-02-12 19:10:01 +01:00
|
|
|
void VControlPointSpline::RefreshCtrlPoint(const qint32 &indexSpline, SplinePointPosition pos,
|
2016-02-27 09:29:32 +01:00
|
|
|
const QPointF &controlPoint, const QPointF &splinePoint, bool freeAngle,
|
|
|
|
bool freeLength)
|
2013-11-04 21:35:15 +01:00
|
|
|
{
|
|
|
|
if (this->indexSpline == indexSpline && this->position == pos)
|
|
|
|
{
|
2016-02-27 09:29:32 +01:00
|
|
|
this->freeAngle = freeAngle;
|
|
|
|
this->freeLength = freeLength;
|
2016-02-12 19:10:01 +01:00
|
|
|
this->setPos(controlPoint);
|
|
|
|
SetCtrlLine(controlPoint, splinePoint);
|
2013-08-09 08:49:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-02 13:11:30 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2014-06-13 19:02:41 +02:00
|
|
|
/**
|
|
|
|
* @brief setEnabledPoint disable or enable control point.
|
|
|
|
* @param enable true - enable.
|
|
|
|
*/
|
2013-11-04 21:35:15 +01:00
|
|
|
void VControlPointSpline::setEnabledPoint(bool enable)
|
|
|
|
{
|
2017-06-16 13:53:08 +02:00
|
|
|
setEnabled(enable);
|
|
|
|
controlLine->setEnabled(enable);
|
2013-08-05 10:37:56 +02:00
|
|
|
}
|