From d21546e397ae5ae3995f4024dd3e2e564e5f282d Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Fri, 3 Sep 2021 11:32:07 +0300 Subject: [PATCH] Add support for "Text as path" feature. --- src/app/puzzle/scene/vpgraphicspiece.cpp | 70 +++++++++++++++++++----- src/app/puzzle/scene/vpgraphicspiece.h | 10 +++- src/libs/vlayout/vlayoutpiece.cpp | 4 +- 3 files changed, 66 insertions(+), 18 deletions(-) diff --git a/src/app/puzzle/scene/vpgraphicspiece.cpp b/src/app/puzzle/scene/vpgraphicspiece.cpp index 2ef608c4c..7a67088dd 100644 --- a/src/app/puzzle/scene/vpgraphicspiece.cpp +++ b/src/app/puzzle/scene/vpgraphicspiece.cpp @@ -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 &newStickyPoint) { @@ -249,7 +276,7 @@ void VPGraphicsPiece::SetStickyPoints(const QVector &newStickyPoint) } //--------------------------------------------------------------------------------------------------------------------- -void VPGraphicsPiece::PaintPieceLabel(const QVector &labelShape, const VTextManager &tm, QPainter *painter) +void VPGraphicsPiece::InitPieceLabel(const QVector &labelShape, const VTextManager &tm) { VPPiecePtr piece = m_piece.toStrongRef(); if (piece.isNull()) @@ -275,6 +302,11 @@ void VPGraphicsPiece::PaintPieceLabel(const QVector &labelShape, const QFontMetrics fm(fnt); + if (m_textAsPaths) + { + dY += fm.height(); + } + if (dY > dH) { break; @@ -330,19 +362,29 @@ void VPGraphicsPiece::PaintPieceLabel(const QVector &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(fm.ascent())/6., fnt, qsText); - dY += (fm.height() + tm.GetSpacing()); + 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(); } } diff --git a/src/app/puzzle/scene/vpgraphicspiece.h b/src/app/puzzle/scene/vpgraphicspiece.h index fe9e23737..a0889b5a9 100644 --- a/src/app/puzzle/scene/vpgraphicspiece.h +++ b/src/app/puzzle/scene/vpgraphicspiece.h @@ -55,6 +55,8 @@ public: void SetStickyPoints(const QVector &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 &labelShape, const VTextManager &tm, QPainter *painter=nullptr); + bool m_textAsPaths{false}; + + QVector m_labelPathItems{}; + QVector m_labelTextItems{}; + + void InitLabels(); + void InitPieceLabel(const QVector &labelShape, const VTextManager &tm); void PaintPiece(QPainter *painter=nullptr); void GroupMove(const QPointF &pos); diff --git a/src/libs/vlayout/vlayoutpiece.cpp b/src/libs/vlayout/vlayoutpiece.cpp index abf892bfe..5783cd297 100644 --- a/src/libs/vlayout/vlayoutpiece.cpp +++ b/src/libs/vlayout/vlayoutpiece.cpp @@ -1565,7 +1565,7 @@ void VLayoutPiece::CreateLabelStrings(QGraphicsItem *parent, const QVector(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 QVectorsetFont(fnt); item->setText(qsText); item->setTransform(labelMatrix);