| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the Qt Quick Extras module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #include "qquicktexthandle.h" |
| 41 | #include <QPainterPath> |
| 42 | |
| 43 | #include <QPainterPath> |
| 44 | |
| 45 | QQuickTextHandle::QQuickTextHandle(QQuickItem *parent) : |
| 46 | QQuickPaintedItem(parent) |
| 47 | { |
| 48 | setAntialiasing(true); |
| 49 | } |
| 50 | |
| 51 | QQuickTextHandle::~QQuickTextHandle() |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | void QQuickTextHandle::paint(QPainter *painter) |
| 56 | { |
| 57 | painter->save(); |
| 58 | paintBulb(painter, color: QColor(0, 0, 0, 38), isShadow: true); |
| 59 | painter->restore(); |
| 60 | |
| 61 | paintBulb(painter, color: mColor, isShadow: false); |
| 62 | painter->fillRect(x: (mType == SelectionHandle ? 10 : 1), y: 0, w: 2, h: 23, b: mColor); |
| 63 | } |
| 64 | |
| 65 | QQuickTextHandle::TextHandleType QQuickTextHandle::type() const |
| 66 | { |
| 67 | return mType; |
| 68 | } |
| 69 | |
| 70 | void QQuickTextHandle::setType(QQuickTextHandle::TextHandleType type) |
| 71 | { |
| 72 | if (mType != type) { |
| 73 | mType = type; |
| 74 | update(); |
| 75 | emit typeChanged(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | QColor QQuickTextHandle::color() const |
| 80 | { |
| 81 | return mColor; |
| 82 | } |
| 83 | |
| 84 | void QQuickTextHandle::setColor(const QColor &color) |
| 85 | { |
| 86 | if (mColor != color) { |
| 87 | mColor = color; |
| 88 | update(); |
| 89 | emit colorChanged(); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | qreal QQuickTextHandle::scaleFactor() const |
| 94 | { |
| 95 | return mScaleFactor; |
| 96 | } |
| 97 | |
| 98 | void QQuickTextHandle::setScaleFactor(qreal scaleFactor) |
| 99 | { |
| 100 | if (mScaleFactor != scaleFactor) { |
| 101 | mScaleFactor = scaleFactor; |
| 102 | setImplicitWidth(qRound(d: 28 * mScaleFactor)); |
| 103 | // + 2 for shadows |
| 104 | setImplicitHeight(qRound(d: (32 + 2) * mScaleFactor)); |
| 105 | update(); |
| 106 | emit scaleFactorChanged(); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | void QQuickTextHandle::paintBulb(QPainter *painter, const QColor &color, bool isShadow) |
| 111 | { |
| 112 | painter->scale(sx: mScaleFactor, sy: mScaleFactor); |
| 113 | QPainterPath path; |
| 114 | |
| 115 | if (mType == SelectionHandle) { |
| 116 | painter->translate(dx: 16, dy: isShadow ? 2 : 0); |
| 117 | path.moveTo(x: 10.242, y: 28.457); |
| 118 | path.cubicTo(ctrlPt1x: 7.8980000000000015, ctrlPt1y: 30.799, ctrlPt2x: 4.099000000000001, ctrlPt2y: 30.799,endPtx: 1.7560000000000002, endPty: 28.457); |
| 119 | path.cubicTo(ctrlPt1x: -0.5859999999999999, ctrlPt1y: 26.115000000000002, ctrlPt2x: -0.5859999999999999,ctrlPt2y: 22.314, endPtx: 1.7560000000000002, endPty: 19.973); |
| 120 | path.cubicTo(ctrlPt1x: 4.748, ctrlPt1y: 16.980999999999998, ctrlPt2x: 11.869, ctrlPt2y: 18.343999999999998, endPtx: 11.869, endPty: 18.343999999999998); |
| 121 | path.cubicTo(ctrlPt1x: 11.869, ctrlPt1y: 18.343999999999998, ctrlPt2x: 13.244, ctrlPt2y: 25.455, endPtx: 10.242, endPty: 28.457); |
| 122 | |
| 123 | } else { |
| 124 | if (isShadow) |
| 125 | painter->translate(dx: 0, dy: 2); |
| 126 | path.moveTo(x: 2.757,y: 28.457); |
| 127 | path.cubicTo(ctrlPt1x: 5.101,ctrlPt1y: 30.799,ctrlPt2x: 8.899000000000001,ctrlPt2y: 30.799,endPtx: 11.243,endPty: 28.457); |
| 128 | path.cubicTo(ctrlPt1x: 13.585,ctrlPt1y: 26.115000000000002,ctrlPt2x: 13.585,ctrlPt2y: 22.314,endPtx: 11.243,endPty: 19.973); |
| 129 | path.cubicTo(ctrlPt1x: 8.251,ctrlPt1y: 16.98,ctrlPt2x: 1.13,ctrlPt2y: 18.344,endPtx: 1.13,endPty: 18.344); |
| 130 | path.cubicTo(ctrlPt1x: 1.13,ctrlPt1y: 18.344,ctrlPt2x: -0.245,ctrlPt2y: 25.455,endPtx: 2.757,endPty: 28.457); |
| 131 | } |
| 132 | |
| 133 | painter->fillPath(path, brush: QBrush(color)); |
| 134 | } |
| 135 | |
| 136 | |