aboutsummaryrefslogtreecommitdiffstats
path: root/src/widgets/windowwidget.cpp
blob: 2cfd3c3b9852028cd2df32e6227f684c5cd28876 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/****************************************************************************
**
** Copyright (C) 2016 Pelagicore AG
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QmlLive tool.
**
** $QT_BEGIN_LICENSE:GPL-QTAS$
** Commercial License Usage
** Licensees holding valid commercial Qt Automotive Suite licenses may use
** this file in accordance with the commercial license agreement provided
** with the Software or, alternatively, in accordance with the terms
** contained in a written agreement between you and The Qt Company.  For
** licensing terms and conditions see https://www.qt.io/terms-conditions.
** For further information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
** SPDX-License-Identifier: GPL-3.0
**
****************************************************************************/

#include "windowwidget.h"
#include <QQuickView>
#include <QQuickItem>
#include <QGuiApplication>
#include <QDebug>
#include <QScrollBar>
#include <QResizeEvent>

WindowWidget::WindowWidget(QWidget *parent) :
    QAbstractScrollArea(parent), m_hostedWindow(0), m_resizing(false), m_centering(false)
{
    setFrameShape(QFrame::NoFrame);
}

void WindowWidget::setHostedWindow(QQuickWindow *hostedWindow)
{
    if (m_hostedWindow == hostedWindow)
        return;

    if (m_hostedWindow) {
        m_hostedWindow->removeEventFilter(this);
        m_hostedWindow->hide();
        m_hostedWindow->setParent(0);
    }

    m_hostedWindow = hostedWindow;

    if (m_hostedWindow) {
        m_hostedWindow->installEventFilter(this);

        // force creation of the TL window in order to get its handle
        if (!testAttribute(Qt::WA_WState_Created))
            create(0, true, true);

        if (!windowHandle())
            qWarning("Could not get a valid windowhandle for our widget based window");

        // Ensure the native platform resources are not allocated before
        // embedding the window. Otherwise it cannot be embedded reliably.
        //
        // For some reason this only happens with QQuickWindow (or the subclass
        // actually instantiated by QML Window element) and not with QQuickView.
        //
        // At least one unfortunate side effect exists: geometry is not
        // preserved due to the way it is stored (see QWindow::resize(QSize))
        // while this is needed for the QQuickView's auto resizing to work
        // correctly.
        //
        // This is the rationale why it is done and why it is done conditionally.
        //
        // Another options would be:
        //
        // a) Remember and reset the geometry here
        // b) Place the QWindow::destroy() call directly after loading a
        //    QQuickWindow from QML in LiveNodeEngine::reloadDocument()
        // c) Eliminate what makes this only happen with QQuickWindow loaded
        //    from QML
        if (!qobject_cast<QQuickView *>(m_hostedWindow)) {
            m_hostedWindow->destroy();
        }

        m_hostedWindow->setFlags(Qt::Tool | Qt::FramelessWindowHint);
        m_hostedWindow->setParent(windowHandle());
        m_hostedWindow->setVisible(isVisible());

        viewport()->setBackgroundRole(backgroundRole());
        viewport()->setPalette(palette());
        viewport()->setBackgroundRole(QPalette::Background);
        setFocusPolicy(Qt::StrongFocus);
        m_hostedWindow->show();
    }
}

QQuickWindow *WindowWidget::hostedWindow() const
{
    return m_hostedWindow;
}

void WindowWidget::setVisible(bool visible)
{
    QAbstractScrollArea::setVisible(visible);
    if (m_hostedWindow)
        m_hostedWindow->setVisible(visible);
}

void WindowWidget::setCenteringEnabled(bool enabled)
{
    m_centering = enabled;
    updateScrollBars();
}

QSize WindowWidget::sizeHint() const
{
    QSize s = qmlSize();

    return s + QSize(frameWidth() * 2, frameWidth() * 2);
}

void WindowWidget::forceInitialResize()
{
    if (QQuickView *view = qobject_cast<QQuickView *>(m_hostedWindow)) {
        if (view->resizeMode() == QQuickView::SizeRootObjectToView)
            view->resize(size());
    }
    updateGeometry();
    updateScrollBars();
    emit widthChanged(width());
    emit heightChanged(height());

}

