2
This file is part of the KDE libraries
4
Copyright (C) 2015 Marco Martin <mart@kde.org>
6
This library is free software; you can redistribute it and/or
7
modify it under the terms of the GNU Library General Public
8
License as published by the Free Software Foundation; either
9
version 2 of the License, or (at your option) any later version.
11
This library is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
Library General Public License for more details.
16
You should have received a copy of the GNU Library General Public License
17
along with this library; see the file COPYING.LIB. If not, write to
18
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
Boston, MA 02110-1301, USA.
23
#include "quickviewsharedengine.h"
29
#include <QQmlContext>
32
#include <QQmlExpression>
33
#include <QQmlProperty>
35
#include <klocalizedstring.h>
36
#include <kdeclarative/qmlobjectsharedengine.h>
38
#include <KPackage/Package>
39
#include <KPackage/PackageLoader>
41
namespace KQuickAddons {
43
class QuickViewSharedEnginePrivate
46
QuickViewSharedEnginePrivate(QuickViewSharedEngine *module)
50
qmlObject = new KDeclarative::QmlObjectSharedEngine(q);
51
QObject::connect(qmlObject, &KDeclarative::QmlObject::statusChanged,
52
q, &QuickViewSharedEngine::statusChanged);
53
QObject::connect(qmlObject, SIGNAL(finished()),
54
q, SLOT(executionFinished()));
57
void executionFinished();
58
void syncResizeMode();
63
QuickViewSharedEngine *q;
64
KDeclarative::QmlObjectSharedEngine *qmlObject;
65
QuickViewSharedEngine::ResizeMode resizeMode;
69
void QuickViewSharedEnginePrivate::executionFinished()
71
if (!qmlObject->rootObject()) {
75
QQuickItem *item = qobject_cast<QQuickItem *>(qmlObject->rootObject());
81
item->setParentItem(q->contentItem());
82
initialSize = QSize(item->width(), item ->height());
83
q->resize(initialSize);
84
q->contentItem()->setWidth(item->width());
85
q->contentItem()->setHeight(item->height());
90
void QuickViewSharedEnginePrivate::syncResizeMode()
92
QQuickItem *item = qobject_cast<QQuickItem *>(qmlObject->rootObject());
98
if (resizeMode == QuickViewSharedEngine::SizeRootObjectToView) {
99
item->setWidth(q->width());
100
item->setHeight(q->height());
102
QObject::disconnect(item, SIGNAL(widthChanged()),
103
q, SLOT(syncWidth()));
104
QObject::disconnect(item, SIGNAL(heightChanged()),
105
q, SLOT(syncHeight()));
109
QObject::connect(item, SIGNAL(widthChanged()),
110
q, SLOT(syncWidth()));
111
QObject::connect(item, SIGNAL(heightChanged()),
112
q, SLOT(syncHeight()));
119
void QuickViewSharedEnginePrivate::syncWidth()
121
QQuickItem *item = qobject_cast<QQuickItem *>(qmlObject->rootObject());
127
q->setWidth(item->width());
130
void QuickViewSharedEnginePrivate::syncHeight()
132
QQuickItem *item = qobject_cast<QQuickItem *>(qmlObject->rootObject());
138
q->setHeight(item->height());
143
QuickViewSharedEngine::QuickViewSharedEngine(QWindow *parent)
144
: QQuickWindow(parent),
145
d(new QuickViewSharedEnginePrivate(this))
149
QuickViewSharedEngine::~QuickViewSharedEngine()
154
QQmlEngine *QuickViewSharedEngine::engine() const
156
return d->qmlObject->engine();
159
QList<QQmlError> QuickViewSharedEngine::errors() const
161
QList<QQmlError> errs;
163
if (d->qmlObject->mainComponent()) {
164
errs = d->qmlObject->mainComponent()->errors();
170
QSize QuickViewSharedEngine::sizeHint() const
172
QQuickItem *item = qobject_cast<QQuickItem *>(d->qmlObject->rootObject());
177
const QSizeF implicitSize(item->implicitWidth(), item->implicitHeight());
179
if (!implicitSize.isEmpty()) {
180
return implicitSize.toSize();
184
return QSize(item->width(), item->height());
190
QSize QuickViewSharedEngine::initialSize() const
192
return d->initialSize;
195
QuickViewSharedEngine::ResizeMode QuickViewSharedEngine::resizeMode() const
197
return d->resizeMode;
200
QQmlContext *QuickViewSharedEngine::rootContext() const
202
return d->qmlObject->rootContext();
205
QQuickItem *QuickViewSharedEngine::rootObject() const
207
return qobject_cast<QQuickItem *>(d->qmlObject->rootObject());
210
void QuickViewSharedEngine::setResizeMode(ResizeMode mode)
212
if (d->resizeMode == mode) {
216
d->resizeMode = mode;
218
emit resizeModeChanged(mode);
220
QQuickItem *item = qobject_cast<QQuickItem *>(d->qmlObject->rootObject());
228
void QuickViewSharedEngine::setSource(const QUrl &url)
230
if (d->qmlObject->source() == url) {
234
d->qmlObject->setSource(url);
235
emit sourceChanged(url);
238
QUrl QuickViewSharedEngine::source() const
240
return d->qmlObject->source();
243
QQmlComponent::Status QuickViewSharedEngine::status() const
245
if (!d->qmlObject->mainComponent()) {
246
return QQmlComponent::Null;
249
return QQmlComponent::Status(d->qmlObject->status());
252
void QuickViewSharedEngine::resizeEvent(QResizeEvent *e)
254
QQuickItem *item = qobject_cast<QQuickItem *>(d->qmlObject->rootObject());
255
if (item && d->resizeMode == SizeRootObjectToView) {
256
item->setWidth(e->size().width());
257
item->setHeight(e->size().height());
260
QQuickWindow::resizeEvent(e);
265
#include "moc_quickviewsharedengine.cpp"