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
|
|
|
|
** This source code is part of the Valentine project, a pattern making
|
|
|
|
** program, whose allow create and modeling patterns of clothing.
|
2015-02-27 11:27:48 +01:00
|
|
|
** Copyright (C) 2013-2015 Valentina project
|
2013-11-15 13:41:26 +01:00
|
|
|
** <https://bitbucket.org/dismine/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"
|
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"
|
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 splinePoint spline point.
|
|
|
|
* @param parent parent object.
|
|
|
|
*/
|
2014-06-12 09:22:29 +02:00
|
|
|
VControlPointSpline::VControlPointSpline(const qint32 &indexSpline, SplinePointPosition position,
|
2017-06-16 13:53:08 +02:00
|
|
|
const QPointF &controlPoint, const QPointF &splinePoint, bool freeAngle,
|
|
|
|
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);
|
|
|
|
|
2016-02-12 19:10:01 +01:00
|
|
|
SetCtrlLine(controlPoint, splinePoint);
|
2013-08-05 10:37:56 +02:00
|
|
|
}
|
|
|
|
|
2015-04-29 19:02:03 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VControlPointSpline::~VControlPointSpline()
|
|
|
|
{
|
|
|
|
//Disable cursor-arrow-openhand
|
2015-06-15 15:47:44 +02:00
|
|
|
RestoreOverrideCursor(cursorArrowOpenHand);
|
2015-04-29 19:02:03 +02:00
|
|
|
}
|
|
|
|
|
2017-06-16 13:53:08 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VControlPointSpline::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|
|
|
{
|
|
|
|
const qreal scale = SceneScale(scene());
|
|
|
|
|
|
|
|
ScaleLinePenWidth(controlLine, scale);
|
|
|
|
|
|
|
|
VScenePoint::paint(painter, option, widget);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QRectF VControlPointSpline::boundingRect() const
|
|
|
|
{
|
|
|
|
QRectF rect = VScenePoint::boundingRect();
|
|
|
|
rect = rect.united(controlLine->boundingRect());
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2016-02-27 09:29:32 +01:00
|
|
|
if (freeAngle || freeLength)
|
|
|
|
{
|
|
|
|
SetOverrideCursor(cursorArrowOpenHand, 1, 1);
|
|
|
|
}
|
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
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2013-11-04 21:35:15 +01:00
|
|
|
void VControlPointSpline::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
|
|
|
|
{
|
2016-02-27 09:29:32 +01:00
|
|
|
if (freeAngle || freeLength)
|
|
|
|
{
|
|
|
|
//Disable cursor-arrow-openhand
|
|
|
|
RestoreOverrideCursor(cursorArrowOpenHand);
|
|
|
|
}
|
2017-06-16 13:53:08 +02:00
|
|
|
VScenePoint::hoverLeaveEvent(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())
|
|
|
|
{
|
|
|
|
if (QGraphicsView *view = viewList.at(0))
|
2016-01-15 13:57:47 +01:00
|
|
|
{
|
2016-02-27 09:29:32 +01:00
|
|
|
const int xmargin = 50;
|
|
|
|
const int ymargin = 50;
|
|
|
|
|
|
|
|
const QRectF viewRect = VMainGraphicsView::SceneVisibleArea(view);
|
|
|
|
const QRectF itemRect = mapToScene(boundingRect()).boundingRect();
|
|
|
|
|
|
|
|
// If item's rect is bigger than view's rect ensureVisible works very unstable.
|
|
|
|
if (itemRect.height() + 2*ymargin < viewRect.height() &&
|
|
|
|
itemRect.width() + 2*xmargin < viewRect.width())
|
|
|
|
{
|
|
|
|
view->ensureVisible(itemRect, xmargin, ymargin);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Ensure visible only small rect around a cursor
|
|
|
|
VMainGraphicsScene *currentScene = qobject_cast<VMainGraphicsScene *>(scene());
|
2016-12-20 19:57:20 +01:00
|
|
|
SCASSERT(currentScene)
|
2016-02-27 09:29:32 +01:00
|
|
|
const QPointF cursorPosition = currentScene->getScenePos();
|
|
|
|
view->ensureVisible(QRectF(cursorPosition.x()-5, cursorPosition.y()-5, 10, 10));
|
|
|
|
}
|
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
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
SetOverrideCursor(cursorArrowCloseHand, 1, 1);
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
//Disable cursor-arrow-closehand
|
|
|
|
RestoreOverrideCursor(cursorArrowCloseHand);
|
|
|
|
}
|
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);
|
|
|
|
|
|
|
|
controlLine = new QGraphicsLineItem(this);
|
2017-06-16 13:53:08 +02:00
|
|
|
|
|
|
|
QPen cPen = controlLine->pen();
|
|
|
|
cPen.setBrush(QBrush(Qt::red));
|
|
|
|
|
|
|
|
controlLine->setPen(cPen);
|
2016-02-12 19:10:01 +01:00
|
|
|
controlLine->setFlag(QGraphicsItem::ItemStacksBehindParent, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VControlPointSpline::SetCtrlLine(const QPointF &controlPoint, const QPointF &splinePoint)
|
|
|
|
{
|
|
|
|
QPointF p1, p2;
|
2017-06-16 13:53:08 +02:00
|
|
|
VGObject::LineIntersectCircle(QPointF(), ScaledRadius(SceneScale(scene())),
|
|
|
|
QLineF( QPointF(), splinePoint-controlPoint), p1, p2);
|
2016-02-12 19:10:01 +01:00
|
|
|
controlLine->setLine(QLineF(splinePoint-controlPoint, p1));
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
setFlag(QGraphicsItem::ItemIsSelectable, enable);
|
|
|
|
setFlag(QGraphicsItem::ItemIsMovable, enable);
|
|
|
|
setAcceptHoverEvents(enable);
|
2013-08-05 10:37:56 +02:00
|
|
|
}
|