summaryrefslogtreecommitdiffstats
path: root/shared/native_view_container_qt.h
blob: 6937d146d95a6ce759c207a89a9d307c61dbadc9 (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
#ifndef NATIVE_VIEW_CONTAINER_QT_H
#define NATIVE_VIEW_CONTAINER_QT_H

#include "native_view_qt.h"

#include <QWindow>
#include <QVBoxLayout>
#include <QQuickItem>

class NativeViewContainerQt : public QObject
{
    Q_OBJECT
public:
    NativeViewContainerQt()
        : m_embeddable(0)
        , m_currentQQuickNativeView(0)
        , m_currentQWidgetNativeView(0)
        , m_isQQuick(false)
    {
    }

    QQuickItem* qQuickItem()
    {
        if (!m_embeddable) {
            QQuickItem* embeddable = new QQuickItem;
            m_isQQuick = true;
            connect(embeddable, SIGNAL(widthChanged()), this, SLOT(resized()));
            connect(embeddable, SIGNAL(heightChanged()), this, SLOT(resized()));
            m_embeddable = embeddable;
        }

        return static_cast<QQuickItem*>(m_embeddable);
    }

    QVBoxLayout* widget()
    {
        if (!m_embeddable)
            m_embeddable = new QVBoxLayout;
        return static_cast<QVBoxLayout*>(m_embeddable);
    }

    void setWidth(qreal width)
    {
        if (m_isQQuick && m_currentQQuickNativeView) {
            m_currentQQuickNativeView->setWidth(width);
            m_currentQQuickNativeView->setContentsSize(QSize(width, m_currentQQuickNativeView->height()));
            qQuickItem()->setWidth(width);
        }
    }

    void setHeight(qreal height)
    {
        if (m_isQQuick && m_currentQQuickNativeView) {
            m_currentQQuickNativeView->setHeight(height);
            m_currentQQuickNativeView->setContentsSize(QSize(m_currentQQuickNativeView->width(), height));
            qQuickItem()->setHeight(height);
        }
    }

    NativeViewQt* createNativeView(content::RenderWidgetHostViewQt* renderWidgetHostView)
    {
        if (m_isQQuick) {
            insert(new QQuickNativeView(renderWidgetHostView));
            return m_currentQQuickNativeView;
        }

        insert(new QWidgetNativeView(renderWidgetHostView));
        return m_currentQWidgetNativeView;
    }

protected:
    void insert(QWidgetNativeView* nativeView)
    {
        widget()->removeWidget(m_currentQWidgetNativeView);
        widget()->addWidget(nativeView);
        m_currentQWidgetNativeView = nativeView;
    }

    void insert(QQuickNativeView* nativeView)
    {
        fprintf(stderr, "replace: %p with %p\n", m_currentQQuickNativeView, nativeView);
        if (m_currentQQuickNativeView)
            m_currentQQuickNativeView->setParentItem(0);

        nativeView->setParentItem(qQuickItem());
        m_currentQQuickNativeView = nativeView;
        setWidth(qQuickItem()->width());
        setHeight(qQuickItem()->height());
    }

public Q_SLOTS:
    void resized()
    {
        int w = static_cast<unsigned int>(qQuickItem()->width());
        int h = static_cast<unsigned int>(qQuickItem()->height());
        if (m_currentQQuickNativeView)
            m_currentQQuickNativeView->resize(w, h);
    }

private:
    QObject* m_embeddable;
    QWidgetNativeView* m_currentQWidgetNativeView;
    QQuickNativeView* m_currentQQuickNativeView;
    bool m_isQQuick;
};

#endif