| 1 | // Copyright (C) 2017 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qquickuniversalfocusrectangle_p.h" |
| 5 | |
| 6 | #include <QtGui/qpixmap.h> |
| 7 | #include <QtGui/qpainter.h> |
| 8 | #include <QtGui/qpixmapcache.h> |
| 9 | #include <QtQuick/private/qquickitem_p.h> |
| 10 | |
| 11 | QT_BEGIN_NAMESPACE |
| 12 | |
| 13 | QQuickUniversalFocusRectangle::QQuickUniversalFocusRectangle(QQuickItem *parent) |
| 14 | : QQuickPaintedItem(parent) |
| 15 | { |
| 16 | QQuickItemPrivate::get(item: this)->setTransparentForPositioner(true); |
| 17 | } |
| 18 | |
| 19 | void QQuickUniversalFocusRectangle::paint(QPainter *painter) |
| 20 | { |
| 21 | if (!isVisible() || width() <= 0 || height() <= 0) |
| 22 | return; |
| 23 | |
| 24 | QRect bounds = boundingRect().toAlignedRect(); |
| 25 | const int boundsWidth = bounds.width(); |
| 26 | const int boundsHeight = bounds.width(); |
| 27 | const QString key = QStringLiteral("qquickuniversalfocusrectangle_%1_%2" ).arg(args: QString::number(boundsWidth), args: QString::number(boundsHeight)); |
| 28 | |
| 29 | QPixmap pixmap(boundsWidth, boundsHeight); |
| 30 | if (!QPixmapCache::find(key, pixmap: &pixmap)) { |
| 31 | bounds.adjust(dx1: 0, dy1: 0, dx2: -1, dy2: -1); |
| 32 | pixmap.fill(fillColor: Qt::transparent); |
| 33 | QPainter p(&pixmap); |
| 34 | |
| 35 | QPen pen; |
| 36 | pen.setWidth(1); |
| 37 | pen.setColor(Qt::white); |
| 38 | p.setPen(pen); |
| 39 | p.drawRect(r: bounds); |
| 40 | |
| 41 | pen.setColor(Qt::black); |
| 42 | pen.setDashPattern(QList<qreal>(2, 1)); |
| 43 | p.setPen(pen); |
| 44 | p.drawRect(r: bounds); |
| 45 | |
| 46 | QPixmapCache::insert(key, pixmap); |
| 47 | } |
| 48 | painter->drawPixmap(x: 0, y: 0, pm: pixmap); |
| 49 | } |
| 50 | |
| 51 | QT_END_NAMESPACE |
| 52 | |
| 53 | #include "moc_qquickuniversalfocusrectangle_p.cpp" |
| 54 | |