Add support for "Text as path" feature.

This commit is contained in:
Roman Telezhynskyi 2021-09-03 11:32:07 +03:00
parent 3b388d9e19
commit d21546e397
3 changed files with 66 additions and 18 deletions

View File

@ -76,6 +76,7 @@ VPGraphicsPiece::VPGraphicsPiece(const VPPiecePtr &piece, QGraphicsItem *parent)
setCursor(Qt::OpenHandCursor);
PaintPiece();
InitLabels();
}
//---------------------------------------------------------------------------------------------------------------------
@ -239,6 +240,32 @@ void VPGraphicsPiece::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
}
}
//---------------------------------------------------------------------------------------------------------------------
void VPGraphicsPiece::SetTextAsPaths(bool newTextAsPaths)
{
m_textAsPaths = newTextAsPaths;
InitLabels();
}
//---------------------------------------------------------------------------------------------------------------------
void VPGraphicsPiece::InitLabels()
{
qDeleteAll(m_labelPathItems);
qDeleteAll(m_labelTextItems);
m_labelPathItems.clear();
m_labelTextItems.clear();
VPPiecePtr piece = m_piece.toStrongRef();
if (piece.isNull())
{
return;
}
InitPieceLabel(piece->GetPieceLabelRect(), piece->GetPieceLabelData());
InitPieceLabel(piece->GetPatternLabelRect(), piece->GetPatternLabelData());
}
//---------------------------------------------------------------------------------------------------------------------
void VPGraphicsPiece::SetStickyPoints(const QVector<QPointF> &newStickyPoint)
{
@ -249,7 +276,7 @@ void VPGraphicsPiece::SetStickyPoints(const QVector<QPointF> &newStickyPoint)
}
//---------------------------------------------------------------------------------------------------------------------
void VPGraphicsPiece::PaintPieceLabel(const QVector<QPointF> &labelShape, const VTextManager &tm, QPainter *painter)
void VPGraphicsPiece::InitPieceLabel(const QVector<QPointF> &labelShape, const VTextManager &tm)
{
VPPiecePtr piece = m_piece.toStrongRef();
if (piece.isNull())
@ -275,6 +302,11 @@ void VPGraphicsPiece::PaintPieceLabel(const QVector<QPointF> &labelShape, const
QFontMetrics fm(fnt);
if (m_textAsPaths)
{
dY += fm.height();
}
if (dY > dH)
{
break;
@ -330,21 +362,31 @@ void VPGraphicsPiece::PaintPieceLabel(const QVector<QPointF> &labelShape, const
labelMatrix *= piece->GetMatrix();
QPainterPath string;
string.addText(QPointF(), fnt, qsText);
string = labelMatrix.map(string);
if (painter != nullptr)
if (m_textAsPaths)
{
painter->save();
painter->setBrush(QBrush(color));
painter->drawPath(string);
painter->restore();
QPainterPath path;
path.addText(0, - static_cast<qreal>(fm.ascent())/6., fnt, qsText);
auto* item = new QGraphicsPathItem(this);
item->setPath(path);
item->setBrush(QBrush(color));
item->setTransform(labelMatrix);
m_labelPathItems.append(item);
dY += tm.GetSpacing();
}
else
{
auto* item = new QGraphicsSimpleTextItem(this);
item->setFont(fnt);
item->setText(qsText);
item->setTransform(labelMatrix);
m_labelTextItems.append(item);
dY += (fm.height() + tm.GetSpacing());
}
}
}
}
//---------------------------------------------------------------------------------------------------------------------
@ -492,9 +534,6 @@ void VPGraphicsPiece::PaintPiece(QPainter *painter)
m_placeLabels.addPath(path);
}
PaintPieceLabel(piece->GetPieceLabelRect(), piece->GetPieceLabelData(), painter);
PaintPieceLabel(piece->GetPatternLabelRect(), piece->GetPatternLabelData(), painter);
if (not m_stickyPoints.isEmpty())
{
m_stickyPath.moveTo(m_stickyPoints.first());
@ -629,6 +668,7 @@ void VPGraphicsPiece::on_RefreshPiece(const VPPiecePtr &piece)
{
prepareGeometryChange();
PaintPiece(); // refresh shapes
InitLabels();
emit PieceTransformationChanged();
}
}

View File

@ -55,6 +55,8 @@ public:
void SetStickyPoints(const QVector<QPointF> &newStickyPoint);
void SetTextAsPaths(bool newTextAsPaths);
signals:
void HideTransformationHandles(bool hide);
void PieceTransformationChanged();
@ -100,7 +102,13 @@ private:
qreal m_stickyTranslateX{0};
qreal m_stickyTranslateY{0};
void PaintPieceLabel(const QVector<QPointF> &labelShape, const VTextManager &tm, QPainter *painter=nullptr);
bool m_textAsPaths{false};
QVector<QGraphicsPathItem *> m_labelPathItems{};
QVector<QGraphicsSimpleTextItem *> m_labelTextItems{};
void InitLabels();
void InitPieceLabel(const QVector<QPointF> &labelShape, const VTextManager &tm);
void PaintPiece(QPainter *painter=nullptr);
void GroupMove(const QPointF &pos);

View File

@ -1565,7 +1565,7 @@ void VLayoutPiece::CreateLabelStrings(QGraphicsItem *parent, const QVector<QPoin
QPainterPath path;
path.addText(0, - static_cast<qreal>(fm.ascent())/6., fnt, qsText);
QGraphicsPathItem* item = new QGraphicsPathItem(parent);
auto* item = new QGraphicsPathItem(parent);
item->setPath(path);
item->setBrush(QBrush(Qt::black));
item->setTransform(labelMatrix);
@ -1574,7 +1574,7 @@ void VLayoutPiece::CreateLabelStrings(QGraphicsItem *parent, const QVector<QPoin
}
else
{
QGraphicsSimpleTextItem* item = new QGraphicsSimpleTextItem(parent);
auto* item = new QGraphicsSimpleTextItem(parent);
item->setFont(fnt);
item->setText(qsText);
item->setTransform(labelMatrix);