bool WindowWidget::event(QEvent *e)
{
    bool handled = false;

    if (m_hostedWindow) {
        switch (e->type()) {
        case QEvent::Wheel: {
            QWheelEvent *oe = static_cast<QWheelEvent *>(e);
            if (!viewport()->geometry().contains(oe->pos()))
                break;

            QWheelEvent ne(m_hostedWindow->mapFromGlobal(oe->globalPos()),
                           oe->globalPos(),
                           oe->pixelDelta(),
                           oe->angleDelta(),
                           oe->delta(),
                           oe->orientation(),
                           oe->buttons(),
                           oe->modifiers());

            qGuiApp->sendEvent(m_hostedWindow, &ne);
            handled = true;
            break;
        }
        case QEvent::KeyPress:
        case QEvent::KeyRelease: {
            qGuiApp->sendEvent(m_hostedWindow, e);
            handled = true;
            break;
        }
        case QEvent::MouseButtonPress:
        case QEvent::MouseButtonRelease:
        case QEvent::MouseButtonDblClick:
        case QEvent::MouseMove: {
            QMouseEvent *oe = static_cast<QMouseEvent *>(e);

            if (e->type() == QEvent::MouseButtonPress || e->type() == QEvent::MouseButtonDblClick) {
                if (!viewport()->geometry().contains(oe->pos()))
                    break;
            }

            QMouseEvent ne(oe->type(),
                           m_hostedWindow->mapFromGlobal(oe->globalPos()),
                           m_hostedWindow->mapFromGlobal(oe->globalPos()),
                           oe->globalPos(),
                           oe->button(),
                           oe->buttons(),
                           oe->modifiers());

            qGuiApp->sendEvent(m_hostedWindow, &ne);
            handled = true;
            break;
        }
        case QEvent::Resize: {
            QResizeEvent *re = static_cast<QResizeEvent*>(e);
            if (QQuickView *view = qobject_cast<QQuickView *>(m_hostedWindow)) {
                if (view->resizeMode() == QQuickView::SizeRootObjectToView)
                    view->resize(re->size());
            }
            updateGeometry();
            updateScrollBars();
            if (re->oldSize().width() != re->size().width())
                emit widthChanged(re->size().width());
            if (re->oldSize().height() != re->size().height())
                emit heightChanged(re->size().height());
            break;
        }
        case QEvent::Hide:
            m_hostedWindow->close();
            break;
        default:
            break;
        }
    }
    return handled ? true : QAbstractScrollArea::event(e);
}

bool WindowWidget::eventFilter(QObject *o, QEvent *e)
{
    if (o && o == m_hostedWindow) {
        switch (e->type()) {
        case QEvent::Resize:
            updateScrollBars();
            break;
        default:
            break;
        }
    }
    return QAbstractScrollArea::eventFilter(o, e);
}

void WindowWidget::scrollContentsBy(int dx, int dy)
{
    Q_UNUSED(dx)
    Q_UNUSED(dy)

    updateWindowPosition();
}

#include <QStyle>

void WindowWidget::updateScrollBars()
{
    if (!m_hostedWindow)
        return;

    if (window() && !m_resizing) {
        m_resizing = true;

        QSize vpSize = viewport()->size();
        QSize vpMaxSize = maximumViewportSize();
        QSize wSize = qmlSize();

        // does the window fit without scrollbars?
        if (vpMaxSize.expandedTo(wSize) == vpMaxSize)
            vpSize = vpMaxSize;

        // fix scrollbars
        horizontalScrollBar()->setRange(0, (wSize - vpSize).width());
        horizontalScrollBar()->setPageStep(vpSize.width());
        verticalScrollBar()->setRange(0, (wSize - vpSize).height());
        verticalScrollBar()->setPageStep(vpSize.height());

        //qWarning() << "wSize:" << wSize << "vpSize:" << vpSize << "vpRect" << viewport()->rect();
        //qWarning() << "Hrange:" << (wSize - vpSize).width() << "Hstep:" << vpSize.width();

        updateWindowPosition();
        m_resizing = false;
    }
}
void WindowWidget::updateWindowPosition()
{
    if (m_hostedWindow && !m_hostedWindow->contentItem()->childItems().isEmpty()) {
        QSize wSize = qmlSize();
        Qt::LayoutDirection dir = layoutDirection();
        QRect scrolled = QStyle::visualRect(dir, viewport()->rect(), QRect(QPoint(-horizontalScrollBar()->value(), -verticalScrollBar()->value()), wSize));
        QRect aligned = QStyle::alignedRect(dir, m_centering ? Qt::AlignCenter : Qt::AlignTop | Qt::AlignLeft, wSize, viewport()->rect());

        //qWarning() << "xAlign:" << aligned.x() << "xScrolled:" << scrolled.x();
        m_hostedWindow->setPosition(wSize.width() < viewport()->width() ? aligned.x() : scrolled.x(),
                                    wSize.height() < viewport()->height() ? aligned.y() : scrolled.y());
    }
}

QSize WindowWidget::qmlSize() const
{
    QSize s;
    if (m_hostedWindow && !m_hostedWindow->contentItem()->childItems().isEmpty()) {
        QQuickItem *const rootItem = m_hostedWindow->contentItem()->childItems().first();
        s = QSize(rootItem->width(), rootItem->height());
    }

    if (s.width() <= 20)
        s.setWidth(800);
    if (s.height() <= 20)
        s.setHeight(600);

    return s;
}