2016-06-23 00:59:45 +02:00
|
|
|
/************************************************************************
|
|
|
|
**
|
|
|
|
** @file vtextgraphicsitem.h
|
|
|
|
** @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/>.
|
|
|
|
**
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QStyleOptionGraphicsItem>
|
|
|
|
#include <QGraphicsSceneMouseEvent>
|
2016-07-02 11:15:41 +02:00
|
|
|
#include <QGraphicsView>
|
2016-06-30 18:04:25 +02:00
|
|
|
#include <QTransform>
|
2016-07-10 12:39:50 +02:00
|
|
|
#include <QCursor>
|
2016-06-23 00:59:45 +02:00
|
|
|
#include <QDebug>
|
|
|
|
|
2016-07-06 20:49:36 +02:00
|
|
|
#include "../vmisc/def.h"
|
2016-06-23 00:59:45 +02:00
|
|
|
#include "vtextgraphicsitem.h"
|
|
|
|
|
|
|
|
#define RESIZE_SQUARE 30
|
2016-06-30 18:04:25 +02:00
|
|
|
#define ROTATE_CIRCLE 20
|
2016-07-01 01:45:16 +02:00
|
|
|
#define ROTATE_RECT 60
|
|
|
|
#define ROTATE_ARC 50
|
2016-06-24 17:57:08 +02:00
|
|
|
#define MIN_W 120
|
|
|
|
#define MIN_H 60
|
|
|
|
#define MIN_FONT_SIZE 12
|
2016-07-03 10:17:59 +02:00
|
|
|
#define MAX_FONT_SIZE 128
|
2016-06-25 17:19:44 +02:00
|
|
|
#define SPACING 2
|
2016-07-02 11:15:41 +02:00
|
|
|
#define TOP_Z 2
|
2016-06-23 00:59:45 +02:00
|
|
|
|
2016-07-06 20:49:36 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
TextLine::TextLine()
|
|
|
|
:m_qsText(), m_iFontSize(MIN_FONT_SIZE), m_eFontWeight(QFont::Normal), m_eStyle(QFont::StyleNormal),
|
|
|
|
m_eAlign(Qt::AlignCenter), m_iHeight(0)
|
|
|
|
{}
|
|
|
|
|
2016-06-23 00:59:45 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VTextGraphicsItem::VTextGraphicsItem(QGraphicsItem* pParent)
|
2016-07-06 20:49:36 +02:00
|
|
|
:QGraphicsObject(pParent), m_eMode(VTextGraphicsItem::mNormal), m_bReleased(false),
|
|
|
|
m_ptStartPos(), m_ptStart(), m_ptRotCenter(), m_szStart(), m_dRotation(0), m_dAngle(0),
|
|
|
|
m_rectResize(), m_iMinH(MIN_H), m_rectBoundingBox(), m_font(), m_liLines(), m_liOutput()
|
|
|
|
|
2016-06-23 00:59:45 +02:00
|
|
|
{
|
2016-06-24 17:57:08 +02:00
|
|
|
m_rectBoundingBox.setTopLeft(QPointF(0, 0));
|
2016-06-27 17:18:43 +02:00
|
|
|
SetSize(MIN_W, m_iMinH);
|
2016-07-02 11:15:41 +02:00
|
|
|
setZValue(TOP_Z);
|
2016-06-23 00:59:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
VTextGraphicsItem::~VTextGraphicsItem()
|
|
|
|
{}
|
|
|
|
|
2016-06-25 17:19:44 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VTextGraphicsItem::SetFont(const QFont& fnt)
|
|
|
|
{
|
|
|
|
m_font = fnt;
|
|
|
|
}
|
|
|
|
|
2016-06-23 00:59:45 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VTextGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|
|
|
{
|
2016-06-29 23:39:52 +02:00
|
|
|
Q_UNUSED(widget);
|
2016-06-23 00:59:45 +02:00
|
|
|
painter->fillRect(option->rect, QColor(251, 251, 175));
|
2016-07-01 01:45:16 +02:00
|
|
|
painter->setRenderHints(QPainter::Antialiasing);
|
2016-06-25 17:19:44 +02:00
|
|
|
|
|
|
|
// draw text lines
|
|
|
|
int iY = 0;
|
2016-06-27 17:18:43 +02:00
|
|
|
int iH = 0;
|
2016-06-25 17:19:44 +02:00
|
|
|
painter->setPen(Qt::black);
|
|
|
|
QFont fnt = m_font;
|
|
|
|
for (int i = 0; i < m_liOutput.count(); ++i)
|
|
|
|
{
|
|
|
|
const TextLine& tl = m_liOutput.at(i);
|
|
|
|
iH = tl.m_iHeight;
|
|
|
|
fnt.setPixelSize(m_font.pixelSize() + tl.m_iFontSize);
|
|
|
|
fnt.setWeight(tl.m_eFontWeight);
|
2016-06-26 14:21:06 +02:00
|
|
|
fnt.setStyle(tl.m_eStyle);
|
2016-06-25 17:19:44 +02:00
|
|
|
painter->setFont(fnt);
|
2016-07-10 12:39:50 +02:00
|
|
|
painter->drawText(0, iY, qRound(boundingRect().width()), iH, tl.m_eAlign, tl.m_qsText);
|
2016-06-25 17:19:44 +02:00
|
|
|
iY += iH + SPACING;
|
|
|
|
}
|
|
|
|
|
2016-06-23 00:59:45 +02:00
|
|
|
|
|
|
|
if (m_eMode != mNormal)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
painter->setPen(Qt::black);
|
|
|
|
painter->setBrush(Qt::black);
|
|
|
|
painter->drawRect(m_rectResize);
|
2016-06-25 17:19:44 +02:00
|
|
|
|
2016-06-30 18:04:25 +02:00
|
|
|
if (m_eMode == mResize)
|
|
|
|
{
|
2016-07-10 12:39:50 +02:00
|
|
|
painter->drawLine(0, 0, qRound(m_rectBoundingBox.width()), qRound(m_rectBoundingBox.height()));
|
|
|
|
painter->drawLine(0, qRound(m_rectBoundingBox.height()), qRound(m_rectBoundingBox.width()), 0);
|
2016-06-30 18:04:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2016-06-25 17:19:44 +02:00
|
|
|
{
|
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-06-30 18:04:25 +02:00
|
|
|
ROTATE_CIRCLE,
|
|
|
|
ROTATE_CIRCLE
|
|
|
|
);
|
2016-07-01 01:45:16 +02:00
|
|
|
painter->setPen(QPen(Qt::black, 3));
|
|
|
|
painter->setBrush(Qt::NoBrush);
|
|
|
|
int iTop = ROTATE_RECT - ROTATE_ARC;
|
|
|
|
int iLeft = ROTATE_RECT - ROTATE_ARC;
|
2016-07-10 12:39:50 +02:00
|
|
|
int iRight = qRound(m_rectBoundingBox.width()) - ROTATE_RECT;
|
|
|
|
int iBottom = qRound(m_rectBoundingBox.height()) - ROTATE_RECT;
|
2016-07-01 01:45:16 +02:00
|
|
|
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-27 17:18:43 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VTextGraphicsItem::Reset()
|
|
|
|
{
|
|
|
|
m_eMode = mNormal;
|
2016-06-30 18:04:25 +02:00
|
|
|
m_bReleased = false;
|
2016-06-27 17:18:43 +02:00
|
|
|
Update();
|
2016-07-02 11:15:41 +02:00
|
|
|
setZValue(TOP_Z);
|
2016-06-27 17:18:43 +02:00
|
|
|
}
|
|
|
|
|
2016-06-25 17:19:44 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VTextGraphicsItem::AddLine(const TextLine& tl)
|
|
|
|
{
|
|
|
|
m_liLines << tl;
|
|
|
|
while (IsBigEnough(MIN_W, m_iMinH, MIN_FONT_SIZE) == false)
|
|
|
|
{
|
|
|
|
m_iMinH += 5;
|
|
|
|
}
|
|
|
|
if (m_rectBoundingBox.height() < m_iMinH)
|
|
|
|
{
|
2016-06-27 17:18:43 +02:00
|
|
|
SetSize(m_rectBoundingBox.width(), m_iMinH);
|
2016-06-25 17:19:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VTextGraphicsItem::Clear()
|
|
|
|
{
|
|
|
|
m_liLines.clear();
|
|
|
|
}
|
|
|
|
|
2016-06-27 01:17:27 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-06-27 17:18:43 +02:00
|
|
|
void VTextGraphicsItem::SetSize(qreal fW, qreal fH)
|
2016-06-27 01:17:27 +02:00
|
|
|
{
|
2016-06-27 17:18:43 +02:00
|
|
|
// don't allow resize under specific size
|
|
|
|
if (fW < MIN_W || fH < m_iMinH)
|
|
|
|
return;
|
|
|
|
|
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);
|
|
|
|
m_rectResize.setTopLeft(QPointF(fW - RESIZE_SQUARE, fH - RESIZE_SQUARE));
|
|
|
|
m_rectResize.setWidth(RESIZE_SQUARE);
|
|
|
|
m_rectResize.setHeight(RESIZE_SQUARE);
|
2016-07-01 01:45:16 +02:00
|
|
|
setTransformOriginPoint(m_rectBoundingBox.center());
|
2016-07-03 10:17:59 +02:00
|
|
|
prepareGeometryChange();
|
2016-06-27 01:17:27 +02:00
|
|
|
}
|
|
|
|
|
2016-06-23 00:59:45 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-06-27 17:18:43 +02:00
|
|
|
void VTextGraphicsItem::Update()
|
2016-06-23 00:59:45 +02:00
|
|
|
{
|
2016-06-25 17:19:44 +02:00
|
|
|
UpdateFont();
|
2016-06-27 17:18:43 +02:00
|
|
|
UpdateBox();
|
2016-06-23 00:59:45 +02:00
|
|
|
}
|
|
|
|
|
2016-07-01 01:45:16 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
int VTextGraphicsItem::GetFontSize() const
|
|
|
|
{
|
|
|
|
return m_font.pixelSize();
|
|
|
|
}
|
|
|
|
|
2016-06-23 00:59:45 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-06-24 17:57:08 +02:00
|
|
|
QRectF VTextGraphicsItem::boundingRect() const
|
2016-06-25 17:19:44 +02:00
|
|
|
{
|
|
|
|
return m_rectBoundingBox;
|
|
|
|
}
|
2016-06-24 17:57:08 +02:00
|
|
|
|
2016-06-23 00:59:45 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
void VTextGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *pME)
|
|
|
|
{
|
2016-06-29 23:52:48 +02:00
|
|
|
if (pME->button() == Qt::LeftButton)
|
2016-06-23 00:59:45 +02:00
|
|
|
{
|
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();
|
|
|
|
if (m_eMode != mRotate)
|
2016-06-29 23:39:52 +02:00
|
|
|
{
|
2016-06-30 18:04:25 +02:00
|
|
|
if (m_rectResize.contains(pME->pos()) == true)
|
|
|
|
{
|
|
|
|
m_eMode = mResize;
|
2016-07-10 12:39:50 +02:00
|
|
|
SetOverrideCursor(Qt::SizeFDiagCursor);
|
2016-06-30 18:04:25 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_eMode = mMove;
|
2016-07-06 20:49:36 +02:00
|
|
|
SetOverrideCursor(cursorArrowCloseHand, 1, 1);
|
2016-06-30 18:04:25 +02:00
|
|
|
}
|
2016-06-29 23:39:52 +02:00
|
|
|
}
|
2016-07-02 11:15:41 +02:00
|
|
|
setZValue(TOP_Z + 1);
|
2016-06-29 23:39:52 +02:00
|
|
|
UpdateBox();
|
2016-06-23 00:59:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-06-25 17:19:44 +02:00
|
|
|
void VTextGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent* pME)
|
2016-06-23 00:59:45 +02:00
|
|
|
{
|
2016-07-10 12:39:50 +02:00
|
|
|
if (m_rectResize.contains(pME->pos()) == true)
|
|
|
|
{
|
|
|
|
SetOverrideCursor(Qt::SizeFDiagCursor);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RestoreOverrideCursor(cursorArrowOpenHand);
|
|
|
|
}
|
2016-06-30 18:04:25 +02:00
|
|
|
QPointF ptDiff = pME->scenePos() - m_ptStart;
|
2016-06-23 00:59:45 +02:00
|
|
|
if (m_eMode == mMove)
|
|
|
|
{
|
2016-07-06 20:49:36 +02:00
|
|
|
QPointF pt = m_ptStartPos + ptDiff;
|
|
|
|
pt.setX(pt.x() + m_rectBoundingBox.width()/2);
|
|
|
|
pt.setY(pt.y() + m_rectBoundingBox.height()/2);
|
2016-07-08 00:46:33 +02:00
|
|
|
QRectF rectBB = parentItem()->boundingRect();
|
|
|
|
if (rectBB.contains(pt) == false)
|
2016-07-06 20:49:36 +02:00
|
|
|
{
|
2016-07-08 00:46:33 +02:00
|
|
|
pt.setX(qMin(rectBB.right(), qMax(pt.x(), rectBB.left())));
|
|
|
|
pt.setY(qMin(rectBB.bottom(), qMax(pt.y(), rectBB.top())));
|
2016-07-06 20:49:36 +02:00
|
|
|
}
|
2016-07-08 00:46:33 +02:00
|
|
|
pt.setX(pt.x() - m_rectBoundingBox.width()/2);
|
|
|
|
pt.setY(pt.y() - m_rectBoundingBox.height()/2);
|
|
|
|
setPos(pt);
|
|
|
|
UpdateBox();
|
2016-06-23 00:59:45 +02:00
|
|
|
}
|
|
|
|
else if (m_eMode == mResize)
|
|
|
|
{
|
2016-07-06 20:49:36 +02:00
|
|
|
QPointF pt = m_ptStartPos;
|
|
|
|
pt.setX(pt.x() + (m_szStart.width() + ptDiff.x())/2);
|
|
|
|
pt.setY(pt.y() + (m_szStart.height() + ptDiff.y())/2);
|
|
|
|
if (parentItem()->boundingRect().contains(pt) == true) {
|
|
|
|
SetSize(m_szStart.width() + ptDiff.x(), m_szStart.height() + ptDiff.y());
|
|
|
|
Update();
|
|
|
|
emit SignalShrink();
|
|
|
|
}
|
2016-06-23 00:59:45 +02:00
|
|
|
}
|
2016-06-30 18:04:25 +02:00
|
|
|
else if (m_eMode == mRotate)
|
|
|
|
{
|
|
|
|
if (fabs(m_dAngle) < 0.01)
|
|
|
|
{
|
|
|
|
m_dAngle = GetAngle(pME->scenePos());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
double dAng = 180*(GetAngle(pME->scenePos()) - m_dAngle)/M_PI;
|
|
|
|
setRotation(m_dRotation + dAng);
|
2016-07-02 01:49:38 +02:00
|
|
|
//emit SignalRotated(rotation());
|
2016-06-30 18:04:25 +02:00
|
|
|
Update();
|
|
|
|
}
|
2016-06-23 00:59:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
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-06 20:49:36 +02:00
|
|
|
RestoreOverrideCursor(cursorArrowCloseHand);
|
2016-06-30 18:04:25 +02:00
|
|
|
double dDist = fabs(pME->scenePos().x() - m_ptStart.x()) + fabs(pME->scenePos().y() - m_ptStart.y());
|
|
|
|
bool bShort = (dDist < 2);
|
|
|
|
|
|
|
|
if (m_eMode == mMove || m_eMode == mResize)
|
|
|
|
{ // when released in mMove or mResize mode
|
|
|
|
if (bShort == true)
|
|
|
|
{
|
|
|
|
if (m_bReleased == true)
|
|
|
|
{
|
|
|
|
m_eMode = mRotate;
|
|
|
|
UpdateBox();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (m_eMode == mMove)
|
|
|
|
{
|
|
|
|
emit SignalMoved(pos());
|
|
|
|
UpdateBox();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
emit SignalResized(m_rectBoundingBox.width(), m_font.pixelSize());
|
|
|
|
Update();
|
|
|
|
}
|
2016-06-29 23:39:52 +02:00
|
|
|
}
|
|
|
|
else
|
2016-06-30 18:04:25 +02:00
|
|
|
{ // when released in mRotate mode
|
|
|
|
if (bShort == true)
|
|
|
|
{
|
|
|
|
m_eMode = mMove;
|
2016-07-06 20:49:36 +02:00
|
|
|
SetOverrideCursor(cursorArrowCloseHand, 1, 1);
|
2016-06-30 18:04:25 +02:00
|
|
|
UpdateBox();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
emit SignalRotated(rotation());
|
|
|
|
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-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-06-24 17:57:08 +02:00
|
|
|
void VTextGraphicsItem::UpdateFont()
|
2016-06-23 00:59:45 +02:00
|
|
|
{
|
2016-06-25 17:19:44 +02:00
|
|
|
int iFS = m_font.pixelSize();
|
2016-06-24 17:57:08 +02:00
|
|
|
|
2016-06-25 17:19:44 +02:00
|
|
|
// increase the font size until the bounding rect is not big enough
|
|
|
|
while (iFS < MAX_FONT_SIZE && IsBigEnough(m_rectBoundingBox.width(), m_rectBoundingBox.height(), iFS) == true)
|
2016-06-24 17:57:08 +02:00
|
|
|
{
|
|
|
|
++iFS;
|
|
|
|
}
|
2016-06-25 17:19:44 +02:00
|
|
|
// decrease the font size until the bounding rect is big enough
|
|
|
|
while (iFS >= MIN_FONT_SIZE && IsBigEnough(m_rectBoundingBox.width(), m_rectBoundingBox.height(), iFS) == false)
|
2016-06-24 17:57:08 +02:00
|
|
|
{
|
|
|
|
--iFS;
|
|
|
|
}
|
2016-06-25 17:19:44 +02:00
|
|
|
m_font.setPixelSize(iFS);
|
2016-06-27 17:18:43 +02:00
|
|
|
UpdateBox();
|
2016-06-23 00:59:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
2016-06-25 17:19:44 +02:00
|
|
|
bool VTextGraphicsItem::IsBigEnough(qreal fW, qreal fH, int iFontSize)
|
|
|
|
{
|
|
|
|
m_liOutput.clear();
|
|
|
|
QFont fnt = m_font;
|
|
|
|
int iY = 0;
|
|
|
|
for (int i = 0; i < m_liLines.count(); ++i)
|
|
|
|
{
|
|
|
|
const TextLine& tl = m_liLines.at(i);
|
|
|
|
TextLine tlOut = tl;
|
|
|
|
fnt.setPixelSize(iFontSize + tl.m_iFontSize);
|
|
|
|
QFontMetrics fm(fnt);
|
|
|
|
tlOut.m_iHeight = fm.height();
|
|
|
|
QStringList qslLines = SplitString(tlOut.m_qsText, fW, fm);
|
2016-06-26 14:21:06 +02:00
|
|
|
for (int iL = 0; iL < qslLines.count(); ++iL)
|
2016-06-25 17:19:44 +02:00
|
|
|
{
|
2016-06-26 14:21:06 +02:00
|
|
|
tlOut.m_qsText = qslLines[iL];
|
2016-06-25 17:19:44 +02:00
|
|
|
m_liOutput << tlOut;
|
|
|
|
iY += tlOut.m_iHeight + SPACING;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return iY < fH;
|
|
|
|
}
|
2016-06-23 00:59:45 +02:00
|
|
|
|
2016-06-25 17:19:44 +02:00
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
QStringList VTextGraphicsItem::SplitString(const QString &qs, qreal fW, const QFontMetrics &fm)
|
|
|
|
{
|
|
|
|
QRegExp reg("\\s+");
|
|
|
|
QStringList qslWords = qs.split(reg);
|
|
|
|
QStringList qslLines;
|
|
|
|
QString qsCurrent;
|
|
|
|
for (int i = 0; i < qslWords.count(); ++i)
|
|
|
|
{
|
|
|
|
if (qsCurrent.length() > 0)
|
|
|
|
{
|
|
|
|
qsCurrent += " ";
|
|
|
|
}
|
|
|
|
if (fm.width(qsCurrent + qslWords[i]) > fW)
|
|
|
|
{
|
|
|
|
qslLines << qsCurrent;
|
|
|
|
qsCurrent = qslWords[i];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qsCurrent += qslWords[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
qslLines << qsCurrent;
|
|
|
|
return qslLines;
|
|
|
|
}
|
2016-06-30 18:04:25 +02:00
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------
|
|
|
|
double VTextGraphicsItem::GetAngle(QPointF pt) const
|
|
|
|
{
|
2016-07-02 11:15:41 +02:00
|
|
|
double dX = pt.x() - m_ptRotCenter.x();
|
|
|
|
double dY = pt.y() - m_ptRotCenter.y();
|
2016-06-30 18:04:25 +02:00
|
|
|
|
|
|
|
if (fabs(dX) < 1 && fabs(dY) < 1)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return atan2(dY, dX);
|
|
|
|
}
|