From a04f5ce3ea2fe0e63d6892465a0ee1058b55ba2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kama=20W=C3=B3jcik?= Date: Tue, 22 Dec 2020 12:30:37 +0100 Subject: [PATCH] Refactor label drawing Change-Id: Id95721b13454f0062a144263b1c518402a556e5f Reviewed-by: Thomas Hartmann --- .../components/formeditor/formeditoritem.cpp | 69 +++++-------------- .../components/formeditor/formeditoritem.h | 4 +- 2 files changed, 21 insertions(+), 52 deletions(-) diff --git a/src/plugins/qmldesigner/components/formeditor/formeditoritem.cpp b/src/plugins/qmldesigner/components/formeditor/formeditoritem.cpp index 1cd42733d9d..09c19a7a84c 100644 --- a/src/plugins/qmldesigner/components/formeditor/formeditoritem.cpp +++ b/src/plugins/qmldesigner/components/formeditor/formeditoritem.cpp @@ -888,6 +888,7 @@ public: , labelFlags(Qt::AlignHCenter | Qt::AlignVCenter | Qt::TextDontClip) , labelFlipSide(false) , hitTesting(hitTest) + , isSelected(false) , events() { // width @@ -1416,37 +1417,7 @@ void FormEditorTransitionItem::updateGeometry() overallBoundingRect = overallBoundingRect.united(connection.fromRect); overallBoundingRect = overallBoundingRect.united(connection.toRect); overallBoundingRect = overallBoundingRect.united(pathBoundingRect); - - // Calculate bounding rect for label - // TODO The calculation should be put into a separate function to avoid code duplication as this - // can also be found in drawLabel() - if (!connection.config.label.isEmpty()) { - const qreal percent = connection.config.labelPosition / 100.0; - const QPointF pos = connection.path.pointAtPercent(percent); - const qreal angle = connection.path.angleAtPercent(percent); - - QLineF tmp(pos, QPointF(10, 10)); - tmp.setLength(connection.config.labelOffset); - tmp.setAngle(angle + (connection.config.labelFlipSide ? 270 : 90)); - - QRectF textRect(0, 0, 100, 50); - textRect.moveCenter(tmp.p2()); - - QRectF labelRect; - - QTransform transform; - transform.translate(textRect.center().x(), textRect.center().y()); - transform.rotate(-normalizeAngle(angle)); - transform.translate(-textRect.center().x(), -textRect.center().y()); - - localPainter.setTransform(transform); - localPainter.drawText(textRect, - connection.config.labelFlags, - connection.config.label, - &labelRect); - QRectF labelBoundingBox = transform.mapRect(labelRect); - overallBoundingRect = overallBoundingRect.united(labelBoundingBox); - } + drawLabels(&localPainter, connection); } } @@ -1461,7 +1432,7 @@ QPointF FormEditorTransitionItem::instancePosition() const return qmlItemNode().flowPosition(); } -static void drawLabel(QPainter *painter, const Connection &connection) +void FormEditorTransitionItem::drawLabels(QPainter *painter, const Connection &connection) { // draw label with event ids if (connection.config.isSelected && !connection.config.events.isEmpty()) @@ -1469,27 +1440,23 @@ static void drawLabel(QPainter *painter, const Connection &connection) qreal offset = connection.config.labelOffset; QStringList events = connection.config.events.split(','); int fontSize = connection.config.fontSize; - QString output = events[0].trimmed(); - qreal minWidth = offset * 12; - int letterWidth = fontSize * 0.6; // assumption on max letter length - if (minWidth < output.size() * letterWidth) minWidth = output.size() * letterWidth; + QString outputText; + qreal minWidth = offset * 12.0; + qreal letterWidth = fontSize * 0.6; // assumption on max letter width int eventCount = events.size(); - for (int i = 1; i < eventCount; ++i) - { - output.append('\n'); - QString id = events[i].trimmed(); - output.append(id); - if (minWidth < id.size() * letterWidth) minWidth = id.size() * letterWidth; - } + std::for_each(events.begin(), events.end(), [&](auto id) { + outputText.append(id.trimmed()); + outputText.append('\n'); + if (minWidth < id.size() * letterWidth) minWidth = id.size() * letterWidth; + }); const QPointF pos = connection.path.pointAtPercent(0.0); painter->save(); painter->setBrush(QColor(70, 70, 70, 200)); painter->setPen(Qt::lightGray); - - painter->drawRoundedRect(pos.x(), pos.y() + offset, minWidth, 1.5 * fontSize * eventCount + offset * 4, offset / 2, offset / 2); - painter->drawText(pos.x(), pos.y() + 2 * offset, minWidth, offset * 2, Qt::AlignHCenter, QObject::tr("Connected Events")); - painter->drawLine(pos.x() + offset, pos.y() + 4 * offset, pos.x() + minWidth - offset, pos.y() + offset * 4); - painter->drawText(pos.x() + offset, pos.y() + 4 * offset, minWidth - offset, 1.5 * fontSize * eventCount, Qt::AlignLeft, output); + painter->drawRoundedRect(pos.x(), pos.y() + offset, minWidth, 1.5 * fontSize * eventCount + offset * 4.0, offset / 2.0, offset / 2.0); + painter->drawText(pos.x(), pos.y() + 2.0 * offset, minWidth, offset * 2.0, Qt::AlignHCenter, QObject::tr("Connected Events")); + painter->drawLine(pos.x() + offset, pos.y() + 4.0 * offset, pos.x() + minWidth - offset, pos.y() + offset * 4.0); + painter->drawText(pos.x() + offset, pos.y() + 4.0 * offset, minWidth - offset, 1.5 * fontSize * eventCount, Qt::AlignLeft, outputText); painter->restore(); } if (connection.config.label.isEmpty()) @@ -1534,7 +1501,7 @@ static void drawArrow(QPainter *painter, painter->restore(); } -static void paintConnection(QPainter *painter, const Connection &connection) +void FormEditorTransitionItem::paintConnection(QPainter *painter, const Connection &connection) { const int arrowLength = 4 * connection.config.adjustedWidth; const int arrowWidth = 8 * connection.config.adjustedWidth; @@ -1586,8 +1553,8 @@ static void paintConnection(QPainter *painter, const Connection &connection) painter->drawEllipse(connection.start, arrowLength / 3, arrowLength / 3); } - // Draw label - drawLabel(painter, connection); + // Draw labels + drawLabels(painter, connection); painter->restore(); } diff --git a/src/plugins/qmldesigner/components/formeditor/formeditoritem.h b/src/plugins/qmldesigner/components/formeditor/formeditoritem.h index b5eddd1407b..58ffff8393c 100644 --- a/src/plugins/qmldesigner/components/formeditor/formeditoritem.h +++ b/src/plugins/qmldesigner/components/formeditor/formeditoritem.h @@ -40,6 +40,7 @@ namespace QmlDesigner { class FormEditorScene; class FormEditorView; class AbstractFormEditorTool; +class Connection; namespace Internal { class GraphicItemResizer; @@ -197,11 +198,12 @@ public: QPointF instancePosition() const override; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override; bool flowHitTest(const QPointF &point) const override; - protected: FormEditorTransitionItem(const QmlItemNode &qmlItemNode, FormEditorScene *scene) : FormEditorItem(qmlItemNode, scene) {} + void paintConnection(QPainter *painter, const Connection &connection); + void drawLabels(QPainter *painter, const Connection &connection); private: mutable bool m_hitTest = false; };