2015-06-20 18:14:04 +02:00
|
|
|
/************************************************************************
|
|
|
|
**
|
|
|
|
** @file vsimplepoint.cpp
|
|
|
|
** @author Roman Telezhynskyi <dismine(at)gmail.com>
|
|
|
|
** @date 20 6, 2015
|
|
|
|
**
|
|
|
|
** @brief
|
|
|
|
** @copyright
|
|
|
|
** This source code is part of the Valentine project, a pattern making
|
|
|
|
** program, whose allow create and modeling patterns of clothing.
|
|
|
|
** Copyright (C) 2015 Valentina project
|
|
|
|
** <https://bitbucket.org/dismine/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 "vsimplepoint.h"
|
|
|
|
#include "vgraphicssimpletextitem.h"
|
|
|
|
#include "../ifc/ifcdef.h"
|
|
|
|
#include "../vgeometry/vgobject.h"
|
|
|
|
#include "../vgeometry/vpointf.h"
|
|
|
|
|
|
|
|
#include <QGraphicsSceneMouseEvent>
|
|
|
|
#include <QStyleOptionGraphicsItem>
|
2015-10-18 21:30:51 +02:00
|
|
|
#include <QPen>
|
2016-05-08 17:04:18 +02:00
|
|
|
#include <QKeyEvent>
|
2016-05-24 11:43:30 +02:00
|
|
|
#include <QGraphicsScene>
|
2015-06-20 18:14:04 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VSimplePoint::VSimplePoint(quint32 id, const QColor ¤tColor, Unit patternUnit, qreal *factor, QObject *parent)
|
|
|
|
:VAbstractSimple(id, currentColor, patternUnit, factor, parent), QGraphicsEllipseItem(),
|
2016-05-12 21:11:21 +02:00
|
|
|
radius(ToPixel(DefPointRadius/*mm*/, Unit::Mm)), namePoint(nullptr), lineName(nullptr)
|
2015-06-20 18:14:04 +02:00
|
|
|
{
|
|
|
|
namePoint = new VGraphicsSimpleTextItem(this);
|
|
|
|
connect(namePoint, &VGraphicsSimpleTextItem::ShowContextMenu, this, &VSimplePoint::ContextMenu);
|
|
|
|
connect(namePoint, &VGraphicsSimpleTextItem::DeleteTool, this, &VSimplePoint::DeleteFromLabel);
|
|
|
|
connect(namePoint, &VGraphicsSimpleTextItem::PointChoosed, this, &VSimplePoint::PointChoosed);
|
2016-04-05 19:14:12 +02:00
|
|
|
connect(namePoint, &VGraphicsSimpleTextItem::PointSelected, this, &VSimplePoint::PointSelected);
|
2015-06-20 18:14:04 +02:00
|
|
|
connect(namePoint, &VGraphicsSimpleTextItem::NameChangePosition, this, &VSimplePoint::ChangedPosition);
|
|
|
|
lineName = new QGraphicsLineItem(this);
|
|
|
|
this->setBrush(QBrush(Qt::NoBrush));
|
|
|
|
SetPen(this, currentColor, WidthHairLine(patternUnit));
|
|
|
|
this->setAcceptHoverEvents(true);
|
2016-05-08 17:04:18 +02:00
|
|
|
this->setFlag(QGraphicsItem::ItemIsFocusable, true);// For keyboard input focus
|
2015-06-20 18:14:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VSimplePoint::~VSimplePoint()
|
|
|
|
{}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-05-12 21:11:21 +02:00
|
|
|
void VSimplePoint::SetCurrentColor(const QColor &value)
|
2015-06-20 18:14:04 +02:00
|
|
|
{
|
2016-05-12 21:11:21 +02:00
|
|
|
SetSimpleCurrentColor(this, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VSimplePoint::ChangedActivDraw(bool flag)
|
|
|
|
{
|
|
|
|
SimpleChangedActivDraw(this, flag);
|
2015-06-20 18:14:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VSimplePoint::RefreshLine()
|
|
|
|
{
|
|
|
|
QRectF nRec = namePoint->sceneBoundingRect();
|
|
|
|
nRec.translate(- scenePos());
|
|
|
|
if (this->rect().intersects(nRec) == false)
|
|
|
|
{
|
|
|
|
const QRectF nameRec = namePoint->sceneBoundingRect();
|
|
|
|
QPointF p1, p2;
|
|
|
|
VGObject::LineIntersectCircle(QPointF(), radius, QLineF(QPointF(), nameRec.center() - scenePos()), p1, p2);
|
|
|
|
const QPointF pRec = VGObject::LineIntersectRect(nameRec, QLineF(scenePos(), nameRec.center()));
|
|
|
|
lineName->setLine(QLineF(p1, pRec - scenePos()));
|
|
|
|
SetPen(lineName, QColor(Qt::black), WidthHairLine(patternUnit));
|
|
|
|
|
|
|
|
if (QLineF(p1, pRec - scenePos()).length() <= ToPixel(4, Unit::Mm))
|
|
|
|
{
|
|
|
|
lineName->setVisible(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lineName->setVisible(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lineName->setVisible(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VSimplePoint::RefreshGeometry(const VPointF &point)
|
|
|
|
{
|
|
|
|
this->setFlag(QGraphicsItem::ItemSendsGeometryChanges, false);
|
|
|
|
SetPen(this, currentColor, WidthHairLine(patternUnit));
|
|
|
|
QRectF rec = QRectF(0, 0, radius*2, radius*2);
|
|
|
|
rec.translate(-rec.center().x(), -rec.center().y());
|
|
|
|
this->setRect(rec);
|
2016-05-14 21:28:09 +02:00
|
|
|
this->setPos(point);
|
2015-06-20 18:14:04 +02:00
|
|
|
this->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
|
|
|
|
namePoint->blockSignals(true);
|
|
|
|
QFont font = namePoint->font();
|
|
|
|
if (factor == nullptr)
|
|
|
|
{
|
|
|
|
font.setPointSize(static_cast<qint32>(namePoint->FontSize()));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
font.setPointSize(static_cast<qint32>(namePoint->FontSize()/ *factor));
|
|
|
|
}
|
|
|
|
namePoint->setFont(font);
|
|
|
|
namePoint->setText(point.name());
|
|
|
|
namePoint->setPos(QPointF(point.mx(), point.my()));
|
|
|
|
namePoint->blockSignals(false);
|
|
|
|
RefreshLine();
|
|
|
|
this->setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VSimplePoint::SetEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
namePoint->setEnabled(enabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VSimplePoint::EnableToolMove(bool move)
|
|
|
|
{
|
|
|
|
namePoint->setFlag(QGraphicsItem::ItemIsMovable, move);
|
|
|
|
}
|
|
|
|
|
2016-03-31 16:01:41 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VSimplePoint::AllowLabelHover(bool enabled)
|
|
|
|
{
|
|
|
|
namePoint->setAcceptHoverEvents(enabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VSimplePoint::AllowLabelSelecting(bool enabled)
|
|
|
|
{
|
|
|
|
namePoint->setFlag(QGraphicsItem::ItemIsSelectable, enabled);
|
|
|
|
}
|
|
|
|
|
2016-03-31 19:10:20 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VSimplePoint::ToolSelectionType(const SelectionType &type)
|
|
|
|
{
|
2016-05-12 21:11:21 +02:00
|
|
|
VAbstractSimple::ToolSelectionType(type);
|
2016-03-31 19:10:20 +02:00
|
|
|
namePoint->LabelSelectionType(type);
|
|
|
|
}
|
|
|
|
|
2015-06-20 18:14:04 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VSimplePoint::DeleteFromLabel()
|
|
|
|
{
|
|
|
|
emit Delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VSimplePoint::PointChoosed()
|
|
|
|
{
|
|
|
|
emit Choosed(id);
|
|
|
|
}
|
|
|
|
|
2016-04-05 19:14:12 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VSimplePoint::PointSelected(bool selected)
|
|
|
|
{
|
2016-05-19 16:11:02 +02:00
|
|
|
setSelected(selected);
|
2016-04-05 19:14:12 +02:00
|
|
|
}
|
|
|
|
|
2015-06-20 18:14:04 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VSimplePoint::ChangedPosition(const QPointF &pos)
|
|
|
|
{
|
2016-05-12 21:11:21 +02:00
|
|
|
emit NameChangedPosition(pos, id);
|
2015-06-20 18:14:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-02-11 12:05:08 +01:00
|
|
|
void VSimplePoint::mousePressEvent(QGraphicsSceneMouseEvent *event)
|
2016-03-31 16:01:41 +02:00
|
|
|
{
|
|
|
|
// Special for not selectable item first need to call standard mousePressEvent then accept event
|
|
|
|
QGraphicsEllipseItem::mousePressEvent(event);
|
2016-05-24 11:43:30 +02:00
|
|
|
|
|
|
|
// Somehow clicking on notselectable object do not clean previous selections.
|
|
|
|
if (not (flags() & ItemIsSelectable) && scene())
|
|
|
|
{
|
|
|
|
scene()->clearSelection();
|
|
|
|
}
|
|
|
|
|
2016-03-31 19:10:20 +02:00
|
|
|
if (selectionType == SelectionType::ByMouseRelease)
|
|
|
|
{
|
|
|
|
event->accept();// Special for not selectable item first need to call standard mousePressEvent then accept event
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (event->button() == Qt::LeftButton)
|
|
|
|
{
|
|
|
|
emit Choosed(id);
|
|
|
|
}
|
|
|
|
}
|
2016-03-31 16:01:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VSimplePoint::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
|
2015-06-20 18:14:04 +02:00
|
|
|
{
|
2016-03-31 19:10:20 +02:00
|
|
|
if (selectionType == SelectionType::ByMouseRelease)
|
2015-06-20 18:14:04 +02:00
|
|
|
{
|
2016-03-31 19:10:20 +02:00
|
|
|
if (event->button() == Qt::LeftButton)
|
|
|
|
{
|
|
|
|
emit Choosed(id);
|
|
|
|
}
|
2015-06-20 18:14:04 +02:00
|
|
|
}
|
2016-03-31 16:01:41 +02:00
|
|
|
QGraphicsEllipseItem::mouseReleaseEvent(event);
|
2015-06-20 18:14:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VSimplePoint::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
|
|
|
|
{
|
|
|
|
SetPen(this, currentColor, WidthMainLine(patternUnit));
|
|
|
|
QGraphicsEllipseItem::hoverEnterEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VSimplePoint::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
|
|
|
|
{
|
|
|
|
SetPen(this, currentColor, WidthHairLine(patternUnit));
|
|
|
|
QGraphicsEllipseItem::hoverLeaveEvent(event);
|
|
|
|
}
|
|
|
|
|
2016-05-08 17:04:18 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VSimplePoint::keyReleaseEvent(QKeyEvent *event)
|
|
|
|
{
|
|
|
|
switch (event->key())
|
|
|
|
{
|
|
|
|
case Qt::Key_Delete:
|
|
|
|
emit Delete();
|
|
|
|
return; //Leave this method immediately after call!!!
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
QGraphicsEllipseItem::keyReleaseEvent ( event );
|
2015-06-20 18:14:04 +02:00
|
|
|
}
|
|
|
|
|
2016-04-05 19:14:12 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QVariant VSimplePoint::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
|
|
|
|
{
|
|
|
|
if (change == QGraphicsItem::ItemSelectedChange)
|
|
|
|
{
|
|
|
|
namePoint->blockSignals(true);
|
|
|
|
namePoint->setSelected(value.toBool());
|
|
|
|
namePoint->blockSignals(false);
|
|
|
|
emit Selected(value.toBool(), id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return QGraphicsEllipseItem::itemChange(change, value);
|
|
|
|
}
|
2016-05-25 10:43:36 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VSimplePoint::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
|
|
|
|
{
|
|
|
|
emit ShowContextMenu(event);
|
|
|
|
}
|