diff options
author | Henning Gruendl <henning.gruendl@qt.io> | 2020-05-12 18:36:54 +0200 |
---|---|---|
committer | Thomas Hartmann <thomas.hartmann@qt.io> | 2020-05-12 18:10:41 +0000 |
commit | d1728fbb6bf5dbfedd5899f93482e553a528882b (patch) | |
tree | 3176da2d0a324b6e4bf274c55f46fc06fa06dc9b | |
parent | c9b56717542749b271d5e48c7bf7698884e94f8f (diff) |
QmlDesigner: Add transition item label
* Add label to transition item
* Change signature of drawArrow()
Task-number: QDS-2085
Change-Id: Ia719958ead404ea083a15fdee440a8e6a306ee62
Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
3 files changed, 86 insertions, 7 deletions
diff --git a/src/plugins/qmldesigner/components/formeditor/formeditoritem.cpp b/src/plugins/qmldesigner/components/formeditor/formeditoritem.cpp index 0c022431c9..def098ca46 100644 --- a/src/plugins/qmldesigner/components/formeditor/formeditoritem.cpp +++ b/src/plugins/qmldesigner/components/formeditor/formeditoritem.cpp @@ -26,6 +26,7 @@ #include "formeditoritem.h" #include "formeditorscene.h" +#include <variantproperty.h> #include <bindingproperty.h> #include <modelnode.h> @@ -814,8 +815,46 @@ static bool horizontalOverlap(const QRectF &from, const QRectF &to) return false; } +static void drawLabel(QPainter *painter, + const QPainterPath &path, + const QString &text, + const qreal percent, + const qreal offset, + bool flipSide) +{ + if (text.isEmpty()) + return; + + const int flags = Qt::AlignHCenter | Qt::AlignVCenter | Qt::TextDontClip; + + QPointF pos = path.pointAtPercent(percent); + qreal angle = path.angleAtPercent(percent); + + QLineF tmp(pos, QPointF(10, 10)); + tmp.setLength(offset); + tmp.setAngle(angle + (flipSide ? 270 : 90)); + + QRectF textRect(0, 0, 100, 50); + textRect.moveCenter(tmp.p2()); + + auto normalizeAngle = [](int angle) { + int newAngle = angle; + while (newAngle <= -90) newAngle += 180; + while (newAngle > 90) newAngle -= 180; + return newAngle; + }; + + painter->save(); + painter->translate(textRect.center()); + painter->rotate(-normalizeAngle(angle)); + painter->translate(-textRect.center()); + painter->drawText(textRect, flags, text); + painter->restore(); +} + static void drawArrow(QPainter *painter, - const QLineF &line, + const QPointF &point, + const qreal &angle, int arrowLength, int arrowWidth) { @@ -825,8 +864,8 @@ static void drawArrow(QPainter *painter, painter->save(); - painter->translate(line.p2()); - painter->rotate(-line.angle()); + painter->translate(point); + painter->rotate(-angle); painter->drawLine(leftP, peakP); painter->drawLine(rightP, peakP); @@ -1029,7 +1068,8 @@ static QPainterPath sShapedConnection(const QPointF &start, static void paintConnection(QPainter *painter, const QRectF &from, const QRectF &to, - const ConnectionStyle &style) + const ConnectionStyle &style, + const QString &label) { painter->save(); painter->setRenderHint(QPainter::Antialiasing); @@ -1126,11 +1166,18 @@ static void paintConnection(QPainter *painter, pen.setStyle(Qt::SolidLine); painter->setPen(pen); - drawArrow(painter, QLineF(path.pointAtPercent(0.9), endP), arrowLength, arrowWidth); + qreal anglePercent = 1.0; + + if (extraLine && style.bezier < 80) + anglePercent = 1.0 - qMin(1.0, (80 - style.bezier) / 10.0) * 0.05; + + drawArrow(painter, endP, path.angleAtPercent(anglePercent), arrowLength, arrowWidth); painter->setBrush(Qt::white); painter->drawEllipse(startP, arrowLength / 3, arrowLength / 3); + drawLabel(painter, path, label, style.labelPosition / 100.0, style.labelOffset, style.labelFlipSide); + painter->restore(); } @@ -1260,10 +1307,35 @@ void FormEditorTransitionItem::paint(QPainter *painter, const QStyleOptionGraphi if (qmlItemNode().modelNode().hasAuxiliaryData("type")) style.type = static_cast<ConnectionType>(qmlItemNode().modelNode().auxiliaryData("type").toInt()); + + QFont font = painter->font(); + font.setPixelSize(16 / scaleFactor); + painter->setFont(font); + + QString label; + + if (qmlItemNode().modelNode().hasBindingProperty("condition")) + label = qmlItemNode().modelNode().bindingProperty("condition").expression(); + + if (qmlItemNode().modelNode().hasVariantProperty("question")) + label = qmlItemNode().modelNode().variantProperty("question").value().toString(); + + style.labelOffset = 14 / scaleFactor; + + style.labelPosition = 50.0; + + if (qmlItemNode().modelNode().hasAuxiliaryData("labelPosition")) + style.labelPosition = qmlItemNode().modelNode().auxiliaryData("labelPosition").toReal(); + + style.labelFlipSide = false; + + if (qmlItemNode().modelNode().hasAuxiliaryData("labelFlipSide")) + style.labelFlipSide = qmlItemNode().modelNode().auxiliaryData("labelFlipSide").toBool(); + if (resolved.isStartLine) fromRect.translate(0, style.outOffset); - paintConnection(painter, fromRect, toRect, style); + paintConnection(painter, fromRect, toRect, style, label); if (resolved.isStartLine) { diff --git a/src/plugins/qmldesigner/components/formeditor/formeditoritem.h b/src/plugins/qmldesigner/components/formeditor/formeditoritem.h index 8569059b7a..a2a491fdbf 100644 --- a/src/plugins/qmldesigner/components/formeditor/formeditoritem.h +++ b/src/plugins/qmldesigner/components/formeditor/formeditoritem.h @@ -66,6 +66,9 @@ public: int radius; int bezier; ConnectionType type; + qreal labelOffset; + qreal labelPosition; + bool labelFlipSide; }; class QMLDESIGNERCORE_EXPORT FormEditorItem : public QGraphicsItem diff --git a/src/plugins/qmldesigner/components/propertyeditor/propertyeditorqmlbackend.cpp b/src/plugins/qmldesigner/components/propertyeditor/propertyeditorqmlbackend.cpp index 171046b4ae..6dbb0d471c 100644 --- a/src/plugins/qmldesigner/components/propertyeditor/propertyeditorqmlbackend.cpp +++ b/src/plugins/qmldesigner/components/propertyeditor/propertyeditorqmlbackend.cpp @@ -179,6 +179,10 @@ QVariant properDefaultAuxiliaryProperties(const QmlObjectNode &qmlObjectNode, return 50; else if (propertyName == "bezier") return 50; + else if (propertyName == "labelPosition") + return 50.0; + else if (propertyName == "labelFlipSide") + return false; else if (propertyName == "customId") return QString(); else if (propertyName == "joinConnection") @@ -248,7 +252,7 @@ void PropertyEditorQmlBackend::setupAuxiliaryProperties(const QmlObjectNode &qml propertyNames.append("customId"); if (itemNode.isFlowTransition()) { - propertyNames.append({"color", "width", "inOffset", "outOffset", "dash", "breakPoint", "type", "radius", "bezier"}); + propertyNames.append({"color", "width", "inOffset", "outOffset", "dash", "breakPoint", "type", "radius", "bezier", "labelPosition", "labelFlipSide"}); } else if (itemNode.isFlowItem()) { propertyNames.append({"color", "width", "inOffset", "outOffset", "joinConnection"}); } else if (itemNode.isFlowActionArea()) { |