| 1 | #include "TipBackgroundBox.hxx" |
| 2 | |
| 3 | #include <QPainter> |
| 4 | #include <QPainterPath> |
| 5 | #include <QDebug> |
| 6 | |
| 7 | namespace { |
| 8 | |
| 9 | void pathLineBy(QPainterPath& pp, double x, double y) |
| 10 | { |
| 11 | const auto c = pp.currentPosition(); |
| 12 | pp.lineTo(c.x() + x, c.y() + y); |
| 13 | } |
| 14 | |
| 15 | const double arrowDim1 = 20.0; |
| 16 | const double arrowEndOffset = 30; |
| 17 | |
| 18 | QPainterPath pathFromArrowAndGeometry(GettingStartedTip::Arrow arrow, const QRectF& g) |
| 19 | { |
| 20 | QPainterPath pp; |
| 21 | |
| 22 | switch (arrow) { |
| 23 | case GettingStartedTip::Arrow::TopCenter: |
| 24 | pp.moveTo(g.center().x(), 0.0); |
| 25 | pp.lineTo(g.center().x() + arrowDim1, arrowDim1); |
| 26 | pp.lineTo(g.right(), arrowDim1); |
| 27 | pp.lineTo(g.right(), g.bottom()); |
| 28 | pp.lineTo(g.left(), g.bottom()); |
| 29 | pp.lineTo(g.left(), arrowDim1); |
| 30 | pp.lineTo(g.center().x() - arrowDim1, arrowDim1); |
| 31 | pp.closeSubpath(); |
| 32 | break; |
| 33 | |
| 34 | case GettingStartedTip::Arrow::BottomRight: |
| 35 | pp.moveTo(g.right() - arrowEndOffset, g.bottom()); |
| 36 | pathLineBy(pp, arrowDim1, -arrowDim1); |
| 37 | pp.lineTo(g.right(), g.bottom() - arrowDim1); |
| 38 | pp.lineTo(g.right(), g.top()); |
| 39 | pp.lineTo(g.left(), g.top()); |
| 40 | pp.lineTo(g.left(), g.bottom() - arrowDim1); |
| 41 | pp.lineTo(g.right() - arrowEndOffset - arrowDim1, g.bottom() - arrowDim1); |
| 42 | pp.closeSubpath(); |
| 43 | break; |
| 44 | |
| 45 | case GettingStartedTip::Arrow::TopRight: |
| 46 | pp.moveTo(g.right() - arrowEndOffset, 0.0); |
| 47 | pathLineBy(pp, arrowDim1, arrowDim1); |
| 48 | pp.lineTo(g.right(), arrowDim1); |
| 49 | pp.lineTo(g.right(), g.bottom()); |
| 50 | pp.lineTo(g.left(), g.bottom()); |
| 51 | pp.lineTo(g.left(), arrowDim1); |
| 52 | pp.lineTo(g.right() - (arrowEndOffset + arrowDim1), arrowDim1); |
| 53 | pp.closeSubpath(); |
| 54 | break; |
| 55 | |
| 56 | case GettingStartedTip::Arrow::TopLeft: |
| 57 | pp.moveTo(arrowEndOffset, 0.0); |
| 58 | pathLineBy(pp, arrowDim1, arrowDim1); |
| 59 | pp.lineTo(g.right(), arrowDim1); |
| 60 | pp.lineTo(g.right(), g.bottom()); |
| 61 | pp.lineTo(g.left(), g.bottom()); |
| 62 | pp.lineTo(g.left(), arrowDim1); |
| 63 | pp.lineTo(arrowEndOffset - arrowDim1, arrowDim1); |
| 64 | pp.closeSubpath(); |
| 65 | break; |
| 66 | |
| 67 | case GettingStartedTip::Arrow::LeftCenter: |
| 68 | pp.moveTo(0.0, g.center().y()); |
| 69 | pathLineBy(pp, arrowDim1, -arrowDim1); |
| 70 | pp.lineTo(arrowDim1, g.top()); |
| 71 | pp.lineTo(g.right(), g.top()); |
| 72 | pp.lineTo(g.right(), g.bottom()); |
| 73 | pp.lineTo(arrowDim1, g.bottom()); |
| 74 | pp.lineTo(arrowDim1, g.center().y() + arrowDim1); |
| 75 | pp.closeSubpath(); |
| 76 | break; |
| 77 | |
| 78 | case GettingStartedTip::Arrow::RightCenter: |
| 79 | pp.moveTo(g.right(), g.center().y()); |
| 80 | pathLineBy(pp, -arrowDim1, arrowDim1); |
| 81 | pp.lineTo(g.right() - arrowDim1, g.bottom()); |
| 82 | pp.lineTo(g.left(), g.bottom()); |
| 83 | pp.lineTo(g.left(), g.top()); |
| 84 | pp.lineTo(g.right() - arrowDim1, g.top()); |
| 85 | pp.lineTo(g.right() - arrowDim1, g.center().y() - arrowDim1); |
| 86 | pp.closeSubpath(); |
| 87 | break; |
| 88 | |
| 89 | |
| 90 | case GettingStartedTip::Arrow::LeftTop: |
| 91 | pp.moveTo(0.0, g.top() + arrowDim1); |
| 92 | pathLineBy(pp, arrowDim1, -arrowDim1); |
| 93 | pp.lineTo(g.right(), g.top()); |
| 94 | pp.lineTo(g.right(), g.bottom()); |
| 95 | pp.lineTo(g.left() + arrowDim1, g.bottom()); |
| 96 | pp.lineTo(g.left() + arrowDim1, -g.left() + arrowDim1); |
| 97 | pp.closeSubpath(); |
| 98 | break; |
| 99 | |
| 100 | case GettingStartedTip::Arrow::NoArrow: |
| 101 | pp.moveTo(0.0, g.top()); |
| 102 | pp.lineTo(g.right(), g.top()); |
| 103 | pp.lineTo(g.right(), g.bottom()); |
| 104 | pp.lineTo(0.0, g.bottom()); |
| 105 | pp.closeSubpath(); |
| 106 | break; |
| 107 | |
| 108 | |
| 109 | default: |
| 110 | qWarning() << Q_FUNC_INFO << "unhandled:" << arrow; |
| 111 | break; |
| 112 | } |
| 113 | |
| 114 | return pp; |
| 115 | } |
| 116 | |
| 117 | |
| 118 | } // of anonymous |
| 119 | |
| 120 | int TipBackgroundBox::arrowSideOffset() |
| 121 | { |
| 122 | return static_cast<int>(arrowEndOffset); |
| 123 | } |
| 124 | |
| 125 | int TipBackgroundBox::arrowHeight() |
| 126 | { |
| 127 | return static_cast<int>(arrowDim1); |
| 128 | } |
| 129 | |
| 130 | TipBackgroundBox::TipBackgroundBox(QQuickItem* pr) : |
| 131 | QQuickPaintedItem(pr) |
| 132 | { |
| 133 | } |
| 134 | |
| 135 | GettingStartedTip::Arrow TipBackgroundBox::arrowPosition() const |
| 136 | { |
| 137 | return _arrow; |
| 138 | } |
| 139 | |
| 140 | QColor TipBackgroundBox::borderColor() const |
| 141 | { |
| 142 | return _borderColor; |
| 143 | } |
| 144 | |
| 145 | int TipBackgroundBox::borderWidth() const |
| 146 | { |
| 147 | return _borderWidth; |
| 148 | } |
| 149 | |
| 150 | QColor TipBackgroundBox::fill() const |
| 151 | { |
| 152 | return _fill; |
| 153 | } |
| 154 | |
| 155 | void TipBackgroundBox::setArrowPosition(GettingStartedTip::Arrow arrow) |
| 156 | { |
| 157 | if (_arrow == arrow) |
| 158 | return; |
| 159 | |
| 160 | _arrow = arrow; |
| 161 | emit arrowPositionChanged(_arrow); |
| 162 | update(); |
| 163 | } |
| 164 | |
| 165 | void TipBackgroundBox::setBorderColor(QColor borderColor) |
| 166 | { |
| 167 | if (_borderColor == borderColor) |
| 168 | return; |
| 169 | |
| 170 | _borderColor = borderColor; |
| 171 | emit borderColorChanged(_borderColor); |
| 172 | update(); |
| 173 | } |
| 174 | |
| 175 | void TipBackgroundBox::setBorderWidth(int borderWidth) |
| 176 | { |
| 177 | if (_borderWidth == borderWidth) |
| 178 | return; |
| 179 | |
| 180 | _borderWidth = borderWidth; |
| 181 | emit borderWidthChanged(_borderWidth); |
| 182 | update(); |
| 183 | } |
| 184 | |
| 185 | void TipBackgroundBox::setFill(QColor fill) |
| 186 | { |
| 187 | if (_fill == fill) |
| 188 | return; |
| 189 | |
| 190 | _fill = fill; |
| 191 | emit fillChanged(_fill); |
| 192 | update(); |
| 193 | } |
| 194 | |
| 195 | void TipBackgroundBox::paint(QPainter *painter) |
| 196 | { |
| 197 | QPainterPath pp = pathFromArrowAndGeometry(_arrow, QRectF{0.0,0.0,width(), height()}); |
| 198 | painter->setBrush(_fill); |
| 199 | painter->setPen(QPen{_borderColor, static_cast<double>(_borderWidth)}); |
| 200 | painter->drawPath(pp); |
| 201 | } |