-
Notifications
You must be signed in to change notification settings - Fork 2
/
Container.cpp
76 lines (66 loc) · 2.54 KB
/
Container.cpp
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
#include "Container.hpp"
#include <QGridLayout>
#include <QPainter>
#include <QQuickWindow>
#include <QTimer>
using namespace std::chrono_literals;
QQuickWidgetContainer::RenderW::RenderW(QQuickWidgetContainer *parent)
{
_parent = parent;
setLayout(new QGridLayout);
layout()->setSpacing(0);
layout()->addWidget(parent->contentItem);
setContentsMargins(0, 0, 0, 0);
QTimer::singleShot(100ms, this, qOverload<>(&RenderW::update));
}
void QQuickWidgetContainer::RenderW::paintEvent(QPaintEvent *)
{
QPainter painter{ this };
const auto position = mapFromParent(pos());
const auto &[rx, ry] = std::tuple(position.x(), position.y());
painter.drawImage(rx, ry, _parent->window()->grabWindow(), x(), y(), width() - rx, height() - ry);
painter.end();
}
bool QQuickWidgetContainer::RenderW::event(QEvent *event)
{
const auto events = { QEvent::MouseButtonRelease, QEvent::MouseButtonPress, QEvent::MouseButtonDblClick,
QEvent::MouseMove, QEvent::UpdateRequest, QEvent::KeyPress,
QEvent::KeyRelease, QEvent::FocusIn, QEvent::FocusOut };
if (std::find(events.begin(), events.end(), event->type()) != events.end())
update();
return QWidget::event(event);
}
QQuickWidgetContainer::QQuickWidgetContainer(QQuickItem *parent) : QQuickItem(parent)
{
setAcceptedMouseButtons(Qt::AllButtons);
setAcceptHoverEvents(Qt::AllButtons);
setAcceptTouchEvents(true);
setFocus(true);
}
QQuickWidgetContainer::~QQuickWidgetContainer()
{
delete renderer;
}
void QQuickWidgetContainer::setupWidget(QWidget *widget)
{
contentItem = widget;
renderer = new RenderW(this);
renderer->setContentsMargins(0, 0, 0, 0);
renderer->winId();
renderer->windowHandle()->setParent(window());
renderer->show();
const auto resizeToParent = [this]()
{
const auto dw = width() == 0 ? renderer->minimumSizeHint().width() : width();
const auto dh = height() == 0 ? renderer->minimumSizeHint().height() : height();
const auto dx = parentItem()->x() + x();
const auto dy = parentItem()->y() + y();
renderer->setGeometry(dx, dy, dw, dh);
renderer->update();
};
connect(this, &QQuickWidgetContainer::widthChanged, this, resizeToParent);
connect(this, &QQuickWidgetContainer::heightChanged, this, resizeToParent);
connect(this, &QQuickWidgetContainer::xChanged, this, resizeToParent);
connect(this, &QQuickWidgetContainer::yChanged, this, resizeToParent);
resizeToParent();
}