2016-06-23 00:59:45 +02:00
|
|
|
/************************************************************************
|
|
|
|
**
|
2016-09-10 18:30:03 +02:00
|
|
|
** @file vtextgraphicsitem.cpp
|
2016-06-23 00:59:45 +02:00
|
|
|
** @author Bojan Kverh
|
|
|
|
** @date June 16, 2016
|
|
|
|
**
|
|
|
|
** @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) 2013-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/>.
|
|
|
|
**
|
|
|
|
*************************************************************************/
|
|
|
|
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QColor>
|
|
|
|
#include <QFlags>
|
|
|
|
#include <QFont>
|
|
|
|
#include <QGraphicsItem>
|
|
|
|
#include <QGraphicsSceneHoverEvent>
|
|
|
|
#include <QGraphicsSceneMouseEvent>
|
2016-06-23 00:59:45 +02:00
|
|
|
#include <QPainter>
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <QPen>
|
|
|
|
#include <QPoint>
|
2016-06-23 00:59:45 +02:00
|
|
|
#include <QStyleOptionGraphicsItem>
|
2016-08-08 13:44:49 +02:00
|
|
|
#include <Qt>
|
2017-01-25 17:07:18 +01:00
|
|
|
#include <QDebug>
|
2016-06-23 00:59:45 +02:00
|
|
|
|
2016-08-09 15:55:46 +02:00
|
|
|
#include "../vmisc/def.h"
|
2016-08-16 18:57:32 +02:00
|
|
|
#include "../vmisc/vmath.h"
|
2016-06-23 00:59:45 +02:00
|
|
|
#include "vtextgraphicsitem.h"
|
|
|
|
|
2016-12-15 07:54:52 +01:00
|
|
|
const qreal resizeSquare = (3./*mm*/ / 25.4) * PrintDPI;
|
|
|
|
const qreal rotateCircle = (2./*mm*/ / 25.4) * PrintDPI;
|
2016-07-01 01:45:16 +02:00
|
|
|
#define ROTATE_RECT 60
|
|
|
|
#define ROTATE_ARC 50
|
2016-12-15 07:54:52 +01:00
|
|
|
const qreal minW = (4./*mm*/ / 25.4) * PrintDPI + resizeSquare;
|
|
|
|
const qreal minH = (4./*mm*/ / 25.4) * PrintDPI + resizeSquare;
|
2016-09-11 12:18:09 +02:00
|
|
|
#define ACTIVE_Z 10
|
2016-06-23 00:59:45 +02:00
|
|
|
|
2017-01-19 11:07:26 +01:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief GetBoundingRect calculates the bounding box around rectBB rectangle, rotated around its center by dRot degrees
|
|
|
|
* @param rectBB rectangle of interest
|
|
|
|
* @param dRot rectangle rotation
|
|
|
|
* @return bounding box around rectBB rotated by dRot
|
|
|
|
*/
|
|
|
|
QRectF GetBoundingRect(QRectF rectBB, qreal dRot)
|
|
|
|
{
|
|
|
|
QPointF apt[4] = { rectBB.topLeft(), rectBB.topRight(), rectBB.bottomLeft(), rectBB.bottomRight() };
|
|
|
|
QPointF ptCenter = rectBB.center();
|
|
|
|
|
|
|
|
qreal dX1 = 0;
|
|
|
|
qreal dX2 = 0;
|
|
|
|
qreal dY1 = 0;
|
|
|
|
qreal dY2 = 0;
|
|
|
|
|
|
|
|
double dAng = qDegreesToRadians(dRot);
|
|
|
|
for (int i = 0; i < 4; ++i)
|
|
|
|
{
|
|
|
|
QPointF pt = apt[i] - ptCenter;
|
|
|
|
qreal dX = pt.x()*cos(dAng) + pt.y()*sin(dAng);
|
|
|
|
qreal dY = -pt.x()*sin(dAng) + pt.y()*cos(dAng);
|
|
|
|
|
|
|
|
if (i == 0)
|
|
|
|
{
|
|
|
|
dX1 = dX2 = dX;
|
|
|
|
dY1 = dY2 = dY;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (dX < dX1)
|
|
|
|
{
|
|
|
|
dX1 = dX;
|
|
|
|
}
|
|
|
|
else if (dX > dX2)
|
|
|
|
{
|
|
|
|
dX2 = dX;
|
|
|
|
}
|
|
|
|
if (dY < dY1)
|
|
|
|
{
|
|
|
|
dY1 = dY;
|
|
|
|
}
|
|
|
|
else if (dY > dY2)
|
|
|
|
{
|
|
|
|
dY2 = dY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
QRectF rect;
|
|
|
|
rect.setTopLeft(ptCenter + QPointF(dX1, dY1));
|
|
|
|
rect.setWidth(dX2 - dX1);
|
|
|
|
rect.setHeight(dY2 - dY1);
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
}//static functions
|
|
|
|
|
2016-06-23 00:59:45 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-07-23 15:26:15 +02:00
|
|
|
/**
|
|
|
|
* @brief VTextGraphicsItem::VTextGraphicsItem constructor
|
|
|
|
* @param pParent pointer to the parent item
|
|
|
|
*/
|
2016-06-23 00:59:45 +02:00
|
|
|
VTextGraphicsItem::VTextGraphicsItem(QGraphicsItem* pParent)
|
2017-01-19 11:07:26 +01:00
|
|
|
: VPieceItem(pParent),
|
2016-12-15 07:54:52 +01:00
|
|
|
m_ptStartPos(),
|
|
|
|
m_ptStart(),
|
|
|
|
m_szStart(),
|
|
|
|
m_dRotation(0),
|
|
|
|
m_dAngle(0),
|
|
|
|
m_rectResize(),
|
|
|
|
m_tm()
|
2016-06-23 00:59:45 +02:00
|
|
|
{
|
2017-01-19 11:07:26 +01:00
|
|
|
m_inactiveZ = 2;
|
2016-12-15 07:54:52 +01:00
|
|
|
SetSize(minW, minH);
|
2017-01-19 11:07:26 +01:00
|
|
|
setZValue(m_inactiveZ);
|
2016-06-23 00:59:45 +02:00
|
|
|
}
|
|
|
|
|
2016-06-25 17:19:44 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-07-23 15:26:15 +02:00
|
|
|
/**
|
|
|
|
* @brief VTextGraphicsItem::SetFont sets the item font
|
|
|
|
* @param fnt font to be used in item
|
|
|
|
*/
|
2016-06-25 17:19:44 +02:00
|
|
|
void VTextGraphicsItem::SetFont(const QFont& fnt)
|
|
|
|
{
|
2016-07-18 22:30:14 +02:00
|
|
|
m_tm.SetFont(fnt);
|
2016-06-25 17:19:44 +02:00
|
|
|
}
|
|
|
|
|
2016-06-23 00:59:45 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-07-23 15:26:15 +02:00
|
|
|
/**
|
|
|
|
* @brief VTextGraphicsItem::paint redraws the item content
|
|
|
|
* @param painter pointer to the QPainter in use
|
|
|
|
* @param option pointer to the object containing the actual label rectangle
|
|
|
|
* @param widget not used
|
|
|
|
*/
|
2016-06-23 00:59:45 +02:00
|
|
|
void VTextGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|
|
|
{
|
2016-12-20 20:19:21 +01:00
|
|
|
Q_UNUSED(widget)
|
|
|
|
Q_UNUSED(option)
|
2016-12-15 07:52:23 +01:00
|
|
|
painter->fillRect(m_rectBoundingBox, QColor(251, 251, 175));
|
2016-07-23 15:26:15 +02:00
|
|
|
painter->setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
|
2016-06-25 17:19:44 +02:00
|
|
|
|
|
|
|
painter->setPen(Qt::black);
|
2016-07-18 22:30:14 +02:00
|
|
|
QFont fnt = m_tm.GetFont();
|
2016-12-14 00:56:30 +01:00
|
|
|
int iW = qFloor(boundingRect().width());
|
2016-08-02 20:16:15 +02:00
|
|
|
// draw text lines
|
|
|
|
int iY = 0;
|
2016-12-14 00:56:30 +01:00
|
|
|
for (int i = 0; i < m_tm.GetSourceLinesCount(); ++i)
|
2016-06-25 17:19:44 +02:00
|
|
|
{
|
2016-12-14 00:56:30 +01:00
|
|
|
const TextLine& tl = m_tm.GetSourceLine(i);
|
2016-12-15 07:46:45 +01:00
|
|
|
|
2016-07-18 22:30:14 +02:00
|
|
|
fnt.setPixelSize(m_tm.GetFont().pixelSize() + tl.m_iFontSize);
|
2016-06-25 17:19:44 +02:00
|
|
|
fnt.setWeight(tl.m_eFontWeight);
|
2016-06-26 14:21:06 +02:00
|
|
|
fnt.setStyle(tl.m_eStyle);
|
2016-12-14 00:56:30 +01:00
|
|
|
|
|
|
|
QString qsText = tl.m_qsText;
|
|
|
|
QFontMetrics fm(fnt);
|
2016-12-15 07:46:45 +01:00
|
|
|
|
|
|
|
// check if the next line will go out of bounds
|
|
|
|
if (iY + fm.height() > boundingRect().height())
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-12-14 00:56:30 +01:00
|
|
|
if (fm.width(qsText) > iW)
|
|
|
|
{
|
2016-12-14 10:59:21 +01:00
|
|
|
qsText = fm.elidedText(qsText, Qt::ElideMiddle, iW);
|
2016-12-14 00:56:30 +01:00
|
|
|
}
|
|
|
|
|
2016-06-25 17:19:44 +02:00
|
|
|
painter->setFont(fnt);
|
2017-06-05 20:22:51 +02:00
|
|
|
painter->drawText(0, iY, iW, fm.height(), static_cast<int>(tl.m_eAlign), qsText);
|
2016-12-15 07:46:45 +01:00
|
|
|
iY += fm.height() + m_tm.GetSpacing();
|
2016-06-25 17:19:44 +02:00
|
|
|
}
|
|
|
|
|
2016-07-23 15:26:15 +02:00
|
|
|
// now draw the features specific to non-normal modes
|
2016-06-23 00:59:45 +02:00
|
|
|
if (m_eMode != mNormal)
|
|
|
|
{
|
2016-07-23 15:26:15 +02:00
|
|
|
// outline the rectangle
|
2016-06-23 00:59:45 +02:00
|
|
|
painter->setPen(QPen(Qt::black, 2, Qt::DashLine));
|
|
|
|
painter->drawRect(boundingRect().adjusted(1, 1, -1, -1));
|
|
|
|
|
2016-06-30 18:04:25 +02:00
|
|
|
if (m_eMode != mRotate)
|
|
|
|
{
|
2016-07-23 15:26:15 +02:00
|
|
|
// draw the resize square
|
2016-06-30 18:04:25 +02:00
|
|
|
painter->setPen(Qt::black);
|
|
|
|
painter->setBrush(Qt::black);
|
2016-12-15 07:52:23 +01:00
|
|
|
painter->drawRect(m_rectResize.adjusted(-1, -1, -1, -1));
|
2016-06-25 17:19:44 +02:00
|
|
|
|
2016-06-30 18:04:25 +02:00
|
|
|
if (m_eMode == mResize)
|
|
|
|
{
|
2016-07-23 15:26:15 +02:00
|
|
|
// draw the resize diagonal lines
|
2016-12-15 07:52:23 +01:00
|
|
|
painter->drawLine(1, 1, qFloor(m_rectBoundingBox.width())-1, qFloor(m_rectBoundingBox.height())-1);
|
|
|
|
painter->drawLine(1, qFloor(m_rectBoundingBox.height())-1, qFloor(m_rectBoundingBox.width())-1, 1);
|
2016-06-30 18:04:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2016-06-25 17:19:44 +02:00
|
|
|
{
|
2016-07-23 15:26:15 +02:00
|
|
|
// in rotate mode, draw the circle in the middle
|
2016-06-30 18:04:25 +02:00
|
|
|
painter->setPen(Qt::black);
|
|
|
|
painter->setBrush(Qt::black);
|
|
|
|
painter->drawEllipse(
|
2016-07-06 22:15:32 +02:00
|
|
|
QPointF(m_rectBoundingBox.width()/2, m_rectBoundingBox.height()/2),
|
2016-12-15 07:54:52 +01:00
|
|
|
rotateCircle,
|
|
|
|
rotateCircle
|
2016-06-30 18:04:25 +02:00
|
|
|
);
|
2016-12-15 07:54:52 +01:00
|
|
|
if (m_rectBoundingBox.width() > minW*3 && m_rectBoundingBox.height() > minH*3)
|
|
|
|
{
|
|
|
|
painter->setPen(QPen(Qt::black, 3));
|
|
|
|
painter->setBrush(Qt::NoBrush);
|
|
|
|
// and then draw the arc in each of the corners
|
|
|
|
int iTop = ROTATE_RECT - ROTATE_ARC;
|
|
|
|
int iLeft = ROTATE_RECT - ROTATE_ARC;
|
|
|
|
int iRight = qRound(m_rectBoundingBox.width()) - ROTATE_RECT;
|
|
|
|
int iBottom = qRound(m_rectBoundingBox.height()) - ROTATE_RECT;
|
|
|
|
painter->drawArc(iLeft, iTop, ROTATE_ARC, ROTATE_ARC, 180*16, -90*16);
|
|
|
|
painter->drawArc(iRight, iTop, ROTATE_ARC, ROTATE_ARC, 90*16, -90*16);
|
|
|
|
painter->drawArc(iLeft, iBottom, ROTATE_ARC, ROTATE_ARC, 270*16, -90*16);
|
|
|
|
painter->drawArc(iRight, iBottom, ROTATE_ARC, ROTATE_ARC, 0*16, -90*16);
|
|
|
|
}
|
2016-06-25 17:19:44 +02:00
|
|
|
}
|
2016-06-23 00:59:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-25 17:19:44 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-07-23 15:26:15 +02:00
|
|
|
/**
|
2016-12-14 00:56:30 +01:00
|
|
|
* @brief VTextGraphicsItem::AddLine adds a line of text to the label list.
|
2016-07-23 15:26:15 +02:00
|
|
|
* @param tl line of text to add
|
|
|
|
*/
|
2016-06-25 17:19:44 +02:00
|
|
|
void VTextGraphicsItem::AddLine(const TextLine& tl)
|
|
|
|
{
|
2016-11-01 09:42:32 +01:00
|
|
|
m_tm.AddSourceLine(tl);
|
2016-06-25 17:19:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-07-23 15:26:15 +02:00
|
|
|
/**
|
|
|
|
* @brief VTextGraphicsItem::Clear deletes all the label texts
|
|
|
|
*/
|
2016-06-25 17:19:44 +02:00
|
|
|
void VTextGraphicsItem::Clear()
|
|
|
|
{
|
2016-11-01 09:42:32 +01:00
|
|
|
m_tm.ClearSourceLines();
|
2016-06-25 17:19:44 +02:00
|
|
|
}
|
|
|
|
|
2016-06-27 01:17:27 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-07-23 15:26:15 +02:00
|
|
|
/**
|
|
|
|
* @brief VTextGraphicsItem::SetSize Tries to set the label size to (fW, fH). If any of those is too small, the label
|
|
|
|
* size does not change.
|
|
|
|
* @param fW label width
|
|
|
|
* @param fH label height
|
|
|
|
*/
|
2016-06-27 17:18:43 +02:00
|
|
|
void VTextGraphicsItem::SetSize(qreal fW, qreal fH)
|
2016-06-27 01:17:27 +02:00
|
|
|
{
|
2016-12-02 00:44:28 +01:00
|
|
|
qDebug() << "Setting size to" << fW << parentItem()->boundingRect().width();
|
2016-06-27 17:18:43 +02:00
|
|
|
// don't allow resize under specific size
|
2016-08-15 17:19:21 +02:00
|
|
|
if (fW > parentItem()->boundingRect().width())
|
|
|
|
{
|
|
|
|
fW = parentItem()->boundingRect().width();
|
|
|
|
}
|
2016-12-15 07:54:52 +01:00
|
|
|
if (fW < minW)
|
2016-08-15 17:19:21 +02:00
|
|
|
{
|
2016-12-15 07:54:52 +01:00
|
|
|
fW = minW;
|
2016-08-15 17:19:21 +02:00
|
|
|
}
|
|
|
|
if (fH > parentItem()->boundingRect().height())
|
|
|
|
{
|
|
|
|
fH = parentItem()->boundingRect().height();
|
|
|
|
}
|
2016-12-15 07:54:52 +01:00
|
|
|
if (fH < minH)
|
2016-08-15 17:19:21 +02:00
|
|
|
{
|
2016-12-15 07:54:52 +01:00
|
|
|
fH = minH;
|
2016-08-15 17:19:21 +02:00
|
|
|
}
|
|
|
|
|
2016-12-15 07:52:23 +01:00
|
|
|
prepareGeometryChange();
|
2016-12-02 00:44:28 +01:00
|
|
|
qDebug() << "Actual size set to" << fW;
|
2016-07-03 10:17:59 +02:00
|
|
|
m_rectBoundingBox.setTopLeft(QPointF(0, 0));
|
2016-06-27 01:17:27 +02:00
|
|
|
m_rectBoundingBox.setWidth(fW);
|
2016-06-27 17:18:43 +02:00
|
|
|
m_rectBoundingBox.setHeight(fH);
|
2016-12-15 07:54:52 +01:00
|
|
|
m_rectResize.setTopLeft(QPointF(fW - resizeSquare, fH - resizeSquare));
|
|
|
|
m_rectResize.setWidth(resizeSquare);
|
|
|
|
m_rectResize.setHeight(resizeSquare);
|
2016-07-01 01:45:16 +02:00
|
|
|
setTransformOriginPoint(m_rectBoundingBox.center());
|
2016-06-27 01:17:27 +02:00
|
|
|
}
|
|
|
|
|
2016-06-23 00:59:45 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-07-23 15:26:15 +02:00
|
|
|
/**
|
2016-09-29 01:39:50 +02:00
|
|
|
* @brief VTextGraphicsItem::Update sets the correct size and font size and redraws the label
|
2016-07-23 15:26:15 +02:00
|
|
|
*/
|
2016-06-27 17:18:43 +02:00
|
|
|
void VTextGraphicsItem::Update()
|
2016-06-23 00:59:45 +02:00
|
|
|
{
|
2016-09-29 01:39:50 +02:00
|
|
|
CorrectLabel();
|
2016-06-27 17:18:43 +02:00
|
|
|
UpdateBox();
|
2016-06-23 00:59:45 +02:00
|
|
|
}
|
|
|
|
|
2016-07-14 02:10:27 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-07-23 15:26:15 +02:00
|
|
|
/**
|
|
|
|
* @brief VTextGraphicsItem::IsContained checks if the bounding box around rotated rectBB is contained in
|
|
|
|
* the parent. If that is not the case, it calculates the amount of movement needed to put it inside the parent
|
|
|
|
* and write it into dX, dY
|
|
|
|
* @param rectBB bounding box in question
|
|
|
|
* @param dRot bounding box rotation in degrees
|
|
|
|
* @param dX horizontal translation needed to put the box inside parent item
|
|
|
|
* @param dY vertical translation needed to put the box inside parent item
|
|
|
|
* @return true, if rectBB is contained in parent item and false otherwise
|
|
|
|
*/
|
2016-07-14 02:10:27 +02:00
|
|
|
bool VTextGraphicsItem::IsContained(QRectF rectBB, qreal dRot, qreal &dX, qreal &dY) const
|
|
|
|
{
|
|
|
|
QRectF rectParent = parentItem()->boundingRect();
|
|
|
|
rectBB = GetBoundingRect(rectBB, dRot);
|
|
|
|
dX = 0;
|
|
|
|
dY = 0;
|
|
|
|
|
|
|
|
if (rectParent.contains(rectBB) == false)
|
|
|
|
{
|
|
|
|
if (rectParent.left() - rectBB.left() > fabs(dX))
|
|
|
|
{
|
|
|
|
dX = rectParent.left() - rectBB.left();
|
|
|
|
}
|
|
|
|
else if (rectBB.right() - rectParent.right() > fabs(dX))
|
|
|
|
{
|
|
|
|
dX = rectParent.right() - rectBB.right();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rectParent.top() - rectBB.top() > fabs(dY))
|
|
|
|
{
|
|
|
|
dY = rectParent.top() - rectBB.top();
|
|
|
|
}
|
|
|
|
else if (rectBB.bottom() - rectParent.bottom() > fabs(dY))
|
|
|
|
{
|
|
|
|
dY = rectParent.bottom() - rectBB.bottom();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-21 21:46:49 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-07-23 15:26:15 +02:00
|
|
|
/**
|
|
|
|
* @brief VTextGraphicsItem::UpdateData Updates the detail label
|
|
|
|
* @param qsName name of detail
|
|
|
|
* @param data reference to VPatternPieceData
|
|
|
|
*/
|
2017-02-23 10:19:27 +01:00
|
|
|
void VTextGraphicsItem::UpdateData(const QString &qsName, const VPieceLabelData &data)
|
2016-07-21 21:46:49 +02:00
|
|
|
{
|
|
|
|
m_tm.Update(qsName, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-07-23 15:26:15 +02:00
|
|
|
/**
|
|
|
|
* @brief VTextGraphicsItem::UpdateData Updates the pattern label
|
|
|
|
* @param pDoc pointer to the pattern object
|
|
|
|
*/
|
2016-08-15 13:03:59 +02:00
|
|
|
void VTextGraphicsItem::UpdateData(const VAbstractPattern* pDoc, qreal dSize, qreal dHeight)
|
2016-07-21 21:46:49 +02:00
|
|
|
{
|
2016-08-15 13:03:59 +02:00
|
|
|
m_tm.Update(pDoc, dSize, dHeight);
|
2016-07-21 21:46:49 +02:00
|
|
|
}
|
|
|
|
|
2016-07-30 13:21:03 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief VTextGraphicsItem::GetTextLines returns the number of lines of text to show
|
|
|
|
* @return number of lines of text
|
|
|
|
*/
|
|
|
|
int VTextGraphicsItem::GetTextLines() const
|
|
|
|
{
|
2016-12-14 00:56:30 +01:00
|
|
|
return m_tm.GetSourceLinesCount();
|
2016-07-30 13:21:03 +02:00
|
|
|
}
|
|
|
|
|
2016-07-01 01:45:16 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-07-23 15:26:15 +02:00
|
|
|
/**
|
|
|
|
* @brief VTextGraphicsItem::GetFontSize returns the currently used text base font size
|
|
|
|
* @return current text base font size
|
|
|
|
*/
|
2016-07-01 01:45:16 +02:00
|
|
|
int VTextGraphicsItem::GetFontSize() const
|
|
|
|
{
|
2016-07-18 22:30:14 +02:00
|
|
|
return m_tm.GetFont().pixelSize();
|
2016-07-01 01:45:16 +02:00
|
|
|
}
|
|
|
|
|
2016-06-23 00:59:45 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-07-23 15:26:15 +02:00
|
|
|
/**
|
|
|
|
* @brief VTextGraphicsItem::mousePressEvent handles left button mouse press events
|
|
|
|
* @param pME pointer to QGraphicsSceneMouseEvent object
|
|
|
|
*/
|
2016-06-23 00:59:45 +02:00
|
|
|
void VTextGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *pME)
|
|
|
|
{
|
2017-02-27 13:30:02 +01:00
|
|
|
if (pME->button() == Qt::LeftButton && pME->type() != QEvent::GraphicsSceneMouseDoubleClick)
|
2016-06-23 00:59:45 +02:00
|
|
|
{
|
2017-03-13 14:54:38 +01:00
|
|
|
if (m_moveType == NotMovable)
|
|
|
|
{
|
|
|
|
pME->ignore();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-23 15:26:15 +02:00
|
|
|
// record the parameters of the mouse press. Specially record the position
|
|
|
|
// of the press as the origin for the following operations
|
2016-06-30 18:04:25 +02:00
|
|
|
m_ptStartPos = pos();
|
|
|
|
m_ptStart = pME->scenePos();
|
2016-06-29 23:39:52 +02:00
|
|
|
m_szStart = m_rectBoundingBox.size();
|
2016-07-02 11:15:41 +02:00
|
|
|
m_ptRotCenter = mapToScene(m_rectBoundingBox.center());
|
2016-06-30 18:04:25 +02:00
|
|
|
m_dAngle = GetAngle(pME->scenePos());
|
|
|
|
m_dRotation = rotation();
|
2016-07-23 15:26:15 +02:00
|
|
|
// in rotation mode, do not do any changes here, because user might want to
|
|
|
|
// rotate the label more.
|
2017-03-01 06:39:13 +01:00
|
|
|
|
2017-03-13 14:54:38 +01:00
|
|
|
if ((m_moveType & AllModifications ) == AllModifications)
|
2016-06-29 23:39:52 +02:00
|
|
|
{
|
2017-03-13 14:54:38 +01:00
|
|
|
AllUserModifications(pME->pos());
|
|
|
|
setZValue(ACTIVE_Z);
|
|
|
|
Update();
|
|
|
|
}
|
|
|
|
else if (m_moveType & IsRotatable)
|
|
|
|
{
|
|
|
|
if (m_moveType & IsResizable)
|
2016-06-30 18:04:25 +02:00
|
|
|
{
|
2017-03-13 14:54:38 +01:00
|
|
|
AllUserModifications(pME->pos());
|
|
|
|
}
|
|
|
|
else if (m_moveType & IsMovable)
|
|
|
|
{
|
|
|
|
UserRotateAndMove();
|
2016-06-30 18:04:25 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-03-13 14:54:38 +01:00
|
|
|
m_eMode = mRotate;
|
2016-07-06 20:49:36 +02:00
|
|
|
SetOverrideCursor(cursorArrowCloseHand, 1, 1);
|
2016-06-30 18:04:25 +02:00
|
|
|
}
|
2017-03-01 06:39:13 +01:00
|
|
|
setZValue(ACTIVE_Z);
|
|
|
|
Update();
|
2016-06-29 23:39:52 +02:00
|
|
|
}
|
2017-03-13 14:54:38 +01:00
|
|
|
else if (m_moveType & IsResizable)
|
2016-08-03 00:32:37 +02:00
|
|
|
{
|
2017-03-13 14:54:38 +01:00
|
|
|
if (m_moveType & IsRotatable)
|
2017-03-01 06:39:13 +01:00
|
|
|
{
|
2017-03-13 14:54:38 +01:00
|
|
|
AllUserModifications(pME->pos());
|
|
|
|
}
|
|
|
|
else if (m_moveType & IsMovable)
|
|
|
|
{
|
|
|
|
UserMoveAndResize(pME->pos());
|
|
|
|
}
|
|
|
|
setZValue(ACTIVE_Z);
|
|
|
|
Update();
|
|
|
|
}
|
|
|
|
else if (m_moveType & IsMovable)
|
|
|
|
{
|
|
|
|
if (m_moveType & IsRotatable)
|
|
|
|
{
|
|
|
|
UserRotateAndMove();
|
|
|
|
}
|
|
|
|
else if (m_moveType & IsResizable)
|
|
|
|
{
|
|
|
|
UserMoveAndResize(pME->pos());
|
2017-03-01 06:39:13 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-03-13 14:54:38 +01:00
|
|
|
m_eMode = mMove;
|
2017-03-01 06:39:13 +01:00
|
|
|
SetOverrideCursor(cursorArrowCloseHand, 1, 1);
|
|
|
|
}
|
2017-03-13 14:54:38 +01:00
|
|
|
|
2017-03-01 06:39:13 +01:00
|
|
|
setZValue(ACTIVE_Z);
|
2017-03-13 14:54:38 +01:00
|
|
|
Update();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pME->ignore();
|
|
|
|
return;
|
2016-08-03 00:32:37 +02:00
|
|
|
}
|
2016-06-23 00:59:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-07-23 15:26:15 +02:00
|
|
|
/**
|
|
|
|
* @brief VTextGraphicsItem::mouseMoveEvent handles mouse move events
|
|
|
|
* @param pME pointer to QGraphicsSceneMouseEvent object
|
|
|
|
*/
|
2016-06-25 17:19:44 +02:00
|
|
|
void VTextGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent* pME)
|
2016-06-23 00:59:45 +02:00
|
|
|
{
|
2016-07-14 02:10:27 +02:00
|
|
|
qreal dX;
|
|
|
|
qreal dY;
|
|
|
|
QRectF rectBB;
|
2016-12-15 08:33:01 +01:00
|
|
|
const QPointF ptDiff = pME->scenePos() - m_ptStart;
|
2017-03-13 14:54:38 +01:00
|
|
|
if (m_eMode == mMove && m_moveType & IsMovable)
|
2016-06-23 00:59:45 +02:00
|
|
|
{
|
2016-07-23 15:26:15 +02:00
|
|
|
// in move mode move the label along the mouse move from the origin
|
2016-07-06 20:49:36 +02:00
|
|
|
QPointF pt = m_ptStartPos + ptDiff;
|
2016-07-14 02:10:27 +02:00
|
|
|
rectBB.setTopLeft(pt);
|
|
|
|
rectBB.setWidth(m_rectBoundingBox.width());
|
|
|
|
rectBB.setHeight(m_rectBoundingBox.height());
|
2016-07-23 15:26:15 +02:00
|
|
|
// before moving label to a new position, check if it will still be inside the parent item
|
2016-07-14 02:10:27 +02:00
|
|
|
if (IsContained(rectBB, rotation(), dX, dY) == false)
|
2016-07-06 20:49:36 +02:00
|
|
|
{
|
2016-07-14 02:10:27 +02:00
|
|
|
pt.setX(pt.x() + dX);
|
|
|
|
pt.setY(pt.y() + dY);
|
2016-07-06 20:49:36 +02:00
|
|
|
}
|
2016-07-08 00:46:33 +02:00
|
|
|
setPos(pt);
|
|
|
|
UpdateBox();
|
2016-06-23 00:59:45 +02:00
|
|
|
}
|
2017-03-13 14:54:38 +01:00
|
|
|
else if (m_eMode == mResize && m_moveType & IsResizable)
|
2016-06-23 00:59:45 +02:00
|
|
|
{
|
2016-07-23 15:26:15 +02:00
|
|
|
// in resize mode, resize the label along the mouse move from the origin
|
2017-03-13 14:54:38 +01:00
|
|
|
QPointF pt;
|
|
|
|
|
2017-03-13 15:20:58 +01:00
|
|
|
if (m_moveType & IsMovable)
|
|
|
|
{
|
2017-03-13 14:54:38 +01:00
|
|
|
pt = m_ptStartPos;
|
2017-03-13 15:20:58 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pt = m_ptRotCenter - QRectF(0, 0, m_szStart.width() + ptDiff.x(),
|
|
|
|
m_szStart.height() + ptDiff.y()).center();
|
|
|
|
}
|
2017-03-13 14:54:38 +01:00
|
|
|
|
2016-07-14 02:10:27 +02:00
|
|
|
rectBB.setTopLeft(pt);
|
2016-07-14 02:59:42 +02:00
|
|
|
QSizeF sz(m_szStart.width() + ptDiff.x(), m_szStart.height() + ptDiff.y());
|
2016-07-14 02:10:27 +02:00
|
|
|
rectBB.setSize(sz);
|
2016-07-23 15:26:15 +02:00
|
|
|
// before resizing the label to a new size, check if it will still be inside the parent item
|
2016-12-15 08:33:01 +01:00
|
|
|
if (IsContained(rectBB, rotation(), dX, dY) == false)
|
2016-07-14 02:59:42 +02:00
|
|
|
{
|
2016-12-15 08:33:01 +01:00
|
|
|
sz = QSizeF(sz.width()+dX, sz.height()+dY);
|
2016-07-06 20:49:36 +02:00
|
|
|
}
|
2017-03-13 15:20:58 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (not (m_moveType & IsMovable))
|
|
|
|
{
|
|
|
|
setPos(pt);
|
|
|
|
}
|
|
|
|
}
|
2016-12-15 08:33:01 +01:00
|
|
|
|
|
|
|
SetSize(sz.width(), sz.height());
|
|
|
|
Update();
|
|
|
|
emit SignalShrink();
|
2016-06-23 00:59:45 +02:00
|
|
|
}
|
2017-03-13 14:54:38 +01:00
|
|
|
else if (m_eMode == mRotate && m_moveType & IsRotatable)
|
2016-06-30 18:04:25 +02:00
|
|
|
{
|
2016-07-23 15:26:15 +02:00
|
|
|
// if the angle from the original position is small (0.5 degrees), just remeber the new angle
|
|
|
|
// new angle will be the starting angle for rotation
|
2016-06-30 18:04:25 +02:00
|
|
|
if (fabs(m_dAngle) < 0.01)
|
|
|
|
{
|
|
|
|
m_dAngle = GetAngle(pME->scenePos());
|
|
|
|
return;
|
|
|
|
}
|
2016-07-23 15:26:15 +02:00
|
|
|
// calculate the angle difference from the starting angle
|
|
|
|
double dAng = qRadiansToDegrees(GetAngle(pME->scenePos()) - m_dAngle);
|
2016-07-14 02:10:27 +02:00
|
|
|
rectBB.setTopLeft(m_ptStartPos);
|
|
|
|
rectBB.setWidth(m_rectBoundingBox.width());
|
|
|
|
rectBB.setHeight(m_rectBoundingBox.height());
|
2016-07-23 15:26:15 +02:00
|
|
|
// check if the rotated label will be inside the parent item and then rotate it
|
2016-07-14 02:10:27 +02:00
|
|
|
if (IsContained(rectBB, m_dRotation + dAng, dX, dY) == true)
|
|
|
|
{
|
|
|
|
setRotation(m_dRotation + dAng);
|
|
|
|
Update();
|
|
|
|
}
|
2016-06-30 18:04:25 +02:00
|
|
|
}
|
2016-06-23 00:59:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-07-23 15:26:15 +02:00
|
|
|
/**
|
|
|
|
* @brief VTextGraphicsItem::mouseReleaseEvent handles left button mouse release events
|
|
|
|
* @param pME pointer to QGraphicsSceneMouseEvent object
|
|
|
|
*/
|
2016-06-25 17:19:44 +02:00
|
|
|
void VTextGraphicsItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* pME)
|
2016-06-23 00:59:45 +02:00
|
|
|
{
|
2016-06-29 23:52:48 +02:00
|
|
|
if (pME->button() == Qt::LeftButton)
|
2016-06-26 14:21:06 +02:00
|
|
|
{
|
2016-07-23 15:26:15 +02:00
|
|
|
// restore the cursor
|
2016-08-03 00:32:37 +02:00
|
|
|
if (m_eMode == mMove || m_eMode == mRotate)
|
2016-07-12 23:11:17 +02:00
|
|
|
{
|
|
|
|
RestoreOverrideCursor(cursorArrowCloseHand);
|
|
|
|
}
|
|
|
|
else if (m_eMode == mResize)
|
|
|
|
{
|
|
|
|
RestoreOverrideCursor(Qt::SizeFDiagCursor);
|
|
|
|
}
|
2016-06-30 18:04:25 +02:00
|
|
|
double dDist = fabs(pME->scenePos().x() - m_ptStart.x()) + fabs(pME->scenePos().y() - m_ptStart.y());
|
2016-07-23 15:26:15 +02:00
|
|
|
// determine if this was just press/release (bShort == true) or user did some operation between press and release
|
2016-06-30 18:04:25 +02:00
|
|
|
bool bShort = (dDist < 2);
|
|
|
|
|
|
|
|
if (m_eMode == mMove || m_eMode == mResize)
|
2016-07-23 15:26:15 +02:00
|
|
|
{ // if user just pressed and released the button, we must switch the mode to rotate
|
|
|
|
// but if user did some operation (move/resize), emit the proper signal and update the label
|
2016-06-30 18:04:25 +02:00
|
|
|
if (bShort == true)
|
|
|
|
{
|
2017-03-14 10:24:36 +01:00
|
|
|
if (m_bReleased == true && m_moveType & IsRotatable)
|
2016-06-30 18:04:25 +02:00
|
|
|
{
|
|
|
|
m_eMode = mRotate;
|
|
|
|
UpdateBox();
|
|
|
|
}
|
|
|
|
}
|
2017-03-13 14:54:38 +01:00
|
|
|
else if (m_eMode == mMove && m_moveType & IsMovable)
|
2016-06-30 18:04:25 +02:00
|
|
|
{
|
|
|
|
emit SignalMoved(pos());
|
|
|
|
UpdateBox();
|
|
|
|
}
|
2017-03-13 14:54:38 +01:00
|
|
|
else if (m_moveType & IsResizable)
|
2016-06-30 18:04:25 +02:00
|
|
|
{
|
2016-07-18 22:30:14 +02:00
|
|
|
emit SignalResized(m_rectBoundingBox.width(), m_tm.GetFont().pixelSize());
|
2016-06-30 18:04:25 +02:00
|
|
|
Update();
|
|
|
|
}
|
2016-06-29 23:39:52 +02:00
|
|
|
}
|
|
|
|
else
|
2016-07-23 15:26:15 +02:00
|
|
|
{ // in rotate mode, if user did just press/release, switch to move mode
|
2017-03-14 10:24:36 +01:00
|
|
|
if (bShort == true && (m_moveType & IsMovable || m_moveType & IsResizable))
|
2016-06-30 18:04:25 +02:00
|
|
|
{
|
2017-03-13 14:54:38 +01:00
|
|
|
m_eMode = mMove;
|
2016-06-30 18:04:25 +02:00
|
|
|
}
|
2017-03-13 14:54:38 +01:00
|
|
|
else if (m_moveType & IsRotatable)
|
2016-06-30 18:04:25 +02:00
|
|
|
{
|
2016-07-23 15:26:15 +02:00
|
|
|
// if user rotated the item, emit proper signal and update the label
|
2016-06-30 18:04:25 +02:00
|
|
|
emit SignalRotated(rotation());
|
|
|
|
}
|
2017-03-13 14:54:38 +01:00
|
|
|
UpdateBox();
|
2016-06-29 23:39:52 +02:00
|
|
|
}
|
2016-06-30 18:04:25 +02:00
|
|
|
m_bReleased = true;
|
2016-06-26 14:21:06 +02:00
|
|
|
}
|
2016-06-23 00:59:45 +02:00
|
|
|
}
|
|
|
|
|
2016-08-03 00:32:37 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief VTextGraphicsItem::hoverMoveEvent checks if cursor has to be changed
|
|
|
|
* @param pHE pointer to the scene hover event
|
|
|
|
*/
|
|
|
|
void VTextGraphicsItem::hoverMoveEvent(QGraphicsSceneHoverEvent* pHE)
|
|
|
|
{
|
2017-03-14 11:53:35 +01:00
|
|
|
if (m_eMode == mResize && m_moveType & IsResizable)
|
2016-08-03 00:32:37 +02:00
|
|
|
{
|
|
|
|
if (m_rectResize.contains(pHE->pos()) == true)
|
|
|
|
{
|
|
|
|
SetOverrideCursor(Qt::SizeFDiagCursor);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RestoreOverrideCursor(Qt::SizeFDiagCursor);
|
|
|
|
}
|
|
|
|
}
|
2017-06-16 13:53:08 +02:00
|
|
|
VPieceItem::hoverMoveEvent(pHE);
|
2016-08-03 00:32:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
|
|
* @brief VTextGraphicsItem::hoverLeaveEvent tries to restore normal mouse cursor
|
|
|
|
* @param pHE not used
|
|
|
|
*/
|
|
|
|
void VTextGraphicsItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* pHE)
|
|
|
|
{
|
|
|
|
RestoreOverrideCursor(Qt::SizeFDiagCursor);
|
2017-06-16 13:53:08 +02:00
|
|
|
VPieceItem::hoverLeaveEvent(pHE);
|
2016-08-03 00:32:37 +02:00
|
|
|
}
|
|
|
|
|
2016-06-23 00:59:45 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-07-23 15:26:15 +02:00
|
|
|
/**
|
|
|
|
* @brief VTextGraphicsItem::UpdateBox redraws the label content
|
|
|
|
*/
|
2016-06-27 17:18:43 +02:00
|
|
|
void VTextGraphicsItem::UpdateBox()
|
2016-06-23 00:59:45 +02:00
|
|
|
{
|
2016-06-25 17:19:44 +02:00
|
|
|
update(m_rectBoundingBox);
|
2016-06-23 00:59:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-07-23 15:26:15 +02:00
|
|
|
/**
|
|
|
|
* @brief VTextGraphicsItem::UpdateFont sets the text font size, so that the entire text will
|
|
|
|
* just fit into the label bounding box
|
|
|
|
*/
|
2016-09-29 01:39:50 +02:00
|
|
|
void VTextGraphicsItem::CorrectLabel()
|
2016-06-23 00:59:45 +02:00
|
|
|
{
|
2016-09-29 01:39:50 +02:00
|
|
|
qreal dX;
|
|
|
|
qreal dY;
|
|
|
|
QRectF rectBB;
|
|
|
|
rectBB.setTopLeft(pos());
|
|
|
|
rectBB.setSize(m_rectBoundingBox.size());
|
|
|
|
if (IsContained(rectBB, rotation(), dX, dY) == false)
|
2016-06-24 17:57:08 +02:00
|
|
|
{
|
2016-09-29 01:39:50 +02:00
|
|
|
// put the label inside the pattern
|
|
|
|
setPos(pos().x() + dX, pos().y() + dY);
|
2016-06-24 17:57:08 +02:00
|
|
|
}
|
2016-12-14 00:56:30 +01:00
|
|
|
m_tm.FitFontSize(m_rectBoundingBox.width(), m_rectBoundingBox.height());
|
2016-06-27 17:18:43 +02:00
|
|
|
UpdateBox();
|
2016-06-23 00:59:45 +02:00
|
|
|
}
|
2017-03-13 14:54:38 +01:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VTextGraphicsItem::AllUserModifications(const QPointF &pos)
|
|
|
|
{
|
|
|
|
if (m_eMode != mRotate)
|
|
|
|
{
|
|
|
|
UserMoveAndResize(pos);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetOverrideCursor(cursorArrowCloseHand, 1, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VTextGraphicsItem::UserRotateAndMove()
|
|
|
|
{
|
|
|
|
if (m_eMode != mRotate)
|
|
|
|
{
|
|
|
|
m_eMode = mMove;
|
|
|
|
}
|
|
|
|
SetOverrideCursor(cursorArrowCloseHand, 1, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VTextGraphicsItem::UserMoveAndResize(const QPointF &pos)
|
|
|
|
{
|
|
|
|
if (m_rectResize.contains(pos) == true)
|
|
|
|
{
|
|
|
|
m_eMode = mResize;
|
|
|
|
SetOverrideCursor(Qt::SizeFDiagCursor);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_eMode = mMove; // block later if need
|
|
|
|
SetOverrideCursor(cursorArrowCloseHand, 1, 1);
|
|
|
|
}
|
|
|
|
}
|