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); setCursor(Qt::OpenHandCursor);
PaintPiece(); 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) 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(); VPPiecePtr piece = m_piece.toStrongRef();
if (piece.isNull()) if (piece.isNull())
@ -275,6 +302,11 @@ void VPGraphicsPiece::PaintPieceLabel(const QVector<QPointF> &labelShape, const
QFontMetrics fm(fnt); QFontMetrics fm(fnt);
if (m_textAsPaths)
{
dY += fm.height();
}
if (dY > dH) if (dY > dH)
{ {
break; break;
@ -330,22 +362,32 @@ void VPGraphicsPiece::PaintPieceLabel(const QVector<QPointF> &labelShape, const
labelMatrix *= piece->GetMatrix(); labelMatrix *= piece->GetMatrix();
QPainterPath string; if (m_textAsPaths)
string.addText(QPointF(), fnt, qsText);
string = labelMatrix.map(string);
if (painter != nullptr)
{ {
painter->save(); QPainterPath path;
painter->setBrush(QBrush(color)); path.addText(0, - static_cast<qreal>(fm.ascent())/6., fnt, qsText);
painter->drawPath(string);
painter->restore(); 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()); dY += (fm.height() + tm.GetSpacing());
} }
} }
} }
}
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
void VPGraphicsPiece::PaintPiece(QPainter *painter) void VPGraphicsPiece::PaintPiece(QPainter *painter)
@ -492,9 +534,6 @@ void VPGraphicsPiece::PaintPiece(QPainter *painter)
m_placeLabels.addPath(path); m_placeLabels.addPath(path);
} }
PaintPieceLabel(piece->GetPieceLabelRect(), piece->GetPieceLabelData(), painter);
PaintPieceLabel(piece->GetPatternLabelRect(), piece->GetPatternLabelData(), painter);
if (not m_stickyPoints.isEmpty()) if (not m_stickyPoints.isEmpty())
{ {
m_stickyPath.moveTo(m_stickyPoints.first()); m_stickyPath.moveTo(m_stickyPoints.first());
@ -629,6 +668,7 @@ void VPGraphicsPiece::on_RefreshPiece(const VPPiecePtr &piece)
{ {
prepareGeometryChange(); prepareGeometryChange();
PaintPiece(); // refresh shapes PaintPiece(); // refresh shapes
InitLabels();
emit PieceTransformationChanged(); emit PieceTransformationChanged();
} }
} }

View File

@ -55,6 +55,8 @@ public:
void SetStickyPoints(const QVector<QPointF> &newStickyPoint); void SetStickyPoints(const QVector<QPointF> &newStickyPoint);
void SetTextAsPaths(bool newTextAsPaths);
signals: signals:
void HideTransformationHandles(bool hide); void HideTransformationHandles(bool hide);
void PieceTransformationChanged(); void PieceTransformationChanged();
@ -100,7 +102,13 @@ private:
qreal m_stickyTranslateX{0}; qreal m_stickyTranslateX{0};
qreal m_stickyTranslateY{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 PaintPiece(QPainter *painter=nullptr);
void GroupMove(const QPointF &pos); void GroupMove(const QPointF &pos);

View File

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