summaryrefslogtreecommitdiffstats
path: root/tests/manual/windowmask/main.cpp
blob: 3055e18a4258520cbbc9dae6e095e61483fb98d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include <QtWidgets>

template<typename Paintable>
class Circle : public Paintable
{
public:
    using Paintable::setMinimumSize;
    using Paintable::setMaximumSize;
    using Paintable::width;
    using Paintable::height;
    using Paintable::screen;
    using Paintable::devicePixelRatio;
    using Paintable::metaObject;
    using Paintable::setMask;
    using Paintable::frameGeometry;
    using Paintable::setPosition;
    using Paintable::startSystemMove;
    using Paintable::setFlags;
    using Paintable::requestUpdate;

    Circle()
    {
        setMinimumSize({200, 200});
        setMaximumSize({200, 200});
        setFlags(Qt::Window | Qt::FramelessWindowHint);
    }

protected:
    void paintEvent(QPaintEvent *) override
    {
        qWarning() << "Painting into a" << this << "with DPR" << devicePixelRatio()
                    << "on a screen with DPR" << screen()->devicePixelRatio();
        QPainter painter(static_cast<Paintable *>(this));
        painter.fillRect(0, 0, width(), height(), devicePixelRatio() == 1 ? Qt::red : Qt::green);
        painter.setPen(QPen(Qt::black, 5));
        painter.drawRect(10, 10, width() - 20, height() - 20);
        painter.drawText(0, 0, width(), height(), Qt::AlignHCenter | Qt::AlignVCenter, metaObject()->className());
    }
    void mousePressEvent(QMouseEvent *event) override
    {
        if (event->button() == Qt::LeftButton) {
            if (event->modifiers() & Qt::ControlModifier)
                requestUpdate();
            else if (event->modifiers() & Qt::AltModifier)
                updateMask();
            else if (event->modifiers() & Qt::ShiftModifier && startSystemMove())
                dragPosition = {};
            else
                dragPosition = event->globalPosition() - frameGeometry().topLeft();
        }
    }
    void mouseMoveEvent(QMouseEvent *event) override
    {
        if (event->buttons() & Qt::LeftButton && !dragPosition.isNull())
            setPosition((event->globalPosition() - dragPosition).toPoint());
    }
    void resizeEvent(QResizeEvent *) override
    {
        updateMask();
    }

    void updateMask()
    {
        int side = qMin(width(), height());
        QRegion maskedRegion(width() / 2 - side / 2, height() / 2 - side / 2, side,
                            side, QRegion::Ellipse);
        qDebug() << "Updating mask for" << this << "to" << maskedRegion.boundingRect();
        setMask(maskedRegion);
    }

    QPointF dragPosition;
};

class WindowLikeWidget : public QWidget
{
public:
    void setPosition(const QPoint &point) { QWidget::move(point); }
    bool startSystemMove() { return windowHandle()->startSystemMove(); }
    void setFlags(Qt::WindowFlags flags) { setWindowFlags(flags); }
    void requestUpdate() { update(); }
};

class Widget : public Circle<WindowLikeWidget>
{
public:
    Widget()
    {
        setAttribute(Qt::WA_TranslucentBackground);
    }
};

class Window : public Circle<QRasterWindow>
{
};

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    Widget widget;
    widget.show();

    Window window;
    window.show();

    auto dumpScreenData = [](QScreen *screen){
        qDebug() << "- name:" << screen->name();
        qDebug() << "- dpr :" << screen->devicePixelRatio();
    };

    QObject::connect(widget.windowHandle(), &QWindow::screenChanged, &widget, [&]{
        qDebug() << "Screen changed for" << &widget;
        dumpScreenData(widget.screen());
    });
    QObject::connect(&window, &QWindow::screenChanged, &window, [&]{
        qDebug() << "Screen changed for" << &window;
        dumpScreenData(window.screen());
    });

    return app.exec();
}