aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/utils/overlaywidget.cpp
blob: ffb3b211b0683c5560ff37ade64c82a74e377566 (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
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "overlaywidget.h"

#include "qtcassert.h"

#include <QEvent>
#include <QPainter>

Utils::OverlayWidget::OverlayWidget(QWidget *parent)
{
    setAttribute(Qt::WA_TransparentForMouseEvents);
    if (parent)
        attachToWidget(parent);
}

void Utils::OverlayWidget::setPaintFunction(const Utils::OverlayWidget::PaintFunction &paint)
{
    m_paint = paint;
}

bool Utils::OverlayWidget::eventFilter(QObject *obj, QEvent *ev)
{
    if (obj == parent() && ev->type() == QEvent::Resize)
        resizeToParent();
    return QWidget::eventFilter(obj, ev);
}

void Utils::OverlayWidget::paintEvent(QPaintEvent *ev)
{
    if (m_paint) {
        QPainter p(this);
        m_paint(this, p, ev);
    }
}

void Utils::OverlayWidget::attachToWidget(QWidget *parent)
{
    if (parentWidget())
        parentWidget()->removeEventFilter(this);
    setParent(parent);
    if (parent) {
        parent->installEventFilter(this);
        resizeToParent();
        raise();
    }
}

void Utils::OverlayWidget::resizeToParent()
{
    QTC_ASSERT(parentWidget(), return );
    setGeometry(QRect(QPoint(0, 0), parentWidget()->size()));
}