| 1 | // Copyright (C) 2016 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 "qshapedpixmapdndwindow_p.h" |
| 5 | |
| 6 | #include "qplatformwindow.h" |
| 7 | |
| 8 | #include <QtGui/QPainter> |
| 9 | #include <QtGui/QCursor> |
| 10 | #include <QtGui/QGuiApplication> |
| 11 | #include <QtGui/QPalette> |
| 12 | #include <QtGui/QBitmap> |
| 13 | |
| 14 | QT_BEGIN_NAMESPACE |
| 15 | |
| 16 | QShapedPixmapWindow::QShapedPixmapWindow(QScreen *screen) |
| 17 | : m_useCompositing(true) |
| 18 | { |
| 19 | setScreen(screen); |
| 20 | QSurfaceFormat format; |
| 21 | format.setAlphaBufferSize(8); |
| 22 | setFormat(format); |
| 23 | setFlags(Qt::FramelessWindowHint | Qt::BypassWindowManagerHint |
| 24 | | Qt::WindowTransparentForInput | Qt::WindowDoesNotAcceptFocus); |
| 25 | } |
| 26 | |
| 27 | QShapedPixmapWindow::~QShapedPixmapWindow() |
| 28 | { |
| 29 | } |
| 30 | |
| 31 | void QShapedPixmapWindow::setPixmap(const QPixmap &pixmap) |
| 32 | { |
| 33 | m_pixmap = pixmap; |
| 34 | if (!m_useCompositing) { |
| 35 | const QBitmap mask = m_pixmap.mask(); |
| 36 | if (!mask.isNull()) { |
| 37 | if (!handle()) |
| 38 | create(); |
| 39 | if (auto platformWindow = handle()) { |
| 40 | const auto pixmapDpr = m_pixmap.devicePixelRatio(); |
| 41 | const auto winDpr = devicePixelRatio(); |
| 42 | const auto maskSize = (QSizeF(m_pixmap.size()) * winDpr / pixmapDpr).toSize(); |
| 43 | platformWindow->setMask(QBitmap::fromPixmap(pixmap: mask.scaled(s: maskSize))); |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | void QShapedPixmapWindow::setHotspot(const QPoint &hotspot) |
| 50 | { |
| 51 | m_hotSpot = hotspot; |
| 52 | } |
| 53 | |
| 54 | void QShapedPixmapWindow::paintEvent(QPaintEvent *) |
| 55 | { |
| 56 | if (!m_pixmap.isNull()) { |
| 57 | const QRect rect(QPoint(0, 0), size()); |
| 58 | QPainter painter(this); |
| 59 | if (m_useCompositing) |
| 60 | painter.setCompositionMode(QPainter::CompositionMode_Source); |
| 61 | else |
| 62 | painter.fillRect(rect, QGuiApplication::palette().base()); |
| 63 | painter.drawPixmap(r: rect, pm: m_pixmap); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | void QShapedPixmapWindow::updateGeometry(const QPoint &pos) |
| 68 | { |
| 69 | QSize size(1, 1); |
| 70 | if (!m_pixmap.isNull()) { |
| 71 | size = m_pixmap.deviceIndependentSize().toSize(); |
| 72 | } |
| 73 | setGeometry(QRect(pos - m_hotSpot, size)); |
| 74 | } |
| 75 | |
| 76 | QT_END_NAMESPACE |
| 77 | |
| 78 | #include "moc_qshapedpixmapdndwindow_p.cpp" |
| 79 |
