Libplasma

quickviewsharedengine.cpp
1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2015 Marco Martin <mart@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "quickviewsharedengine.h"
9#include "sharedqmlengine.h"
10
11#include <QQmlContext>
12#include <QQmlEngine>
13#include <QQuickItem>
14
15#include <KLocalizedString>
16
17namespace PlasmaQuick
18{
19class QuickViewSharedEnginePrivate
20{
21public:
22 QuickViewSharedEnginePrivate(QuickViewSharedEngine *module)
23 : q(module)
24 , resizeMode(QuickViewSharedEngine::ResizeMode::SizeRootObjectToView)
25 , initialSize(0, 0)
26 {
27 qmlObject = new SharedQmlEngine(q);
28 QObject::connect(qmlObject, &SharedQmlEngine::statusChanged, q, &QuickViewSharedEngine::statusChanged);
29 QObject::connect(qmlObject, &SharedQmlEngine::finished, q, [this]() {
30 executionFinished();
31 });
32 }
33
34 void executionFinished();
35 void syncResizeMode();
36 void syncWidth();
37 void syncHeight();
38
39 QuickViewSharedEngine *q;
40 SharedQmlEngine *qmlObject;
41 QuickViewSharedEngine::ResizeMode resizeMode;
42 QSize initialSize;
43};
44
45void QuickViewSharedEnginePrivate::executionFinished()
46{
47 if (!qmlObject->rootObject()) {
48 return;
49 }
50
51 QQuickItem *item = qobject_cast<QQuickItem *>(qmlObject->rootObject());
52
53 if (!item) {
54 return;
55 }
56
57 item->setParentItem(q->contentItem());
58 initialSize = QSize(item->width(), item->height());
59
60 if (q->size().isEmpty()) {
61 q->resize(initialSize);
62 q->contentItem()->setSize(initialSize);
63 }
64
65 syncResizeMode();
66}
67
68void QuickViewSharedEnginePrivate::syncResizeMode()
69{
70 QQuickItem *item = qobject_cast<QQuickItem *>(qmlObject->rootObject());
71
72 if (!item) {
73 return;
74 }
75
76 if (resizeMode == QuickViewSharedEngine::SizeRootObjectToView) {
77 item->setSize(QSize(q->width(), q->height()));
78
81
82 } else {
83 QObject::connect(item, &QQuickItem::widthChanged, q, [this]() {
84 syncWidth();
85 });
87 syncHeight();
88 });
89
90 syncWidth();
91 syncHeight();
92 }
93}
94
95void QuickViewSharedEnginePrivate::syncWidth()
96{
97 QQuickItem *item = qobject_cast<QQuickItem *>(qmlObject->rootObject());
98
99 if (!item) {
100 return;
101 }
102
103 q->setWidth(item->width());
104}
105
106void QuickViewSharedEnginePrivate::syncHeight()
107{
108 QQuickItem *item = qobject_cast<QQuickItem *>(qmlObject->rootObject());
109
110 if (!item) {
111 return;
112 }
113
114 q->setHeight(item->height());
115}
116
117QuickViewSharedEngine::QuickViewSharedEngine(QWindow *parent)
118 : QQuickWindow(parent)
119 , d(new QuickViewSharedEnginePrivate(this))
120{
121}
122
123QuickViewSharedEngine::~QuickViewSharedEngine()
124{
125 delete d->qmlObject;
126}
127
128void QuickViewSharedEngine::setTranslationDomain(const QString &translationDomain)
129{
130 d->qmlObject->setTranslationDomain(translationDomain);
131}
132
133QString QuickViewSharedEngine::translationDomain() const
134{
135 return d->qmlObject->translationDomain();
136}
137
138std::shared_ptr<QQmlEngine> QuickViewSharedEngine::engine() const
139{
140 return d->qmlObject->engine();
141}
142
143QList<QQmlError> QuickViewSharedEngine::errors() const
144{
145 QList<QQmlError> errs;
146
147 if (d->qmlObject->mainComponent()) {
148 errs = d->qmlObject->mainComponent()->errors();
149 }
150
151 return errs;
152}
153
154QSize QuickViewSharedEngine::sizeHint() const
155{
156 QQuickItem *item = qobject_cast<QQuickItem *>(d->qmlObject->rootObject());
157 if (!item) {
158 return QSize();
159 }
160
161 const QSizeF implicitSize(item->implicitWidth(), item->implicitHeight());
162
163 if (!implicitSize.isEmpty()) {
164 return implicitSize.toSize();
165 }
166
167 return QSize(item->width(), item->height());
168}
169
170QSize QuickViewSharedEngine::initialSize() const
171{
172 return d->initialSize;
173}
174
175QuickViewSharedEngine::ResizeMode QuickViewSharedEngine::resizeMode() const
176{
177 return d->resizeMode;
178}
179
180QQmlContext *QuickViewSharedEngine::rootContext() const
181{
182 return d->qmlObject->rootContext();
183}
184
185QQuickItem *QuickViewSharedEngine::rootObject() const
186{
187 return qobject_cast<QQuickItem *>(d->qmlObject->rootObject());
188}
189
190void QuickViewSharedEngine::setResizeMode(ResizeMode mode)
191{
192 if (d->resizeMode == mode) {
193 return;
194 }
195
196 d->resizeMode = mode;
197
198 Q_EMIT resizeModeChanged(mode);
199
200 QQuickItem *item = qobject_cast<QQuickItem *>(d->qmlObject->rootObject());
201 if (!item) {
202 return;
203 }
204
205 d->syncResizeMode();
206}
207
208void QuickViewSharedEngine::setSource(const QUrl &url)
209{
210 if (d->qmlObject->source() == url) {
211 return;
212 }
213
214 d->qmlObject->setSource(url);
215 Q_EMIT sourceChanged(url);
216}
217
218QUrl QuickViewSharedEngine::source() const
219{
220 return d->qmlObject->source();
221}
222
223QQmlComponent::Status QuickViewSharedEngine::status() const
224{
225 if (!d->qmlObject->mainComponent()) {
226 return QQmlComponent::Null;
227 }
228
229 return QQmlComponent::Status(d->qmlObject->status());
230}
231
232void QuickViewSharedEngine::resizeEvent(QResizeEvent *e)
233{
234 QQuickItem *item = qobject_cast<QQuickItem *>(d->qmlObject->rootObject());
235 if (item && d->resizeMode == SizeRootObjectToView) {
236 item->setSize(e->size());
237 }
238
240}
241
242}
243
244#include "moc_quickviewsharedengine.cpp"
void finished()
Emitted when the parsing and execution of the QML file is terminated.
The EdgeEventForwarder class This class forwards edge events to be replayed within the given margin T...
Definition action.h:20
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool disconnect(const QMetaObject::Connection &connection)
void setParentItem(QQuickItem *parent)
void setSize(const QSizeF &size)
virtual void resizeEvent(QResizeEvent *ev) override
const QSize & size() const const
bool isEmpty() const const
void resize(const QSize &newSize)
virtual QSize size() const const override
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:06:24 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.