summaryrefslogtreecommitdiffstats
path: root/src/manager-lib/qmlinprocessapplicationmanagerwindow.cpp
blob: 0c7412b53fbb58ccdd5573832164c23437cf8fc9 (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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/****************************************************************************
**
** Copyright (C) 2018 Pelagicore AG
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Pelagicore Application Manager.
**
** $QT_BEGIN_LICENSE:LGPL-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 Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or 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.GPL2 and 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-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
** SPDX-License-Identifier: LGPL-3.0
**
****************************************************************************/

#include "logging.h"
#include "qmlinprocessapplicationmanagerwindow.h"
#include "inprocesssurfaceitem.h"
#include "qmlinprocessruntime.h"
#include <private/qqmlcomponentattached_p.h>

#include <QtQuick/private/qquickitem_p.h>

QT_BEGIN_NAMESPACE_AM

QmlInProcessApplicationManagerWindow::QmlInProcessApplicationManagerWindow(QObject *parent)
    : QObject(parent)
    , m_surfaceItem(new InProcessSurfaceItem)
{
    m_surfaceItem->setInProcessApplicationManagerWindow(this);

    connect(m_surfaceItem.data(), &QQuickItem::widthChanged,
            this, &QmlInProcessApplicationManagerWindow::widthChanged);

    connect(m_surfaceItem.data(), &QQuickItem::heightChanged,
            this, &QmlInProcessApplicationManagerWindow::heightChanged);

    connect(m_surfaceItem.data(), &InProcessSurfaceItem::windowPropertyChanged,
            this, &QmlInProcessApplicationManagerWindow::windowPropertyChanged);

    connect(m_surfaceItem.data(), &InProcessSurfaceItem::closeRequested,
            this, &QmlInProcessApplicationManagerWindow::close);
}

QmlInProcessApplicationManagerWindow::~QmlInProcessApplicationManagerWindow()
{
    setVisible(false);
}

bool QmlInProcessApplicationManagerWindow::isVisible() const
{
    return m_surfaceItem->visibleClientSide();
}

void QmlInProcessApplicationManagerWindow::setVisible(bool visible)
{
    if (visible != m_surfaceItem->visibleClientSide()) {
        m_surfaceItem->setVisibleClientSide(visible);
        emit visibleChanged();
        notifyRuntimeAboutSurface();
    }
}

QColor QmlInProcessApplicationManagerWindow::color() const
{
    return m_surfaceItem->color();
}

void QmlInProcessApplicationManagerWindow::setColor(const QColor &c)
{
    if (color() != c) {
        m_surfaceItem->setColor(c);
        emit colorChanged();
    }
}

void QmlInProcessApplicationManagerWindow::close()
{
    setVisible(false);
    if (m_runtime) {
        // Queued because the runtime might end up deleting this object
        QMetaObject::invokeMethod(m_runtime, &QmlInProcessRuntime::stopIfNoVisibleSurfaces, Qt::QueuedConnection);
    }
}

void QmlInProcessApplicationManagerWindow::showFullScreen()
{
    setVisible(true);
}

void QmlInProcessApplicationManagerWindow::showMaximized()
{
    setVisible(true);
}

void QmlInProcessApplicationManagerWindow::showNormal()
{
    setVisible(true);
}

bool QmlInProcessApplicationManagerWindow::setWindowProperty(const QString &name, const QVariant &value)
{
    return m_surfaceItem->setWindowProperty(name, value);
}

QVariant QmlInProcessApplicationManagerWindow::windowProperty(const QString &name) const
{
    return m_surfaceItem->windowProperty(name);
}

QVariantMap QmlInProcessApplicationManagerWindow::windowProperties() const
{
    return m_surfaceItem->windowPropertiesAsVariantMap();
}

QQmlListProperty<QObject> QmlInProcessApplicationManagerWindow::data()
{
    return QQmlListProperty<QObject>(this, nullptr,
            QmlInProcessApplicationManagerWindow::data_append,
            QmlInProcessApplicationManagerWindow::data_count,
            QmlInProcessApplicationManagerWindow::data_at,
            QmlInProcessApplicationManagerWindow::data_clear);
}

void QmlInProcessApplicationManagerWindow::data_append(QQmlListProperty<QObject> *property, QObject *value)
{
    auto *that = static_cast<QmlInProcessApplicationManagerWindow*>(property->object);

    QQmlListProperty<QObject> itemProperty = QQuickItemPrivate::get(that->contentItem())->data();
    itemProperty.append(&itemProperty, value);
}

int QmlInProcessApplicationManagerWindow::data_count(QQmlListProperty<QObject> *property)
{
    auto *that = static_cast<QmlInProcessApplicationManagerWindow*>(property->object);
    if (!QQuickItemPrivate::get(that->contentItem())->data().count)
        return 0;
    QQmlListProperty<QObject> itemProperty = QQuickItemPrivate::get(that->contentItem())->data();
    return itemProperty.count(&itemProperty);
}

QObject *QmlInProcessApplicationManagerWindow::data_at(QQmlListProperty<QObject> *property, int index)
{
    auto *that = static_cast<QmlInProcessApplicationManagerWindow*>(property->object);
    QQmlListProperty<QObject> itemProperty = QQuickItemPrivate::get(that->contentItem())->data();
    return itemProperty.at(&itemProperty, index);
}

void QmlInProcessApplicationManagerWindow::data_clear(QQmlListProperty<QObject> *property)
{
    auto *that = static_cast<QmlInProcessApplicationManagerWindow*>(property->object);
    QQmlListProperty<QObject> itemProperty = QQuickItemPrivate::get(that->contentItem())->data();
    itemProperty.clear(&itemProperty);
}

void QmlInProcessApplicationManagerWindow::componentComplete()
{
    qCDebug(LogSystem) << "QmlInProcessApplicationManagerWindow componentComplete() this:" << this;

    if (!m_runtime)
        m_runtime = QmlInProcessRuntime::determineRuntime(this);

    findParentWindow(parent());

    // This part is scary, but we need to make sure that all Component.onComplete: handlers on
    // the QML side have been run, before we hand this window over to the WindowManager for the
    // onWindowReady signal. The problem here is that the C++ componentComplete() handler (this
    // function) is called *before* the QML side, plus, to make matters worse, the QML incubator
    // could switch back to the event loop a couple of times before finally calling the QML
    // onCompleted handler(s).
    // The workaround is to setup watchers for all Component.onCompleted handlers for this object
    // and wait until the last of them has been dealt with, to finally call our addSurfaceItem
    // function (we are also relying on the signal emission order, so that our lambda is called
    // after the actual QML handler).

    for (auto a = QQmlComponent::qmlAttachedProperties(this); a; a = a->next) {
        auto appWindow = qobject_cast<QmlInProcessApplicationManagerWindow *>(a->parent());
        if (!appWindow || appWindow != this)
            continue;

        m_attachedCompleteHandlers << a;

        connect(a, &QQmlComponentAttached::completed, this, [this, a]() {
            m_attachedCompleteHandlers.removeAll(a);

            if (m_attachedCompleteHandlers.isEmpty())
                notifyRuntimeAboutSurface();
        });
    }

    // If we do not even have a single Component.onCompleted handler on the QML side, we need to
    // show the window immediately.
    if (m_attachedCompleteHandlers.isEmpty()) {
        notifyRuntimeAboutSurface();
    }
}

void QmlInProcessApplicationManagerWindow::notifyRuntimeAboutSurface()
{
    if (!m_runtime)
        return;

    if (isVisible() && m_attachedCompleteHandlers.isEmpty() && (!m_parentWindow || m_parentWindow->isVisible()))
        m_runtime->addSurfaceItem(m_surfaceItem);
}

QQuickItem *QmlInProcessApplicationManagerWindow::contentItem()
{
    return m_surfaceItem.data();
}

void QmlInProcessApplicationManagerWindow::findParentWindow(QObject *object)
{
    if (!object)
        return;

    auto surfaceItem = qobject_cast<InProcessSurfaceItem*>(object);
    if (surfaceItem) {
        setParentWindow(static_cast<QmlInProcessApplicationManagerWindow*>(surfaceItem->inProcessApplicationManagerWindow()));
    } else {
        auto inProcessAppWindow = qobject_cast<QmlInProcessApplicationManagerWindow*>(object);
        if (inProcessAppWindow)
            setParentWindow(inProcessAppWindow);
        else
            findParentWindow(object->parent());
    }
}

void QmlInProcessApplicationManagerWindow::setParentWindow(QmlInProcessApplicationManagerWindow *inProcessAppWindow)
{
    if (m_parentWindow)
        disconnect(m_parentWindow, nullptr, this, nullptr);

    m_parentWindow = inProcessAppWindow;

    if (m_parentWindow)
        connect(m_parentWindow, &QmlInProcessApplicationManagerWindow::visibleChanged,
                this, &QmlInProcessApplicationManagerWindow::notifyRuntimeAboutSurface);
}

void QmlInProcessApplicationManagerWindow::setTitle(const QString &value)
{
    if (m_title != value) {
        m_title = value;
        emit titleChanged();
    }
}

void QmlInProcessApplicationManagerWindow::setX(int value)
{
    if (m_x != value) {
        m_x = value;
        emit xChanged();
    }
}

void QmlInProcessApplicationManagerWindow::setY(int value)
{
    if (m_y != value) {
        m_y = value;
        emit yChanged();
    }
}

int QmlInProcessApplicationManagerWindow::width() const
{
    return m_surfaceItem->width();
}

void QmlInProcessApplicationManagerWindow::setWidth(int value)
{
    m_surfaceItem->setWidth(value);
}

int QmlInProcessApplicationManagerWindow::height() const
{
    return m_surfaceItem->height();
}

void QmlInProcessApplicationManagerWindow::setHeight(int value)
{
    m_surfaceItem->setHeight(value);
}

void QmlInProcessApplicationManagerWindow::setMinimumWidth(int value)
{
    if (m_minimumWidth != value) {
        m_minimumWidth = value;
        emit minimumWidthChanged();
    }
}

void QmlInProcessApplicationManagerWindow::setMinimumHeight(int value)
{
    if (m_minimumHeight != value) {
        m_minimumHeight = value;
        emit minimumHeightChanged();
    }
}

void QmlInProcessApplicationManagerWindow::setMaximumWidth(int value)
{
    if (m_maximumWidth != value) {
        m_maximumWidth = value;
        emit maximumWidthChanged();
    }
}

void QmlInProcessApplicationManagerWindow::setMaximumHeight(int value)
{
    if (m_maximumHeight != value) {
        m_maximumHeight = value;
        emit maximumHeightChanged();
    }
}

void QmlInProcessApplicationManagerWindow::setOpacity(qreal value)
{
    if (m_opacity != value) {
        m_opacity = value;
        emit opacityChanged();
    }
}

QT_END_NAMESPACE_AM